@@ -13,9 +13,11 @@ import (
1313 . "github.com/onsi/ginkgo/v2"
1414 . "github.com/onsi/gomega"
1515 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
16+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1617 "k8s.io/apimachinery/pkg/types"
1718
1819 cmdCRDInstaller "go.goms.io/fleet/cmd/crdinstaller/utils"
20+ "go.goms.io/fleet/pkg/utils"
1921)
2022
2123const (
@@ -33,53 +35,113 @@ const (
3335// It ensures that the installer can create a CRD, update it with new fields, and handle ownership label correctly.
3436// The original CRD has 4 properties, and the updated CRD has a new property to simulate CRD upgrade.
3537var _ = Describe ("Test CRD Installer, Create and Update CRD" , Ordered , func () {
36- It ("should create original CRD" , func () {
37- crd , err := cmdCRDInstaller .GetCRDFromPath (originalCRDPath , scheme )
38- Expect (err ).NotTo (HaveOccurred (), "should get CRD from path %s" , originalCRDPath )
39- Expect (cmdCRDInstaller .InstallCRD (ctx , k8sClient , crd )).To (Succeed ())
38+ AfterEach (OncePerOrdered , func () {
39+ deleteCRD (crdName )
4040 })
4141
42- It ("should verify original CRD installation" , func () {
43- ensureCRDExistsWithLabels (map [string ]string {
44- cmdCRDInstaller .CRDInstallerLabelKey : "true" ,
45- cmdCRDInstaller .AzureManagedLabelKey : cmdCRDInstaller .FleetLabelValue ,
42+ Context ("without ARC installation mode" , func () {
43+ It ("should create original CRD" , func () {
44+ crd , err := cmdCRDInstaller .GetCRDFromPath (originalCRDPath , scheme )
45+ Expect (err ).NotTo (HaveOccurred (), "should get CRD from path %s" , originalCRDPath )
46+ Eventually (func () error {
47+ return cmdCRDInstaller .InstallCRD (ctx , k8sClient , crd , cmdCRDInstaller .ModeMember )
48+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
4649 })
47- crd := & apiextensionsv1.CustomResourceDefinition {}
48- Expect (k8sClient .Get (ctx , types.NamespacedName {Name : crdName }, crd )).NotTo (HaveOccurred (), "CRD %s should be installed" , crdName )
49- spec := fetchSpecJSONSchemaProperties (crd )
50- // Original CRD should have 4 properties defined in spec.
51- Expect (len (spec .Properties )).Should (Equal (4 ), "CRD %s should have 4 properties defined in spec" , crdName )
52- Expect (spec .Properties ["bar" ].Type ).Should (Equal ("string" ), "CRD %s should have 'bar' property of type string defined in properties" , crdName )
53- _ , ok := spec .Properties ["newField" ]
54- Expect (ok ).To (BeFalse (), "CRD %s should not have 'newField' property defined in properties" , crdName )
55- })
5650
57- It ("update the CRD to add a random label" , func () {
58- updateCRDLabels (crdName , map [string ]string {randomLabelKey : "true" })
59- })
51+ It ("should verify original CRD installation" , func () {
52+ ensureCRDExistsWithLabels (map [string ]string {
53+ cmdCRDInstaller .CRDInstallerLabelKey : "true" ,
54+ cmdCRDInstaller .AzureManagedLabelKey : cmdCRDInstaller .FleetLabelValue ,
55+ })
56+ crd := & apiextensionsv1.CustomResourceDefinition {}
57+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : crdName }, crd )).NotTo (HaveOccurred (), "CRD %s should be installed" , crdName )
58+ spec := fetchSpecJSONSchemaProperties (crd )
59+ // Original CRD should have 4 properties defined in spec.
60+ Expect (len (spec .Properties )).Should (Equal (4 ), "CRD %s should have 4 properties defined in spec" , crdName )
61+ Expect (spec .Properties ["bar" ].Type ).Should (Equal ("string" ), "CRD %s should have 'bar' property of type string defined in properties" , crdName )
62+ _ , ok := spec .Properties ["newField" ]
63+ Expect (ok ).To (BeFalse (), "CRD %s should not have 'newField' property defined in properties" , crdName )
64+ })
65+
66+ It ("update the CRD to add a random label" , func () {
67+ updateCRDLabels (crdName , map [string ]string {randomLabelKey : "true" })
68+ })
6069
61- It ("should update the CRD with new field in spec with crdinstaller label" , func () {
62- crd , err := cmdCRDInstaller .GetCRDFromPath (updatedCRDPath , scheme )
63- Expect (err ).NotTo (HaveOccurred (), "should get CRD from path %s" , updatedCRDPath )
64- Expect (cmdCRDInstaller .InstallCRD (ctx , k8sClient , crd )).To (Succeed ())
70+ It ("should update the CRD with new field in spec with crdinstaller label" , func () {
71+ crd , err := cmdCRDInstaller .GetCRDFromPath (updatedCRDPath , scheme )
72+ Expect (err ).NotTo (HaveOccurred (), "should get CRD from path %s" , updatedCRDPath )
73+ Eventually (func () error {
74+ return cmdCRDInstaller .InstallCRD (ctx , k8sClient , crd , cmdCRDInstaller .ModeMember )
75+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
76+ })
77+
78+ It ("should verify updated CRD" , func () {
79+ // ensure we don't overwrite the random label.
80+ ensureCRDExistsWithLabels (map [string ]string {
81+ randomLabelKey : "true" ,
82+ cmdCRDInstaller .CRDInstallerLabelKey : "true" ,
83+ cmdCRDInstaller .AzureManagedLabelKey : cmdCRDInstaller .FleetLabelValue ,
84+ })
85+ crd := & apiextensionsv1.CustomResourceDefinition {}
86+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : crdName }, crd )).NotTo (HaveOccurred (), "CRD %s should be installed" , crdName )
87+ spec := fetchSpecJSONSchemaProperties (crd )
88+ // Updated CRD should have 5 properties defined in spec.
89+ Expect (len (spec .Properties )).Should (Equal (5 ), "CRD %s should have 5 properties defined in spec" , crdName )
90+ Expect (spec .Properties ["bar" ].Type ).Should (Equal ("string" ), "CRD %s should have 'bar' property of type string defined in properties" , crdName )
91+ _ , ok := spec .Properties ["newField" ]
92+ Expect (ok ).To (BeTrue (), "CRD %s should have 'newField' property defined in properties" , crdName )
93+ Expect (spec .Properties ["newField" ].Type ).Should (Equal ("string" ), "CRD %s should have 'newField' property of type string defined in properties" , crdName )
94+ })
6595 })
6696
67- It ("should verify updated CRD" , func () {
68- // ensure we don't overwrite the random label.
69- ensureCRDExistsWithLabels (map [string ]string {
70- randomLabelKey : "true" ,
71- cmdCRDInstaller .CRDInstallerLabelKey : "true" ,
72- cmdCRDInstaller .AzureManagedLabelKey : cmdCRDInstaller .FleetLabelValue ,
97+ Context ("with ARC installation mode" , func () {
98+ It ("should create CRD with ARC installation mode" , func () {
99+ crd , err := cmdCRDInstaller .GetCRDFromPath (originalCRDPath , scheme )
100+ Expect (err ).NotTo (HaveOccurred (), "should get CRD from path %s" , originalCRDPath )
101+ Eventually (func () error {
102+ return cmdCRDInstaller .InstallCRD (ctx , k8sClient , crd , cmdCRDInstaller .ModeArcMember )
103+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
104+ })
105+
106+ It ("should verify CRD has ARC installation label" , func () {
107+ ensureCRDExistsWithLabels (map [string ]string {
108+ cmdCRDInstaller .CRDInstallerLabelKey : "true" ,
109+ cmdCRDInstaller .AzureManagedLabelKey : cmdCRDInstaller .ModeArcMember ,
110+ })
111+ crd := & apiextensionsv1.CustomResourceDefinition {}
112+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : crdName }, crd )).NotTo (HaveOccurred (), "CRD %s should be installed" , crdName )
113+ spec := fetchSpecJSONSchemaProperties (crd )
114+ // Original CRD should have 4 properties defined in spec.
115+ Expect (len (spec .Properties )).Should (Equal (4 ), "CRD %s should have 4 properties defined in spec" , crdName )
116+ })
117+
118+ It ("update the CRD to add a random label" , func () {
119+ updateCRDLabels (crdName , map [string ]string {randomLabelKey : "true" })
120+ })
121+
122+ It ("should update the CRD with new field in spec while preserving ARC label" , func () {
123+ crd , err := cmdCRDInstaller .GetCRDFromPath (updatedCRDPath , scheme )
124+ Expect (err ).NotTo (HaveOccurred (), "should get CRD from path %s" , updatedCRDPath )
125+ Eventually (func () error {
126+ return cmdCRDInstaller .InstallCRD (ctx , k8sClient , crd , cmdCRDInstaller .ModeArcMember )
127+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
128+ })
129+
130+ It ("should verify updated CRD has all labels including ARC" , func () {
131+ // ensure we don't overwrite the random label and ARC label is preserved.
132+ ensureCRDExistsWithLabels (map [string ]string {
133+ randomLabelKey : "true" ,
134+ cmdCRDInstaller .CRDInstallerLabelKey : "true" ,
135+ cmdCRDInstaller .AzureManagedLabelKey : cmdCRDInstaller .ModeArcMember ,
136+ })
137+ crd := & apiextensionsv1.CustomResourceDefinition {}
138+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : crdName }, crd )).NotTo (HaveOccurred (), "CRD %s should be installed" , crdName )
139+ spec := fetchSpecJSONSchemaProperties (crd )
140+ // Updated CRD should have 5 properties defined in spec.
141+ Expect (len (spec .Properties )).Should (Equal (5 ), "CRD %s should have 5 properties defined in spec" , crdName )
142+ _ , ok := spec .Properties ["newField" ]
143+ Expect (ok ).To (BeTrue (), "CRD %s should have 'newField' property defined in properties" , crdName )
73144 })
74- crd := & apiextensionsv1.CustomResourceDefinition {}
75- Expect (k8sClient .Get (ctx , types.NamespacedName {Name : crdName }, crd )).NotTo (HaveOccurred (), "CRD %s should be installed" , crdName )
76- spec := fetchSpecJSONSchemaProperties (crd )
77- // Updated CRD should have 5 properties defined in spec.
78- Expect (len (spec .Properties )).Should (Equal (5 ), "CRD %s should have 5 properties defined in spec" , crdName )
79- Expect (spec .Properties ["bar" ].Type ).Should (Equal ("string" ), "CRD %s should have 'bar' property of type string defined in properties" , crdName )
80- _ , ok := spec .Properties ["newField" ]
81- Expect (ok ).To (BeTrue (), "CRD %s should have 'newField' property defined in properties" , crdName )
82- Expect (spec .Properties ["newField" ].Type ).Should (Equal ("string" ), "CRD %s should have 'newField' property of type string defined in properties" , crdName )
83145 })
84146})
85147
@@ -115,5 +177,17 @@ func ensureCRDExistsWithLabels(wantLabels map[string]string) {
115177 return fmt .Errorf ("crd labels mismatch (-want, +got) :\n %s" , diff )
116178 }
117179 return nil
118- }, eventuallyDuration , eventuallyInterval ).ShouldNot (HaveOccurred (), "CRD %s should exist with labels %v" , crdName )
180+ }, eventuallyDuration , eventuallyInterval ).ShouldNot (HaveOccurred (), "CRD %s should exist with labels %v" , crdName , wantLabels )
181+ }
182+
183+ func deleteCRD (crdName string ) {
184+ crd := & apiextensionsv1.CustomResourceDefinition {
185+ ObjectMeta : metav1.ObjectMeta {
186+ Name : crdName ,
187+ },
188+ }
189+ Expect (k8sClient .Delete (ctx , crd )).Should (SatisfyAny (Succeed (), utils.NotFoundMatcher {}))
190+ Eventually (func () error {
191+ return k8sClient .Get (ctx , types.NamespacedName {Name : crdName }, crd )
192+ }, eventuallyDuration , eventuallyInterval ).Should (utils.NotFoundMatcher {}, "CRD %s should be deleted" , crdName )
119193}
0 commit comments