github.com/cilium/cilium@v1.16.2/operator/identitygc/crd_gc_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package identitygc 5 6 import ( 7 "context" 8 "testing" 9 "time" 10 11 "github.com/cilium/hive/cell" 12 "github.com/cilium/hive/hivetest" 13 "github.com/google/go-cmp/cmp" 14 meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 15 16 "github.com/cilium/cilium/operator/k8s" 17 tu "github.com/cilium/cilium/operator/pkg/ciliumendpointslice/testutils" 18 "github.com/cilium/cilium/pkg/hive" 19 cilium_v2a1 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1" 20 k8sClient "github.com/cilium/cilium/pkg/k8s/client" 21 "github.com/cilium/cilium/pkg/k8s/resource" 22 "github.com/cilium/cilium/pkg/testutils" 23 ) 24 25 func TestUsedIdentitiesInCESs(t *testing.T) { 26 var fakeClient k8sClient.FakeClientset 27 var ciliumEndpointSlice resource.Resource[*cilium_v2a1.CiliumEndpointSlice] 28 hive := hive.New( 29 k8sClient.FakeClientCell, 30 k8s.ResourcesCell, 31 cell.Invoke(func(c *k8sClient.FakeClientset, ces resource.Resource[*cilium_v2a1.CiliumEndpointSlice]) error { 32 fakeClient = *c 33 ciliumEndpointSlice = ces 34 return nil 35 }), 36 ) 37 tlog := hivetest.Logger(t) 38 err := hive.Start(tlog, context.Background()) 39 if err != nil { 40 t.Fatalf("unable to start hive for the test: %s", err) 41 } 42 43 cesStore, _ := ciliumEndpointSlice.Store(context.Background()) 44 45 // Empty store. 46 gotIdentities := usedIdentitiesInCESs(cesStore) 47 wantIdentities := make(map[string]bool) 48 assertEqualIDs(t, wantIdentities, gotIdentities) 49 50 // 5 IDs in the store. 51 cesA := tu.CreateCESWithIDs("cesA", []int64{1, 2, 3, 4, 5}) 52 fakeClient.CiliumV2alpha1().CiliumEndpointSlices().Create(context.Background(), cesA, meta_v1.CreateOptions{}) 53 err = testutils.WaitUntil(isCESPresent("cesA", cesStore), time.Second) 54 if err != nil { 55 t.Fatalf("cesA not present in the store after timeout: %s", err) 56 } 57 wantIdentities["1"] = true 58 wantIdentities["2"] = true 59 wantIdentities["3"] = true 60 wantIdentities["4"] = true 61 wantIdentities["5"] = true 62 gotIdentities = usedIdentitiesInCESs(cesStore) 63 assertEqualIDs(t, wantIdentities, gotIdentities) 64 65 // 10 IDs in the store. 66 cesB := tu.CreateCESWithIDs("cesB", []int64{10, 20, 30, 40, 50}) 67 fakeClient.CiliumV2alpha1().CiliumEndpointSlices().Create(context.Background(), cesB, meta_v1.CreateOptions{}) 68 err = testutils.WaitUntil(isCESPresent("cesB", cesStore), time.Second) 69 if err != nil { 70 t.Fatalf("cesB not present in the store after timeout: %s", err) 71 } 72 wantIdentities["10"] = true 73 wantIdentities["20"] = true 74 wantIdentities["30"] = true 75 wantIdentities["40"] = true 76 wantIdentities["50"] = true 77 gotIdentities = usedIdentitiesInCESs(cesStore) 78 assertEqualIDs(t, wantIdentities, gotIdentities) 79 80 err = hive.Stop(tlog, context.Background()) 81 if err != nil { 82 t.Fatalf("unable to stop hive for the test: %s", err) 83 } 84 } 85 86 func isCESPresent(cesName string, cesStore resource.Store[*cilium_v2a1.CiliumEndpointSlice]) testutils.ConditionFunc { 87 return func() bool { 88 _, exists, _ := cesStore.GetByKey(resource.Key{Name: cesName}) 89 return exists 90 } 91 } 92 93 func assertEqualIDs(t *testing.T, wantIdentities, gotIdentities map[string]bool) { 94 t.Helper() 95 if diff := cmp.Diff(wantIdentities, gotIdentities); diff != "" { 96 t.Errorf("Unexpected Identites in the CES store (-want +got): \n%s", diff) 97 } 98 }