Skip to content

Commit

Permalink
Contract test between api-gateway and serverless (#17894)
Browse files Browse the repository at this point in the history
* add api-gateway test logic for node

* adjust tests to check the runtime

* add python39 test

* uncomment main

* adjust pod selector

* add check if all labels exists

* adjust labels and error handling

* rename package
  • Loading branch information
MichalKalke authored Aug 1, 2023
1 parent 09b1347 commit 32990c0
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/function-controller/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var availableScenarios = map[string][]scenario{
},
"git-auth-integration": {{displayName: "gitauth", scenario: scenarios.GitAuthTestSteps}},
"simple-tracing": {{displayName: "tracing", scenario: scenarios.SimpleFunctionTracingTest}},
"simple-api-gateway": {{displayName: "api-gateway", scenario: scenarios.SimpleFunctionAPIGatewayTest}},
}

type config struct {
Expand Down
87 changes: 87 additions & 0 deletions tests/function-controller/testsuite/scenarios/api_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package scenarios

import (
"fmt"
"github.com/pkg/errors"
typedappsv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
"time"

"github.com/kyma-project/kyma/tests/function-controller/pkg/function"

serverlessv1alpha2 "github.com/kyma-project/kyma/components/function-controller/pkg/apis/serverless/v1alpha2"
"github.com/sirupsen/logrus"
"k8s.io/client-go/dynamic"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"

"github.com/kyma-project/kyma/tests/function-controller/pkg/shared"
"github.com/kyma-project/kyma/tests/function-controller/pkg/step"
"github.com/kyma-project/kyma/tests/function-controller/testsuite"
"github.com/kyma-project/kyma/tests/function-controller/testsuite/runtimes"
"github.com/kyma-project/kyma/tests/function-controller/testsuite/teststep"
)

const (
nodejs16 = "nodejs16"
nodejs18 = "nodejs18"
python39 = "python39"
)

func SimpleFunctionAPIGatewayTest(restConfig *rest.Config, cfg testsuite.Config, logf *logrus.Entry) (step.Step, error) {
now := time.Now()
cfg.Namespace = fmt.Sprintf("%s-%02dh%02dm%02ds", "test-simple-api-gateway", now.Hour(), now.Minute(), now.Second())

dynamicCli, err := dynamic.NewForConfig(restConfig)
if err != nil {
return nil, errors.Wrapf(err, "while creating dynamic client")
}

coreCli, err := typedcorev1.NewForConfig(restConfig)
if err != nil {
return nil, errors.Wrap(err, "while creating k8s CoreV1Client")
}

appsCli, err := typedappsv1.NewForConfig(restConfig)
if err != nil {
return nil, errors.Wrapf(err, "while creating k8s apps client")
}

python39Logger := logf.WithField(scenarioKey, "python39")
nodejs16Logger := logf.WithField(scenarioKey, "nodejs16")
nodejs18Logger := logf.WithField(scenarioKey, "nodejs18")

genericContainer := shared.Container{
DynamicCli: dynamicCli,
Namespace: cfg.Namespace,
WaitTimeout: cfg.WaitTimeout,
Verbose: cfg.Verbose,
Log: logf,
}

python39Fn := function.NewFunction("python39", cfg.KubectlProxyEnabled, genericContainer.WithLogger(python39Logger))

nodejs16Fn := function.NewFunction("nodejs16", cfg.KubectlProxyEnabled, genericContainer.WithLogger(nodejs16Logger))

nodejs18Fn := function.NewFunction("nodejs18", cfg.KubectlProxyEnabled, genericContainer.WithLogger(nodejs18Logger))

logf.Infof("Testing function in namespace: %s", cfg.Namespace)

return step.NewSerialTestRunner(logf, "Runtime test",
teststep.NewNamespaceStep("Create test namespace", coreCli, genericContainer),
teststep.NewApplication("Create HTTP basic application", HTTPAppName, HTTPAppImage, int32(80), appsCli.Deployments(genericContainer.Namespace), coreCli.Services(genericContainer.Namespace), genericContainer),
step.NewParallelRunner(logf, "Fn tests",
step.NewSerialTestRunner(python39Logger, "Python39 test",
teststep.CreateFunction(python39Logger, python39Fn, "Create Python39 Function", runtimes.BasicPythonFunction("Hello from python39", serverlessv1alpha2.Python39)),
teststep.NewAPIGatewayFunctionCheck("python39", python39Fn, coreCli, genericContainer.Namespace, python39),
),
step.NewSerialTestRunner(nodejs16Logger, "NodeJS16 test",
teststep.CreateFunction(nodejs16Logger, nodejs16Fn, "Create NodeJS16 Function", runtimes.BasicNodeJSFunction("Hello from nodejs16", serverlessv1alpha2.NodeJs16)),
teststep.NewAPIGatewayFunctionCheck("nodejs16", nodejs16Fn, coreCli, genericContainer.Namespace, nodejs16),
),
step.NewSerialTestRunner(nodejs18Logger, "NodeJS18 test",
teststep.CreateFunction(nodejs18Logger, nodejs18Fn, "Create NodeJS18 Function", runtimes.BasicNodeJSFunction("Hello from nodejs18", serverlessv1alpha2.NodeJs18)),
teststep.NewAPIGatewayFunctionCheck("nodejs18", nodejs18Fn, coreCli, genericContainer.Namespace, nodejs18),
),
),
), nil
}
112 changes: 112 additions & 0 deletions tests/function-controller/testsuite/teststep/check.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package teststep

import (
"context"
"encoding/json"
goerrors "errors"
"fmt"
"io"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -232,3 +238,109 @@ func (t TracingHTTPCheck) assertTracingResponse(response tracingResponse) error

return nil
}

const (
resourceLabel = "serverless.kyma-project.io/resource"
functionNameLabel = "serverless.kyma-project.io/function-name"
manageByLabel = "serverless.kyma-project.io/managed-by"
uuidLabel = "serverless.kyma-project.io/uuid"
)

type APIGatewayFunctionCheck struct {
name string
fn *function.Function
client *v1.CoreV1Client
namespace string
runtime string
}

func NewAPIGatewayFunctionCheck(name string, fn *function.Function, coreV1 *v1.CoreV1Client, ns string, rt string) *APIGatewayFunctionCheck {
return &APIGatewayFunctionCheck{
name: name,
fn: fn,
client: coreV1,
namespace: ns,
runtime: rt,
}
}

func (d APIGatewayFunctionCheck) Name() string {
return d.name
}

func (d APIGatewayFunctionCheck) Run() error {

svc, err := d.client.Services(d.namespace).Get(context.Background(), d.name, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, "while trying to get service")
}

pod, err := d.client.Pods(d.namespace).List(context.Background(), metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=deployment,%s=%s", resourceLabel, functionNameLabel, d.name)})
if err != nil {
return errors.Wrap(err, "while trying to get pod")
}

err = checkIfRequiredLabelsExists(svc.Spec.Selector, true)
if err != nil {
return errors.Wrap(err, " while checking the service labels")
}
err = checkIfRequiredLabelsExists(pod.Items[0].ObjectMeta.Labels, false)
if err != nil {
return errors.Wrap(err, " while checking the pod labels")
}

err = checkIfContractIsFulfilled(pod.Items[0], *svc)
if err != nil {
return errors.Wrap(err, " while checking labels")
}

if len(svc.Spec.Selector) != 0 {
return errors.New("The labels are not matching")
}

return nil
}

func (d APIGatewayFunctionCheck) Cleanup() error {
return nil
}

func (d APIGatewayFunctionCheck) OnError() error {
return nil
}

func checkIfContractIsFulfilled(pod corev1.Pod, service corev1.Service) error {
var errJoined error

for k, v := range pod.Labels {
if val, exists := service.Spec.Selector[k]; exists {
if val == v {
delete(service.Spec.Selector, k)
} else {
err := errors.Errorf("Expected %s but got %s", v, val)
errJoined = goerrors.Join(err)
}
}
}
return errJoined
}

func checkIfRequiredLabelsExists(labels map[string]string, isService bool) error {
requiredLabels := []string{resourceLabel, functionNameLabel, manageByLabel, uuidLabel}

if isService {
if len(labels) != 4 {
return errors.New(fmt.Sprintf("Service has got %s istead of 4 labels", len(labels)))
}
}

var errJoined error

for _, label := range requiredLabels {
if _, exists := labels[label]; !exists {
err := errors.New(fmt.Sprintf("Label %s is missing", label))
errJoined = goerrors.Join(err)
}
}
return errJoined
}

0 comments on commit 32990c0

Please sign in to comment.