Skip to content

Commit 85cd950

Browse files
authored
[NPM] Add NPM Error structs (#840)
* add NPM tests structs * remove codecov annotations
1 parent 0b1086b commit 85cd950

File tree

3 files changed

+408
-11
lines changed

3 files changed

+408
-11
lines changed

codecov.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ comment:
3030

3131
ignore:
3232
- "vendor/"
33+
34+
github_checks:
35+
annotations: false

npm/ipsm/ipsm_test.go

Lines changed: 254 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -422,31 +422,31 @@ func TestDeleteFromSetWithPodCache(t *testing.T) {
422422
var ip = "10.0.2.8"
423423
var pod1 = "pod1"
424424
if err := ipsMgr.AddToSet(setname, ip, util.IpsetNetHashFlag, pod1); err != nil {
425-
t.Errorf("TestDeleteFromSetWithPodCache failed for pod1 @ ipsMgr.AddToSet")
425+
t.Errorf("TestDeleteFromSetWithPodCache failed for pod1 @ ipsMgr.AddToSet with err %+v", err)
426426
}
427427

428428
if len(ipsMgr.SetMap[setname].elements) != 1 {
429429
t.Errorf("TestDeleteFromSetWithPodCache failed @ ipsMgr.AddToSet")
430430
}
431431

432432
if err := ipsMgr.DeleteFromSet(setname, ip, pod1); err != nil {
433-
t.Errorf("TestDeleteFromSetWithPodCache for pod1 failed @ ipsMgr.DeleteFromSet")
433+
t.Errorf("TestDeleteFromSetWithPodCache for pod1 failed @ ipsMgr.DeleteFromSet with err %+v", err)
434434
}
435435

436436
// now add the set again and then replace it with pod2
437437
var pod2 = "pod2"
438438
if err := ipsMgr.AddToSet(setname, ip, util.IpsetNetHashFlag, pod1); err != nil {
439-
t.Errorf("TestDeleteFromSetWithPodCache failed for pod1 @ ipsMgr.AddToSet")
439+
t.Errorf("TestDeleteFromSetWithPodCache failed for pod1 @ ipsMgr.AddToSet with err %+v", err)
440440
}
441441

442442
// Add Pod2 with same ip (This could happen if AddPod2 is served before DeletePod1)
443443
if err := ipsMgr.AddToSet(setname, ip, util.IpsetNetHashFlag, pod2); err != nil {
444-
t.Errorf("TestDeleteFromSetWithPodCache failed for pod2 @ ipsMgr.AddToSet")
444+
t.Errorf("TestDeleteFromSetWithPodCache failed for pod2 @ ipsMgr.AddToSet with err %+v", err)
445445
}
446446

447447
// Process DeletePod1
448448
if err := ipsMgr.DeleteFromSet(setname, ip, pod1); err != nil {
449-
t.Errorf("TestDeleteFromSetWithPodCache for pod1 failed @ ipsMgr.DeleteFromSet")
449+
t.Errorf("TestDeleteFromSetWithPodCache for pod1 failed @ ipsMgr.DeleteFromSet with err %+v", err)
450450
}
451451

452452
// note the set will stil exist with pod ip
@@ -457,7 +457,7 @@ func TestDeleteFromSetWithPodCache(t *testing.T) {
457457

458458
// Now cleanup and delete pod2
459459
if err := ipsMgr.DeleteFromSet(setname, ip, pod2); err != nil {
460-
t.Errorf("TestDeleteFromSetWithPodCache for pod2 failed @ ipsMgr.DeleteFromSet")
460+
t.Errorf("TestDeleteFromSetWithPodCache for pod2 failed @ ipsMgr.DeleteFromSet with err %+v", err)
461461
}
462462

463463
if _, exists := ipsMgr.SetMap[setname]; exists {
@@ -478,7 +478,7 @@ func TestClean(t *testing.T) {
478478
}()
479479

480480
if err := ipsMgr.CreateSet("test-set", append([]string{util.IpsetNetHashFlag})); err != nil {
481-
t.Errorf("TestClean failed @ ipsMgr.CreateSet")
481+
t.Errorf("TestClean failed @ ipsMgr.CreateSet with err %+v", err)
482482
}
483483

484484
if err := ipsMgr.Clean(); err != nil {
@@ -501,7 +501,7 @@ func TestDestroy(t *testing.T) {
501501
setName := "test-destroy"
502502
testIP := "1.2.3.4"
503503
if err := ipsMgr.AddToSet(setName, testIP, util.IpsetNetHashFlag, ""); err != nil {
504-
t.Errorf("TestDestroy failed @ ipsMgr.AddToSet")
504+
t.Errorf("TestDestroy failed @ ipsMgr.AddToSet with err %+v", err)
505505
}
506506

507507
// Call Destroy and validate. Destroy can only work when no ipset is referenced from iptables.
@@ -513,7 +513,7 @@ func TestDestroy(t *testing.T) {
513513
}
514514

515515
if _, err := ipsMgr.Run(entry); err == nil {
516-
t.Errorf("TestDestroy failed @ ipsMgr.Destroy since %s still exist in kernel", setName)
516+
t.Errorf("TestDestroy failed @ ipsMgr.Destroy since %s still exist in kernel with err %+v", setName, err)
517517
}
518518
} else {
519519
// Validate ipset entries are gone from flush command when destroy can not happen.
@@ -524,7 +524,7 @@ func TestDestroy(t *testing.T) {
524524
}
525525

526526
if _, err := ipsMgr.Run(entry); err == nil {
527-
t.Errorf("TestDestroy failed @ ipsMgr.Destroy since %s still exist in ipset", testIP)
527+
t.Errorf("TestDestroy failed @ ipsMgr.Destroy since %s still exist in ipset with err %+v", testIP, err)
528528
}
529529
}
530530
}
@@ -547,7 +547,7 @@ func TestRun(t *testing.T) {
547547
spec: append([]string{util.IpsetNetHashFlag}),
548548
}
549549
if _, err := ipsMgr.Run(entry); err != nil {
550-
t.Errorf("TestRun failed @ ipsMgr.Run")
550+
t.Errorf("TestRun failed @ ipsMgr.Run with err %+v", err)
551551
}
552552
}
553553

@@ -573,6 +573,249 @@ func TestDestroyNpmIpsets(t *testing.T) {
573573
}
574574
}
575575

576+
// Enable these tests once the the changes for ipsm are enabled
577+
/*
578+
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
579+
580+
func GetIPSetName() string {
581+
b := make([]byte, 8)
582+
583+
for i := range b {
584+
b[i] = letters[rand.Intn(len(letters))]
585+
}
586+
587+
return "npm-test-" + string(b)
588+
}
589+
590+
// "Set cannot be destroyed: it is in use by a kernel component"
591+
func TestSetCannotBeDestroyed(t *testing.T) {
592+
ipsMgr := NewIpsetManager()
593+
if err := ipsMgr.Save(util.IpsetTestConfigFile); err != nil {
594+
t.Errorf("TestAddToList failed @ ipsMgr.Save")
595+
}
596+
597+
defer func() {
598+
if err := ipsMgr.Restore(util.IpsetTestConfigFile); err != nil {
599+
t.Errorf("TestAddToList failed @ ipsMgr.Restore")
600+
}
601+
}()
602+
603+
testset1 := GetIPSetName()
604+
testlist1 := GetIPSetName()
605+
606+
if err := ipsMgr.CreateSet(testset1, append([]string{util.IpsetNetHashFlag})); err != nil {
607+
t.Errorf("Failed to create set with err %v", err)
608+
}
609+
610+
if err := ipsMgr.AddToSet(testset1, fmt.Sprintf("%s", "1.1.1.1"), util.IpsetIPPortHashFlag, "0"); err != nil {
611+
t.Errorf("Failed to add to set with err %v", err)
612+
}
613+
614+
if err := ipsMgr.AddToList(testlist1, testset1); err != nil {
615+
t.Errorf("Failed to add to list with err %v", err)
616+
}
617+
618+
// Delete set and validate set is not exist.
619+
if err := ipsMgr.DeleteSet(testset1); err != nil {
620+
if err.ErrID != npmerr.SetCannotBeDestroyedInUseByKernelComponent {
621+
t.Errorf("Expected to error with ipset in use by kernel component")
622+
}
623+
}
624+
}
625+
626+
func TestElemSeparatorSupportsNone(t *testing.T) {
627+
ipsMgr := NewIpsetManager()
628+
if err := ipsMgr.Save(util.IpsetTestConfigFile); err != nil {
629+
t.Errorf("TestAddToList failed @ ipsMgr.Save")
630+
}
631+
632+
defer func() {
633+
if err := ipsMgr.Restore(util.IpsetTestConfigFile); err != nil {
634+
t.Errorf("TestAddToList failed @ ipsMgr.Restore")
635+
}
636+
}()
637+
638+
testset1 := GetIPSetName()
639+
640+
if err := ipsMgr.CreateSet(testset1, append([]string{util.IpsetNetHashFlag})); err != nil {
641+
t.Errorf("TestAddToList failed @ ipsMgr.CreateSet")
642+
}
643+
644+
entry := &ipsEntry{
645+
operationFlag: util.IpsetTestFlag,
646+
set: util.GetHashedName(testset1),
647+
spec: append([]string{fmt.Sprintf("10.104.7.252,3000")}),
648+
}
649+
650+
if _, err := ipsMgr.Run(entry); err == nil || err.ErrID != ElemSeperatorNotSupported {
651+
t.Errorf("Expected elem seperator error: %+v", err)
652+
}
653+
}
654+
655+
func TestIPSetWithGivenNameDoesNotExist(t *testing.T) {
656+
ipsMgr := NewIpsetManager()
657+
if err := ipsMgr.Save(util.IpsetTestConfigFile); err != nil {
658+
t.Errorf("TestAddToList failed @ ipsMgr.Save with err %+v", err)
659+
}
660+
661+
defer func() {
662+
if err := ipsMgr.Restore(util.IpsetTestConfigFile); err != nil {
663+
t.Errorf("TestAddToList failed @ ipsMgr.Restore with err %+v", err)
664+
}
665+
}()
666+
667+
testset1 := GetIPSetName()
668+
testset2 := GetIPSetName()
669+
670+
entry := &ipsEntry{
671+
operationFlag: util.IpsetAppendFlag,
672+
set: util.GetHashedName(testset1),
673+
spec: append([]string{util.GetHashedName(testset2)}),
674+
}
675+
676+
var err *NPMError
677+
if _, err = ipsMgr.Run(entry); err == nil || err.ErrID != SetWithGivenNameDoesNotExist {
678+
t.Errorf("Expected set to not exist when adding to nonexistent set %+v", err)
679+
}
680+
}
681+
682+
func TestIPSetWithGivenNameAlreadyExists(t *testing.T) {
683+
ipsMgr := NewIpsetManager()
684+
if err := ipsMgr.Save(util.IpsetTestConfigFile); err != nil {
685+
t.Errorf("TestAddToList failed @ ipsMgr.Save with err %+v", err)
686+
}
687+
688+
defer func() {
689+
if err := ipsMgr.Restore(util.IpsetTestConfigFile); err != nil {
690+
t.Errorf("TestAddToList failed @ ipsMgr.Restore with err %+v", err)
691+
}
692+
}()
693+
694+
testset1 := GetIPSetName()
695+
696+
entry := &ipsEntry{
697+
name: testset1,
698+
operationFlag: util.IpsetCreationFlag,
699+
// Use hashed string for set name to avoid string length limit of ipset.
700+
set: util.GetHashedName(testset1),
701+
spec: append([]string{util.IpsetNetHashFlag}),
702+
}
703+
704+
if errCode, err := ipsMgr.Run(entry); err != nil && errCode != 1 {
705+
t.Errorf("Expected err")
706+
}
707+
708+
entry = &ipsEntry{
709+
name: testset1,
710+
operationFlag: util.IpsetCreationFlag,
711+
// Use hashed string for set name to avoid string length limit of ipset.
712+
set: util.GetHashedName(testset1),
713+
spec: append([]string{util.IpsetSetListFlag}),
714+
}
715+
716+
if _, err := ipsMgr.Run(entry); err == nil || err.ErrID != IPSetWithGivenNameAlreadyExists {
717+
t.Errorf("Expected error code to match when set does not exist: %+v", err)
718+
}
719+
}
720+
721+
func TestIPSetSecondElementIsMissingWhenAddingIpWithNoPort(t *testing.T) {
722+
ipsMgr := NewIpsetManager()
723+
if err := ipsMgr.Save(util.IpsetTestConfigFile); err != nil {
724+
t.Errorf("TestAddToList failed @ ipsMgr.Save with err: %+v", err)
725+
}
726+
727+
defer func() {
728+
if err := ipsMgr.Restore(util.IpsetTestConfigFile); err != nil {
729+
t.Errorf("TestAddToList failed @ ipsMgr.Restore")
730+
}
731+
}()
732+
733+
testset1 := GetIPSetName()
734+
735+
spec := append([]string{util.IpsetIPPortHashFlag})
736+
if err := ipsMgr.CreateSet(testset1, spec); err != nil {
737+
t.Errorf("TestCreateSet failed @ ipsMgr.CreateSet when creating port set")
738+
}
739+
740+
entry := &ipsEntry{
741+
operationFlag: util.IpsetAppendFlag,
742+
set: util.GetHashedName(testset1),
743+
spec: append([]string{fmt.Sprintf("%s", "1.1.1.1")}),
744+
}
745+
746+
if _, err := ipsMgr.Run(entry); err == nil || err.ErrID != SecondElementIsMissing {
747+
t.Errorf("Expected to fail when adding ip with no port to set that requires port: %+v", err)
748+
}
749+
}
750+
751+
func TestIPSetMissingSecondMandatoryArgument(t *testing.T) {
752+
ipsMgr := NewIpsetManager()
753+
if err := ipsMgr.Save(util.IpsetTestConfigFile); err != nil {
754+
t.Errorf("TestAddToList failed @ ipsMgr.Save")
755+
}
756+
757+
defer func() {
758+
if err := ipsMgr.Restore(util.IpsetTestConfigFile); err != nil {
759+
t.Errorf("TestAddToList failed @ ipsMgr.Restore")
760+
}
761+
}()
762+
763+
testset1 := GetIPSetName()
764+
765+
spec := append([]string{util.IpsetIPPortHashFlag})
766+
if err := ipsMgr.CreateSet(testset1, spec); err != nil {
767+
t.Errorf("TestCreateSet failed @ ipsMgr.CreateSet when creating port set")
768+
}
769+
770+
entry := &ipsEntry{
771+
operationFlag: util.IpsetAppendFlag,
772+
set: util.GetHashedName(testset1),
773+
spec: append([]string{}),
774+
}
775+
776+
if _, err := ipsMgr.Run(entry); err == nil || err.ErrID != MissingSecondMandatoryArgument {
777+
t.Errorf("Expected to fail when running ipset command with no second argument: %+v", err)
778+
}
779+
}
780+
781+
func TestIPSetCannotBeAddedAsElementDoesNotExist(t *testing.T) {
782+
ipsMgr := NewIpsetManager()
783+
if err := ipsMgr.Save(util.IpsetTestConfigFile); err != nil {
784+
t.Errorf("TestAddToList failed @ ipsMgr.Save")
785+
}
786+
787+
defer func() {
788+
if err := ipsMgr.Restore(util.IpsetTestConfigFile); err != nil {
789+
t.Errorf("TestAddToList failed @ ipsMgr.Restore")
790+
}
791+
}()
792+
793+
testset1 := GetIPSetName()
794+
testset2 := GetIPSetName()
795+
796+
spec := append([]string{util.IpsetSetListFlag})
797+
entry := &ipsEntry{
798+
operationFlag: util.IpsetCreationFlag,
799+
set: util.GetHashedName(testset1),
800+
spec: spec,
801+
}
802+
803+
if _, err := ipsMgr.Run(entry); err != nil {
804+
t.Errorf("Expected to not fail when creating ipset: %+v", err)
805+
}
806+
807+
entry = &ipsEntry{
808+
operationFlag: util.IpsetAppendFlag,
809+
set: util.GetHashedName(testset1),
810+
spec: append([]string{util.GetHashedName(testset2)}),
811+
}
812+
813+
if _, err := ipsMgr.Run(entry); err == nil || err.ErrID != SetToBeAddedDeletedTestedDoesNotExist {
814+
t.Errorf("Expected to fail when adding set to list and the set doesn't exist: %+v", err)
815+
}
816+
}
817+
818+
*/
576819
func TestMain(m *testing.M) {
577820
metrics.InitializeAll()
578821
ipsMgr := NewIpsetManager()

0 commit comments

Comments
 (0)