Skip to content

Commit b4eaa16

Browse files
committed
IsEmpty unit tests
1 parent 53c4afa commit b4eaa16

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

pkg/ingressCache/ingressCache_test.go

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/v3io/scaler/pkg/ingressCache/mock"
77

8+
"github.com/nuclio/errors"
89
"github.com/nuclio/logger"
910
nucliozap "github.com/nuclio/zap"
1011
"github.com/stretchr/testify/suite"
@@ -151,12 +152,20 @@ func (suite *IngressCacheTest) TestSet() {
151152
}
152153

153154
func (suite *IngressCacheTest) TestDelete() {
155+
type getFunctionAfterDeleteArgs struct { // this struct enables multiple get tests after delete
156+
args testIngressCacheArgs
157+
expectedResult []string
158+
shouldFail bool
159+
errorMessage string
160+
}
161+
154162
for _, testCase := range []struct {
155-
name string
156-
args testIngressCacheArgs
157-
shouldFail bool
158-
errorMessage string
159-
testMocks mockFunction
163+
name string
164+
args testIngressCacheArgs
165+
shouldFail bool
166+
errorMessage string
167+
testMocks mockFunction
168+
getAfterDeleteArgs []getFunctionAfterDeleteArgs
160169
}{
161170
{
162171
name: "Delete not existed host",
@@ -165,13 +174,55 @@ func (suite *IngressCacheTest) TestDelete() {
165174
return nil
166175
}, // nil is used to check for non-existing host
167176
}, {
168-
name: "Delete existed host and path",
177+
name: "Delete last function in host, validate host deletion",
169178
args: testIngressCacheArgs{testHost, testPath, testFunctionName2},
170179
testMocks: func() *mock.SafeTrie {
171180
m := &mock.SafeTrie{}
172181
m.On("DeleteFunctionName", testPath, testFunctionName2).Return(nil).Once()
182+
m.On("IsEmpty").Return(true).Once()
173183
return m
174184
},
185+
getAfterDeleteArgs: []getFunctionAfterDeleteArgs{
186+
{
187+
args: testIngressCacheArgs{testHost, testPath, testFunctionName1},
188+
expectedResult: nil,
189+
shouldFail: true,
190+
errorMessage: "host does not exist",
191+
},
192+
},
193+
}, {
194+
name: "Fail to delete and validate host wasn't deleted",
195+
args: testIngressCacheArgs{testHost, testPath, testFunctionName2},
196+
testMocks: func() *mock.SafeTrie {
197+
m := &mock.SafeTrie{}
198+
m.On("DeleteFunctionName", testPath, testFunctionName2).Return(errors.New("mock error")).Once()
199+
m.On("GetFunctionName", testPath).Return([]string{testFunctionName2}, nil).Once()
200+
return m
201+
},
202+
getAfterDeleteArgs: []getFunctionAfterDeleteArgs{
203+
{
204+
args: testIngressCacheArgs{testHost, testPath, testFunctionName2},
205+
expectedResult: []string{testFunctionName2},
206+
},
207+
},
208+
shouldFail: true,
209+
errorMessage: "cache delete failed",
210+
}, {
211+
name: "Delete not last function in path and validate host wasn't deleted",
212+
args: testIngressCacheArgs{testHost, testPath, testFunctionName2},
213+
testMocks: func() *mock.SafeTrie {
214+
m := &mock.SafeTrie{}
215+
m.On("DeleteFunctionName", testPath, testFunctionName2).Return(nil).Once()
216+
m.On("IsEmpty").Return(false).Once()
217+
m.On("GetFunctionName", testPath).Return([]string{testFunctionName1}, nil).Once()
218+
return m
219+
},
220+
getAfterDeleteArgs: []getFunctionAfterDeleteArgs{
221+
{
222+
args: testIngressCacheArgs{testHost, testPath, testFunctionName1},
223+
expectedResult: []string{testFunctionName1},
224+
},
225+
},
175226
},
176227
} {
177228
suite.Run(testCase.name, func() {
@@ -184,6 +235,18 @@ func (suite *IngressCacheTest) TestDelete() {
184235
} else {
185236
suite.Require().NoError(err)
186237
}
238+
239+
// After delete, check that the expected paths and functions are still there
240+
for _, getAfterDeleteArgs := range testCase.getAfterDeleteArgs {
241+
result, err := suite.ingressCache.Get(getAfterDeleteArgs.args.host, getAfterDeleteArgs.args.path)
242+
if getAfterDeleteArgs.shouldFail {
243+
suite.Require().Error(err)
244+
suite.Require().Contains(err.Error(), getAfterDeleteArgs.errorMessage)
245+
} else {
246+
suite.Require().NoError(err)
247+
suite.Require().Equal(getAfterDeleteArgs.expectedResult, result)
248+
}
249+
}
187250
})
188251
}
189252
}

pkg/ingressCache/mock/safeTrie.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,17 @@ func (_m *SafeTrie) SetFunctionName(path string, function string) error {
9393

9494
return r0
9595
}
96+
97+
//nolint:gocritic // this function is being generated by mockery
98+
func (_m *SafeTrie) IsEmpty() bool {
99+
ret := _m.Called()
100+
101+
var r0 bool
102+
if rf, ok := ret.Get(0).(func() bool); ok {
103+
r0 = rf()
104+
} else {
105+
r0 = ret.Get(0).(bool)
106+
}
107+
108+
return r0
109+
}

pkg/ingressCache/safeTrie_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,31 @@ func (suite *SafeTrieTest) TestPathTreeDelete() {
406406
})
407407
}
408408
}
409+
func (suite *SafeTrieTest) TestPathTreeIsEmpty() {
410+
for _, testCase := range []struct {
411+
initialState []safeTrieFunctionArgs // initial state of the path tree before delete
412+
name string
413+
expectedResult bool
414+
}{
415+
{
416+
name: "is empty with empty trie",
417+
expectedResult: true,
418+
}, {
419+
name: "is empty with not empty trie",
420+
expectedResult: false,
421+
initialState: []safeTrieFunctionArgs{
422+
{testPath, testFunctionName1},
423+
},
424+
},
425+
} {
426+
suite.Run(testCase.name, func() {
427+
suite.SetupSubTest(testCase.initialState)
428+
429+
result := suite.safeTrie.IsEmpty()
430+
suite.Require().Equal(testCase.expectedResult, result)
431+
})
432+
}
433+
}
409434

410435
// --- SafeTrieTest suite methods ---
411436

0 commit comments

Comments
 (0)