Skip to content

Commit

Permalink
add e2e test for api
Browse files Browse the repository at this point in the history
Signed-off-by: myan <[email protected]>
  • Loading branch information
yanmxa committed May 31, 2024
1 parent 253a300 commit f7b2b98
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 67 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,14 @@ e2e-test/teardown:
./test/e2e/setup/e2e_teardown.sh
.PHONY: e2e-test/teardown

e2e-test: e2e-test/teardown e2e-test/setup
e2e-test:
@if [ -z "$$consumer_name" ] || [ -z "$$api_server" ] || [ -z "$$consumer_kubeconfig" ]; then \
make e2e-test/teardown; \
make e2e-test/setup; \
fi
ginkgo --output-dir="${PWD}/test/e2e/report" --json-report=report.json --junit-report=report.xml \
${PWD}/test/e2e/pkg -- -consumer_name=$(shell cat ${PWD}/test/e2e/.consumer_name) \
-api-server=https://$(shell cat ${PWD}/test/e2e/.external_host_ip):30080 -kubeconfig=${PWD}/test/e2e/.kubeconfig
${PWD}/test/e2e/pkg -- \
-api-server=$${api_server:-https://$$(cat ${PWD}/test/e2e/.external_host_ip):30080} \
-consumer-name=$${consumer_name:-$$(cat ${PWD}/test/e2e/.consumer_name)} \
-consumer-kubeconfig=$${consumer_kubeconfig:-${PWD}/test/e2e/.kubeconfig}
.PHONY: e2e-test
202 changes: 202 additions & 0 deletions test/e2e/pkg/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
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
}
Loading

0 comments on commit f7b2b98

Please sign in to comment.