-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: myan <[email protected]>
- Loading branch information
Showing
5 changed files
with
259 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.