From c4483458ec6bc3c281ff0984c180b8d003d97910 Mon Sep 17 00:00:00 2001 From: Mario Manno Date: Wed, 11 Dec 2024 18:28:18 +0100 Subject: [PATCH] Cleanup agent integrationtests * remove init() for setting up test data per file, as test data is shared across files now. * helpers don't Expect so missing resources are retried --- integrationtests/agent/adoption_test.go | 16 ---- .../agent/bundle_deployment_drift_test.go | 32 ++----- .../agent/bundle_deployment_status_test.go | 21 ----- .../agent/helm_capabilities_test.go | 32 ------- integrationtests/agent/suite_test.go | 91 ++++++++++++++++++- 5 files changed, 96 insertions(+), 96 deletions(-) diff --git a/integrationtests/agent/adoption_test.go b/integrationtests/agent/adoption_test.go index 1b85dd8e9b..57f17b48aa 100644 --- a/integrationtests/agent/adoption_test.go +++ b/integrationtests/agent/adoption_test.go @@ -16,22 +16,6 @@ import ( "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1" ) -func init() { - resources["BundleDeploymentConfigMap"] = []v1alpha1.BundleResource{ - { - Name: "configmap.yaml", - Content: `apiVersion: v1 -kind: ConfigMap -metadata: - name: cm1 -data: - key: value -`, - Encoding: "", - }, - } -} - var _ = Describe("Adoption", Label("adopt"), func() { var ( namespace string diff --git a/integrationtests/agent/bundle_deployment_drift_test.go b/integrationtests/agent/bundle_deployment_drift_test.go index 3e687ede6f..2f53e034db 100644 --- a/integrationtests/agent/bundle_deployment_drift_test.go +++ b/integrationtests/agent/bundle_deployment_drift_test.go @@ -3,7 +3,6 @@ package agent_test import ( "context" "fmt" - "os" "time" . "github.com/onsi/ginkgo/v2" @@ -21,27 +20,6 @@ import ( "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1" ) -func init() { - withStatus, _ := os.ReadFile(assetsPath + "/deployment-with-status.yaml") - withDeployment, _ := os.ReadFile(assetsPath + "/deployment-with-deployment.yaml") - - resources["with-status"] = []v1alpha1.BundleResource{ - { - Name: "deployment-with-status.yaml", - Content: string(withStatus), - Encoding: "", - }, - } - - resources["with-deployment"] = []v1alpha1.BundleResource{ - { - Name: "deployment-with-deployment.yaml", - Content: string(withDeployment), - Encoding: "", - }, - } -} - var _ = Describe("BundleDeployment drift correction", Ordered, func() { const svcName = "svc-test" @@ -280,8 +258,12 @@ var _ = Describe("BundleDeployment drift correction", Ordered, func() { It("Updates the BundleDeployment status as not Ready, including the error message", func() { By("Receiving a modification on a service") - svc, err := env.getService(svcName) - Expect(err).NotTo(HaveOccurred()) + svc := corev1.Service{} + Eventually(func(g Gomega) { + var err error + svc, err = env.getService(svcName) + g.Expect(err).NotTo(HaveOccurred()) + }).Should(Succeed()) patchedSvc := svc.DeepCopy() patchedSvc.Spec.Ports[0].TargetPort = intstr.FromInt(4242) patchedSvc.Spec.Ports[0].Port = 4242 @@ -311,7 +293,7 @@ var _ = Describe("BundleDeployment drift correction", Ordered, func() { nsn := types.NamespacedName{Namespace: clusterNS, Name: name} bd := v1alpha1.BundleDeployment{} - err = k8sClient.Get(ctx, nsn, &bd, &client.GetOptions{}) + err := k8sClient.Get(ctx, nsn, &bd, &client.GetOptions{}) Expect(err).ToNot(HaveOccurred()) patchedBD := bd.DeepCopy() diff --git a/integrationtests/agent/bundle_deployment_status_test.go b/integrationtests/agent/bundle_deployment_status_test.go index 8da98a9da5..bcf6848060 100644 --- a/integrationtests/agent/bundle_deployment_status_test.go +++ b/integrationtests/agent/bundle_deployment_status_test.go @@ -2,7 +2,6 @@ package agent_test import ( "context" - "os" "time" . "github.com/onsi/ginkgo/v2" @@ -17,26 +16,6 @@ import ( "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1" ) -func init() { - v1, _ := os.ReadFile(assetsPath + "/deployment-v1.yaml") - v2, _ := os.ReadFile(assetsPath + "/deployment-v2.yaml") - - resources["v1"] = []v1alpha1.BundleResource{ - { - Name: "deployment-v1.yaml", - Content: string(v1), - Encoding: "", - }, - } - resources["v2"] = []v1alpha1.BundleResource{ - { - Name: "deployment-v2.yaml", - Content: string(v2), - Encoding: "", - }, - } -} - var _ = Describe("BundleDeployment status", Ordered, func() { const ( diff --git a/integrationtests/agent/helm_capabilities_test.go b/integrationtests/agent/helm_capabilities_test.go index 408e2a4642..340e30bbaf 100644 --- a/integrationtests/agent/helm_capabilities_test.go +++ b/integrationtests/agent/helm_capabilities_test.go @@ -14,38 +14,6 @@ import ( "k8s.io/apimachinery/pkg/types" ) -func init() { - resources["capabilitiesv1"] = []v1alpha1.BundleResource{ - { - Content: "apiVersion: v2\nname: config-chart\ndescription: A test chart that verifies its config\ntype: application\nversion: 0.1.0\nappVersion: \"1.16.0\"\nkubeVersion: '>= 1.20.0-0'\n", - Name: "config-chart/Chart.yaml", - }, - { - Content: "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: test-simple-chart-config\ndata:\n test: \"value123\"\n name: {{ .Values.name }}\n kubeVersion: {{ .Capabilities.KubeVersion.Version }}\n apiVersions: {{ join \", \" .Capabilities.APIVersions | }}\n helmVersion: {{ .Capabilities.HelmVersion.Version }}\n", - Name: "config-chart/templates/configmap.yaml", - }, - { - Content: "helm:\n chart: config-chart\n values:\n name: example-value\n", - Name: "fleet.yaml", - }, - } - - resources["capabilitiesv2"] = []v1alpha1.BundleResource{ - { - Content: "apiVersion: v2\nname: config-chart\ndescription: A test chart that verifies its config\ntype: application\nversion: 0.1.0\nappVersion: \"1.16.0\"\nkubeVersion: '>= 920.920.0-0'\n", - Name: "config-chart/Chart.yaml", - }, - { - Content: "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: test-simple-chart-config\ndata:\n test: \"value123\"\n name: {{ .Values.name }}\n", - Name: "config-chart/templates/configmap.yaml", - }, - { - Content: "helm:\n chart: config-chart\n values:\n name: example-value\n", - Name: "fleet.yaml", - }, - } -} - var _ = Describe("Helm Chart uses Capabilities", Ordered, func() { var ( diff --git a/integrationtests/agent/suite_test.go b/integrationtests/agent/suite_test.go index c27d903c88..a4fb81e15a 100644 --- a/integrationtests/agent/suite_test.go +++ b/integrationtests/agent/suite_test.go @@ -107,6 +107,8 @@ var _ = BeforeSuite(func() { }) Expect(err).ToNot(HaveOccurred()) + setupFakeContents() + driftChan := make(chan event.GenericEvent) // Set up the bundledeployment reconciler @@ -269,7 +271,10 @@ type specEnv struct { func (se specEnv) isNotReadyAndModified(name string, modifiedStatus v1alpha1.ModifiedStatus, message string) (bool, string) { bd := &v1alpha1.BundleDeployment{} err := k8sClient.Get(context.TODO(), types.NamespacedName{Namespace: clusterNS, Name: name}, bd, &client.GetOptions{}) - Expect(err).NotTo(HaveOccurred()) + if err != nil { + return false, err.Error() + } + isReadyCondition := checkCondition(bd.Status.Conditions, "Ready", "False", message) isOK := cmp.Equal(bd.Status.ModifiedStatus, []v1alpha1.ModifiedStatus{modifiedStatus}) && @@ -286,7 +291,10 @@ func (se specEnv) isNotReadyAndModified(name string, modifiedStatus v1alpha1.Mod func (se specEnv) isBundleDeploymentReadyAndNotModified(name string) bool { bd := &v1alpha1.BundleDeployment{} err := k8sClient.Get(context.TODO(), types.NamespacedName{Namespace: clusterNS, Name: name}, bd, &client.GetOptions{}) - Expect(err).NotTo(HaveOccurred()) + if err != nil { + return false + } + return bd.Status.Ready && bd.Status.NonModified } @@ -337,3 +345,82 @@ func createNamespace() string { return namespace } + +func setupFakeContents() { + withStatus, _ := os.ReadFile(assetsPath + "/deployment-with-status.yaml") + withDeployment, _ := os.ReadFile(assetsPath + "/deployment-with-deployment.yaml") + v1, _ := os.ReadFile(assetsPath + "/deployment-v1.yaml") + v2, _ := os.ReadFile(assetsPath + "/deployment-v2.yaml") + + resources = map[string][]v1alpha1.BundleResource{ + "with-status": []v1alpha1.BundleResource{ + { + Name: "deployment-with-status.yaml", + Content: string(withStatus), + Encoding: "", + }, + }, + "with-deployment": []v1alpha1.BundleResource{ + { + Name: "deployment-with-deployment.yaml", + Content: string(withDeployment), + Encoding: "", + }, + }, + "BundleDeploymentConfigMap": []v1alpha1.BundleResource{ + { + Name: "configmap.yaml", + Content: `apiVersion: v1 +kind: ConfigMap +metadata: + name: cm1 +data: + key: value +`, + Encoding: "", + }, + }, + "v1": []v1alpha1.BundleResource{ + { + Name: "deployment-v1.yaml", + Content: string(v1), + Encoding: "", + }, + }, + "v2": []v1alpha1.BundleResource{ + { + Name: "deployment-v2.yaml", + Content: string(v2), + Encoding: "", + }, + }, + "capabilitiesv1": []v1alpha1.BundleResource{ + { + Content: "apiVersion: v2\nname: config-chart\ndescription: A test chart that verifies its config\ntype: application\nversion: 0.1.0\nappVersion: \"1.16.0\"\nkubeVersion: '>= 1.20.0-0'\n", + Name: "config-chart/Chart.yaml", + }, + { + Content: "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: test-simple-chart-config\ndata:\n test: \"value123\"\n name: {{ .Values.name }}\n kubeVersion: {{ .Capabilities.KubeVersion.Version }}\n apiVersions: {{ join \", \" .Capabilities.APIVersions | }}\n helmVersion: {{ .Capabilities.HelmVersion.Version }}\n", + Name: "config-chart/templates/configmap.yaml", + }, + { + Content: "helm:\n chart: config-chart\n values:\n name: example-value\n", + Name: "fleet.yaml", + }, + }, + "capabilitiesv2": []v1alpha1.BundleResource{ + { + Content: "apiVersion: v2\nname: config-chart\ndescription: A test chart that verifies its config\ntype: application\nversion: 0.1.0\nappVersion: \"1.16.0\"\nkubeVersion: '>= 920.920.0-0'\n", + Name: "config-chart/Chart.yaml", + }, + { + Content: "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: test-simple-chart-config\ndata:\n test: \"value123\"\n name: {{ .Values.name }}\n", + Name: "config-chart/templates/configmap.yaml", + }, + { + Content: "helm:\n chart: config-chart\n values:\n name: example-value\n", + Name: "fleet.yaml", + }, + }, + } +}