From a27f7d4d1699b01f376b54dfbbf1c0b8ef452af8 Mon Sep 17 00:00:00 2001 From: clyang82 Date: Tue, 28 May 2024 14:22:44 +0800 Subject: [PATCH] Add e2e tests Signed-off-by: clyang82 --- test/e2e/pkg/resources_test.go | 145 ++++++++++++++++++++++++++++++-- test/e2e/pkg/serverside_test.go | 6 ++ 2 files changed, 143 insertions(+), 8 deletions(-) diff --git a/test/e2e/pkg/resources_test.go b/test/e2e/pkg/resources_test.go index 84ab71c0..7e400d6b 100644 --- a/test/e2e/pkg/resources_test.go +++ b/test/e2e/pkg/resources_test.go @@ -15,12 +15,9 @@ import ( var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { - It("is CRUD tests", func() { - }) - var resource *openapi.Resource - Context("Create Resource", func() { + Context("Resource CRUD Tests", func() { It("post the nginx resource to the maestro api", func() { res := helper.NewAPIResource(consumer_name, 1) @@ -39,9 +36,6 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { return nil }, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred()) }) - }) - - Context("Patch Resource", func() { It("patch the nginx resource", func() { @@ -63,9 +57,144 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { return fmt.Errorf("replicas is not 2") }, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred()) }) + + It("delete the nginx resource", func() { + + resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdDelete(context.Background(), *resource.Id).Execute() + Expect(err).ShouldNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusNoContent)) + + Eventually(func() error { + _, err := kubeClient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err + } + return fmt.Errorf("nginx deployment still exists") + }, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred()) + }) + }) + + Context("Resource Delete Option Tests", func() { + res := helper.NewAPIResource(consumer_name, 1) + It("post the nginx resource to the maestro api", func() { + var resp *http.Response + var err error + resource, resp, err = apiClient.DefaultApi.ApiMaestroV1ResourcesPost(context.Background()).Resource(res).Execute() + Expect(err).ShouldNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusCreated)) + Expect(*resource.Id).ShouldNot(BeEmpty()) + + Eventually(func() error { + _, err := kubeClient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + if err != nil { + return err + } + return nil + }, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred()) + }) + + It("patch the nginx resource with orphan delete option", func() { + + patchedResource, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdPatch(context.Background(), *resource.Id). + ResourcePatchRequest(openapi.ResourcePatchRequest{Version: resource.Version, Manifest: res.Manifest, DeleteOption: map[string]interface{}{"propagationPolicy": "Orphan"}}).Execute() + Expect(err).ShouldNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + Expect(*patchedResource.Version).To(Equal(*resource.Version + 1)) + + }) + + It("delete the nginx resource from the maestro api", func() { + + resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdDelete(context.Background(), *resource.Id).Execute() + Expect(err).ShouldNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusNoContent)) + + retry := 0 + Eventually(func() error { + // Attempt to retrieve the "nginx" deployment in the "default" namespace + _, err := kubeClient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + + // If an error occurs + if err != nil { + // Return any other errors directly + return err + } + + // Increment the retry counter + retry++ + + // If the retry count reaches 30, consider it successful + if retry == 30 { + return nil + } + + // Otherwise, indicate that another retry is needed + return fmt.Errorf("need to retry again") + }, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred()) + }) + + It("delete the nginx deployment", func() { + err := kubeClient.AppsV1().Deployments("default").Delete(context.Background(), "nginx", metav1.DeleteOptions{}) + Expect(err).ShouldNot(HaveOccurred()) + + Eventually(func() error { + _, err := kubeClient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err + } + return fmt.Errorf("nginx deployment still exists") + }, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred()) + }) + }) - Context("Delete Resource", func() { + Context("Resource Update Strategy Tests", func() { + + It("post the nginx resource to the maestro api with createOnly updateStrategy", func() { + res := helper.NewAPIResource(consumer_name, 1) + var resp *http.Response + var err error + res.UpdateStrategy = map[string]interface{}{"type": "CreateOnly"} + resource, resp, err = apiClient.DefaultApi.ApiMaestroV1ResourcesPost(context.Background()).Resource(res).Execute() + Expect(err).ShouldNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusCreated)) + Expect(*resource.Id).ShouldNot(BeEmpty()) + + Eventually(func() error { + _, err := kubeClient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + if err != nil { + return err + } + return nil + }, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred()) + }) + + It("patch the nginx resource", func() { + + newRes := helper.NewAPIResource(consumer_name, 2) + patchedResource, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdPatch(context.Background(), *resource.Id). + ResourcePatchRequest(openapi.ResourcePatchRequest{Version: resource.Version, Manifest: newRes.Manifest}).Execute() + Expect(err).ShouldNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + Expect(*patchedResource.Version).To(Equal(*resource.Version + 1)) + + Eventually(func() error { + deploy, err := kubeClient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + if err != nil { + return err + } + if *deploy.Spec.Replicas == 1 { + return nil + } + return fmt.Errorf("replicas is not 1") + }, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred()) + }) It("delete the nginx resource", func() { diff --git a/test/e2e/pkg/serverside_test.go b/test/e2e/pkg/serverside_test.go index b164992b..61675c22 100644 --- a/test/e2e/pkg/serverside_test.go +++ b/test/e2e/pkg/serverside_test.go @@ -103,5 +103,11 @@ var _ = Describe("Server Side Apply", func() { return nil }, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred()) + + // cleanup the job + resp, err = apiClient.DefaultApi.ApiMaestroV1ResourcesIdDelete(context.Background(), resourceID).Execute() + Expect(err).ShouldNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusNoContent)) + }) })