From bbf3ba9f1ccdac7c615163f02968d592ff1e0ec7 Mon Sep 17 00:00:00 2001 From: Pasquale Congiusti Date: Thu, 7 Nov 2024 18:31:02 +0100 Subject: [PATCH] chore(e2e): strengthen openapi tests Ref #5924 --- e2e/common/misc/openapi_test.go | 3 +- e2e/common/traits/openapi_test.go | 3 +- e2e/knative/files/petstore-cf.yaml | 43 +++++++++++++++++++ e2e/knative/openapi_test.go | 68 ++++++++++++++++++++++++------ 4 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 e2e/knative/files/petstore-cf.yaml diff --git a/e2e/common/misc/openapi_test.go b/e2e/common/misc/openapi_test.go index e98dd75966..ca4d2a8d9c 100644 --- a/e2e/common/misc/openapi_test.go +++ b/e2e/common/misc/openapi_test.go @@ -39,7 +39,7 @@ import ( v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" ) -func TestOpenAPI(t *testing.T) { +func TestOpenAPIContractFirst(t *testing.T) { t.Parallel() WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) { name := RandomizedSuffixName("petstore") @@ -55,6 +55,7 @@ func TestOpenAPI(t *testing.T) { g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutMedium). Should(Equal(corev1.ConditionTrue)) + g.Eventually(Service(t, ctx, ns, name), TestTimeoutShort).ShouldNot(BeNil()) g.Eventually(IntegrationPodPhase(t, ctx, ns, name)).Should(Equal(corev1.PodRunning)) // Let's make sure the Integration is ready to receive traffic g.Eventually(IntegrationLogs(t, ctx, ns, name)).Should(ContainSubstring("Listening on: http://0.0.0.0:8080")) diff --git a/e2e/common/traits/openapi_test.go b/e2e/common/traits/openapi_test.go index 8c61e166b3..b87ebee48f 100644 --- a/e2e/common/traits/openapi_test.go +++ b/e2e/common/traits/openapi_test.go @@ -39,7 +39,7 @@ import ( v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" ) -func TestOpenAPI(t *testing.T) { +func TestOpenAPITrait(t *testing.T) { t.Parallel() WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) { name := RandomizedSuffixName("petstore") @@ -55,6 +55,7 @@ func TestOpenAPI(t *testing.T) { g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutMedium). Should(Equal(corev1.ConditionTrue)) + g.Eventually(Service(t, ctx, ns, name), TestTimeoutShort).ShouldNot(BeNil()) g.Eventually(IntegrationPodPhase(t, ctx, ns, name)).Should(Equal(corev1.PodRunning)) // Let's make sure the Integration is ready to receive traffic g.Eventually(IntegrationLogs(t, ctx, ns, name)).Should(ContainSubstring("Listening on: http://0.0.0.0:8080")) diff --git a/e2e/knative/files/petstore-cf.yaml b/e2e/knative/files/petstore-cf.yaml new file mode 100644 index 0000000000..ca4e483e38 --- /dev/null +++ b/e2e/knative/files/petstore-cf.yaml @@ -0,0 +1,43 @@ +# camel-k: language=yaml + +# --------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# --------------------------------------------------------------------------- + +- rest: + openApi: + specification: petstore-api.yaml + +- from: + uri: "direct:listPets" + steps: + - setBody: + simple: "listPets" + - to: "log:info" + +- from: + uri: "direct:createPets" + steps: + - setBody: + simple: "createPets" + - to: "log:info" + +- from: + uri: "direct:showPetById" + steps: + - setBody: + simple: "showPetById" + - to: "log:info" diff --git a/e2e/knative/openapi_test.go b/e2e/knative/openapi_test.go index 30adb9d4e9..00ea85fdf9 100644 --- a/e2e/knative/openapi_test.go +++ b/e2e/knative/openapi_test.go @@ -24,36 +24,78 @@ package knative import ( "context" + "fmt" + "os" "testing" + "time" "io/ioutil" . "github.com/apache/camel-k/v2/e2e/support" + v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" . "github.com/onsi/gomega" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" ) -/* - go test -v -tags=integration ./e2e/knative --run TestOpenAPIService -*/ - -func TestOpenAPIService(t *testing.T) { +func TestOpenAPIKnativeTrait(t *testing.T) { WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) { + name := RandomizedSuffixName("petstore") openapiContent, err := ioutil.ReadFile("./files/petstore-api.yaml") require.NoError(t, err) var cmDataProps = make(map[string]string) cmDataProps["petstore-api.yaml"] = string(openapiContent) CreatePlainTextConfigmap(t, ctx, ns, "my-openapi-knative", cmDataProps) - g.Expect(KamelRun(t, ctx, ns, "--name", "petstore", "--open-api", "configmap:my-openapi-knative", "files/petstore.yaml").Execute()).To(Succeed()) + g.Expect(KamelRun(t, ctx, ns, "--name", name, "--open-api", "configmap:my-openapi-knative", "files/petstore.yaml").Execute()).To(Succeed()) - g.Eventually(KnativeService(t, ctx, ns, "petstore"), TestTimeoutLong). + g.Eventually(KnativeService(t, ctx, ns, name), TestTimeoutLong). Should(Not(BeNil())) - g.Eventually(IntegrationLogs(t, ctx, ns, "petstore"), TestTimeoutMedium). - Should(ContainSubstring("Started listPets (rest://get:/v1:/pets)")) - g.Eventually(IntegrationLogs(t, ctx, ns, "petstore"), TestTimeoutMedium). - Should(ContainSubstring("Started createPets (rest://post:/v1:/pets)")) - g.Eventually(IntegrationLogs(t, ctx, ns, "petstore"), TestTimeoutMedium). - Should(ContainSubstring("Started showPetById (rest://get:/v1:/pets/%7BpetId%7D)")) + g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutMedium). + Should(Equal(corev1.ConditionTrue)) + g.Eventually(IntegrationPodPhase(t, ctx, ns, name)).Should(Equal(corev1.PodRunning)) + // Let's make sure the Integration is ready to receive traffic + g.Eventually(IntegrationLogs(t, ctx, ns, name)).Should(ContainSubstring("Listening on: http://0.0.0.0:8080")) + pod := IntegrationPod(t, ctx, ns, name)() + g.Expect(pod).NotTo(BeNil()) + response, err := TestClient(t).CoreV1().RESTClient().Get(). + Timeout(30 * time.Second). + AbsPath(fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/proxy/v1/pets", pod.Namespace, pod.Name)). + DoRaw(ctx) + require.NoError(t, err) + assert.Equal(t, "listPets", string(response)) + }) +} + +func TestOpenAPIKnativeContractFirst(t *testing.T) { + t.Parallel() + WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) { + name := RandomizedSuffixName("petstore") + openapiContent, err := os.ReadFile("./files/petstore-api.yaml") + require.NoError(t, err) + var cmDataProps = make(map[string]string) + cmDataProps["petstore-api.yaml"] = string(openapiContent) + CreatePlainTextConfigmap(t, ctx, ns, "my-openapi", cmDataProps) + + g.Expect(KamelRun(t, ctx, ns, + "--name", name, "--resource", "configmap:my-openapi", "files/petstore-cf.yaml"). + Execute()).To(Succeed()) + + g.Eventually(KnativeService(t, ctx, ns, name), TestTimeoutLong). + Should(Not(BeNil())) + g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutMedium). + Should(Equal(corev1.ConditionTrue)) + g.Eventually(IntegrationPodPhase(t, ctx, ns, name)).Should(Equal(corev1.PodRunning)) + // Let's make sure the Integration is ready to receive traffic + g.Eventually(IntegrationLogs(t, ctx, ns, name)).Should(ContainSubstring("Listening on: http://0.0.0.0:8080")) + pod := IntegrationPod(t, ctx, ns, name)() + g.Expect(pod).NotTo(BeNil()) + response, err := TestClient(t).CoreV1().RESTClient().Get(). + Timeout(30 * time.Second). + AbsPath(fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/proxy/v1/pets", pod.Namespace, pod.Name)). + DoRaw(ctx) + require.NoError(t, err) + assert.Equal(t, "listPets", string(response)) }) }