@@ -40,10 +40,11 @@ func TestAddSNATRules(t *testing.T) {
4040 }
4141
4242 tests := []struct {
43- name string
44- input * cns.CreateNetworkContainerRequest
45- preExistingRules []preExistingRule
46- expectedChains []chainExpectation
43+ name string
44+ input * cns.CreateNetworkContainerRequest
45+ preExistingRules []preExistingRule
46+ expectedChains []chainExpectation
47+ expectedClearChainCalls int
4748 }{
4849 {
4950 // in pod subnet, the primary nic ip is in the same address space as the pod subnet
@@ -83,6 +84,7 @@ func TestAddSNATRules(t *testing.T) {
8384 },
8485 },
8586 },
87+ expectedClearChainCalls : 1 ,
8688 },
8789 {
8890 // test with pre-existing SWIFT rule that should be migrated
@@ -156,6 +158,7 @@ func TestAddSNATRules(t *testing.T) {
156158 },
157159 },
158160 },
161+ expectedClearChainCalls : 1 ,
159162 },
160163 {
161164 // test after migration has already completed
@@ -238,6 +241,7 @@ func TestAddSNATRules(t *testing.T) {
238241 },
239242 },
240243 },
244+ expectedClearChainCalls : 0 ,
241245 },
242246 {
243247 // in vnet scale, the primary nic ip becomes the node ip (diff address space from pod subnet)
@@ -277,17 +281,16 @@ func TestAddSNATRules(t *testing.T) {
277281 },
278282 },
279283 },
284+ expectedClearChainCalls : 1 ,
280285 },
281286 }
282287
283288 for _ , tt := range tests {
284289 t .Run (tt .name , func (t * testing.T ) {
285290 service := getTestService (cns .KubernetesCRD )
286- service .iptables = & FakeIPTablesProvider {}
287-
288- ipt , err := service .iptables .GetIPTables ()
289- if err != nil {
290- t .Fatal ("failed to get iptables client:" , err )
291+ ipt := fakes .NewIPTablesMock ()
292+ service .iptables = & FakeIPTablesProvider {
293+ iptables : ipt ,
291294 }
292295
293296 // setup pre-existing rules
@@ -296,13 +299,13 @@ func TestAddSNATRules(t *testing.T) {
296299 chainExists , _ := ipt .ChainExists (preRule .table , preRule .chain )
297300
298301 if ! chainExists {
299- err = ipt .NewChain (preRule .table , preRule .chain )
302+ err : = ipt .NewChain (preRule .table , preRule .chain )
300303 if err != nil {
301304 t .Fatal ("failed to setup pre-existing rule chain:" , err )
302305 }
303306 }
304307
305- err = ipt .Append (preRule .table , preRule .chain , preRule .rule ... )
308+ err : = ipt .Append (preRule .table , preRule .chain , preRule .rule ... )
306309 if err != nil {
307310 t .Fatal ("failed to setup pre-existing rule:" , err )
308311 }
@@ -333,6 +336,12 @@ func TestAddSNATRules(t *testing.T) {
333336 }
334337 }
335338 }
339+
340+ // verify ClearChain was called the expected number of times
341+ actualClearChainCalls := ipt .ClearChainCallCount ()
342+ if actualClearChainCalls != tt .expectedClearChainCalls {
343+ t .Fatalf ("ClearChain call count mismatch: got %d, expected %d" , actualClearChainCalls , tt .expectedClearChainCalls )
344+ }
336345 })
337346 }
338347}
0 commit comments