Skip to content
This repository was archived by the owner on Jun 7, 2021. It is now read-only.

First test e2e #19

Merged
merged 4 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
88 changes: 88 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions test/e2e/deploy_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package e2e

import (
goctx "context"
jsfunction "github.com/openshift-cloud-functions/js-function-operator/pkg/apis/faas/v1alpha1"
"github.com/openshift-cloud-functions/js-function-operator/test"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knservingclient "knative.dev/serving/pkg/client/clientset/versioned"
"testing"
)

var (
functionName = "test-function"
functionSrc = `
module.exports = context => {
return "ok";
};`
)

func TestDeployFunction(t *testing.T) {
ctx, f, namespace := test.E2EBootstrap(t)

t.Logf("Deploying function in namespace %v", namespace)

exampleJs := &jsfunction.JSFunction{
ObjectMeta: metav1.ObjectMeta{
Name: functionName,
Namespace: namespace,
},
Spec: jsfunction.JSFunctionSpec{
Func: functionSrc,
},
}

// Create the service
err := f.Client.Create(goctx.TODO(), exampleJs, &framework.CleanupOptions{TestContext: ctx})
assert.NoError(t, err)

knClient := knservingclient.NewForConfigOrDie(f.KubeConfig).ServingV1alpha1()

// Wait for knative service to be ready
knService := test.WaitForKnativeServiceReadyDefault(t, &knClient, functionName, namespace)
serviceURL := knService.Status.Address.URL

test.AssertGetRequest(t, serviceURL.String(), 200, []byte("ok"))

}
10 changes: 10 additions & 0 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package e2e

import (
f "github.com/operator-framework/operator-sdk/pkg/test"
"testing"
)

func TestMain(m *testing.M) {
f.MainEntry(m)
}
15 changes: 15 additions & 0 deletions test/run_e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

kubectl create namespace operator-test
kubectl create -f deploy/crds/faas_v1alpha1_jsfunction_crd.yaml
kubectl create -f deploy/service_account.yaml --namespace operator-test
kubectl create -f deploy/role.yaml --namespace operator-test
kubectl create -f deploy/role_binding.yaml --namespace operator-test
kubectl create -f deploy/operator.yaml --namespace operator-test

oc adm policy add-role-to-user edit -z js-function-operator
oc adm policy add-scc-to-user privileged -z js-function-operator

operator-sdk test local ./test/e2e --namespace operator-test --no-setup --verbose

kubectl delete namespace operator-test
108 changes: 108 additions & 0 deletions test/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package test

import (
"github.com/openshift-cloud-functions/js-function-operator/pkg/apis"
jsfunction "github.com/openshift-cloud-functions/js-function-operator/pkg/apis/faas/v1alpha1"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
"github.com/stretchr/testify/assert"
"io/ioutil"
metav1errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/serving/pkg/apis/serving/v1alpha1"
servingv1alpha1 "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1"
"net/http"
"testing"
"time"
)

const (
defaultRetryInterval = 1 * time.Second
defaultTimeout = defaultRetryInterval * 40
cleanupRetryInterval = time.Second * 1
cleanupTimeout = time.Second * 5
OPERATOR_NAME = "js-function-operator"
)

func AssertGetRequest(t *testing.T, url string, expectedStatusCode int, expectedBody []byte) {
res, err := http.Get(url)
if err != nil {
assert.Fail(t, err.Error())
return
}

assert.Equal(t, expectedStatusCode, res.StatusCode)

b, err := ioutil.ReadAll(res.Body)
if err != nil {
assert.Fail(t, err.Error())
return
}

assert.Equal(t, expectedBody, b)
}

func WaitForKnativeServiceReadyDefault(t *testing.T, servingClient *servingv1alpha1.ServingV1alpha1Interface, name string, namespace string) *v1alpha1.Service {
return WaitForKnativeServiceReady(t, servingClient, name, namespace, defaultRetryInterval, defaultTimeout)
}

func WaitForKnativeServiceReady(t *testing.T, servingClient *servingv1alpha1.ServingV1alpha1Interface, name string, namespace string, retryInterval, timeout time.Duration) *v1alpha1.Service {
var service *v1alpha1.Service

err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
service, err = (*servingClient).Services(namespace).Get(name, metav1.GetOptions{IncludeUninitialized: true})
if err != nil {
if metav1errors.IsNotFound(err) {
t.Logf("Waiting for availability of %s service\n", name)
return false, nil
}
return false, err
}

if service.Status.IsReady() {
return true, nil
}
t.Logf("Waiting for availability of %s service. Actual status: %+v\n", name, service.Status)
return false, nil
})

assert.NoError(t, err)

return service
}

// This function bootstraps an e2e adding CRDs, initializing the test ctx and deploying the operator
func E2EBootstrap(t *testing.T) (*framework.TestCtx, *framework.Framework, string) {
// Add CRDs
jsFunctionList := &jsfunction.JSFunctionList{}
err := framework.AddToFrameworkScheme(apis.AddToScheme, jsFunctionList)
if err != nil {
t.Fatalf("failed to add custom resource scheme to framework: %v", err)
}
t.Log("Added CRDs")

// Setup test framework
ctx := framework.NewTestCtx(t)
t.Log("Test Framework context ready")

// Initialize cluster resources
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval})
if err != nil {
t.Fatalf("failed to initialize cluster resources: %v", err)
}
t.Log("Initialized cluster resources")

namespace, err := ctx.GetNamespace()
if err != nil {
t.Fatal(err)
}
f := framework.Global
err = e2eutil.WaitForDeployment(t, f.KubeClient, namespace, OPERATOR_NAME, 1, defaultRetryInterval, defaultTimeout)
if err != nil {
t.Fatal(err)
}
t.Log("Operator ready")

return ctx, f, namespace
}