Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: zhujian <[email protected]>
  • Loading branch information
zhujian7 committed Oct 30, 2023
1 parent 179cd05 commit ed5a051
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
10 changes: 10 additions & 0 deletions cmd/example/helloworld_helm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
87 changes: 84 additions & 3 deletions test/e2e/helloworld_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
})

})

0 comments on commit ed5a051

Please sign in to comment.