Skip to content

Commit

Permalink
Add e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: clyang82 <[email protected]>
  • Loading branch information
clyang82 committed May 28, 2024
1 parent 2c0d4b5 commit a27f7d4
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 8 deletions.
145 changes: 137 additions & 8 deletions test/e2e/pkg/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {

Expand All @@ -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() {

Expand Down
6 changes: 6 additions & 0 deletions test/e2e/pkg/serverside_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

})
})

0 comments on commit a27f7d4

Please sign in to comment.