Skip to content

Commit

Permalink
Improve some Antrea IPAM unit tests (#6758)
Browse files Browse the repository at this point in the history
The tests should wait for the informers cache to have synced. Also
remove some unnecessary code in TestStatefulSetLifecycle.

Signed-off-by: Antonin Bas <[email protected]>
  • Loading branch information
antoninbas authored Oct 22, 2024
1 parent 83d37c0 commit 90b1cb9
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions pkg/controller/ipam/antrea_ipam_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
crdinformers "antrea.io/antrea/pkg/client/informers/externalversions"
listers "antrea.io/antrea/pkg/client/listers/crd/v1beta1"
annotation "antrea.io/antrea/pkg/ipam"
"antrea.io/antrea/pkg/ipam/poolallocator"
)

type fakeAntreaIPAMController struct {
Expand Down Expand Up @@ -122,9 +121,8 @@ func initTestObjects(annotateNamespace bool, annotateStatefulSet bool, replicas
return namespace, pool, statefulSet
}

func verifyPoolAllocatedSize(t *testing.T, poolName string, poolLister listers.IPPoolLister, size int) {

err := wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 1*time.Second, true,
func verifyPoolAllocatedSize(ctx context.Context, t *testing.T, poolName string, poolLister listers.IPPoolLister, size int) {
err := wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, 1*time.Second, true,
func(ctx context.Context) (bool, error) {
pool, err := poolLister.Get(poolName)
if err != nil {
Expand All @@ -141,7 +139,6 @@ func verifyPoolAllocatedSize(t *testing.T, poolName string, poolLister listers.I
}

func TestStatefulSetLifecycle(t *testing.T) {

tests := []struct {
name string
dedicatedPool bool
Expand Down Expand Up @@ -176,38 +173,27 @@ func TestStatefulSetLifecycle(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
stopCh := make(chan struct{})
defer close(stopCh)

namespace, pool, statefulSet := initTestObjects(!tt.dedicatedPool, tt.dedicatedPool, tt.replicas)
controller := newFakeAntreaIPAMController(pool, namespace, statefulSet)
controller.informerFactory.Start(stopCh)
controller.crdInformerFactory.Start(stopCh)
controller.informerFactory.WaitForCacheSync(stopCh)
controller.crdInformerFactory.WaitForCacheSync(stopCh)

go controller.Run(stopCh)

var allocator *poolallocator.IPPoolAllocator
var err error
// Wait until pool propagates to the informer
pollErr := wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 3*time.Second, true,
func(ctx context.Context) (bool, error) {
allocator, err = poolallocator.NewIPPoolAllocator(pool.Name, controller.crdClient, controller.poolLister)
if err != nil {
return false, nil
}
return true, nil
})
require.NoError(t, pollErr)
defer allocator.ReleaseStatefulSet(statefulSet.Namespace, statefulSet.Name)

// Verify create event was handled by the controller
verifyPoolAllocatedSize(t, pool.Name, controller.poolLister, tt.expectAllocatedSize)
verifyPoolAllocatedSize(ctx, t, pool.Name, controller.poolLister, tt.expectAllocatedSize)

// Delete StatefulSet
controller.fakeK8sClient.AppsV1().StatefulSets(namespace.Name).Delete(context.TODO(), statefulSet.Name, metav1.DeleteOptions{})
controller.fakeK8sClient.AppsV1().StatefulSets(namespace.Name).Delete(ctx, statefulSet.Name, metav1.DeleteOptions{})

// Verify Delete event was processed
verifyPoolAllocatedSize(t, pool.Name, controller.poolLister, 0)
verifyPoolAllocatedSize(ctx, t, pool.Name, controller.poolLister, 0)
})
}
}
Expand Down Expand Up @@ -259,6 +245,8 @@ func TestReleaseStaleAddresses(t *testing.T) {
controller := newFakeAntreaIPAMController(pool, namespace, statefulSet)
controller.informerFactory.Start(stopCh)
controller.crdInformerFactory.Start(stopCh)
controller.informerFactory.WaitForCacheSync(stopCh)
controller.crdInformerFactory.WaitForCacheSync(stopCh)

go controller.Run(stopCh)

Expand Down Expand Up @@ -333,6 +321,8 @@ func TestAntreaIPAMController_getIPPoolsForStatefulSet(t *testing.T) {
controller := newFakeAntreaIPAMController(pool, namespace, statefulSet)
controller.informerFactory.Start(stopCh)
controller.crdInformerFactory.Start(stopCh)
controller.informerFactory.WaitForCacheSync(stopCh)
controller.crdInformerFactory.WaitForCacheSync(stopCh)

got, got1 := controller.getIPPoolsForStatefulSet(statefulSet)
var want []string
Expand Down

0 comments on commit 90b1cb9

Please sign in to comment.