Skip to content

Commit

Permalink
Fix failing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
muralov committed Dec 15, 2023
1 parent 1fa20e6 commit 6fb8ffa
Showing 1 changed file with 42 additions and 44 deletions.
86 changes: 42 additions & 44 deletions internal/controller/operator/eventing/eventmesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ import (
"fmt"
"testing"

Check failure on line 8 in internal/controller/operator/eventing/eventmesh_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kyma-project/eventing-manager) -s blank -s dot --custom-order (gci)
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
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"
"github.com/kyma-project/eventing-manager/pkg/env"
Expand All @@ -25,6 +17,12 @@ import (
submgrmanagermocks "github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/manager/mocks"
submgrmocks "github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/mocks"
"github.com/kyma-project/eventing-manager/test/utils"
"github.com/stretchr/testify/assert"

Check failure on line 20 in internal/controller/operator/eventing/eventmesh_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kyma-project/eventing-manager) -s blank -s dot --custom-order (gci)
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
kcorev1 "k8s.io/api/core/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand Down Expand Up @@ -373,6 +371,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
},
Expand Down Expand Up @@ -403,6 +402,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
Expand Down Expand Up @@ -704,50 +704,44 @@ 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
wantTokenURL []byte
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,
Expand All @@ -766,20 +760,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)
Expand Down

0 comments on commit 6fb8ffa

Please sign in to comment.