Skip to content

Commit 0c6fbdd

Browse files
committed
fix integration tests
1 parent b6ed34d commit 0c6fbdd

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

controllers/contentlibrary/clustercontentlibraryitem/clustercontentlibraryitem_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sigs.k8s.io/controller-runtime/pkg/manager"
99

1010
imgregv1a1 "github.com/vmware-tanzu/image-registry-operator-api/api/v1alpha1"
11+
imgregv1 "github.com/vmware-tanzu/image-registry-operator-api/api/v1alpha2"
1112

1213
"github.com/vmware-tanzu/vm-operator/controllers/contentlibrary/utils"
1314
pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config"
@@ -22,7 +23,7 @@ import (
2223
// AddToManager adds this package's controller to the provided manager.
2324
func AddToManager(ctx *pkgctx.ControllerManagerContext, mgr manager.Manager) error {
2425
if pkgcfg.FromContext(ctx).Features.InventoryContentLibrary {
25-
return utils.AddToManagerV1A2(ctx, mgr, &imgregv1a1.ClusterContentLibraryItem{})
26+
return utils.AddToManagerV1A2(ctx, mgr, &imgregv1.ClusterContentLibraryItem{})
2627
}
2728
return utils.AddToManager(ctx, mgr, &imgregv1a1.ClusterContentLibraryItem{})
2829
}

controllers/contentlibrary/contentlibraryitem/contentlibraryitem_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sigs.k8s.io/controller-runtime/pkg/manager"
99

1010
imgregv1a1 "github.com/vmware-tanzu/image-registry-operator-api/api/v1alpha1"
11+
imgregv1 "github.com/vmware-tanzu/image-registry-operator-api/api/v1alpha2"
1112

1213
"github.com/vmware-tanzu/vm-operator/controllers/contentlibrary/utils"
1314
pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config"
@@ -22,7 +23,7 @@ import (
2223
// AddToManager adds this package's controller to the provided manager.
2324
func AddToManager(ctx *pkgctx.ControllerManagerContext, mgr manager.Manager) error {
2425
if pkgcfg.FromContext(ctx).Features.InventoryContentLibrary {
25-
return utils.AddToManagerV1A2(ctx, mgr, &imgregv1a1.ContentLibraryItem{})
26+
return utils.AddToManagerV1A2(ctx, mgr, &imgregv1.ContentLibraryItem{})
2627
}
2728
return utils.AddToManager(ctx, mgr, &imgregv1a1.ContentLibraryItem{})
2829
}

test/builder/test_suite.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,29 @@ func (s *TestSuite) beforeSuiteForIntegrationTesting() {
532532
Expect(s.config).ToNot(BeNil())
533533
})
534534

535+
By("updating image registry CRD storage version", func() {
536+
// Update storage version of ContentLibraryItems if necessary
537+
cliCrd := s.GetInstalledCRD("contentlibraryitems.imageregistry.vmware.com")
538+
Expect(cliCrd).ToNot(BeNil())
539+
s.UpdateCRDStorage(cliCrd)
540+
// Update storage version of ClusterContentLibraryItems if necessary
541+
ccliCrd := s.GetInstalledCRD("clustercontentlibraryitems.imageregistry.vmware.com")
542+
Expect(ccliCrd).ToNot(BeNil())
543+
s.UpdateCRDStorage(ccliCrd)
544+
// Update storage version of ContentLibraries if necessary
545+
clCrd := s.GetInstalledCRD("contentlibraries.imageregistry.vmware.com")
546+
Expect(clCrd).ToNot(BeNil())
547+
s.UpdateCRDStorage(clCrd)
548+
// Update storage version of ClusterContentLibraries if necessary
549+
cclCrd := s.GetInstalledCRD("clustercontentlibraries.imageregistry.vmware.com")
550+
Expect(cclCrd).ToNot(BeNil())
551+
s.UpdateCRDStorage(cclCrd)
552+
// Update storage version of ContentLibraryItemImportRequests if necessary
553+
cliImportReqCrd := s.GetInstalledCRD("contentlibraryitemimportrequests.imageregistry.vmware.com")
554+
Expect(cliImportReqCrd).ToNot(BeNil())
555+
s.UpdateCRDStorage(cliImportReqCrd)
556+
})
557+
535558
// If one or more webhooks are being tested then go ahead and generate a
536559
// PKI toolchain to use with the webhook server.
537560
if s.isWebhookTest() {
@@ -631,6 +654,32 @@ func (s *TestSuite) GetInstalledCRD(crdName string) *apiextensionsv1.CustomResou
631654
return nil
632655
}
633656

657+
func (s *TestSuite) UpdateCRDStorage(oldCrd *apiextensionsv1.CustomResourceDefinition) {
658+
659+
err := envtest.UninstallCRDs(s.envTest.Config, envtest.CRDInstallOptions{
660+
CRDs: []*apiextensionsv1.CustomResourceDefinition{oldCrd},
661+
})
662+
Expect(err).ToNot(HaveOccurred())
663+
664+
crds := make([]*apiextensionsv1.CustomResourceDefinition, 0)
665+
updatedCrd := updateImgRegStorageVersion(s.Context, *oldCrd)
666+
667+
Eventually(func() error {
668+
newCrd := &apiextensionsv1.CustomResourceDefinition{
669+
ObjectMeta: metav1.ObjectMeta{
670+
Name: updatedCrd.Name,
671+
Annotations: updatedCrd.Annotations,
672+
},
673+
Spec: updatedCrd.Spec,
674+
}
675+
crds, err = envtest.InstallCRDs(s.envTest.Config, envtest.CRDInstallOptions{
676+
CRDs: []*apiextensionsv1.CustomResourceDefinition{newCrd},
677+
})
678+
return err
679+
}).ShouldNot(HaveOccurred())
680+
s.envTest.CRDs = append(s.envTest.CRDs, crds...)
681+
}
682+
634683
func (s *TestSuite) UpdateCRDScope(oldCrd *apiextensionsv1.CustomResourceDefinition, newScope string) {
635684
// crd.spec.scope is immutable, uninstall first
636685
err := envtest.UninstallCRDs(s.envTest.Config, envtest.CRDInstallOptions{

test/builder/util.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"os"
1414
"path/filepath"
1515

16+
pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config"
17+
1618
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1719
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1820
"k8s.io/apimachinery/pkg/runtime"
@@ -47,6 +49,37 @@ func applyFeatureStateFnsToCRD(
4749
return crd
4850
}
4951

52+
func updateImgRegStorageVersion(ctx context.Context, crd apiextensionsv1.CustomResourceDefinition) apiextensionsv1.CustomResourceDefinition {
53+
54+
v1a1Idx := indexOfVersion(crd, "v1alpha1")
55+
v1a2Idx := indexOfVersion(crd, "v1alpha2")
56+
57+
if pkgcfg.FromContext(ctx).Features.InventoryContentLibrary {
58+
if v1a1Idx >= 0 {
59+
crd.Spec.Versions[v1a1Idx].Storage = !(v1a2Idx >= 0) //nolint:staticcheck
60+
crd.Spec.Versions[v1a1Idx].Served = true
61+
}
62+
63+
if v1a2Idx >= 0 {
64+
crd.Spec.Versions[v1a2Idx].Storage = true
65+
crd.Spec.Versions[v1a2Idx].Served = true
66+
}
67+
} else if v1a1Idx >= 0 {
68+
crd.Spec.Versions[v1a1Idx].Storage = true
69+
crd.Spec.Versions[v1a1Idx].Served = true
70+
71+
if v1a2Idx >= 0 {
72+
var zeroVal apiextensionsv1.CustomResourceDefinitionVersion
73+
74+
copy(crd.Spec.Versions[v1a2Idx:], crd.Spec.Versions[v1a2Idx+1:])
75+
crd.Spec.Versions[len(crd.Spec.Versions)-1] = zeroVal
76+
crd.Spec.Versions = crd.Spec.Versions[:len(crd.Spec.Versions)-1]
77+
}
78+
}
79+
80+
return crd
81+
}
82+
5083
// indexOfVersion returns the index of the specified schema version for a given
5184
// CRD. This function is useful for writing the functions that are passed into
5285
// the applyFeatureStateFnsToCRD function.

0 commit comments

Comments
 (0)