Skip to content

Commit

Permalink
chore(e2e): strengthen openapi tests
Browse files Browse the repository at this point in the history
Ref #5924
  • Loading branch information
squakez committed Nov 8, 2024
1 parent 78bf1f6 commit bbf3ba9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 15 deletions.
3 changes: 2 additions & 1 deletion e2e/common/misc/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"))
Expand Down
3 changes: 2 additions & 1 deletion e2e/common/traits/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"))
Expand Down
43 changes: 43 additions & 0 deletions e2e/knative/files/petstore-cf.yaml
Original file line number Diff line number Diff line change
@@ -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"
68 changes: 55 additions & 13 deletions e2e/knative/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}

0 comments on commit bbf3ba9

Please sign in to comment.