diff --git a/test/e2e/pkg/api_test.go b/test/e2e/pkg/api_test.go deleted file mode 100644 index 8023a2df..00000000 --- a/test/e2e/pkg/api_test.go +++ /dev/null @@ -1,202 +0,0 @@ -package e2e_test - -import ( - "encoding/json" - "errors" - "fmt" - "net/http" - "reflect" - "time" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "github.com/openshift-online/maestro/pkg/api/openapi" -) - -// go test -v ./test/e2e/pkg -args -api-server=$api_server -consumer-name=$consumer_name -consumer-kubeconfig=$consumer_kubeconfig -ginkgo.focus "Server Side API" -var _ = Describe("Server Side API", Ordered, func() { - var testConsumerName string - var testConsumerID string - BeforeAll(func() { - testConsumerName = "test-consumer" - }) - - Context("consumer API", func() { - It("list the default consumer", func() { - consumerList, resp, err := apiClient.DefaultApi.ApiMaestroV1ConsumersGet(ctx).Execute() - Expect(err).To(Succeed()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) - Expect(consumerList).NotTo(BeNil()) - Expect(len(consumerList.Items) > 0).To(BeTrue()) - - got := false - for _, consumer := range consumerList.Items { - if *consumer.Name == consumerOpts.Name { - got = true - } - } - Expect(got).To(BeTrue()) - }) - - It("create consumer", func() { - consumer := openapi.Consumer{Name: &testConsumerName} - created, resp, err := apiClient.DefaultApi.ApiMaestroV1ConsumersPost(ctx).Consumer(consumer).Execute() - Expect(err).To(Succeed()) - Expect(resp.StatusCode).To(Equal(http.StatusCreated)) - Expect(*created.Id).NotTo(BeEmpty()) - testConsumerID = *created.Id - - got, resp, err := apiClient.DefaultApi.ApiMaestroV1ConsumersIdGet(ctx, testConsumerID).Execute() - Expect(err).To(Succeed()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) - Expect(got).NotTo(BeNil()) - }) - - It("patch consumer", func() { - labels := &map[string]string{"hello": "world"} - patched, resp, err := apiClient.DefaultApi.ApiMaestroV1ConsumersIdPatch(ctx, testConsumerID). - ConsumerPatchRequest(openapi.ConsumerPatchRequest{Labels: labels}).Execute() - Expect(err).To(Succeed()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) - _, ok := patched.GetLabelsOk() - Expect(ok).To(BeTrue()) - - got, resp, err := apiClient.DefaultApi.ApiMaestroV1ConsumersIdGet(ctx, testConsumerID).Execute() - Expect(err).To(Succeed()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) - Expect(got).NotTo(BeNil()) - eq := reflect.DeepEqual(*labels, *got.Labels) - Expect(eq).To(BeTrue()) - }) - - AfterAll(func() { - // TODO: add the conusmer deletion - }) - }) - - Context("resource API on fake consumer", func() { - var resourceID string - var resourceVersion *int32 - It("create resource", func() { - manifestIn, err := GetDeployManifest(1) - Expect(err).To(Succeed()) - - res := openapi.Resource{ - Manifest: manifestIn, - ConsumerName: &testConsumerName, - } - created, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesPost(ctx).Resource(res).Execute() - Expect(err).To(Succeed()) - Expect(resp.StatusCode).To(Equal(http.StatusCreated)) - Expect(*created.Id).ShouldNot(BeEmpty()) - resourceID = *created.Id - - got, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdGet(ctx, *created.Id).Execute() - Expect(err).To(Succeed()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) - Expect(got).NotTo(BeNil()) - resourceVersion = got.Version - - // assert consumer name - Expect(*got.ConsumerName).To(Equal(*res.ConsumerName)) - - // assert manifest: job - manifestOut := got.Manifest - Expect(err).To(Succeed()) - Expect(reflect.DeepEqual(manifestIn, manifestOut)).To(BeTrue()) - }) - - It("patch resource", func() { - manifestIn, err := GetDeployManifest(2) - Expect(err).To(Succeed()) - - patchRequest := openapi.ResourcePatchRequest{ - Manifest: manifestIn, - Version: resourceVersion, - } - patched, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdPatch(ctx, resourceID).ResourcePatchRequest( - patchRequest).Execute() - Expect(err).To(Succeed()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) - _, ok := patched.GetManifestOk() - Expect(ok).To(BeTrue()) - - got, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdGet(ctx, resourceID).Execute() - Expect(err).To(Succeed()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) - Expect(got).NotTo(BeNil()) - - Expect(*got.ConsumerName).To(Equal(testConsumerName)) - Expect(reflect.DeepEqual(manifestIn, got.Manifest)).To(BeTrue()) - }) - - It("delete resource", func() { - resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdDelete(ctx, resourceID).Execute() - Expect(err).To(Succeed()) - Expect(resp.StatusCode).To(Equal(http.StatusNoContent)) - - Eventually(func() error { - resourceList, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesGet(ctx).Execute() - if err != nil { - return err - } - if resp.StatusCode != http.StatusOK { - return fmt.Errorf("statusCode: want %d, but got %d", http.StatusOK, resp.StatusCode) - } - if resourceList == nil { - return errors.New("the resource List shouldn't be nil") - } - - got := false - for _, resource := range resourceList.Items { - metadata := resource.Manifest["metadata"].(map[string]interface{}) - if metadata["name"] == "nginx-api" { - got = true - } - } - if got { - return nil - } - return fmt.Errorf("the deploy should not be deleted from the consumer %s", testConsumerName) - }, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred()) - }) - }) -}) - -func GetDeployManifest(replicas int) (map[string]interface{}, error) { - const deployTemplate = `{ - "apiVersion": "apps/v1", - "kind": "Deployment", - "metadata": { - "name": "nginx-api", - "namespace": "default" - }, - "spec": { - "replicas": %d, - "selector": { - "matchLabels": { - "app": "nginx-api" - } - }, - "template": { - "metadata": { - "labels": { - "app": "nginx-api" - } - }, - "spec": { - "containers": [ - { - "image": "nginxinc/nginx-unprivileged", - "name": "nginx-api" - } - ] - } - } - } - } - ` - manifest := map[string]interface{}{} - err := json.Unmarshal([]byte(fmt.Sprintf(deployTemplate, replicas)), &manifest) - return manifest, err -} diff --git a/test/e2e/pkg/consumer_test.go b/test/e2e/pkg/consumer_test.go new file mode 100644 index 00000000..311d6377 --- /dev/null +++ b/test/e2e/pkg/consumer_test.go @@ -0,0 +1,72 @@ +package e2e_test + +import ( + "net/http" + "reflect" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/openshift-online/maestro/pkg/api/openapi" +) + +// go test -v ./test/e2e/pkg -args -api-server=$api_server -consumer-name=$consumer_name -consumer-kubeconfig=$consumer_kubeconfig -ginkgo.focus "Server Side API" +var _ = Describe("Consumer", Ordered, func() { + var testConsumerName string + var testConsumerID string + BeforeAll(func() { + testConsumerName = "test-consumer" + }) + + Context("Consumer CRUD Tests", func() { + It("list the default consumer", func() { + consumerList, resp, err := apiClient.DefaultApi.ApiMaestroV1ConsumersGet(ctx).Execute() + Expect(err).To(Succeed()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + Expect(consumerList).NotTo(BeNil()) + Expect(len(consumerList.Items) > 0).To(BeTrue()) + + got := false + for _, consumer := range consumerList.Items { + if *consumer.Name == consumerOpts.Name { + got = true + } + } + Expect(got).To(BeTrue()) + }) + + It("create consumer", func() { + consumer := openapi.Consumer{Name: &testConsumerName} + created, resp, err := apiClient.DefaultApi.ApiMaestroV1ConsumersPost(ctx).Consumer(consumer).Execute() + Expect(err).To(Succeed()) + Expect(resp.StatusCode).To(Equal(http.StatusCreated)) + Expect(*created.Id).NotTo(BeEmpty()) + testConsumerID = *created.Id + + got, resp, err := apiClient.DefaultApi.ApiMaestroV1ConsumersIdGet(ctx, testConsumerID).Execute() + Expect(err).To(Succeed()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + Expect(got).NotTo(BeNil()) + }) + + It("patch consumer", func() { + labels := &map[string]string{"hello": "world"} + patched, resp, err := apiClient.DefaultApi.ApiMaestroV1ConsumersIdPatch(ctx, testConsumerID). + ConsumerPatchRequest(openapi.ConsumerPatchRequest{Labels: labels}).Execute() + Expect(err).To(Succeed()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + _, ok := patched.GetLabelsOk() + Expect(ok).To(BeTrue()) + + got, resp, err := apiClient.DefaultApi.ApiMaestroV1ConsumersIdGet(ctx, testConsumerID).Execute() + Expect(err).To(Succeed()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + Expect(got).NotTo(BeNil()) + eq := reflect.DeepEqual(*labels, *got.Labels) + Expect(eq).To(BeTrue()) + }) + + AfterAll(func() { + // TODO: add the conusmer deletion + }) + }) +}) diff --git a/test/e2e/pkg/resources_test.go b/test/e2e/pkg/resource_test.go similarity index 85% rename from test/e2e/pkg/resources_test.go rename to test/e2e/pkg/resource_test.go index 1873ba4a..b7602d96 100644 --- a/test/e2e/pkg/resources_test.go +++ b/test/e2e/pkg/resource_test.go @@ -1,7 +1,6 @@ package e2e_test import ( - "context" "fmt" "net/http" "time" @@ -22,13 +21,13 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { res := helper.NewAPIResource(consumerOpts.Name, 1) var resp *http.Response var err error - resource, resp, err = apiClient.DefaultApi.ApiMaestroV1ResourcesPost(context.Background()).Resource(res).Execute() + resource, resp, err = apiClient.DefaultApi.ApiMaestroV1ResourcesPost(ctx).Resource(res).Execute() Expect(err).ShouldNot(HaveOccurred()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) Expect(*resource.Id).ShouldNot(BeEmpty()) Eventually(func() error { - _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(ctx, "nginx", metav1.GetOptions{}) if err != nil { return err } @@ -38,14 +37,14 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { It("patch the nginx resource", func() { newRes := helper.NewAPIResource(consumerOpts.Name, 2) - patchedResource, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdPatch(context.Background(), *resource.Id). + patchedResource, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdPatch(ctx, *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 := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + deploy, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(ctx, "nginx", metav1.GetOptions{}) if err != nil { return err } @@ -57,12 +56,12 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { }) It("delete the nginx resource", func() { - resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdDelete(context.Background(), *resource.Id).Execute() + resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdDelete(ctx, *resource.Id).Execute() Expect(err).ShouldNot(HaveOccurred()) Expect(resp.StatusCode).To(Equal(http.StatusNoContent)) Eventually(func() error { - _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(ctx, "nginx", metav1.GetOptions{}) if err != nil { if errors.IsNotFound(err) { return nil @@ -79,13 +78,13 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { 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() + resource, resp, err = apiClient.DefaultApi.ApiMaestroV1ResourcesPost(ctx).Resource(res).Execute() Expect(err).ShouldNot(HaveOccurred()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) Expect(*resource.Id).ShouldNot(BeEmpty()) Eventually(func() error { - _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(ctx, "nginx", metav1.GetOptions{}) if err != nil { return err } @@ -94,7 +93,7 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { }) It("patch the nginx resource with orphan delete option", func() { - patchedResource, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdPatch(context.Background(), *resource.Id). + patchedResource, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdPatch(ctx, *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)) @@ -102,14 +101,14 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { }) It("delete the nginx resource from the maestro api", func() { - resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdDelete(context.Background(), *resource.Id).Execute() + resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdDelete(ctx, *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 := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(ctx, "nginx", metav1.GetOptions{}) // If an error occurs if err != nil { // Return any other errors directly @@ -130,11 +129,11 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { }) It("delete the nginx deployment", func() { - err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Delete(context.Background(), "nginx", metav1.DeleteOptions{}) + err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Delete(ctx, "nginx", metav1.DeleteOptions{}) Expect(err).ShouldNot(HaveOccurred()) Eventually(func() error { - _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(ctx, "nginx", metav1.GetOptions{}) if err != nil { if errors.IsNotFound(err) { return nil @@ -152,13 +151,13 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { 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() + resource, resp, err = apiClient.DefaultApi.ApiMaestroV1ResourcesPost(ctx).Resource(res).Execute() Expect(err).ShouldNot(HaveOccurred()) Expect(resp.StatusCode).To(Equal(http.StatusCreated)) Expect(*resource.Id).ShouldNot(BeEmpty()) Eventually(func() error { - _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(ctx, "nginx", metav1.GetOptions{}) if err != nil { return err } @@ -168,14 +167,14 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { It("patch the nginx resource", func() { newRes := helper.NewAPIResource(consumerOpts.Name, 2) - patchedResource, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdPatch(context.Background(), *resource.Id). + patchedResource, resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdPatch(ctx, *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 := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + deploy, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(ctx, "nginx", metav1.GetOptions{}) if err != nil { return err } @@ -187,12 +186,12 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() { }) It("delete the nginx resource", func() { - resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdDelete(context.Background(), *resource.Id).Execute() + resp, err := apiClient.DefaultApi.ApiMaestroV1ResourcesIdDelete(ctx, *resource.Id).Execute() Expect(err).ShouldNot(HaveOccurred()) Expect(resp.StatusCode).To(Equal(http.StatusNoContent)) Eventually(func() error { - _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(context.Background(), "nginx", metav1.GetOptions{}) + _, err := consumerOpts.Kubeclient.AppsV1().Deployments("default").Get(ctx, "nginx", metav1.GetOptions{}) if err != nil { if errors.IsNotFound(err) { return nil