Skip to content

Contract test between api-gateway and serverless #17894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
75 changes: 75 additions & 0 deletions tests/function-controller/testsuite/teststep/check.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package teststep

import (
"context"
"encoding/json"
"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 +236,74 @@ func (t TracingHTTPCheck) assertTracingResponse(response tracingResponse) error

return nil
}

const (
resourceLabel = "serverless.kyma-project.io/resource"
)

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")
}

pods, err := d.client.Pods(d.namespace).List(context.Background(), metav1.ListOptions{LabelSelector: resourceLabel})
Copy link
Contributor

@dbadura dbadura Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can implement such selector:

kubectl get pod -n test-simple-tracing-11h22m49s  -l serverless.kyma-project.io/function-name=nodejs16,serverless.kyma-project.io/resource=deployment

if err != nil {
return errors.Wrap(err, "while trying to get pods")
}

var podSelector corev1.Pod

for _, element := range pods.Items {
if element.Spec.Containers[0].Env[0].Value == d.runtime {
podSelector = element
}
}

for k, v := range podSelector.ObjectMeta.Labels {
if val, exists := svc.Spec.Selector[k]; exists {
if val == v {
delete(svc.Spec.Selector, k)
} else {
return errors.Errorf("Expected %s but got %s", v, val)
}
}
}

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
}