diff --git a/internal/controller/operator/eventing/integrationtests/controller/integration_test.go b/internal/controller/operator/eventing/integrationtests/controller/integration_test.go index 53f69d2b..bf1ba854 100644 --- a/internal/controller/operator/eventing/integrationtests/controller/integration_test.go +++ b/internal/controller/operator/eventing/integrationtests/controller/integration_test.go @@ -580,16 +580,16 @@ func Test_WatcherEventingCRK8sObjects(t *testing.T) { } func Test_EventMesh_MalformattedSecret(t *testing.T) { - testCases := []struct { name string givenObjName string givenEventMeshNamespace string givenMessages []eventingcontroller.Message + wantMatcher gomegatypes.GomegaMatcher }{ { name: "should have ready state when EventMesh secret is malformatted", - givenObjName: "something", + givenObjName: "everything-ok", givenEventMeshNamespace: "event-mesh-namespace", givenMessages: []eventingcontroller.Message{ { @@ -604,31 +604,36 @@ func Test_EventMesh_MalformattedSecret(t *testing.T) { }, URI: "uri", }}, + wantMatcher: gomega.And( + matchers.HaveStatusReady(), + ), }, } for _, tc := range testCases { - tc := tc t.Run(tc.name, func(t *testing.T) { - // given + //g := gomega.NewWithT(t) + + // Given: + // Make the Deployment always beeing ready. eventingcontroller.IsDeploymentReady = func(deployment *kappsv1.Deployment) bool { return true } - // Create an unique namespace for this test run. - givenNamespace := fmt.Sprintf("namespace-%s", tc.givenObjName) - testEnvironment.EnsureNamespaceCreation(t, givenNamespace) - - // Create an Eventing CR. We do not care about the details. - givenEveningCR := utils.NewEventingCR( - utils.WithEventMeshBackend(fmt.Sprintf("test-%s", tc.givenObjName)), + // Create an Eventing CR. We do not care about the details in this test. + givenEventingCR := utils.NewEventingCR( + utils.WithEventMeshBackend("test-secret-name"), utils.WithEventingPublisherData(1, 1, "199m", "99Mi", "399m", "199Mi"), utils.WithEventingEventTypePrefix("test-prefix"), ) - // Create a EventMesh Secret. This is the crucial part of the test. - // If the Secret is malformatted, the Eventing CR should have a warning state. - secretData, err := json.Marshal(tc.givenMessages) + // Create an unique Namespace for this test run. + givenNamespace := givenEventingCR.Namespace + testEnvironment.EnsureNamespaceCreation(t, givenNamespace) + + // Create an EventMesh Secret. This is the crucial part of the test; + // if the Secret is malformatted, Eventing should be in warning state. + messageBytes, err := json.Marshal(tc.givenMessages) if err != nil { t.Fail() } @@ -639,13 +644,39 @@ func Test_EventMesh_MalformattedSecret(t *testing.T) { }, Data: map[string][]byte{ "namespace": []byte(tc.givenEventMeshNamespace), - "message": secretData, + "message": messageBytes, }, } - testEnvironment.EnsuretEventMeshSecretCreated(t, givenEveningCR, &secret) + testEnvironment.EnsureEventMeshSecretCreated(t, givenEventingCR, &secret) - // when + // Set up the K8s client to be able to mock the K8s API. + // First, to be able to reversible replace the original K8s client with a mocked one, + // we need to store the original K8s client. + originalKubeClient := testEnvironment.KubeClient + // Now we can replace the original K8s client with a mocked one in the test environment. + mockedKubeClient := &MockKubeClient{ + Client: originalKubeClient, + } + testEnvironment.KubeClient = mockedKubeClient + testEnvironment.Reconciler.SetKubeClient(mockedKubeClient) + + // This func will be executed to clean up the resources after the test. + defer func() { + // Set the original K8s client back. + testEnvironment.KubeClient = originalKubeClient + testEnvironment.Reconciler.SetKubeClient(originalKubeClient) + + // Clean up the resources. + testEnvironment.EnsureNamespaceDeleted(t, givenNamespace) + }() + + // When: + // Create the Eventing CR with the given EventMesh Secret. + //testEnvironment.EnsureK8sResourceCreated(t, givenEventingCR) + // Then: + // Check the Eventing CR status against the expected status. + //estEnvironment.GetEventingAssert(g, givenEventingCR).Should(tc.wantMatcher) }) } } diff --git a/test/utils/integration/integration.go b/test/utils/integration/integration.go index 5572610c..1719ff02 100644 --- a/test/utils/integration/integration.go +++ b/test/utils/integration/integration.go @@ -919,10 +919,10 @@ func (env TestEnvironment) EnsureDefaultEventMeshSecretCreated(t *testing.T, eve t.Helper() subarr := strings.Split(eventing.Spec.Backend.Config.EventMeshSecret, "/") secret := testutils.NewEventMeshSecret(subarr[1], subarr[0]) - env.EnsuretEventMeshSecretCreated(t, eventing, secret) + env.EnsureEventMeshSecretCreated(t, eventing, secret) } -func (env TestEnvironment) EnsuretEventMeshSecretCreated(t *testing.T, eventing *v1alpha1.Eventing, secret *kcorev1.Secret) { +func (env TestEnvironment) EnsureEventMeshSecretCreated(t *testing.T, eventing *v1alpha1.Eventing, secret *kcorev1.Secret) { t.Helper() env.EnsureK8sResourceCreated(t, secret) } diff --git a/test/utils/utils.go b/test/utils/utils.go index c47c8879..f43d46d3 100644 --- a/test/utils/utils.go +++ b/test/utils/utils.go @@ -109,6 +109,8 @@ func NewAPIRuleCRD() *kapiextensionsv1.CustomResourceDefinition { return result } +// NewEventingCR creates a new Eventing CR with the given options. +// If no options are provided, the default (e. g. random name and namespace) will be used. func NewEventingCR(opts ...EventingOption) *v1alpha1.Eventing { name := fmt.Sprintf(NameFormat, GetRandString(randomNameLen)) namespace := fmt.Sprintf(NamespaceFormat, GetRandString(randomNameLen))