diff --git a/hack/e2e/cleanup/cleanup_test.go b/hack/e2e/cleanup/cleanup_test.go index 291b0b52..4886c381 100644 --- a/hack/e2e/cleanup/cleanup_test.go +++ b/hack/e2e/cleanup/cleanup_test.go @@ -9,73 +9,31 @@ package cleanup_test import ( "context" - "fmt" "os" "testing" - "time" eventingv1alpha1 "github.com/kyma-project/eventing-manager/api/v1alpha1" - "github.com/kyma-project/eventing-manager/hack/e2e/env" + "github.com/kyma-project/eventing-manager/hack/e2e/common/testenvironment" "github.com/kyma-project/eventing-manager/pkg/eventing" "github.com/pkg/errors" "github.com/stretchr/testify/require" k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "go.uber.org/zap" - k8stypes "k8s.io/apimachinery/pkg/types" - - "k8s.io/client-go/kubernetes" - "sigs.k8s.io/controller-runtime/pkg/client" - . "github.com/kyma-project/eventing-manager/hack/e2e/common" . "github.com/kyma-project/eventing-manager/hack/e2e/common/fixtures" ) -// Constants for retries. -const ( - interval = 2 * time.Second - attempts = 60 -) - -// clientSet is what is used to access K8s build-in resources like Pods, Namespaces and so on. -var clientSet *kubernetes.Clientset //nolint:gochecknoglobals // This will only be accessible in e2e tests. - -// k8sClient is what is used to access the Eventing CR. -var k8sClient client.Client //nolint:gochecknoglobals // This will only be accessible in e2e tests. - -var logger *zap.Logger - -var testConfigs *env.E2EConfig +var testEnvironment *testenvironment.TestEnvironment // TestMain runs before all the other test functions. It sets up all the resources that are shared between the different // test functions. It will then run the tests and finally shuts everything down. func TestMain(m *testing.M) { - var err error - logger, err = SetupLogger() - if err != nil { - logger.Fatal(err.Error()) - } + testEnvironment = testenvironment.NewTestEnvironment() - testConfigs, err = env.GetE2EConfig() - if err != nil { - logger.Fatal(err.Error()) - - } - logger.Info(fmt.Sprintf("##### NOTE: Tests will run w.r.t. backend: %s", testConfigs.BackendType)) - - clientSet, k8sClient, err = GetK8sClients() - if err != nil { - logger.Fatal(err.Error()) - } - - ctx := context.TODO() // Delete the Eventing CR used for testing. - err = Retry(attempts, interval, func() error { - return k8sClient.Delete(ctx, EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType))) - }) - if err != nil { - logger.Fatal(err.Error()) + if err := testEnvironment.DeleteEventingCR(); err != nil { + testEnvironment.Logger.Fatal(err.Error()) } // Run the tests and exit. @@ -86,9 +44,8 @@ func TestMain(m *testing.M) { func Test_NoEventingCRExists(t *testing.T) { t.Parallel() - ctx := context.TODO() - err := Retry(attempts, interval, func() error { - _, crErr := getEventingCR(ctx, CRName, NamespaceName) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + _, crErr := testEnvironment.GetEventingCRFromK8s(CRName, NamespaceName) // This is what we want here. if k8serrors.IsNotFound(crErr) { return nil @@ -107,9 +64,9 @@ func Test_NoEventingCRExists(t *testing.T) { func Test_NoPublisherServiceAccountExists(t *testing.T) { t.Parallel() ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - err := Retry(attempts, interval, func() error { - _, getErr := clientSet.CoreV1().ServiceAccounts(NamespaceName).Get(ctx, + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + _, getErr := testEnvironment.K8sClientset.CoreV1().ServiceAccounts(NamespaceName).Get(ctx, eventing.GetPublisherServiceAccountName(*eventingCR), metav1.GetOptions{}) if getErr == nil { return errors.New("PublisherServiceAccount should have been deleted") @@ -122,14 +79,14 @@ func Test_NoPublisherServiceAccountExists(t *testing.T) { require.NoError(t, err) } -// Enable this test once https://github.com/kyma-project/eventing-manager/issues/34 is done! +//// Enable this test once https://github.com/kyma-project/eventing-manager/issues/34 is done! //// Test_NoPublisherClusterRoleExists tests if the publisher-proxy ClusterRole was deleted. //func Test_NoPublisherClusterRoleExists(t *testing.T) { // t.Parallel() // ctx := context.TODO() -// eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) -// err := Retry(attempts, interval, func() error { -// _, getErr := clientSet.RbacV1().ClusterRoles().Get(ctx, +// eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) +// err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { +// _, getErr := testEnvironment.K8sClientset.RbacV1().ClusterRoles().Get(ctx, // eventing.GetPublisherClusterRoleName(*eventingCR), metav1.GetOptions{}) // if getErr == nil { // return errors.New("PublisherClusterRole should have been deleted") @@ -141,15 +98,15 @@ func Test_NoPublisherServiceAccountExists(t *testing.T) { // }) // require.NoError(t, err) //} - -// Enable this test once https://github.com/kyma-project/eventing-manager/issues/34 is done! +// +//// Enable this test once https://github.com/kyma-project/eventing-manager/issues/34 is done! //// Test_NoPublisherClusterRoleBindingExists tests if the publisher-proxy ClusterRoleBinding was deleted. //func Test_NoPublisherClusterRoleBindingExists(t *testing.T) { // t.Parallel() // ctx := context.TODO() -// eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) -// err := Retry(attempts, interval, func() error { -// _, getErr := clientSet.RbacV1().ClusterRoleBindings().Get(ctx, +// eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) +// err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { +// _, getErr := testEnvironment.K8sClientset.RbacV1().ClusterRoleBindings().Get(ctx, // eventing.GetPublisherClusterRoleBindingName(*eventingCR), metav1.GetOptions{}) // if getErr == nil { // return errors.New("PublisherClusterRoleBinding should have been deleted") @@ -166,10 +123,10 @@ func Test_NoPublisherServiceAccountExists(t *testing.T) { func Test_NoPublisherServicesExists(t *testing.T) { t.Parallel() ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - err := Retry(attempts, interval, func() error { + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { // check service to expose event publishing endpoint, was deleted. - _, getErr := clientSet.CoreV1().Services(NamespaceName).Get(ctx, + _, getErr := testEnvironment.K8sClientset.CoreV1().Services(NamespaceName).Get(ctx, eventing.GetPublisherPublishServiceName(*eventingCR), metav1.GetOptions{}) if getErr == nil { return errors.New("Publisher PublishService should have been deleted") @@ -179,7 +136,7 @@ func Test_NoPublisherServicesExists(t *testing.T) { } // check service to expose metrics endpoint, was deleted. - _, getErr = clientSet.CoreV1().Services(NamespaceName).Get(ctx, + _, getErr = testEnvironment.K8sClientset.CoreV1().Services(NamespaceName).Get(ctx, eventing.GetPublisherMetricsServiceName(*eventingCR), metav1.GetOptions{}) if getErr == nil { return errors.New("Publisher MetricsService should have been deleted") @@ -189,7 +146,7 @@ func Test_NoPublisherServicesExists(t *testing.T) { } // check service to expose health endpoint, was deleted. - _, getErr = clientSet.CoreV1().Services(NamespaceName).Get(ctx, + _, getErr = testEnvironment.K8sClientset.CoreV1().Services(NamespaceName).Get(ctx, eventing.GetPublisherHealthServiceName(*eventingCR), metav1.GetOptions{}) if getErr == nil { return errors.New("Publisher HealthService should have been deleted") @@ -206,9 +163,9 @@ func Test_NoPublisherServicesExists(t *testing.T) { func Test_NoPublisherHPAExists(t *testing.T) { t.Parallel() ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - err := Retry(attempts, interval, func() error { - _, getErr := clientSet.AutoscalingV2().HorizontalPodAutoscalers(NamespaceName).Get(ctx, + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + _, getErr := testEnvironment.K8sClientset.AutoscalingV2().HorizontalPodAutoscalers(NamespaceName).Get(ctx, eventing.GetPublisherDeploymentName(*eventingCR), metav1.GetOptions{}) if !k8serrors.IsNotFound(getErr) { return getErr @@ -222,9 +179,9 @@ func Test_NoPublisherHPAExists(t *testing.T) { func Test_NoPublisherProxyDeploymentExists(t *testing.T) { t.Parallel() ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - err := Retry(attempts, interval, func() error { - _, getErr := clientSet.AppsV1().Deployments(NamespaceName).Get(ctx, + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + _, getErr := testEnvironment.K8sClientset.AppsV1().Deployments(NamespaceName).Get(ctx, eventing.GetPublisherDeploymentName(*eventingCR), metav1.GetOptions{}) if !k8serrors.IsNotFound(getErr) { return getErr @@ -233,12 +190,3 @@ func Test_NoPublisherProxyDeploymentExists(t *testing.T) { }) require.NoError(t, err) } - -func getEventingCR(ctx context.Context, name, namespace string) (*eventingv1alpha1.Eventing, error) { - var eventingCR eventingv1alpha1.Eventing - err := k8sClient.Get(ctx, k8stypes.NamespacedName{ - Name: name, - Namespace: namespace, - }, &eventingCR) - return &eventingCR, err -} diff --git a/hack/e2e/common/testenvironment/test_environment.go b/hack/e2e/common/testenvironment/test_environment.go index 4a79126e..e105882a 100644 --- a/hack/e2e/common/testenvironment/test_environment.go +++ b/hack/e2e/common/testenvironment/test_environment.go @@ -11,6 +11,8 @@ import ( cloudevents "github.com/cloudevents/sdk-go/v2" "github.com/cloudevents/sdk-go/v2/binding" + eventingv1alpha1 "github.com/kyma-project/eventing-manager/api/v1alpha1" + "github.com/kyma-project/eventing-manager/hack/e2e/common" "github.com/kyma-project/eventing-manager/hack/e2e/common/eventing" "github.com/kyma-project/eventing-manager/hack/e2e/common/fixtures" @@ -482,3 +484,84 @@ func (te *TestEnvironment) CompareCloudEvents(expectedEvent cloudevents.Event, g return resultError } + +func (te *TestEnvironment) SetupEventingCR() error { + return common.Retry(Attempts, Interval, func() error { + ctx := context.TODO() + eventingCR := fixtures.EventingCR(eventingv1alpha1.BackendType(te.TestConfigs.BackendType)) + errEvnt := te.K8sClient.Create(ctx, eventingCR) + if k8serrors.IsAlreadyExists(errEvnt) { + gotEventingCR, getErr := te.GetEventingCRFromK8s(eventingCR.Name, eventingCR.Namespace) + if getErr != nil { + return getErr + } + + // If Backend type is changed then update the CR. + if gotEventingCR.Spec.Backend.Type != eventingCR.Spec.Backend.Type { + eventingCR.ObjectMeta = gotEventingCR.ObjectMeta + if errEvnt = te.K8sClient.Update(ctx, eventingCR); errEvnt != nil { + return errEvnt + } + } else { + te.Logger.Warn( + "error while creating Eventing CR, resource already exist; test will continue with existing CR", + ) + } + return nil + } + return errEvnt + }) +} + +func (te *TestEnvironment) DeleteEventingCR() error { + return common.Retry(Attempts, Interval, func() error { + return client.IgnoreNotFound(te.K8sClient.Delete(te.Context, + fixtures.EventingCR(eventingv1alpha1.BackendType(te.TestConfigs.BackendType)))) + }) +} + +func (te *TestEnvironment) GetEventingCRFromK8s(name, namespace string) (*eventingv1alpha1.Eventing, error) { + var eventingCR eventingv1alpha1.Eventing + err := te.K8sClient.Get(te.Context, k8stypes.NamespacedName{ + Name: name, + Namespace: namespace, + }, &eventingCR) + return &eventingCR, err +} + +func (te *TestEnvironment) GetDeployment(name, namespace string) (*appsv1.Deployment, error) { + return te.K8sClientset.AppsV1().Deployments(namespace).Get(te.Context, name, metav1.GetOptions{}) +} + +func (te *TestEnvironment) WaitForEventingCRReady() error { + // RetryGet the Eventing CR and test status. + return common.Retry(Attempts, Interval, func() error { + te.Logger.Debug(fmt.Sprintf("waiting for Eventing CR to get ready. "+ + "CR name: %s, namespace: %s", fixtures.CRName, fixtures.NamespaceName)) + + // Get the Eventing CR from the cluster. + gotEventingCR, err := common.RetryGet(Attempts, Interval, func() (*eventingv1alpha1.Eventing, error) { + return te.GetEventingCRFromK8s(fixtures.CRName, fixtures.NamespaceName) + }) + if err != nil { + return err + } + + if gotEventingCR.Spec.Backend.Type != gotEventingCR.Status.ActiveBackend { + err := fmt.Errorf("waiting for Eventing CR to switch backend") + te.Logger.Debug(err.Error()) + return err + } + + if gotEventingCR.Status.State != eventingv1alpha1.StateReady { + err := fmt.Errorf("waiting for Eventing CR to get ready state") + te.Logger.Debug(err.Error()) + return err + } + + // Everything is fine. + te.Logger.Debug(fmt.Sprintf("Eventing CR is ready. "+ + "CR name: %s, namespace: %s", fixtures.CRName, fixtures.NamespaceName)) + return nil + }) +} diff --git a/hack/e2e/setup/setup_test.go b/hack/e2e/setup/setup_test.go index 643dc2ca..5405e6fc 100644 --- a/hack/e2e/setup/setup_test.go +++ b/hack/e2e/setup/setup_test.go @@ -15,116 +15,53 @@ import ( "testing" "time" + "github.com/kyma-project/eventing-manager/hack/e2e/common/testenvironment" + eventingv1alpha1 "github.com/kyma-project/eventing-manager/api/v1alpha1" - "github.com/kyma-project/eventing-manager/hack/e2e/env" "github.com/kyma-project/eventing-manager/pkg/eventing" "github.com/pkg/errors" - "github.com/stretchr/testify/require" - "go.uber.org/zap" - appsv1 "k8s.io/api/apps/v1" - k8serrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - k8stypes "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/kubernetes" - "sigs.k8s.io/controller-runtime/pkg/client" - . "github.com/kyma-project/eventing-manager/hack/e2e/common" . "github.com/kyma-project/eventing-manager/hack/e2e/common/fixtures" + "github.com/stretchr/testify/require" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// Constants for retries. -const ( - interval = 2 * time.Second - attempts = 60 -) - -// clientSet is what is used to access K8s build-in resources like Pods, Namespaces and so on. -var clientSet *kubernetes.Clientset //nolint:gochecknoglobals // This will only be accessible in e2e tests. - -// k8sClient is what is used to access the Eventing CR. -var k8sClient client.Client //nolint:gochecknoglobals // This will only be accessible in e2e tests. - -var logger *zap.Logger - -var testConfigs *env.E2EConfig +var testEnvironment *testenvironment.TestEnvironment // TestMain runs before all the other test functions. It sets up all the resources that are shared between the different // test functions. It will then run the tests and finally shuts everything down. func TestMain(m *testing.M) { - var err error - logger, err = SetupLogger() - if err != nil { - logger.Fatal(err.Error()) - } + testEnvironment = testenvironment.NewTestEnvironment() - testConfigs, err = env.GetE2EConfig() - if err != nil { - logger.Fatal(err.Error()) - } - logger.Info(fmt.Sprintf("##### NOTE: Tests will run w.r.t. backend: %s", testConfigs.BackendType)) - - clientSet, k8sClient, err = GetK8sClients() - if err != nil { - logger.Fatal(err.Error()) - } - - ctx := context.TODO() - // Create the Namespace used for testing. - err = Retry(attempts, interval, func() error { - // It's fine if the Namespace already exists. - return client.IgnoreAlreadyExists(k8sClient.Create(ctx, Namespace(testConfigs.TestNamespace))) - }) - if err != nil { - logger.Fatal(err.Error()) + // create test namespace, + if err := testEnvironment.CreateTestNamespace(); err != nil { + testEnvironment.Logger.Fatal(err.Error()) } // Wait for eventing-manager deployment to get ready. - if testConfigs.ManagerImage == "" { - logger.Warn( + if testEnvironment.TestConfigs.ManagerImage == "" { + testEnvironment.Logger.Warn( "ENV `MANAGER_IMAGE` is not set. Test will not verify if the " + "manager deployment image is correct or not.", ) } - if err := waitForEventingManagerDeploymentReady(testConfigs.ManagerImage); err != nil { - logger.Fatal(err.Error()) + if err := testEnvironment.WaitForDeploymentReady(ManagerDeploymentName, NamespaceName, testEnvironment.TestConfigs.ManagerImage); err != nil { + testEnvironment.Logger.Fatal(err.Error()) } // Create the Eventing CR used for testing. - err = Retry(attempts, interval, func() error { - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - errEvnt := k8sClient.Create(ctx, eventingCR) - if k8serrors.IsAlreadyExists(errEvnt) { - gotEventingCR, getErr := getEventingCR(ctx, eventingCR.Name, eventingCR.Namespace) - if getErr != nil { - return err - } - // If Backend type is changed then update the CR. - if gotEventingCR.Spec.Backend.Type != eventingCR.Spec.Backend.Type { - eventingCR.ObjectMeta = gotEventingCR.ObjectMeta - if errEvnt = k8sClient.Update(ctx, eventingCR); getErr != nil { - return err - } - } else { - logger.Warn( - "error while creating Eventing CR, resource already exist; test will continue with existing CR", - ) - } - return nil - } - return errEvnt - }) - if err != nil { - logger.Fatal(err.Error()) + if err := testEnvironment.SetupEventingCR(); err != nil { + testEnvironment.Logger.Fatal(err.Error()) } - // wait for an interval for reconciliation to update status. - time.Sleep(interval) + // wait for an testenvironment.Interval for reconciliation to update status. + time.Sleep(testenvironment.Interval) // Wait for Eventing CR to get ready. - if err := waitForEventingCRReady(); err != nil { - logger.Fatal(err.Error()) + if err := testEnvironment.WaitForEventingCRReady(); err != nil { + testEnvironment.Logger.Fatal(err.Error()) } // Run the tests and exit. @@ -136,8 +73,8 @@ func TestMain(m *testing.M) { func Test_WebhookServerCertSecret(t *testing.T) { t.Parallel() ctx := context.TODO() - err := Retry(attempts, interval, func() error { - _, getErr := clientSet.CoreV1().Secrets(NamespaceName).Get(ctx, WebhookServerCertSecretName, metav1.GetOptions{}) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + _, getErr := testEnvironment.K8sClientset.CoreV1().Secrets(NamespaceName).Get(ctx, WebhookServerCertSecretName, metav1.GetOptions{}) if getErr != nil { return getErr } @@ -150,8 +87,8 @@ func Test_WebhookServerCertSecret(t *testing.T) { func Test_WebhookServerCertJob(t *testing.T) { t.Parallel() ctx := context.TODO() - err := Retry(attempts, interval, func() error { - _, getErr := clientSet.BatchV1().Jobs(NamespaceName).Get(ctx, WebhookServerCertJobName, metav1.GetOptions{}) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + _, getErr := testEnvironment.K8sClientset.BatchV1().Jobs(NamespaceName).Get(ctx, WebhookServerCertJobName, metav1.GetOptions{}) if getErr != nil { return getErr } @@ -164,8 +101,8 @@ func Test_WebhookServerCertJob(t *testing.T) { func Test_WebhookServerCertCronJob(t *testing.T) { t.Parallel() ctx := context.TODO() - err := Retry(attempts, interval, func() error { - _, getErr := clientSet.BatchV1().CronJobs(NamespaceName).Get(ctx, WebhookServerCertJobName, metav1.GetOptions{}) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + _, getErr := testEnvironment.K8sClientset.BatchV1().CronJobs(NamespaceName).Get(ctx, WebhookServerCertJobName, metav1.GetOptions{}) if getErr != nil { return getErr } @@ -178,9 +115,9 @@ func Test_WebhookServerCertCronJob(t *testing.T) { func Test_PublisherServiceAccount(t *testing.T) { t.Parallel() ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - err := Retry(attempts, interval, func() error { - _, getErr := clientSet.CoreV1().ServiceAccounts(NamespaceName).Get(ctx, + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + _, getErr := testEnvironment.K8sClientset.CoreV1().ServiceAccounts(NamespaceName).Get(ctx, eventing.GetPublisherServiceAccountName(*eventingCR), metav1.GetOptions{}) if getErr != nil { return getErr @@ -194,9 +131,9 @@ func Test_PublisherServiceAccount(t *testing.T) { func Test_PublisherClusterRole(t *testing.T) { t.Parallel() ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - err := Retry(attempts, interval, func() error { - _, getErr := clientSet.RbacV1().ClusterRoles().Get(ctx, + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + _, getErr := testEnvironment.K8sClientset.RbacV1().ClusterRoles().Get(ctx, eventing.GetPublisherClusterRoleName(*eventingCR), metav1.GetOptions{}) if getErr != nil { return getErr @@ -210,9 +147,9 @@ func Test_PublisherClusterRole(t *testing.T) { func Test_PublisherClusterRoleBinding(t *testing.T) { t.Parallel() ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - err := Retry(attempts, interval, func() error { - _, getErr := clientSet.RbacV1().ClusterRoleBindings().Get(ctx, + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + _, getErr := testEnvironment.K8sClientset.RbacV1().ClusterRoleBindings().Get(ctx, eventing.GetPublisherClusterRoleBindingName(*eventingCR), metav1.GetOptions{}) if getErr != nil { return getErr @@ -226,24 +163,24 @@ func Test_PublisherClusterRoleBinding(t *testing.T) { func Test_PublisherServices(t *testing.T) { t.Parallel() ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - err := Retry(attempts, interval, func() error { + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { // check service to expose event publishing endpoint. - _, getErr := clientSet.CoreV1().Services(NamespaceName).Get(ctx, + _, getErr := testEnvironment.K8sClientset.CoreV1().Services(NamespaceName).Get(ctx, eventing.GetPublisherPublishServiceName(*eventingCR), metav1.GetOptions{}) if getErr != nil { return errors.Wrap(getErr, "failed to ensure existence of publish service") } // check service to expose metrics endpoint. - _, getErr = clientSet.CoreV1().Services(NamespaceName).Get(ctx, + _, getErr = testEnvironment.K8sClientset.CoreV1().Services(NamespaceName).Get(ctx, eventing.GetPublisherMetricsServiceName(*eventingCR), metav1.GetOptions{}) if getErr != nil { return errors.Wrap(getErr, "failed to ensure existence of metrics service") } // check service to expose health endpoint. - _, getErr = clientSet.CoreV1().Services(NamespaceName).Get(ctx, + _, getErr = testEnvironment.K8sClientset.CoreV1().Services(NamespaceName).Get(ctx, eventing.GetPublisherHealthServiceName(*eventingCR), metav1.GetOptions{}) if getErr != nil { return errors.Wrap(getErr, "failed to ensure existence of health service") @@ -258,9 +195,9 @@ func Test_PublisherServices(t *testing.T) { func Test_PublisherHPA(t *testing.T) { t.Parallel() ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - err := Retry(attempts, interval, func() error { - gotHPA, getErr := clientSet.AutoscalingV2().HorizontalPodAutoscalers(NamespaceName).Get(ctx, + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + gotHPA, getErr := testEnvironment.K8sClientset.AutoscalingV2().HorizontalPodAutoscalers(NamespaceName).Get(ctx, eventing.GetPublisherDeploymentName(*eventingCR), metav1.GetOptions{}) if getErr != nil { return getErr @@ -286,10 +223,9 @@ func Test_PublisherHPA(t *testing.T) { // Test_PublisherProxyDeployment checks the publisher-proxy deployment. func Test_PublisherProxyDeployment(t *testing.T) { t.Parallel() - ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) - err := Retry(attempts, interval, func() error { - gotDeployment, getErr := getDeployment(ctx, eventing.GetPublisherDeploymentName(*eventingCR), NamespaceName) + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { + gotDeployment, getErr := testEnvironment.GetDeployment(eventing.GetPublisherDeploymentName(*eventingCR), NamespaceName) if getErr != nil { return getErr } @@ -298,7 +234,7 @@ func Test_PublisherProxyDeployment(t *testing.T) { if gotDeployment.Status.Replicas != gotDeployment.Status.UpdatedReplicas || gotDeployment.Status.Replicas != gotDeployment.Status.ReadyReplicas { err := fmt.Errorf("waiting for publisher-proxy deployment to get ready") - logger.Debug(err.Error()) + testEnvironment.Logger.Debug(err.Error()) return err } @@ -315,17 +251,17 @@ func Test_PublisherProxyPods(t *testing.T) { t.Parallel() ctx := context.TODO() - eventingCR := EventingCR(eventingv1alpha1.BackendType(testConfigs.BackendType)) + eventingCR := EventingCR(eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType)) // RetryGet the Pods and test them. - err := Retry(attempts, interval, func() error { + err := Retry(testenvironment.Attempts, testenvironment.Interval, func() error { // get publisher deployment - gotDeployment, getErr := getDeployment(ctx, eventing.GetPublisherDeploymentName(*eventingCR), NamespaceName) + gotDeployment, getErr := testEnvironment.GetDeployment(eventing.GetPublisherDeploymentName(*eventingCR), NamespaceName) if getErr != nil { return getErr } // RetryGet the Pods via labels. - pods, err := clientSet.CoreV1().Pods(NamespaceName).List(ctx, metav1.ListOptions{ + pods, err := testEnvironment.K8sClientset.CoreV1().Pods(NamespaceName).List(ctx, metav1.ListOptions{ LabelSelector: ConvertSelectorLabelsToString(gotDeployment.Spec.Selector.MatchLabels)}) if err != nil { return err @@ -361,7 +297,7 @@ func Test_PublisherProxyPods(t *testing.T) { // check if the ENV `BACKEND` is defined correctly. wantBackendENVValue := "nats" - if eventingv1alpha1.BackendType(testConfigs.BackendType) == eventingv1alpha1.EventMeshBackendType { + if eventingv1alpha1.BackendType(testEnvironment.TestConfigs.BackendType) == eventingv1alpha1.EventMeshBackendType { wantBackendENVValue = "beb" } @@ -387,89 +323,3 @@ func Test_PublisherProxyPods(t *testing.T) { }) require.NoError(t, err) } - -func getEventingCR(ctx context.Context, name, namespace string) (*eventingv1alpha1.Eventing, error) { - var eventingCR eventingv1alpha1.Eventing - err := k8sClient.Get(ctx, k8stypes.NamespacedName{ - Name: name, - Namespace: namespace, - }, &eventingCR) - return &eventingCR, err -} - -func getDeployment(ctx context.Context, name, namespace string) (*appsv1.Deployment, error) { - return clientSet.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{}) -} - -// Wait for Eventing CR to get ready. -func waitForEventingCRReady() error { - // RetryGet the Eventing CR and test status. - return Retry(attempts, interval, func() error { - logger.Debug(fmt.Sprintf("waiting for Eventing CR to get ready. "+ - "CR name: %s, namespace: %s", CRName, NamespaceName)) - - ctx := context.TODO() - // Get the Eventing CR from the cluster. - gotEventingCR, err := RetryGet(attempts, interval, func() (*eventingv1alpha1.Eventing, error) { - return getEventingCR(ctx, CRName, NamespaceName) - }) - if err != nil { - return err - } - - if gotEventingCR.Spec.Backend.Type != gotEventingCR.Status.ActiveBackend { - err := fmt.Errorf("waiting for Eventing CR to switch backend") - logger.Debug(err.Error()) - return err - } - - if gotEventingCR.Status.State != eventingv1alpha1.StateReady { - err := fmt.Errorf("waiting for Eventing CR to get ready state") - logger.Debug(err.Error()) - return err - } - - // Everything is fine. - logger.Debug(fmt.Sprintf("Eventing CR is ready. "+ - "CR name: %s, namespace: %s", CRName, NamespaceName)) - return nil - }) -} - -// Wait for eventing-manager deployment to get ready with correct image. -func waitForEventingManagerDeploymentReady(image string) error { - // RetryGet the Eventing Manager and test status. - return Retry(attempts, interval, func() error { - logger.Debug(fmt.Sprintf("waiting for eventing-manager deployment to get ready with image: %s", image)) - ctx := context.TODO() - // Get the eventing-manager deployment from the cluster. - gotDeployment, err := RetryGet(attempts, interval, func() (*appsv1.Deployment, error) { - return getDeployment(ctx, ManagerDeploymentName, NamespaceName) - }) - if err != nil { - return err - } - - // if image is provided, then check if the deployment has correct image. - if image != "" && gotDeployment.Spec.Template.Spec.Containers[0].Image != image { - err := fmt.Errorf("expected eventing-manager image to be: %s, but found: %s", image, - gotDeployment.Spec.Template.Spec.Containers[0].Image, - ) - logger.Debug(err.Error()) - return err - } - - // check if the deployment is ready. - if *gotDeployment.Spec.Replicas != gotDeployment.Status.UpdatedReplicas || - *gotDeployment.Spec.Replicas != gotDeployment.Status.ReadyReplicas || - *gotDeployment.Spec.Replicas != gotDeployment.Status.AvailableReplicas { - err := fmt.Errorf("waiting for eventing-manager deployment to get ready") - logger.Debug(err.Error()) - return err - } - - // Everything is fine. - logger.Debug(fmt.Sprintf("eventing-manager deployment is ready with image: %s", image)) - return nil - }) -}