Skip to content
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

Add prerun TestMain function to test utils #5205

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions tests/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ func GetKubernetesClient(t *testing.T) *kubernetes.Clientset {
return KubeClient
}

func GetKubernetesClientOrError() (*kubernetes.Clientset, error) {
if KubeClient != nil && KubeConfig != nil {
return KubeClient, nil
}
var err error
KubeConfig, err = config.GetConfig()
if err != nil {
return nil, err
}
KubeClient, err = kubernetes.NewForConfig(KubeConfig)
if err != nil {
return nil, err
}
return KubeClient, nil
}

func GetKedaKubernetesClient(t *testing.T) *v1alpha1.KedaV1alpha1Client {
Copy link
Member

Choose a reason for hiding this comment

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

I will take a look later if I can manage to remove this call from all tests so the test setup logic is in a single place

if KedaKubeClient != nil && KubeConfig != nil {
return KedaKubeClient
Expand Down
22 changes: 22 additions & 0 deletions tests/utils/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build e2e
// +build e2e

package utils

import (
"fmt"
"os"
"testing"

. "github.com/kedacore/keda/v2/tests/helper"
)

func TestMain(m *testing.M) {
Copy link
Member

Choose a reason for hiding this comment

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

might be interesting to see if we can move this to either helper package or some common test package later

var err error
KubeClient, err = GetKubernetesClientOrError()
if err != nil {
fmt.Fprintf(os.Stderr, "error getting kubernetes client - %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
12 changes: 0 additions & 12 deletions tests/utils/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ func TestVerifyCommands(t *testing.T) {
}
}

func TestKubernetesConnection(t *testing.T) {
KubeClient = GetKubernetesClient(t)
}

func TestKubernetesVersion(t *testing.T) {
out, err := ExecuteCommand("kubectl version")
require.NoErrorf(t, err, "error getting kubernetes version - %s", err)
Expand Down Expand Up @@ -77,7 +73,6 @@ func TestSetupCertManager(t *testing.T) {
_, err = ExecuteCommand("helm repo update jetstack")
require.NoErrorf(t, err, "cannot update jetstack helm repo - %s", err)

KubeClient = GetKubernetesClient(t)
CreateNamespace(t, KubeClient, CertManagerNamespace)

_, err = ExecuteCommand(fmt.Sprintf("helm upgrade --install cert-manager jetstack/cert-manager --namespace %s --set installCRDs=true",
Expand All @@ -99,7 +94,6 @@ func TestSetupWorkloadIdentityComponents(t *testing.T) {
_, err = ExecuteCommand("helm repo update azure-workload-identity")
require.NoErrorf(t, err, "cannot update workload identity helm repo - %s", err)

KubeClient = GetKubernetesClient(t)
CreateNamespace(t, KubeClient, AzureWorkloadIdentityNamespace)

_, err = ExecuteCommand(fmt.Sprintf("helm upgrade --install workload-identity-webhook azure-workload-identity/workload-identity-webhook --namespace %s --set azureTenantID=%s",
Expand All @@ -121,7 +115,6 @@ func TestSetupAwsIdentityComponents(t *testing.T) {
_, err = ExecuteCommand("helm repo update jkroepke")
require.NoErrorf(t, err, "cannot update jkroepke helm repo - %s", err)

KubeClient = GetKubernetesClient(t)
CreateNamespace(t, KubeClient, AwsIdentityNamespace)

_, err = ExecuteCommand(fmt.Sprintf("helm upgrade --install aws-identity-webhook jkroepke/amazon-eks-pod-identity-webhook --namespace %s --set fullnameOverride=aws-identity-webhook",
Expand All @@ -143,7 +136,6 @@ func TestSetupGcpIdentityComponents(t *testing.T) {
_, err = ExecuteCommand("helm repo update gcp-workload-identity-federation-webhook")
require.NoErrorf(t, err, "cannot update gcp-workload-identity-federation-webhook helm repo - %s", err)

KubeClient = GetKubernetesClient(t)
CreateNamespace(t, KubeClient, GcpIdentityNamespace)

_, err = ExecuteCommand(fmt.Sprintf("helm upgrade --install gcp-identity-webhook gcp-workload-identity-federation-webhook/gcp-workload-identity-federation-webhook --namespace %s --set fullnameOverride=gcp-identity-webhook --set controllerManager.manager.args[0]=--token-default-mode=0444",
Expand Down Expand Up @@ -197,7 +189,6 @@ func TestSetupOpentelemetryComponents(t *testing.T) {
}

func TestDeployKEDA(t *testing.T) {
KubeClient = GetKubernetesClient(t)
CreateNamespace(t, KubeClient, KEDANamespace)

caCtr, _ := GetTestCA(t)
Expand Down Expand Up @@ -244,7 +235,6 @@ func TestSetupAadPodIdentityComponents(t *testing.T) {
_, err = ExecuteCommand("helm repo update aad-pod-identity")
require.NoErrorf(t, err, "cannot update aad pod identity helm repo - %s", err)

KubeClient = GetKubernetesClient(t)
CreateNamespace(t, KubeClient, AzureAdPodIdentityNamespace)

_, err = ExecuteCommand(fmt.Sprintf("helm upgrade --install "+
Expand All @@ -267,8 +257,6 @@ func TestSetUpStrimzi(t *testing.T) {
_, err = ExecuteCommand("helm repo update")
assert.NoErrorf(t, err, "cannot execute command - %s", err)

KubeClient = GetKubernetesClient(t)

CreateNamespace(t, KubeClient, StrimziNamespace)

_, err = ExecuteCommand(fmt.Sprintf(`helm upgrade --install --namespace %s --wait %s strimzi/strimzi-kafka-operator --version %s --set watchAnyNamespace=true`,
Expand Down
Loading