@@ -9,9 +9,11 @@ import (
9
9
"time"
10
10
11
11
"github.com/stretchr/testify/assert"
12
+ "github.com/stretchr/testify/mock"
12
13
"golang.org/x/time/rate"
13
14
"k8s.io/apimachinery/pkg/util/cache"
14
15
"k8s.io/apimachinery/pkg/util/sets"
16
+ "k8s.io/apimachinery/pkg/util/wait"
15
17
16
18
"github.com/AliyunContainerService/terway/pkg/factory"
17
19
factorymocks "github.com/AliyunContainerService/terway/pkg/factory/mocks"
@@ -571,3 +573,161 @@ func TestAllocFromFactory(t *testing.T) {
571
573
assert .Equal (t , req1 , local .allocatingV4 [0 ])
572
574
assert .Equal (t , req1 , local .allocatingV6 [0 ])
573
575
}
576
+
577
+ func Test_factoryDisposeWorker_unAssignIP (t * testing.T ) {
578
+ f := factorymocks .NewFactory (t )
579
+ // even we have two jobs ,we only get one ip
580
+ f .On ("UnAssignNIPv4" , "eni-1" , []netip.Addr {netip .MustParseAddr ("192.0.2.1" )}, mock .Anything ).Return (nil ).Once ()
581
+ f .On ("UnAssignNIPv6" , "eni-1" , []netip.Addr {netip .MustParseAddr ("fd00::1" )}, mock .Anything ).Return (nil ).Once ()
582
+
583
+ local := NewLocalTest (& daemon.ENI {ID : "eni-1" }, f , & types.PoolConfig {
584
+ EnableIPv4 : true ,
585
+ EnableIPv6 : true ,
586
+ BatchSize : 10 ,
587
+ }, "" )
588
+ local .status = statusInUse
589
+
590
+ local .ipv4 .Add (& IP {
591
+ ip : netip .MustParseAddr ("192.0.2.1" ),
592
+ primary : false ,
593
+ podID : "" ,
594
+ status : ipStatusDeleting ,
595
+ })
596
+
597
+ local .ipv4 .Add (& IP {
598
+ ip : netip .MustParseAddr ("192.0.2.2" ),
599
+ primary : false ,
600
+ podID : "" ,
601
+ status : ipStatusValid ,
602
+ })
603
+
604
+ local .ipv6 .Add (& IP {
605
+ ip : netip .MustParseAddr ("fd00::1" ),
606
+ primary : false ,
607
+ podID : "" ,
608
+ status : ipStatusDeleting ,
609
+ })
610
+
611
+ ctx , cancel := context .WithCancel (context .Background ())
612
+ defer cancel ()
613
+ go local .factoryDisposeWorker (ctx )
614
+
615
+ err := wait .ExponentialBackoff (wait.Backoff {
616
+ Duration : 100 * time .Millisecond ,
617
+ Steps : 10 ,
618
+ }, func () (done bool , err error ) {
619
+ local .cond .L .Lock ()
620
+ defer local .cond .L .Unlock ()
621
+
622
+ if len (local .ipv6 ) == 0 && len (local .ipv4 ) == 1 {
623
+ return true , nil
624
+ }
625
+ return false , nil
626
+ })
627
+ assert .NoError (t , err )
628
+ }
629
+
630
+ func Test_factoryDisposeWorker_releaseIP (t * testing.T ) {
631
+ f := factorymocks .NewFactory (t )
632
+ // even we have two jobs ,we only get one ip
633
+ f .On ("DeleteNetworkInterface" , "eni-1" ).Return (nil ).Once ()
634
+
635
+ local := NewLocalTest (& daemon.ENI {ID : "eni-1" }, f , & types.PoolConfig {
636
+ EnableIPv4 : true ,
637
+ EnableIPv6 : true ,
638
+ BatchSize : 10 ,
639
+ }, "" )
640
+ local .status = statusDeleting
641
+
642
+ ctx , cancel := context .WithCancel (context .Background ())
643
+ defer cancel ()
644
+
645
+ go local .factoryDisposeWorker (ctx )
646
+
647
+ err := wait .ExponentialBackoff (wait.Backoff {
648
+ Duration : 100 * time .Millisecond ,
649
+ Steps : 10 ,
650
+ }, func () (done bool , err error ) {
651
+ local .cond .L .Lock ()
652
+ defer local .cond .L .Unlock ()
653
+ if local .eni == nil {
654
+ return true , nil
655
+ }
656
+ return false , nil
657
+ })
658
+
659
+ assert .NoError (t , err )
660
+ }
661
+
662
+ func Test_commit_responsed (t * testing.T ) {
663
+ f := factorymocks .NewFactory (t )
664
+ local := NewLocalTest (& daemon.ENI {ID : "eni-1" }, f , & types.PoolConfig {
665
+ EnableIPv4 : true ,
666
+ EnableIPv6 : true ,
667
+ BatchSize : 10 ,
668
+ }, "" )
669
+ local .status = statusInUse
670
+
671
+ ctx , cancel := context .WithCancel (context .Background ())
672
+ defer cancel ()
673
+
674
+ respCh := make (chan * AllocResp )
675
+ ipv4 := & IP {
676
+ ip : netip .MustParseAddr ("127.0.0.1" ),
677
+ primary : false ,
678
+ podID : "" ,
679
+ status : ipStatusValid ,
680
+ }
681
+ ipv6 := & IP {
682
+ ip : netip .MustParseAddr ("fd00::1" ),
683
+ primary : false ,
684
+ podID : "" ,
685
+ status : ipStatusValid ,
686
+ }
687
+ wg := sync.WaitGroup {}
688
+ wg .Add (1 )
689
+ go func () {
690
+ defer wg .Done ()
691
+
692
+ <- respCh
693
+ }()
694
+
695
+ local .commit (ctx , respCh , ipv4 , ipv6 , "foo" )
696
+
697
+ wg .Wait ()
698
+
699
+ assert .Equal (t , "foo" , ipv4 .podID )
700
+ assert .Equal (t , "foo" , ipv6 .podID )
701
+ }
702
+
703
+ func Test_commit_canceled (t * testing.T ) {
704
+ f := factorymocks .NewFactory (t )
705
+ local := NewLocalTest (& daemon.ENI {ID : "eni-1" }, f , & types.PoolConfig {
706
+ EnableIPv4 : true ,
707
+ EnableIPv6 : true ,
708
+ BatchSize : 10 ,
709
+ }, "" )
710
+ local .status = statusInUse
711
+
712
+ ctx , cancel := context .WithCancel (context .Background ())
713
+ cancel ()
714
+
715
+ respCh := make (chan * AllocResp )
716
+ ipv4 := & IP {
717
+ ip : netip .MustParseAddr ("127.0.0.1" ),
718
+ primary : false ,
719
+ podID : "foo" ,
720
+ status : ipStatusValid ,
721
+ }
722
+ ipv6 := & IP {
723
+ ip : netip .MustParseAddr ("fd00::1" ),
724
+ primary : false ,
725
+ podID : "foo" ,
726
+ status : ipStatusValid ,
727
+ }
728
+
729
+ local .commit (ctx , respCh , ipv4 , ipv6 , "foo" )
730
+
731
+ assert .Equal (t , "" , ipv4 .podID )
732
+ assert .Equal (t , "" , ipv6 .podID )
733
+ }
0 commit comments