From 4f69db8ea1606da2b4b0dbfdde9158de07a9eec9 Mon Sep 17 00:00:00 2001 From: Mansur Uralov Date: Fri, 15 Dec 2023 07:02:09 +0100 Subject: [PATCH] Fix failing unit tests --- .../operator/eventing/eventmesh_test.go | 73 +++++++++---------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/internal/controller/operator/eventing/eventmesh_test.go b/internal/controller/operator/eventing/eventmesh_test.go index 4ced715e..9f20ae1a 100644 --- a/internal/controller/operator/eventing/eventmesh_test.go +++ b/internal/controller/operator/eventing/eventmesh_test.go @@ -12,7 +12,6 @@ import ( kcorev1 "k8s.io/api/core/v1" kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" "github.com/kyma-project/eventing-manager/api/operator/v1alpha1" "github.com/kyma-project/eventing-manager/internal/label" @@ -373,6 +372,7 @@ func Test_reconcileEventMeshSubManager_ReadClusterDomain(t *testing.T) { }, givenKubeClientMock: func() (k8s.Client, *k8smocks.Client) { mockKubeClient := new(k8smocks.Client) + mockKubeClient.On("GetSecret", mock.Anything, mock.Anything).Return(givenOauthSecret, nil).Once() mockKubeClient.On("PatchApply", ctx, mock.Anything).Return(nil).Once() return mockKubeClient, mockKubeClient }, @@ -403,6 +403,7 @@ func Test_reconcileEventMeshSubManager_ReadClusterDomain(t *testing.T) { }, givenKubeClientMock: func() (k8s.Client, *k8smocks.Client) { mockKubeClient := new(k8smocks.Client) + mockKubeClient.On("GetSecret", mock.Anything, mock.Anything).Return(givenOauthSecret, nil).Once() mockKubeClient.On("GetConfigMap", ctx, mock.Anything, mock.Anything).Return(givenConfigMap, nil).Once() mockKubeClient.On("PatchApply", ctx, mock.Anything).Return(nil).Once() return mockKubeClient, mockKubeClient @@ -704,7 +705,7 @@ func Test_GetSecretForPublisher(t *testing.T) { func Test_getOAuth2ClientCredentials(t *testing.T) { testCases := []struct { name string - givenSecrets []*kcorev1.Secret + givenSecret *kcorev1.Secret wantError bool wantClientID []byte wantClientSecret []byte @@ -712,42 +713,36 @@ func Test_getOAuth2ClientCredentials(t *testing.T) { wantCertsURL []byte }{ { - name: "secret does not exist", - givenSecrets: nil, - wantError: true, + name: "secret does not exist", + givenSecret: nil, + wantError: true, }, { name: "secret exists with missing data", - givenSecrets: []*kcorev1.Secret{ - // required secret - { - ObjectMeta: kmetav1.ObjectMeta{ - Name: defaultEventingWebhookAuthSecretName, - Namespace: defaultEventingWebhookAuthSecretNamespace, - }, - Data: map[string][]byte{ - secretKeyClientID: []byte("test-client-id-0"), - // missing data - }, + givenSecret: &kcorev1.Secret{ + ObjectMeta: kmetav1.ObjectMeta{ + Name: defaultEventingWebhookAuthSecretName, + Namespace: defaultEventingWebhookAuthSecretNamespace, + }, + Data: map[string][]byte{ + secretKeyClientID: []byte("test-client-id-0"), + // missing data }, }, wantError: true, }, { name: "secret exists with all data", - givenSecrets: []*kcorev1.Secret{ - // required secret - { - ObjectMeta: kmetav1.ObjectMeta{ - Name: defaultEventingWebhookAuthSecretName, - Namespace: defaultEventingWebhookAuthSecretNamespace, - }, - Data: map[string][]byte{ - secretKeyClientID: []byte("test-client-id-0"), - secretKeyClientSecret: []byte("test-client-secret-0"), - secretKeyTokenURL: []byte("test-token-url-0"), - secretKeyCertsURL: []byte("test-certs-url-0"), - }, + givenSecret: &kcorev1.Secret{ + ObjectMeta: kmetav1.ObjectMeta{ + Name: defaultEventingWebhookAuthSecretName, + Namespace: defaultEventingWebhookAuthSecretNamespace, + }, + Data: map[string][]byte{ + secretKeyClientID: []byte("test-client-id-0"), + secretKeyClientSecret: []byte("test-client-secret-0"), + secretKeyTokenURL: []byte("test-token-url-0"), + secretKeyCertsURL: []byte("test-certs-url-0"), }, }, wantError: false, @@ -766,20 +761,24 @@ func Test_getOAuth2ClientCredentials(t *testing.T) { t.Run(tc.name, func(t *testing.T) { // given ctx := context.Background() + + kubeClient := new(k8smocks.Client) + + notFoundErr := errors.New("secret not found") + if tc.givenSecret != nil { + kubeClient.On("GetSecret", mock.Anything, mock.Anything).Return(tc.givenSecret, nil).Once() + } else { + kubeClient.On("GetSecret", mock.Anything, mock.Anything).Return(nil, notFoundErr).Once() + } + r := Reconciler{ - Client: fake.NewClientBuilder().WithObjects().Build(), - logger: l, + kubeClient: kubeClient, + logger: l, backendConfig: env.BackendConfig{ EventingWebhookAuthSecretName: defaultEventingWebhookAuthSecretName, EventingWebhookAuthSecretNamespace: defaultEventingWebhookAuthSecretNamespace, }, } - if len(tc.givenSecrets) > 0 { - for _, secret := range tc.givenSecrets { - err := r.Client.Create(ctx, secret) - require.NoError(t, err) - } - } // when credentials, err := r.getOAuth2ClientCredentials(ctx, defaultEventingWebhookAuthSecretNamespace)