Skip to content

Commit

Permalink
Improve EventMesh Tests for Better error comparison
Browse files Browse the repository at this point in the history
require.ErrorAs only compares types. Instead erro should be compared properly.
  • Loading branch information
muralov committed Dec 14, 2023
1 parent e7a0190 commit 717231e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
8 changes: 4 additions & 4 deletions internal/controller/operator/eventing/eventmesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func (r *Reconciler) reconcileEventMeshSubManager(ctx context.Context, eventing
// gets oauth2ClientID and secret and stops the EventMesh subscription manager if changed
err := r.syncOauth2ClientIDAndSecret(ctx, eventing)
if err != nil {
return errors.Errorf("failed to sync OAuth secret: %v", err)
return fmt.Errorf("failed to sync OAuth secret: %v", err)
}
// CreateOrUpdate deployment for publisher proxy secret
secretForPublisher, err := r.SyncPublisherProxySecret(ctx, eventMeshSecret)
if err != nil {
return errors.Errorf("failed to sync Publisher Proxy secret: %v", err)
return fmt.Errorf("failed to sync Publisher Proxy secret: %v", err)
}

// Set environment with secrets for EventMesh subscription controller
Expand Down Expand Up @@ -235,8 +235,8 @@ func (r *Reconciler) getOAuth2ClientCredentials(ctx context.Context, secretNames

r.namedLogger().Infof("Reading secret %s", oauth2SecretNamespacedName.String())

if getErr := r.Get(ctx, oauth2SecretNamespacedName, oauth2Secret); getErr != nil {
return nil, getErr
if oauth2Secret, err = r.kubeClient.GetSecret(ctx, oauth2SecretNamespacedName.String()); err != nil {
return nil, err
}

if clientID, exists = oauth2Secret.Data[secretKeyClientID]; !exists {
Expand Down
23 changes: 15 additions & 8 deletions internal/controller/operator/eventing/eventmesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eventing
import (
"context"
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -89,7 +90,13 @@ func Test_reconcileEventMeshSubManager(t *testing.T) {
givenManagerFactoryMock: func(_ *submgrmanagermocks.Manager) *submgrmocks.ManagerFactory {
return nil
},
wantError: errors.New("failed to sync OAuth secret"),
givenKubeClientMock: func() k8s.Client {
mockKubeClient := new(k8smocks.Client)
mockKubeClient.On("GetSecret", ctx, mock.Anything, mock.Anything).Return(
nil, errors.New("secret not found")).Once()
return mockKubeClient
},
wantError: fmt.Errorf("failed to sync OAuth secret: secret not found"),
wantHashAfter: int64(0),
},
{
Expand All @@ -110,11 +117,11 @@ func Test_reconcileEventMeshSubManager(t *testing.T) {
givenKubeClientMock: func() k8s.Client {
mockKubeClient := new(k8smocks.Client)
mockKubeClient.On("GetSecret", ctx, mock.Anything, mock.Anything).Return(
utils.NewEventMeshSecret("test-secret", givenEventing.Namespace), nil).Once()
utils.NewOAuthSecret("test-secret", givenEventing.Namespace), nil).Once()
mockKubeClient.On("PatchApply", ctx, mock.Anything).Return(errors.New("failed to apply patch")).Once()
return mockKubeClient
},
wantError: errors.New("failed to sync Publisher Proxy secret"),
wantError: errors.New("failed to sync Publisher Proxy secret: failed to apply patch"),
},
{
name: "it should do nothing because subscription manager is already started",
Expand All @@ -138,7 +145,7 @@ func Test_reconcileEventMeshSubManager(t *testing.T) {
mockKubeClient.On("GetConfigMap", ctx, mock.Anything, mock.Anything).Return(givenConfigMap, nil).Once()
mockKubeClient.On("PatchApply", ctx, mock.Anything).Return(nil).Once()
mockKubeClient.On("GetSecret", ctx, mock.Anything, mock.Anything).Return(
utils.NewEventMeshSecret("test-secret", givenEventing.Namespace), nil).Once()
utils.NewOAuthSecret("test-secret", givenEventing.Namespace), nil).Once()
return mockKubeClient
},
wantHashAfter: int64(4922936597877296700),
Expand Down Expand Up @@ -169,7 +176,7 @@ func Test_reconcileEventMeshSubManager(t *testing.T) {
mockKubeClient.On("GetConfigMap", ctx, mock.Anything, mock.Anything).Return(givenConfigMap, nil).Once()
mockKubeClient.On("PatchApply", ctx, mock.Anything).Return(nil).Once()
mockKubeClient.On("GetSecret", ctx, mock.Anything, mock.Anything).Return(
utils.NewEventMeshSecret("test-secret", givenEventing.Namespace), nil).Once()
utils.NewOAuthSecret("test-secret", givenEventing.Namespace), nil).Once()
return mockKubeClient
},
wantAssertCheck: true,
Expand Down Expand Up @@ -200,7 +207,7 @@ func Test_reconcileEventMeshSubManager(t *testing.T) {
mockKubeClient := new(k8smocks.Client)
mockKubeClient.On("PatchApply", ctx, mock.Anything).Return(nil).Twice()
mockKubeClient.On("GetSecret", ctx, mock.Anything, mock.Anything).Return(
utils.NewEventMeshSecret("test-secret", givenEventing.Namespace), nil).Twice()
utils.NewOAuthSecret("test-secret", givenEventing.Namespace), nil).Twice()
return mockKubeClient
},
wantAssertCheck: true,
Expand Down Expand Up @@ -234,7 +241,7 @@ func Test_reconcileEventMeshSubManager(t *testing.T) {
mockKubeClient := new(k8smocks.Client)
mockKubeClient.On("PatchApply", ctx, mock.Anything).Return(nil).Twice()
mockKubeClient.On("GetSecret", ctx, mock.Anything, mock.Anything).Return(
utils.NewEventMeshSecret("test-secret", givenEventing.Namespace), nil).Twice()
utils.NewOAuthSecret("test-secret", givenEventing.Namespace), nil).Twice()
return mockKubeClient
},
wantAssertCheck: true,
Expand Down Expand Up @@ -295,7 +302,7 @@ func Test_reconcileEventMeshSubManager(t *testing.T) {
// then
if tc.wantError != nil {
require.Error(t, err)
require.ErrorAs(t, err, &tc.wantError)
require.Equal(t, err.Error(), tc.wantError.Error())
} else {
require.NoError(t, err)
require.NotNil(t, testEnv.Reconciler.eventMeshSubManager)
Expand Down

0 comments on commit 717231e

Please sign in to comment.