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
153154func (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}
0 commit comments