Skip to content

Commit 61fb84d

Browse files
authored
fix: Resolve flaky TestDestroy ipset unit test (#671)
* fix: Improve TestDestroy unit test validation to make it not flaky.
1 parent 13677e6 commit 61fb84d

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

npm/ipsm/ipsm_test.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func TestDeleteSet(t *testing.T) {
244244
}
245245
}()
246246

247-
testSetName := "test-set"
247+
testSetName := "test-delete-set"
248248
if err := ipsMgr.CreateSet(testSetName, append([]string{util.IpsetNetHashFlag})); err != nil {
249249
t.Errorf("TestDeleteSet failed @ ipsMgr.CreateSet")
250250
}
@@ -350,7 +350,7 @@ func TestDeleteFromSet(t *testing.T) {
350350
}
351351
}()
352352

353-
testSetName := "test-set"
353+
testSetName := "test-delete-from-set"
354354
if err := ipsMgr.AddToSet(testSetName, "1.2.3.4", util.IpsetNetHashFlag, ""); err != nil {
355355
t.Errorf("TestDeleteFromSet failed @ ipsMgr.AddToSet")
356356
}
@@ -468,12 +468,34 @@ func TestDestroy(t *testing.T) {
468468
}
469469
}()
470470

471-
if err := ipsMgr.AddToSet("test-destroy-set", "1.2.3.4", util.IpsetNetHashFlag, ""); err != nil {
471+
setName := "test-destroy"
472+
testIP := "1.2.3.4"
473+
if err := ipsMgr.AddToSet(setName, testIP, util.IpsetNetHashFlag, ""); err != nil {
472474
t.Errorf("TestDestroy failed @ ipsMgr.AddToSet")
473475
}
474476

475-
if err := ipsMgr.Destroy(); err != nil {
476-
t.Errorf("TestDestroy failed @ ipsMgr.Destroy")
477+
// Call Destroy and validate. Destroy can only work when no ipset is referenced from iptables.
478+
if err := ipsMgr.Destroy(); err == nil {
479+
// Validate ipset is not exist when destroy can happen.
480+
entry := &ipsEntry{
481+
operationFlag: util.IPsetCheckListFlag,
482+
set: util.GetHashedName(setName),
483+
}
484+
485+
if _, err := ipsMgr.Run(entry); err == nil {
486+
t.Errorf("TestDestroy failed @ ipsMgr.Destroy since %s still exist in kernel", setName)
487+
}
488+
} else {
489+
// Validate ipset entries are gone from flush command when destroy can not happen.
490+
entry := &ipsEntry{
491+
operationFlag: util.IpsetTestFlag,
492+
set: util.GetHashedName(setName),
493+
spec: append([]string{testIP}),
494+
}
495+
496+
if _, err := ipsMgr.Run(entry); err == nil {
497+
t.Errorf("TestDestroy failed @ ipsMgr.Destroy since %s still exist in ipset", testIP)
498+
}
477499
}
478500
}
479501

0 commit comments

Comments
 (0)