Skip to content

Commit bfb74f4

Browse files
committed
add test to check if test case flushes chain
1 parent f974fb3 commit bfb74f4

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

cns/fakes/iptablesfake.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ var (
1313
errChainNotFound = errors.New("chain not found")
1414
errRuleExists = errors.New("rule already exists")
1515
errRuleNotFound = errors.New("rule not found")
16+
errIndexBounds = errors.New("index out of bounds")
1617
)
1718

1819
type IPTablesMock struct {
19-
state map[string]map[string][]string
20+
state map[string]map[string][]string
21+
clearChainCalled int
2022
}
2123

2224
func NewIPTablesMock() *IPTablesMock {
@@ -106,8 +108,10 @@ func (c *IPTablesMock) Insert(table, chain string, pos int, rulespec ...string)
106108
index = 0
107109
}
108110

109-
if index >= len(chainRules) {
111+
if index == len(chainRules) {
110112
c.state[table][chain] = append(chainRules, targetRule)
113+
} else if index > len(chainRules) {
114+
return errIndexBounds
111115
} else {
112116
c.state[table][chain] = append(chainRules[:index], append([]string{targetRule}, chainRules[index:]...)...)
113117
}
@@ -151,6 +155,7 @@ func (c *IPTablesMock) List(table, chain string) ([]string, error) {
151155
}
152156

153157
func (c *IPTablesMock) ClearChain(table, chain string) error {
158+
c.clearChainCalled++
154159
c.ensureTableExists(table)
155160

156161
chainExists, _ := c.ChainExists(table, chain)
@@ -183,3 +188,7 @@ func (c *IPTablesMock) Delete(table, chain string, rulespec ...string) error {
183188

184189
return errRuleNotFound
185190
}
191+
192+
func (c *IPTablesMock) ClearChainCallCount() int {
193+
return c.clearChainCalled
194+
}

cns/restserver/internalapi_linux.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func (service *HTTPRestService) programSNATRules(req *cns.CreateNetworkContainer
7272
if swiftRuleIndex != -1 {
7373
// jump SWIFT rule exists, insert SWIFT-POSTROUTING rule at the same position so it ends up running first
7474
// first, remove any existing SWIFT-POSTROUTING rules to avoid duplicates
75+
// note: inserting at len(rules) and deleting a jump to SWIFT-POSTROUTING is mutually exclusive
7576
swiftPostroutingExists, err := ipt.Exists(iptables.Nat, iptables.Postrouting, "-j", SWIFT)
7677
if err != nil {
7778
return types.UnexpectedError, fmt.Sprintf("[Azure CNS] Error. Failed to check for existence of SWIFT-POSTROUTING rule: %v", err)

cns/restserver/internalapi_linux_test.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)