From 387fe925b59af2c7dc622f5fc1458ec9b2fde1a3 Mon Sep 17 00:00:00 2001 From: Artem Bortnikov Date: Fri, 27 Dec 2024 16:57:28 +0200 Subject: [PATCH] rework objects creation in tests Signed-off-by: Artem Bortnikov --- .../clusterdeployment_controller_test.go | 362 +++++++++--------- .../controller/management_controller_test.go | 35 +- internal/controller/suite_test.go | 40 +- .../controller/template_controller_test.go | 41 +- 4 files changed, 252 insertions(+), 226 deletions(-) diff --git a/internal/controller/clusterdeployment_controller_test.go b/internal/controller/clusterdeployment_controller_test.go index 655824deb..0064875de 100644 --- a/internal/controller/clusterdeployment_controller_test.go +++ b/internal/controller/clusterdeployment_controller_test.go @@ -15,7 +15,6 @@ package controller import ( - "context" "time" hcv2 "github.com/fluxcd/helm-controller/api/v2" @@ -24,11 +23,9 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/rest" - "sigs.k8s.io/controller-runtime/pkg/client" . "sigs.k8s.io/controller-runtime/pkg/envtest/komega" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -38,64 +35,112 @@ import ( var _ = Describe("ClusterDeployment Controller", func() { Context("When reconciling a resource", func() { const ( - clusterDeploymentName = "test-cluster-deployment" - clusterDeploymentNamespace = "test" + helmChartURL = "http://source-controller.hmc-system.svc.cluster.local/helmchart/hmc-system/test-chart/0.1.0.tar.gz" + ) - templateName = "test-template" - svcTemplateName = "test-svc-template" - credentialName = "test-credential" + // resources required for ClusterDeployment reconciliation + var ( + namespace = corev1.Namespace{} + credential = hmc.Credential{} + clusterTemplate = hmc.ClusterTemplate{} + serviceTemplate = hmc.ServiceTemplate{} + helmRepo = sourcev1.HelmRepository{} + clusterTemplateHelmChart = sourcev1.HelmChart{} + serviceTemplateHelmChart = sourcev1.HelmChart{} - helmChartURL = "http://source-controller.hmc-system.svc.cluster.local/helmchart/hmc-system/test-chart/0.1.0.tar.gz" + clusterDeployment = hmc.ClusterDeployment{} + clusterDeploymentKey = types.NamespacedName{} ) - ctx := context.Background() + BeforeEach(func() { + By("Ensure namespace", func() { + namespace = corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: "test-namespace-", + }, + } + Expect(k8sClient.Create(ctx, &namespace)).To(Succeed()) + }) - typeNamespacedName := types.NamespacedName{ - Name: clusterDeploymentName, - Namespace: clusterDeploymentNamespace, - } - clusterDeployment := &hmc.ClusterDeployment{} - template := &hmc.ClusterTemplate{} - svcTemplate := &hmc.ServiceTemplate{} - management := &hmc.Management{} - credential := &hmc.Credential{} - namespace := &corev1.Namespace{} + By("Ensure HelmRepository resource", func() { + helmRepo = sourcev1.HelmRepository{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: "test-repository-", + Namespace: namespace.Name, + }, + Spec: sourcev1.HelmRepositorySpec{ + Insecure: true, + Interval: metav1.Duration{ + Duration: 10 * time.Minute, + }, + Provider: "generic", + Type: "oci", + URL: "oci://hmc-local-registry:5000/charts", + }, + } + Expect(k8sClient.Create(ctx, &helmRepo)).To(Succeed()) + }) - helmRepo := &sourcev1.HelmRepository{} - helmChart := &sourcev1.HelmChart{} + By("Ensure HelmChart resources", func() { + clusterTemplateHelmChart = sourcev1.HelmChart{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: "test-cluster-template-chart-", + Namespace: namespace.Name, + }, + Spec: sourcev1.HelmChartSpec{ + Chart: "test-cluster", + Interval: metav1.Duration{ + Duration: 10 * time.Minute, + }, + ReconcileStrategy: sourcev1.ReconcileStrategyChartVersion, + SourceRef: sourcev1.LocalHelmChartSourceReference{ + Kind: "HelmRepository", + Name: helmRepo.Name, + }, + Version: "0.1.0", + }, + } + Expect(k8sClient.Create(ctx, &clusterTemplateHelmChart)).To(Succeed()) - BeforeEach(func() { - By("creating ClusterDeployment namespace") - err := k8sClient.Get(ctx, types.NamespacedName{Name: clusterDeploymentNamespace}, namespace) - if err != nil && errors.IsNotFound(err) { - namespace = &corev1.Namespace{ + serviceTemplateHelmChart = sourcev1.HelmChart{ ObjectMeta: metav1.ObjectMeta{ - Name: clusterDeploymentNamespace, + GenerateName: "test-service-template-chart-", + Namespace: namespace.Name, + }, + Spec: sourcev1.HelmChartSpec{ + Chart: "test-service", + Interval: metav1.Duration{ + Duration: 10 * time.Minute, + }, + ReconcileStrategy: sourcev1.ReconcileStrategyChartVersion, + SourceRef: sourcev1.LocalHelmChartSourceReference{ + Kind: "HelmRepository", + Name: helmRepo.Name, + }, + Version: "0.1.0", }, } - Expect(k8sClient.Create(ctx, namespace)).To(Succeed()) - } + Expect(k8sClient.Create(ctx, &serviceTemplateHelmChart)).To(Succeed()) + }) - By("creating the custom resource for the Kind ClusterTemplate") - err = k8sClient.Get(ctx, typeNamespacedName, template) - if err != nil && errors.IsNotFound(err) { - template = &hmc.ClusterTemplate{ + By("Ensure ClusterTemplate resource", func() { + clusterTemplate = hmc.ClusterTemplate{ ObjectMeta: metav1.ObjectMeta{ - Name: templateName, - Namespace: clusterDeploymentNamespace, + GenerateName: "test-cluster-template-", + Namespace: namespace.Name, }, Spec: hmc.ClusterTemplateSpec{ Helm: hmc.HelmSpec{ ChartRef: &hcv2.CrossNamespaceSourceReference{ Kind: "HelmChart", - Name: "ref-test", - Namespace: "default", + Name: clusterTemplateHelmChart.Name, + Namespace: namespace.Name, }, }, }, } - Expect(k8sClient.Create(ctx, template)).To(Succeed()) - template.Status = hmc.ClusterTemplateStatus{ + Expect(k8sClient.Create(ctx, &clusterTemplate)).To(Succeed()) + clusterTemplate.Status = hmc.ClusterTemplateStatus{ TemplateStatusCommon: hmc.TemplateStatusCommon{ TemplateValidationStatus: hmc.TemplateValidationStatus{ Valid: true, @@ -106,67 +151,46 @@ var _ = Describe("ClusterDeployment Controller", func() { }, Providers: hmc.Providers{"infrastructure-aws"}, } - Expect(k8sClient.Status().Update(ctx, template)).To(Succeed()) - } + Expect(k8sClient.Status().Update(ctx, &clusterTemplate)).To(Succeed()) + }) - By("creating the custom resource for the Kind ServiceTemplate") - err = k8sClient.Get(ctx, client.ObjectKey{Namespace: clusterDeploymentNamespace, Name: svcTemplateName}, svcTemplate) - if err != nil && errors.IsNotFound(err) { - svcTemplate = &hmc.ServiceTemplate{ + By("Ensure ServiceTemplate resource", func() { + serviceTemplate = hmc.ServiceTemplate{ ObjectMeta: metav1.ObjectMeta{ - Name: svcTemplateName, - Namespace: clusterDeploymentNamespace, + GenerateName: "test-service-template-", + Namespace: namespace.Name, }, Spec: hmc.ServiceTemplateSpec{ Helm: hmc.HelmSpec{ ChartRef: &hcv2.CrossNamespaceSourceReference{ Kind: "HelmChart", - Name: "ref-test", - Namespace: "default", + Name: serviceTemplateHelmChart.Name, + Namespace: namespace.Name, }, }, }, } - Expect(k8sClient.Create(ctx, svcTemplate)).To(Succeed()) - svcTemplate.Status = hmc.ServiceTemplateStatus{ + Expect(k8sClient.Create(ctx, &serviceTemplate)).To(Succeed()) + serviceTemplate.Status = hmc.ServiceTemplateStatus{ TemplateStatusCommon: hmc.TemplateStatusCommon{ ChartRef: &hcv2.CrossNamespaceSourceReference{ Kind: "HelmChart", - Name: "ref-test", - Namespace: "default", + Name: serviceTemplateHelmChart.Name, + Namespace: namespace.Name, }, TemplateValidationStatus: hmc.TemplateValidationStatus{ Valid: true, }, }, } - Expect(k8sClient.Status().Update(ctx, svcTemplate)).To(Succeed()) - } + Expect(k8sClient.Status().Update(ctx, &serviceTemplate)).To(Succeed()) + }) - By("creating the custom resource for the Kind Management") - err = k8sClient.Get(ctx, typeNamespacedName, management) - if err != nil && errors.IsNotFound(err) { - management = &hmc.Management{ + By("Ensure Credential resource", func() { + credential = hmc.Credential{ ObjectMeta: metav1.ObjectMeta{ - Name: hmc.ManagementName, - }, - Spec: hmc.ManagementSpec{ - Release: "test-release", - }, - } - Expect(k8sClient.Create(ctx, management)).To(Succeed()) - management.Status = hmc.ManagementStatus{ - AvailableProviders: hmc.Providers{"infrastructure-aws"}, - } - Expect(k8sClient.Status().Update(ctx, management)).To(Succeed()) - } - By("creating the custom resource for the Kind Credential") - err = k8sClient.Get(ctx, typeNamespacedName, credential) - if err != nil && errors.IsNotFound(err) { - credential = &hmc.Credential{ - ObjectMeta: metav1.ObjectMeta{ - Name: credentialName, - Namespace: clusterDeploymentNamespace, + GenerateName: "test-credential-", + Namespace: namespace.Name, }, Spec: hmc.CredentialSpec{ IdentityRef: &corev1.ObjectReference{ @@ -176,53 +200,54 @@ var _ = Describe("ClusterDeployment Controller", func() { }, }, } - Expect(k8sClient.Create(ctx, credential)).To(Succeed()) + Expect(k8sClient.Create(ctx, &credential)).To(Succeed()) credential.Status = hmc.CredentialStatus{ Ready: true, } - Expect(k8sClient.Status().Update(ctx, credential)).To(Succeed()) - } + Expect(k8sClient.Status().Update(ctx, &credential)).To(Succeed()) + }) - By("creating the custom resource for the Kind ClusterDeployment") - err = k8sClient.Get(ctx, typeNamespacedName, clusterDeployment) - if err != nil && errors.IsNotFound(err) { - clusterDeployment = &hmc.ClusterDeployment{ + By("Ensure ClusterDeployment resource", func() { + clusterDeployment = hmc.ClusterDeployment{ ObjectMeta: metav1.ObjectMeta{ - Name: clusterDeploymentName, - Namespace: clusterDeploymentNamespace, + GenerateName: "test-cluster-deployment-", + Namespace: namespace.Name, }, Spec: hmc.ClusterDeploymentSpec{ - Template: templateName, - Credential: credentialName, + Template: clusterTemplate.Name, + Credential: credential.Name, Services: []hmc.ServiceSpec{ { - Template: svcTemplateName, - Name: "test-svc-name", + Template: serviceTemplate.Name, + Name: "test-service", }, }, }, } - Expect(k8sClient.Create(ctx, clusterDeployment)).To(Succeed()) - } + Expect(k8sClient.Create(ctx, &clusterDeployment)).To(Succeed()) + clusterDeploymentKey = types.NamespacedName{ + Namespace: clusterDeployment.Namespace, + Name: clusterDeployment.Name, + } + }) }) AfterEach(func() { - By("Cleanup") - - controllerReconciler := &ClusterDeploymentReconciler{ - Client: k8sClient, - } - - Expect(k8sClient.Delete(ctx, clusterDeployment)).To(Succeed()) - // Running reconcile to remove the finalizer and delete the ClusterDeployment - _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: typeNamespacedName}) - Expect(err).NotTo(HaveOccurred()) + By("Cleanup", func() { + controllerReconciler := &ClusterDeploymentReconciler{ + Client: mgrClient, + } - Eventually(k8sClient.Get, 1*time.Minute, 5*time.Second).WithArguments(ctx, typeNamespacedName, clusterDeployment).Should(HaveOccurred()) + // Running reconcile to remove the finalizer and delete the ClusterDeployment + Eventually(func(g Gomega) { + g.Expect(k8sClient.Delete(ctx, &clusterDeployment)).To(Succeed()) + _, _ = controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: clusterDeploymentKey}) + g.Expect(Get(&clusterDeployment)()).ShouldNot(Succeed()) + }).Should(Succeed()) - Expect(k8sClient.Delete(ctx, template)).To(Succeed()) - Expect(k8sClient.Delete(ctx, management)).To(Succeed()) - Expect(k8sClient.Delete(ctx, namespace)).To(Succeed()) + Expect(k8sClient.Delete(ctx, &clusterTemplate)).To(Succeed()) + Expect(k8sClient.Delete(ctx, &namespace)).To(Succeed()) + }) }) It("should successfully reconcile the resource", func() { @@ -232,96 +257,69 @@ var _ = Describe("ClusterDeployment Controller", func() { } By("Ensure finalizer is added", func() { - _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ - NamespacedName: typeNamespacedName, - }) - Expect(err).NotTo(HaveOccurred()) - Eventually(Object(clusterDeployment)).Should(SatisfyAll( - HaveField("Finalizers", ContainElement(hmc.ClusterDeploymentFinalizer)), - )) + Eventually(func(g Gomega) { + _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: clusterDeploymentKey, + }) + Expect(err).NotTo(HaveOccurred()) + g.Expect(Object(&clusterDeployment)()).Should(SatisfyAll( + HaveField("Finalizers", ContainElement(hmc.ClusterDeploymentFinalizer)), + )) + }).Should(Succeed()) }) By("Reconciling resource with finalizer", func() { - _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ - NamespacedName: typeNamespacedName, - }) - Expect(err).To(HaveOccurred()) - Eventually(Object(clusterDeployment)).Should(SatisfyAll( - HaveField("Status.Conditions", ContainElement(SatisfyAll( - HaveField("Type", hmc.TemplateReadyCondition), - HaveField("Status", metav1.ConditionTrue), - HaveField("Reason", hmc.SucceededReason), - HaveField("Message", "Template is valid"), - ))), - )) + Eventually(func(g Gomega) { + _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: clusterDeploymentKey, + }) + Expect(err).To(HaveOccurred()) + g.Expect(Object(&clusterDeployment)()).Should(SatisfyAll( + HaveField("Status.Conditions", ContainElement(SatisfyAll( + HaveField("Type", hmc.TemplateReadyCondition), + HaveField("Status", metav1.ConditionTrue), + HaveField("Reason", hmc.SucceededReason), + HaveField("Message", "Template is valid"), + ))), + )) + }).Should(Succeed()) }) - By("Creating absent required resources: HelmChart, HelmRepository", func() { - helmRepo = &sourcev1.HelmRepository{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-repository", - Namespace: "default", - }, - Spec: sourcev1.HelmRepositorySpec{ - Insecure: true, - Interval: metav1.Duration{ - Duration: 10 * time.Minute, - }, - Provider: "generic", - Type: "oci", - URL: "oci://hmc-local-registry:5000/charts", - }, - } - Expect(k8sClient.Create(ctx, helmRepo)).To(Succeed()) - - helmChart = &sourcev1.HelmChart{ - ObjectMeta: metav1.ObjectMeta{ - Name: "ref-test", - Namespace: "default", - }, - Spec: sourcev1.HelmChartSpec{ - Chart: "test", - Interval: metav1.Duration{ - Duration: 10 * time.Minute, - }, - ReconcileStrategy: sourcev1.ReconcileStrategyChartVersion, - SourceRef: sourcev1.LocalHelmChartSourceReference{ - Kind: "HelmRepository", - Name: helmRepo.Name, - }, - Version: "0.1.0", - }, - } - Expect(k8sClient.Create(ctx, helmChart)).To(Succeed()) - - _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ - NamespacedName: typeNamespacedName, - }) - Expect(err).To(HaveOccurred()) + By("Reconciling when dependencies are not in valid state", func() { + Eventually(func(g Gomega) { + _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: clusterDeploymentKey, + }) + g.Expect(err).To(HaveOccurred()) + g.Expect(err.Error()).To(ContainSubstring("helm chart source is not provided")) + }).Should(Succeed()) }) - // By("Patching ClusterTemplate status", func() { - // Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(template), template)).Should(Succeed()) - // template.Status.ChartRef = &hcv2.CrossNamespaceSourceReference{ + // By("Patching ClusterTemplate and corresponding HelmChart statuses", func() { + // Expect(Get(&clusterTemplate)()).To(Succeed()) + // clusterTemplate.Status.ChartRef = &hcv2.CrossNamespaceSourceReference{ // Kind: "HelmChart", - // Name: "ref-test", - // Namespace: "default", + // Name: clusterTemplateHelmChart.Name, + // Namespace: namespace.Name, // } - // Expect(k8sClient.Status().Update(ctx, template)).To(Succeed()) + // Expect(k8sClient.Status().Update(ctx, &clusterTemplate)).To(Succeed()) // - // helmChart.Status.URL = helmChartURL - // helmChart.Status.Artifact = &sourcev1.Artifact{ + // Expect(Get(&clusterTemplateHelmChart)()).To(Succeed()) + // clusterTemplateHelmChart.Status.URL = helmChartURL + // clusterTemplateHelmChart.Status.Artifact = &sourcev1.Artifact{ // URL: helmChartURL, // LastUpdateTime: metav1.Now(), // } - // Expect(k8sClient.Status().Update(ctx, helmChart)).To(Succeed()) + // Expect(k8sClient.Status().Update(ctx, &clusterTemplateHelmChart)).To(Succeed()) // // // todo: next error occurs due to dependency on helm library. The best way to mitigate this is to // // inject an interface into the reconciler struct that can be mocked out for testing. - // _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ - // NamespacedName: typeNamespacedName, - // }) - // Expect(err).To(HaveOccurred()) + // Eventually(func(g Gomega) { + // _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + // NamespacedName: clusterDeploymentKey, + // }) + // g.Expect(err).To(HaveOccurred()) + // }).Should(Succeed()) // }) }) }) diff --git a/internal/controller/management_controller_test.go b/internal/controller/management_controller_test.go index 4f05de291..cfd082f55 100644 --- a/internal/controller/management_controller_test.go +++ b/internal/controller/management_controller_test.go @@ -40,37 +40,26 @@ import ( var _ = Describe("Management Controller", func() { Context("When reconciling a resource", func() { - const resourceName = "test-resource" - ctx := context.Background() - - typeNamespacedName := types.NamespacedName{ - Name: resourceName, - Namespace: "default", - } - management := &hmcmirantiscomv1alpha1.Management{} + management := hmcmirantiscomv1alpha1.Management{} BeforeEach(func() { By("creating the custom resource for the Kind Management") - err := k8sClient.Get(ctx, typeNamespacedName, management) - if err != nil && apierrors.IsNotFound(err) { - resource := &hmcmirantiscomv1alpha1.Management{ - ObjectMeta: metav1.ObjectMeta{ - Name: resourceName, - Namespace: "default", - }, - Spec: hmcmirantiscomv1alpha1.ManagementSpec{ - Release: "test-release-name", - }, - } - Expect(k8sClient.Create(ctx, resource)).To(Succeed()) + management = hmcmirantiscomv1alpha1.Management{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: "test-management-", + }, + Spec: hmcmirantiscomv1alpha1.ManagementSpec{ + Release: "test-release-name", + }, } + Expect(k8sClient.Create(ctx, &management)).To(Succeed()) }) AfterEach(func() { resource := &hmcmirantiscomv1alpha1.Management{} - err := k8sClient.Get(ctx, typeNamespacedName, resource) - Expect(err).NotTo(HaveOccurred()) + key := client.ObjectKeyFromObject(&management) + Expect(k8sClient.Get(ctx, key, resource)).To(Succeed()) By("Cleanup the specific resource instance Management") Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) @@ -85,7 +74,7 @@ var _ = Describe("Management Controller", func() { } _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ - NamespacedName: typeNamespacedName, + NamespacedName: client.ObjectKeyFromObject(&management), }) Expect(err).NotTo(HaveOccurred()) }) diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index 95fd51ef5..df197d3fa 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -32,6 +32,8 @@ import ( . "github.com/onsi/gomega" sveltosv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1" admissionv1 "k8s.io/api/admissionregistration/v1" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" @@ -58,7 +60,7 @@ const ( validatingWebhookKind = "ValidatingWebhookConfiguration" testSystemNamespace = "test-system-namespace" - pollingInterval = 50 * time.Millisecond + pollingInterval = 30 * time.Millisecond eventuallyTimeout = 3 * time.Second ) @@ -205,6 +207,8 @@ var _ = BeforeSuite(func() { } return conn.Close() }).Should(Succeed()) + + Expect(seedClusterScopedResources(ctx, k8sClient)).To(Succeed()) }) var _ = AfterSuite(func() { @@ -252,3 +256,37 @@ func loadWebhooks(path string) ([]*admissionv1.ValidatingWebhookConfiguration, [ } return validatingWebhooks, mutatingWebhooks, err } + +func seedClusterScopedResources(ctx context.Context, k8sClient client.Client) error { + var ( + someProviderName = "test-provider-name" + otherProviderName = "test-provider-name-other" + someExposedContract = "v1beta1_v1beta2" + otherExposedContract = "v1beta1" + capiVersion = "v1beta1" + ) + management := &hmcmirantiscomv1alpha1.Management{} + + By("creating the custom resource for the Kind Management") + managementKey := client.ObjectKey{ + Name: hmcmirantiscomv1alpha1.ManagementName, + } + err := mgrClient.Get(ctx, managementKey, management) + if errors.IsNotFound(err) { + management = &hmcmirantiscomv1alpha1.Management{ + ObjectMeta: metav1.ObjectMeta{ + Name: hmcmirantiscomv1alpha1.ManagementName, + }, + Spec: hmcmirantiscomv1alpha1.ManagementSpec{ + Release: "test-release", + }, + } + Expect(k8sClient.Create(ctx, management)).To(Succeed()) + management.Status = hmcmirantiscomv1alpha1.ManagementStatus{ + AvailableProviders: []string{someProviderName, otherProviderName, "infrastructure-aws"}, + CAPIContracts: map[string]hmcmirantiscomv1alpha1.CompatibilityContracts{someProviderName: {capiVersion: someExposedContract}, otherProviderName: {capiVersion: otherExposedContract}}, + } + Expect(k8sClient.Status().Update(ctx, management)).To(Succeed()) + } + return client.IgnoreNotFound(err) +} diff --git a/internal/controller/template_controller_test.go b/internal/controller/template_controller_test.go index c44f842de..5ec32b353 100644 --- a/internal/controller/template_controller_test.go +++ b/internal/controller/template_controller_test.go @@ -256,22 +256,25 @@ var _ = Describe("Template Controller", func() { return nil }).WithTimeout(timeout).WithPolling(interval).Should(Succeed()) - By("Creating a management cluster object with proper required versions in status") - // must set status here since it's controller by another ctrl - mgmt := &hmcmirantiscomv1alpha1.Management{ - ObjectMeta: metav1.ObjectMeta{ - Name: mgmtName, - }, - Spec: hmcmirantiscomv1alpha1.ManagementSpec{ - Release: "test-release", - }, - } - Expect(k8sClient.Create(ctx, mgmt)).To(Succeed()) - mgmt.Status = hmcmirantiscomv1alpha1.ManagementStatus{ - AvailableProviders: []string{someProviderName, otherProviderName}, - CAPIContracts: map[string]hmcmirantiscomv1alpha1.CompatibilityContracts{someProviderName: {capiVersion: someExposedContract}, otherProviderName: {capiVersion: otherExposedContract}}, - } - Expect(k8sClient.Status().Update(ctx, mgmt)).To(Succeed()) + // By("Creating a management cluster object with proper required versions in status") + // // must set status here since it's controller by another ctrl + // mgmt := &hmcmirantiscomv1alpha1.Management{ + // ObjectMeta: metav1.ObjectMeta{ + // Name: mgmtName, + // }, + // Spec: hmcmirantiscomv1alpha1.ManagementSpec{ + // Release: "test-release", + // }, + // } + // Expect(k8sClient.Create(ctx, mgmt)).To(Succeed()) + // mgmt.Status = hmcmirantiscomv1alpha1.ManagementStatus{ + // AvailableProviders: []string{someProviderName, otherProviderName}, + // CAPIContracts: map[string]hmcmirantiscomv1alpha1.CompatibilityContracts{someProviderName: {capiVersion: someExposedContract}, otherProviderName: {capiVersion: otherExposedContract}}, + // } + // Expect(k8sClient.Status().Update(ctx, mgmt)).To(Succeed()) + mgmt := &hmcmirantiscomv1alpha1.Management{} + key := client.ObjectKey{Name: mgmtName} + Expect(k8sClient.Get(ctx, key, mgmt)).To(Succeed()) By("Checking the management cluster appears") Eventually(func() error { @@ -279,7 +282,7 @@ var _ = Describe("Template Controller", func() { return err } - if l := len(mgmt.Status.AvailableProviders); l != 2 { + if l := len(mgmt.Status.AvailableProviders); l != 3 { return fmt.Errorf("expected .status.availableProviders length to be exactly 2, got %d", l) } if l := len(mgmt.Status.CAPIContracts); l != 2 { @@ -323,13 +326,11 @@ var _ = Describe("Template Controller", func() { Expect(clusterTemplate.Status.ProviderContracts).To(BeEquivalentTo(map[string]string{otherProviderName: otherRequiredContract, someProviderName: someRequiredContract})) By("Removing the created objects") - Expect(k8sClient.Delete(ctx, mgmt)).To(Succeed()) Expect(k8sClient.Delete(ctx, clusterTemplate)).To(Succeed()) By("Checking the created objects have been removed") Eventually(func() bool { - return apierrors.IsNotFound(k8sClient.Get(ctx, client.ObjectKeyFromObject(mgmt), &hmcmirantiscomv1alpha1.Management{})) && - apierrors.IsNotFound(k8sClient.Get(ctx, client.ObjectKeyFromObject(clusterTemplate), &hmcmirantiscomv1alpha1.ClusterTemplate{})) + return apierrors.IsNotFound(k8sClient.Get(ctx, client.ObjectKeyFromObject(clusterTemplate), &hmcmirantiscomv1alpha1.ClusterTemplate{})) }).WithTimeout(timeout).WithPolling(interval).Should(BeTrue()) }) })