Skip to content

Commit

Permalink
Add some sugar to rest APIs apache#1316
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Mar 6, 2020
1 parent 6853327 commit 2039f7d
Show file tree
Hide file tree
Showing 19 changed files with 4,754 additions and 113 deletions.
3,136 changes: 3,136 additions & 0 deletions deploy/camel-catalog-1.2.0-SNAPSHOT-main.yaml

Large diffs are not rendered by default.

892 changes: 892 additions & 0 deletions deploy/camel-catalog-1.2.0-SNAPSHOT-quarkus.yaml

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions deploy/resources.go

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions e2e/files/petstore-api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
format: int32
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
operationId: createPets
tags:
- pets
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Pets:
type: array
items:
$ref: "#/components/schemas/Pet"
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
29 changes: 29 additions & 0 deletions e2e/files/petstore.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// camel-k: language=groovy
/*
* 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.
*/

//
// kamel run --dev --name petstore --open-api examples/petstore-api.yaml examples/petstore.groovy
//

from('direct:listPets')
.log('listPets')
from('direct:createPets')
.log('createPets')
from('direct:showPetById')
.log('showPetById')

85 changes: 85 additions & 0 deletions e2e/openapi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// +build knative

// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "knative"

/*
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.
*/

package e2e

import (
"testing"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
)

func TestOpenAPIService(t *testing.T) {
withNewTestNamespaceWithKnativeBroker(t, func(ns string) {
Expect(kamel("install", "-n", ns, "--trait-profile", string(v1.TraitProfileKnative)).Execute()).Should(BeNil())
Expect(kamel(
"run",
"-n", ns,
"--name", "petstore",
"--open-api", "examples/petstore-api.yaml",
"files/petstore.groovy",
).Execute()).Should(BeNil())

Eventually(integrationPodPhase(ns, "petstore"), testTimeoutLong).
Should(Equal(corev1.PodRunning))
Eventually(knativeService(ns, "petstore"), testTimeoutLong).
Should(Not(BeNil()))

Eventually(integrationLogs(ns, "petstore"), testTimeoutMedium).
Should(ContainSubstring("Route: listPets started and consuming from: http://0.0.0.0:8080/v1/pets"))
Eventually(integrationLogs(ns, "petstore"), testTimeoutMedium).
Should(ContainSubstring("Route: createPets started and consuming from: http://0.0.0.0:8080/v1/pets"))
Eventually(integrationLogs(ns, "petstore"), testTimeoutMedium).
Should(ContainSubstring("Route: showPetById started and consuming from: http://0.0.0.0:8080/v1/pets"))

Expect(kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
})
}

func TestOpenAPIDeployment(t *testing.T) {
withNewTestNamespaceWithKnativeBroker(t, func(ns string) {
Expect(kamel("install", "-n", ns, "--trait-profile", string(v1.TraitProfileKubernetes)).Execute()).Should(BeNil())
Expect(kamel(
"run",
"-n", ns,
"--name", "petstore",
"--open-api", "examples/petstore-api.yaml",
"files/petstore.groovy",
).Execute()).Should(BeNil())

Eventually(integrationPodPhase(ns, "petstore"), testTimeoutLong).
Should(Equal(corev1.PodRunning))
Eventually(deployment(ns, "petstore"), testTimeoutLong).
Should(Not(BeNil()))

Eventually(integrationLogs(ns, "petstore"), testTimeoutMedium).
Should(ContainSubstring("Route: listPets started and consuming from: http://0.0.0.0:8080/v1/pets"))
Eventually(integrationLogs(ns, "petstore"), testTimeoutMedium).
Should(ContainSubstring("Route: createPets started and consuming from: http://0.0.0.0:8080/v1/pets"))
Eventually(integrationLogs(ns, "petstore"), testTimeoutMedium).
Should(ContainSubstring("Route: showPetById started and consuming from: http://0.0.0.0:8080/v1/pets"))

Expect(kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
})
}
81 changes: 68 additions & 13 deletions e2e/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
rbacv1 "k8s.io/api/rbac/v1"
"os"
"os/exec"
"strings"
"testing"
"time"
"io"

servingv1 "knative.dev/serving/pkg/apis/serving/v1"

rbacv1 "k8s.io/api/rbac/v1"

"github.com/apache/camel-k/e2e/util"
"github.com/apache/camel-k/pkg/apis/camel/v1"
Expand Down Expand Up @@ -61,9 +64,9 @@ import (
_ "github.com/apache/camel-k/addons"
)

var testTimeoutShort = 1*time.Minute
var testTimeoutMedium = 5*time.Minute
var testTimeoutLong = 10*time.Minute
var testTimeoutShort = 1 * time.Minute
var testTimeoutMedium = 5 * time.Minute
var testTimeoutLong = 10 * time.Minute

var testContext context.Context
var testClient client.Client
Expand Down Expand Up @@ -97,7 +100,7 @@ func init() {
testImageVersion = imageVersion
}

// Timeouts
// Timeouts
var duration time.Duration
if value, ok := os.LookupEnv("CAMEL_K_TEST_TIMEOUT_SHORT"); ok {
if duration, err = time.ParseDuration(value); err == nil {
Expand Down Expand Up @@ -152,18 +155,18 @@ func kamelWithContext(ctx context.Context, args ...string) *cobra.Command {
c = &cobra.Command{
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {

externalBin := exec.Command(kamelBin, args...)
var stdout io.Reader
stdout , err = externalBin.StdoutPipe()
stdout, err = externalBin.StdoutPipe()
if err != nil {
panic(err)
}

externalBin.Start()
io.Copy(c.OutOrStdout(), stdout)
externalBin.Wait()

},
}
} else {
Expand Down Expand Up @@ -467,6 +470,58 @@ func configmap(ns string, name string) func() *corev1.ConfigMap {
}
}

func knativeService(ns string, name string) func() *servingv1.Service {
return func() *servingv1.Service {
cm := servingv1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: servingv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
}
key := k8sclient.ObjectKey{
Namespace: ns,
Name: name,
}
if err := testClient.Get(testContext, key, &cm); err != nil && k8serrors.IsNotFound(err) {
return nil
} else if err != nil {
log.Errorf(err, "Error while retrieving knative service %s", name)
return nil
}
return &cm
}
}

func deployment(ns string, name string) func() *appsv1.Deployment {
return func() *appsv1.Deployment {
cm := appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: appsv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
}
key := k8sclient.ObjectKey{
Namespace: ns,
Name: name,
}
if err := testClient.Get(testContext, key, &cm); err != nil && k8serrors.IsNotFound(err) {
return nil
} else if err != nil {
log.Errorf(err, "Error while retrieving deployment %s", name)
return nil
}
return &cm
}
}

func build(ns string, name string) func() *v1.Build {
return func() *v1.Build {
build := v1.NewBuild(ns, name)
Expand Down Expand Up @@ -514,7 +569,7 @@ func deletePlatform(ns string) func() bool {
}
}

func setPlatformVersion(ns string, version string) func()error {
func setPlatformVersion(ns string, version string) func() error {
return func() error {
p := platform(ns)()
if p == nil {
Expand Down Expand Up @@ -675,7 +730,7 @@ func clusterrole(ns string) func() *rbacv1.ClusterRole {
lst := rbacv1.ClusterRoleList{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRole",
APIVersion: rbacv1.SchemeGroupVersion.String(),
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
}
err := testClient.List(testContext, &lst,
Expand All @@ -698,7 +753,7 @@ func serviceaccount(ns, name string) func() *corev1.ServiceAccount {
lst := corev1.ServiceAccountList{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceAccount",
APIVersion: corev1.SchemeGroupVersion.String(),
APIVersion: corev1.SchemeGroupVersion.String(),
},
}
err := testClient.List(testContext, &lst,
Expand Down
2 changes: 1 addition & 1 deletion examples/greetings.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

//
// kamel run --dev --name greetings --property camel.rest.port=8080 --open-api examples/greetings-api.json examples/greetings.groovy
// kamel run --dev --name greetings --open-api examples/greetings-api.json examples/greetings.groovy
//

from('direct:greeting-api')
Expand Down
Loading

0 comments on commit 2039f7d

Please sign in to comment.