Skip to content

Commit

Permalink
Generate mock for controller-runtime/pkg/reconciler interface using v…
Browse files Browse the repository at this point in the history
…ectra/mockery (#2388)

* Fix wrong mock name

* Fix wrong package aliases and add doc to contributing.md

* Generate mock for controller-runtime/pkg/reconcile interface using vectra/mockery
  • Loading branch information
albertogdd authored Nov 21, 2023
1 parent c88f127 commit 005a67c
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ packages:
sigs.k8s.io/controller-runtime/pkg/manager:
interfaces:
Manager:
sigs.k8s.io/controller-runtime/pkg/reconcile:
interfaces:
Reconciler:
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ To move our existing codebase to mockery you have to look out for these pitfalls
//modifierMock.AssertNumberOfCalls(t, "Modify", 0)
```

> ❗ In the case of using multiple mock packages in the same test file, the standard package alias naming is `{struct}mock`, e.g. `clientmock`.
>
> ```go
> clientmock "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/clients/dynatrace"
> installermock "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/injection/codemodule/installer"
> reconcilermock "github.com/Dynatrace/dynatrace-operator/test/mocks/sigs.k8s.io/controller-runtime/pkg/reconcile"
> ```
## E2E tests
> **Prerequisites:**
Expand Down
51 changes: 20 additions & 31 deletions pkg/controllers/csi/provisioner/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/csi/metadata"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/connectioninfo"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/dynatraceclient"
mockedclient "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/clients/dynatrace"
mockedinstaller "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/injection/codemodule/installer"
clientmock "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/clients/dynatrace"
installermock "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/injection/codemodule/installer"
reconcilermock "github.com/Dynatrace/dynatrace-operator/test/mocks/sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/spf13/afero"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -35,15 +35,6 @@ const (
testZip = "UEsDBAoAAAAAAKh0p1JsLSFnGQAAABkAAAAIABwAdGVzdC50eHRVVAkAA3w0lWATB55gdXgLAAEE6AMAAAToAwAAeW91IGZvdW5kIHRoZSBlYXN0ZXIgZWdnClBLAwQKAAAAAADAOa5SAAAAAAAAAAAAAAAABQAcAHRlc3QvVVQJAAMXB55gHQeeYHV4CwABBOgDAAAE6AMAAFBLAwQKAAAAAACodKdSbC0hZxkAAAAZAAAADQAcAHRlc3QvdGVzdC50eHRVVAkAA3w0lWATB55gdXgLAAEE6AMAAAToAwAAeW91IGZvdW5kIHRoZSBlYXN0ZXIgZWdnClBLAwQKAAAAAADCOa5SAAAAAAAAAAAAAAAACgAcAHRlc3QvdGVzdC9VVAkAAxwHnmAgB55gdXgLAAEE6AMAAAToAwAAUEsDBAoAAAAAAKh0p1JsLSFnGQAAABkAAAASABwAdGVzdC90ZXN0L3Rlc3QudHh0VVQJAAN8NJVgHAeeYHV4CwABBOgDAAAE6AMAAHlvdSBmb3VuZCB0aGUgZWFzdGVyIGVnZwpQSwMECgAAAAAA2zquUgAAAAAAAAAAAAAAAAYAHABhZ2VudC9VVAkAAy4JnmAxCZ5gdXgLAAEE6AMAAAToAwAAUEsDBAoAAAAAAOI6rlIAAAAAAAAAAAAAAAALABwAYWdlbnQvY29uZi9VVAkAAzgJnmA+CZ5gdXgLAAEE6AMAAAToAwAAUEsDBAoAAAAAAKh0p1JsLSFnGQAAABkAAAATABwAYWdlbnQvY29uZi90ZXN0LnR4dFVUCQADfDSVYDgJnmB1eAsAAQToAwAABOgDAAB5b3UgZm91bmQgdGhlIGVhc3RlciBlZ2cKUEsBAh4DCgAAAAAAqHSnUmwtIWcZAAAAGQAAAAgAGAAAAAAAAQAAAKSBAAAAAHRlc3QudHh0VVQFAAN8NJVgdXgLAAEE6AMAAAToAwAAUEsBAh4DCgAAAAAAwDmuUgAAAAAAAAAAAAAAAAUAGAAAAAAAAAAQAO1BWwAAAHRlc3QvVVQFAAMXB55gdXgLAAEE6AMAAAToAwAAUEsBAh4DCgAAAAAAqHSnUmwtIWcZAAAAGQAAAA0AGAAAAAAAAQAAAKSBmgAAAHRlc3QvdGVzdC50eHRVVAUAA3w0lWB1eAsAAQToAwAABOgDAABQSwECHgMKAAAAAADCOa5SAAAAAAAAAAAAAAAACgAYAAAAAAAAABAA7UH6AAAAdGVzdC90ZXN0L1VUBQADHAeeYHV4CwABBOgDAAAE6AMAAFBLAQIeAwoAAAAAAKh0p1JsLSFnGQAAABkAAAASABgAAAAAAAEAAACkgT4BAAB0ZXN0L3Rlc3QvdGVzdC50eHRVVAUAA3w0lWB1eAsAAQToAwAABOgDAABQSwECHgMKAAAAAADbOq5SAAAAAAAAAAAAAAAABgAYAAAAAAAAABAA7UGjAQAAYWdlbnQvVVQFAAMuCZ5gdXgLAAEE6AMAAAToAwAAUEsBAh4DCgAAAAAA4jquUgAAAAAAAAAAAAAAAAsAGAAAAAAAAAAQAO1B4wEAAGFnZW50L2NvbmYvVVQFAAM4CZ5gdXgLAAEE6AMAAAToAwAAUEsBAh4DCgAAAAAAqHSnUmwtIWcZAAAAGQAAABMAGAAAAAAAAQAAAKSBKAIAAGFnZW50L2NvbmYvdGVzdC50eHRVVAUAA3w0lWB1eAsAAQToAwAABOgDAABQSwUGAAAAAAgACACKAgAAjgIAAAAA"
)

type CSIGarbageCollectorMock struct {
mock.Mock
}

func (m *CSIGarbageCollectorMock) Reconcile(context.Context, reconcile.Request) (reconcile.Result, error) {
args := m.Called()
return args.Get(0).(reconcile.Result), args.Error(1)
}

type mkDirAllErrorFs struct {
afero.Fs
}
Expand All @@ -57,7 +48,7 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
dynakubeName := "test-dk"

t.Run("no dynakube instance", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc := reconcilermock.NewReconciler(t)
provisioner := &OneAgentProvisioner{
apiReader: fake.NewClient(),
db: metadata.FakeMemoryDB(),
Expand All @@ -70,7 +61,7 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
require.Equal(t, reconcile.Result{}, result)
})
t.Run("dynakube deleted", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc := reconcilermock.NewReconciler(t)
db := metadata.FakeMemoryDB()
dynakube := metadata.Dynakube{TenantUUID: tenantUUID, LatestVersion: agentVersion, Name: dkName}
_ = db.InsertDynakube(ctx, &dynakube)
Expand All @@ -90,7 +81,7 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
require.Nil(t, ten)
})
t.Run("application monitoring disabled", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc := reconcilermock.NewReconciler(t)
provisioner := &OneAgentProvisioner{
apiReader: fake.NewClient(
&dynatracev1beta1.DynaKube{
Expand All @@ -112,7 +103,7 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
require.Equal(t, reconcile.Result{RequeueAfter: longRequeueDuration}, result)
})
t.Run("csi driver not enabled", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc := reconcilermock.NewReconciler(t)
provisioner := &OneAgentProvisioner{
apiReader: fake.NewClient(
&dynatracev1beta1.DynaKube{
Expand All @@ -138,7 +129,7 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
require.Equal(t, reconcile.Result{RequeueAfter: longRequeueDuration}, result)
})
t.Run("csi driver disabled", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc := reconcilermock.NewReconciler(t)
db := metadata.FakeMemoryDB()
_ = db.InsertDynakube(ctx, &metadata.Dynakube{Name: dynakubeName})
provisioner := &OneAgentProvisioner{
Expand Down Expand Up @@ -191,13 +182,12 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
},
},
)
mockClient := mockedclient.NewClient(t)
mockClient := clientmock.NewClient(t)
mockDtcBuilder := &dynatraceclient.StubBuilder{
DynatraceClient: mockClient,
}

gc := &CSIGarbageCollectorMock{}
gc.On("Reconcile").Return(reconcile.Result{}, nil)
gc := reconcilermock.NewReconciler(t)
db := metadata.FakeMemoryDB()

provisioner := &OneAgentProvisioner{
Expand All @@ -220,7 +210,7 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
require.Len(t, dynakubeMetadatas, 1)
})
t.Run("no tokens", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc := reconcilermock.NewReconciler(t)
provisioner := &OneAgentProvisioner{
apiReader: fake.NewClient(
&dynatracev1beta1.DynaKube{
Expand Down Expand Up @@ -253,7 +243,7 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
require.Equal(t, reconcile.Result{}, result)
})
t.Run("error when creating dynatrace client", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc := reconcilermock.NewReconciler(t)
mockDtcBuilder := &dynatraceclient.StubBuilder{
Err: fmt.Errorf(errorMsg),
}
Expand Down Expand Up @@ -298,11 +288,11 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
require.Equal(t, reconcile.Result{}, result)
})
t.Run("error creating directories", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc := reconcilermock.NewReconciler(t)
errorfs := &mkDirAllErrorFs{
Fs: afero.NewMemMapFs(),
}
mockClient := mockedclient.NewClient(t)
mockClient := clientmock.NewClient(t)
mockDtcBuilder := &dynatraceclient.StubBuilder{
DynatraceClient: mockClient,
}
Expand Down Expand Up @@ -343,10 +333,9 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
log.Info("")
})
t.Run("error getting latest agent version", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc.On("Reconcile").Return(reconcile.Result{}, nil)
gc := reconcilermock.NewReconciler(t)
memFs := afero.NewMemMapFs()
mockClient := mockedclient.NewClient(t)
mockClient := clientmock.NewClient(t)
mockDtcBuilder := &dynatraceclient.StubBuilder{
DynatraceClient: mockClient,
}
Expand All @@ -361,7 +350,7 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
},
},
}
installerMock := mockedinstaller.NewInstaller(t)
installerMock := installermock.NewInstaller(t)

provisioner := &OneAgentProvisioner{
apiReader: fake.NewClient(
Expand Down Expand Up @@ -406,9 +395,9 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
require.True(t, exists)
})
t.Run("error getting dynakube from db", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc := reconcilermock.NewReconciler(t)
memFs := afero.NewMemMapFs()
mockClient := mockedclient.NewClient(t)
mockClient := clientmock.NewClient(t)
mockDtcBuilder := &dynatraceclient.StubBuilder{
DynatraceClient: mockClient,
}
Expand Down Expand Up @@ -450,7 +439,7 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) { //nolint:revive
require.Empty(t, result)
})
t.Run("correct directories are created", func(t *testing.T) {
gc := &CSIGarbageCollectorMock{}
gc := reconcilermock.NewReconciler(t)
memFs := afero.NewMemMapFs()
memDB := metadata.FakeMemoryDB()
dynakube := &dynatracev1beta1.DynaKube{
Expand Down
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 005a67c

Please sign in to comment.