diff --git a/tests/helper/helper.go b/tests/helper/helper.go index e8c101a8744..81f3fb09151 100644 --- a/tests/helper/helper.go +++ b/tests/helper/helper.go @@ -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 { if KedaKubeClient != nil && KubeConfig != nil { return KedaKubeClient diff --git a/tests/utils/main_test.go b/tests/utils/main_test.go new file mode 100644 index 00000000000..82b37be69e0 --- /dev/null +++ b/tests/utils/main_test.go @@ -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) { + 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()) +} diff --git a/tests/utils/setup_test.go b/tests/utils/setup_test.go index be8f2bc0cac..5eb35cfd901 100644 --- a/tests/utils/setup_test.go +++ b/tests/utils/setup_test.go @@ -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) @@ -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", @@ -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", @@ -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", @@ -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", @@ -197,7 +189,6 @@ func TestSetupOpentelemetryComponents(t *testing.T) { } func TestDeployKEDA(t *testing.T) { - KubeClient = GetKubernetesClient(t) CreateNamespace(t, KubeClient, KEDANamespace) caCtr, _ := GetTestCA(t) @@ -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 "+ @@ -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`,