Skip to content

Commit

Permalink
Replace controller-runtime.Manager custom mock with mockery generated…
Browse files Browse the repository at this point in the history
… Manager (#2382)

* replace custom mock client with mockery generated client
  • Loading branch information
aorcholski authored Nov 20, 2023
1 parent 5175f7e commit 6c05819
Show file tree
Hide file tree
Showing 15 changed files with 982 additions and 156 deletions.
6 changes: 6 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ packages:
github.com/Dynatrace/dynatrace-operator/cmd/config:
interfaces:
Provider:
github.com/Dynatrace/dynatrace-operator/cmd/manager:
interfaces:
Provider:
github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/version:
interfaces:
StatusUpdater:
Expand All @@ -31,3 +34,6 @@ packages:
github.com/Dynatrace/dynatrace-operator/pkg/webhook:
interfaces:
PodMutator:
sigs.k8s.io/controller-runtime/pkg/manager:
interfaces:
Manager:
8 changes: 4 additions & 4 deletions cmd/csi/provisioner/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"io/fs"
"testing"

cmdManager "github.com/Dynatrace/dynatrace-operator/cmd/manager"
dtcsi "github.com/Dynatrace/dynatrace-operator/pkg/controllers/csi"
"github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/config"
configmock "github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/config"
managermock "github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/manager"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)
Expand All @@ -25,13 +25,13 @@ func TestCsiCommandBuilder(t *testing.T) {

assert.NotNil(t, builder)

expectedProvider := &mocks.Provider{}
expectedProvider := &configmock.Provider{}
builder = builder.SetConfigProvider(expectedProvider)

assert.Equal(t, expectedProvider, builder.configProvider)
})
t.Run("set manager provider", func(t *testing.T) {
expectedProvider := &cmdManager.MockProvider{}
expectedProvider := managermock.NewProvider(t)
builder := NewCsiProvisionerCommandBuilder().setManagerProvider(expectedProvider)

assert.Equal(t, expectedProvider, builder.managerProvider)
Expand Down
11 changes: 6 additions & 5 deletions cmd/csi/provisioner/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ package provisioner
import (
"testing"

cmdManager "github.com/Dynatrace/dynatrace-operator/cmd/manager"
dtfake "github.com/Dynatrace/dynatrace-operator/pkg/api/scheme/fake"
dtcsi "github.com/Dynatrace/dynatrace-operator/pkg/controllers/csi"
"github.com/Dynatrace/dynatrace-operator/pkg/util/logger"
"github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/config"
configmock "github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/config"
providermock "github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/manager"
managermock "github.com/Dynatrace/dynatrace-operator/test/mocks/sigs.k8s.io/controller-runtime/pkg/manager"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"k8s.io/client-go/rest"
)

func TestCsiCommand(t *testing.T) {
configProvider := mocks.NewProvider(t)
configProvider := configmock.NewProvider(t)
configProvider.On("GetConfig").Return(&rest.Config{}, nil)

clt := dtfake.NewClient()
cmdMgr := &cmdManager.MockManager{}
cmdMgr := managermock.NewManager(t)
cmdMgr.On("GetAPIReader", mock.Anything, mock.Anything).Return(clt, nil)

managerProvider := &cmdManager.MockProvider{}
managerProvider := providermock.NewProvider(t)
managerProvider.On("CreateManager", mock.Anything, mock.Anything).Return(cmdMgr, nil)

memFs := afero.NewMemMapFs()
Expand Down
6 changes: 3 additions & 3 deletions cmd/csi/provisioner/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package provisioner
import (
"testing"

cmdManager "github.com/Dynatrace/dynatrace-operator/cmd/manager"
"github.com/Dynatrace/dynatrace-operator/pkg/api/scheme"
managermock "github.com/Dynatrace/dynatrace-operator/test/mocks/sigs.k8s.io/controller-runtime/pkg/manager"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -34,7 +34,7 @@ func TestCsiDriverManagerProvider(t *testing.T) {
const addHealthzCheck = "AddHealthzCheck"

operatorMgrProvider := csiDriverManagerProvider{}
mockMgr := &cmdManager.MockManager{}
mockMgr := managermock.NewManager(t)
mockMgr.On(addHealthzCheck, livezEndpointName, mock.AnythingOfType("healthz.Checker")).Return(nil)

err := operatorMgrProvider.addHealthzCheck(mockMgr)
Expand All @@ -43,7 +43,7 @@ func TestCsiDriverManagerProvider(t *testing.T) {
mockMgr.AssertCalled(t, addHealthzCheck, livezEndpointName, mock.AnythingOfType("healthz.Checker"))

expectedError := errors.New("healthz error")
mockMgr = &cmdManager.MockManager{}
mockMgr = managermock.NewManager(t)
mockMgr.On(addHealthzCheck, mock.Anything, mock.Anything).Return(expectedError)

err = operatorMgrProvider.addHealthzCheck(mockMgr)
Expand Down
8 changes: 4 additions & 4 deletions cmd/csi/server/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"io/fs"
"testing"

cmdManager "github.com/Dynatrace/dynatrace-operator/cmd/manager"
dtcsi "github.com/Dynatrace/dynatrace-operator/pkg/controllers/csi"
mocks "github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/config"
configmock "github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/config"
providermock "github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/manager"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)
Expand All @@ -25,13 +25,13 @@ func TestCsiCommandBuilder(t *testing.T) {

assert.NotNil(t, builder)

expectedProvider := &mocks.Provider{}
expectedProvider := &configmock.Provider{}
builder = builder.SetConfigProvider(expectedProvider)

assert.Equal(t, expectedProvider, builder.configProvider)
})
t.Run("set manager provider", func(t *testing.T) {
expectedProvider := &cmdManager.MockProvider{}
expectedProvider := providermock.NewProvider(t)
builder := NewCsiServerCommandBuilder().setManagerProvider(expectedProvider)

assert.Equal(t, expectedProvider, builder.managerProvider)
Expand Down
11 changes: 6 additions & 5 deletions cmd/csi/server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ package server
import (
"testing"

cmdManager "github.com/Dynatrace/dynatrace-operator/cmd/manager"
dtfake "github.com/Dynatrace/dynatrace-operator/pkg/api/scheme/fake"
dtcsi "github.com/Dynatrace/dynatrace-operator/pkg/controllers/csi"
"github.com/Dynatrace/dynatrace-operator/pkg/util/logger"
"github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/config"
configmock "github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/config"
providermock "github.com/Dynatrace/dynatrace-operator/test/mocks/cmd/manager"
managermock "github.com/Dynatrace/dynatrace-operator/test/mocks/sigs.k8s.io/controller-runtime/pkg/manager"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"k8s.io/client-go/rest"
)

func TestCsiCommand(t *testing.T) {
configProvider := &mocks.Provider{}
configProvider := &configmock.Provider{}
configProvider.On("GetConfig").Return(&rest.Config{}, nil)

clt := dtfake.NewClient()
cmdMgr := &cmdManager.MockManager{}
cmdMgr := managermock.NewManager(t)
cmdMgr.On("GetAPIReader", mock.Anything, mock.Anything).Return(clt, nil)

managerProvider := &cmdManager.MockProvider{}
managerProvider := providermock.NewProvider(t)
managerProvider.On("CreateManager", mock.Anything, mock.Anything).Return(cmdMgr, nil)

memFs := afero.NewMemMapFs()
Expand Down
6 changes: 3 additions & 3 deletions cmd/csi/server/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package server
import (
"testing"

cmdManager "github.com/Dynatrace/dynatrace-operator/cmd/manager"
"github.com/Dynatrace/dynatrace-operator/pkg/api/scheme"
managermock "github.com/Dynatrace/dynatrace-operator/test/mocks/sigs.k8s.io/controller-runtime/pkg/manager"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -35,7 +35,7 @@ func TestCsiDriverManagerProvider(t *testing.T) {
const addHealthzCheck = "AddHealthzCheck"

operatorMgrProvider := csiDriverManagerProvider{}
mockMgr := &cmdManager.MockManager{}
mockMgr := managermock.NewManager(t)
mockMgr.On(addHealthzCheck, livezEndpointName, mock.AnythingOfType("healthz.Checker")).Return(nil)

err := operatorMgrProvider.addHealthzCheck(mockMgr)
Expand All @@ -44,7 +44,7 @@ func TestCsiDriverManagerProvider(t *testing.T) {
mockMgr.AssertCalled(t, addHealthzCheck, livezEndpointName, mock.AnythingOfType("healthz.Checker"))

expectedError := errors.New("healthz error")
mockMgr = &cmdManager.MockManager{}
mockMgr = managermock.NewManager(t)
mockMgr.On(addHealthzCheck, mock.Anything, mock.Anything).Return(expectedError)

err = operatorMgrProvider.addHealthzCheck(mockMgr)
Expand Down
67 changes: 0 additions & 67 deletions cmd/manager/mock.go

This file was deleted.

26 changes: 0 additions & 26 deletions cmd/manager/mock_test.go

This file was deleted.

Loading

0 comments on commit 6c05819

Please sign in to comment.