From ed5a05154d8a9e00d26a5e1a7a8e4e635594f508 Mon Sep 17 00:00:00 2001 From: zhujian Date: Mon, 30 Oct 2023 07:44:11 +0000 Subject: [PATCH] Add e2e test Signed-off-by: zhujian --- cmd/example/helloworld_helm/main.go | 10 ++++ test/e2e/helloworld_helm_test.go | 87 ++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/cmd/example/helloworld_helm/main.go b/cmd/example/helloworld_helm/main.go index 5b1c69746..d2bfba893 100644 --- a/cmd/example/helloworld_helm/main.go +++ b/cmd/example/helloworld_helm/main.go @@ -99,6 +99,10 @@ func runController(ctx context.Context, kubeConfig *rest.Config) error { kubeConfig, helloworld_helm.AddonName, utilrand.String(5)) + // Set agent install namespace from addon deployment config if it exists + registrationOption.AgentInstallNamespace = utils.AgentInstallNamespaceFromDeploymentConfigFunc( + utils.NewAddOnDeploymentConfigGetter(addonClient), + ) agentAddon, err := addonfactory.NewAgentAddonFactory(helloworld_helm.AddonName, helloworld_helm.FS, "manifests/charts/helloworld"). WithConfigGVRs( @@ -121,6 +125,12 @@ func runController(ctx context.Context, kubeConfig *rest.Config) error { helloworld_helm.GetImageValues(kubeClient), addonfactory.GetValuesFromAddonAnnotation, ).WithAgentRegistrationOption(registrationOption). + WithAgentInstallNamespace( + // Set agent install namespace from addon deployment config if it exists + utils.AgentInstallNamespaceFromDeploymentConfigFunc( + utils.NewAddOnDeploymentConfigGetter(addonClient), + ), + ). BuildHelmAgentAddon() if err != nil { klog.Errorf("failed to build agent %v", err) diff --git a/test/e2e/helloworld_helm_test.go b/test/e2e/helloworld_helm_test.go index 77b823a8e..5db734564 100644 --- a/test/e2e/helloworld_helm_test.go +++ b/test/e2e/helloworld_helm_test.go @@ -50,14 +50,30 @@ var _ = ginkgo.Describe("install/uninstall helloworld helm addons", func() { _, err = hubAddOnClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Get(context.Background(), helloWorldHelmAddonName, metav1.GetOptions{}) if err != nil { if errors.IsNotFound(err) { - _, err = hubAddOnClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), &helloworldhelmAddon, metav1.CreateOptions{}) - if err != nil { - return err + _, cerr := hubAddOnClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Create(context.Background(), &helloworldhelmAddon, metav1.CreateOptions{}) + if cerr != nil { + return cerr } } return err } + ginkgo.By("Make sure the agent namespace is created") + testNs := &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: agentInstallNamespaceConfig, + }, + } + _, err = hubKubeClient.CoreV1().Namespaces().Create(context.Background(), testNs, metav1.CreateOptions{}) + if err != nil { + if errors.IsAlreadyExists(err) { + ginkgo.By("The agent namespace is already created") + return nil + } + return err + } + ginkgo.By("The agent namespace is created") + return nil }, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred()) }) @@ -106,6 +122,23 @@ var _ = ginkgo.Describe("install/uninstall helloworld helm addons", func() { return fmt.Errorf("addon agent deployment %s/%s is not deleted", addonInstallNamespace, agentDeploymentName) }, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred()) + gomega.Eventually(func() error { + _, err := hubKubeClient.CoreV1().Namespaces().Get(context.Background(), + agentInstallNamespaceConfig, metav1.GetOptions{}) + if errors.IsNotFound(err) { + return nil + } + + if err == nil { + errd := hubKubeClient.CoreV1().Namespaces().Delete(context.Background(), + agentInstallNamespaceConfig, metav1.DeleteOptions{}) + if errd != nil { + return errd + } + } + + return err + }, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred()) }) ginkgo.It("addon should be available", func() { @@ -565,4 +598,52 @@ var _ = ginkgo.Describe("install/uninstall helloworld helm addons", func() { }, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred()) }) + ginkgo.It("addon registraion agent install namespace should work", func() { + ginkgo.By("Prepare a AddOnDeploymentConfig for addon agent install namespace") + gomega.Eventually(func() error { + return prepareAgentInstallNamespaceAddOnDeploymentConfig(managedClusterName) + }, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred()) + + ginkgo.By("Add the configs to ManagedClusterAddOn") + gomega.Eventually(func() error { + addon, err := hubAddOnClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Get( + context.Background(), helloWorldHelmAddonName, metav1.GetOptions{}) + if err != nil { + return err + } + newAddon := addon.DeepCopy() + newAddon.Spec.Configs = []addonapiv1alpha1.AddOnConfig{ + { + ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{ + Group: "addon.open-cluster-management.io", + Resource: "addondeploymentconfigs", + }, + ConfigReferent: addonapiv1alpha1.ConfigReferent{ + Namespace: managedClusterName, + Name: deployAgentInstallNamespaceConfigName, + }, + }, + } + _, err = hubAddOnClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterName).Update( + context.Background(), newAddon, metav1.UpdateOptions{}) + if err != nil { + return err + } + return nil + }, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred()) + + ginkgo.By("Make sure addon is configured") + gomega.Eventually(func() error { + _, err := hubKubeClient.CoreV1().Secrets(agentInstallNamespaceConfig).Get( + context.Background(), "helloworldhelm-hub-kubeconfig", metav1.GetOptions{}) + return err + }, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred()) + + gomega.Eventually(func() error { + _, err := hubKubeClient.AppsV1().Deployments(agentInstallNamespaceConfig).Get( + context.Background(), "helloworldhelm-agent", metav1.GetOptions{}) + return err + }, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred()) + }) + })