From 4c68f91fe6442b4c73141a473342ae9c0b9e01a7 Mon Sep 17 00:00:00 2001 From: Sebastian Laskawiec Date: Fri, 13 Sep 2019 12:04:55 +0200 Subject: [PATCH] INTLY-3462 An initial end to end test --- test/e2e/keycloak_test.go | 95 +++++++++++++++++++++++++++++++++++++++ test/e2e/main_test.go | 11 +++++ 2 files changed, 106 insertions(+) create mode 100644 test/e2e/keycloak_test.go create mode 100644 test/e2e/main_test.go diff --git a/test/e2e/keycloak_test.go b/test/e2e/keycloak_test.go new file mode 100644 index 000000000..ae4e46ef4 --- /dev/null +++ b/test/e2e/keycloak_test.go @@ -0,0 +1,95 @@ +package e2e + +import ( + goctx "context" + "fmt" + apis "github.com/keycloak/keycloak-operator/pkg/apis" + keycloakv1alpha1 "github.com/keycloak/keycloak-operator/pkg/apis/keycloak/v1alpha1" + "testing" + "time" + + framework "github.com/operator-framework/operator-sdk/pkg/test" + "github.com/operator-framework/operator-sdk/pkg/test/e2eutil" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" +) + +const testKeycloakCRDName = "keycloak-test" +const retryInterval = time.Second * 5 +const timeout = time.Second * 60 +const cleanupRetryInterval = time.Second * 1 +const cleanupTimeout = time.Second * 5 + +type testWithDeployedOperator func(*testing.T, *framework.Framework, *framework.TestCtx) error + +func TestKeycloak(t *testing.T) { + keycloakType := &keycloakv1alpha1.Keycloak{} + err := framework.AddToFrameworkScheme(apis.AddToScheme, keycloakType) + if err != nil { + t.Fatalf("failed to add custom resource scheme to framework: %v", err) + } + // run subtests + t.Run("keycloak-group", func(t *testing.T) { + runTest(t, keycloakDeploymentTest) + }) +} + +func keycloakDeploymentTest(t *testing.T, f *framework.Framework, ctx *framework.TestCtx) error { + namespace, err := ctx.GetNamespace() + if err != nil { + return fmt.Errorf("could not get namespace: %v", err) + } + + keycloakCRD := &keycloakv1alpha1.Keycloak{ + ObjectMeta: metav1.ObjectMeta{ + Name: testKeycloakCRDName, + Namespace: namespace, + }, + Spec: keycloakv1alpha1.KeycloakSpec{ + // FIXME: More code after the initial implementation + }, + } + // use TestCtx's create helper to create the object and add a cleanup function for the new object + err = f.Client.Create(goctx.TODO(), keycloakCRD, &framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval}) + if err != nil { + return err + } + // FIXME: More code after the initial implementation + //err = e2eutil.WaitForDeployment(t, f.KubeClient, namespace, testKeycloakCRDName, 1, retryInterval, timeout) + //if err != nil { + // return err + //} + + err = f.Client.Get(goctx.TODO(), types.NamespacedName{Name: testKeycloakCRDName, Namespace: namespace}, keycloakCRD) + if err != nil { + return err + } + // FIXME: More code after the initial implementation + return err +} + +func runTest(t *testing.T, testCase testWithDeployedOperator) { + t.Parallel() + ctx := framework.NewTestCtx(t) + defer ctx.Cleanup() + 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) + } + // get global framework variables + f := framework.Global + // wait for Keycloak Operator to be ready + err = e2eutil.WaitForOperatorDeployment(t, f.KubeClient, namespace, testKeycloakCRDName, 1, retryInterval, timeout) + if err != nil { + t.Fatal(err) + } + + if err = testCase(t, f, ctx); err != nil { + t.Fatal(err) + } +} diff --git a/test/e2e/main_test.go b/test/e2e/main_test.go new file mode 100644 index 000000000..6d2e6988a --- /dev/null +++ b/test/e2e/main_test.go @@ -0,0 +1,11 @@ +package e2e + +import ( + "testing" + + f "github.com/operator-framework/operator-sdk/pkg/test" +) + +func TestMain(m *testing.M) { + f.MainEntry(m) +}