From 18301483b6477da044269b1e7195346ea4ee1a2f Mon Sep 17 00:00:00 2001 From: Jess <70455379+jesslourenco@users.noreply.github.com> Date: Thu, 13 Jun 2024 19:19:07 -0400 Subject: [PATCH] Create config file for Mockery instead of using explicit CLI flags in the Makefile (#5623) ## Which problem is this PR solving? - Part of #5569 ## Description of the changes - Created the configuration file (.mockery.yaml) to match what was being done by `make generate-mocks` - Removed a few mock files from es/mocks that were defined within the subpackage `client`. These files are still (auto) generated but within es/client/mocks only ## How was this change tested? - manually by running `mockery` - by running `make test` ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [ ] I have added unit tests for the new functionality - [ ] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `yarn lint` and `yarn test` --------- Signed-off-by: jessica lourenco Signed-off-by: jesslourenco <70455379+jesslourenco@users.noreply.github.com> Co-authored-by: Yuri Shkuro --- .mockery.yaml | 20 +++++++ Makefile | 4 +- cmd/es-rollover/app/init/action_test.go | 28 +++++----- cmd/es-rollover/app/lookback/action_test.go | 10 ++-- cmd/es-rollover/app/rollover/action_test.go | 16 +++--- pkg/es/{ => client}/mocks/ClusterAPI.go | 0 pkg/es/{ => client}/mocks/IndexAPI.go | 0 .../mocks/IndexManagementLifecycleAPI.go | 0 pkg/es/client/mocks/cluster_client.go | 28 ---------- pkg/es/client/mocks/ilm_client.go | 26 --------- pkg/es/client/mocks/index_client.go | 54 ------------------- 11 files changed, 48 insertions(+), 138 deletions(-) create mode 100644 .mockery.yaml rename pkg/es/{ => client}/mocks/ClusterAPI.go (100%) rename pkg/es/{ => client}/mocks/IndexAPI.go (100%) rename pkg/es/{ => client}/mocks/IndexManagementLifecycleAPI.go (100%) delete mode 100644 pkg/es/client/mocks/cluster_client.go delete mode 100644 pkg/es/client/mocks/ilm_client.go delete mode 100644 pkg/es/client/mocks/index_client.go diff --git a/.mockery.yaml b/.mockery.yaml new file mode 100644 index 00000000000..9e7210dc1c1 --- /dev/null +++ b/.mockery.yaml @@ -0,0 +1,20 @@ +with-expecter: false +disable-version-string: true +outpkg: "mocks" +dir: "{{.InterfaceDir}}/mocks/" +mockname: "{{.InterfaceName}}" +filename: "{{.InterfaceName}}.go" +packages: + github.com/jaegertracing/jaeger/pkg/es: + config: + all: true + github.com/jaegertracing/jaeger/storage/spanstore: + config: + all: true + github.com/jaegertracing/jaeger/proto-gen/storage_v1: + config: + all: true + github.com/jaegertracing/jaeger/pkg/es/client: + config: + all: true + diff --git a/Makefile b/Makefile index 43123363911..b4d181e2e2c 100644 --- a/Makefile +++ b/Makefile @@ -468,9 +468,7 @@ init-submodules: MOCKERY_FLAGS := --all --disable-version-string .PHONY: generate-mocks generate-mocks: $(MOCKERY) - $(MOCKERY) $(MOCKERY_FLAGS) --dir ./pkg/es/ --output ./pkg/es/mocks - $(MOCKERY) $(MOCKERY_FLAGS) --dir ./storage/spanstore/ --output ./storage/spanstore/mocks - $(MOCKERY) $(MOCKERY_FLAGS) --dir ./proto-gen/storage_v1/ --output ./proto-gen/storage_v1/mocks + $(MOCKERY) .PHONY: certs certs: diff --git a/cmd/es-rollover/app/init/action_test.go b/cmd/es-rollover/app/init/action_test.go index e1ee8115c39..881a0428113 100644 --- a/cmd/es-rollover/app/init/action_test.go +++ b/cmd/es-rollover/app/init/action_test.go @@ -76,7 +76,7 @@ func TestIndexCreateIfNotExist(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - indexClient := &mocks.MockIndexAPI{} + indexClient := &mocks.IndexAPI{} indexClient.On("CreateIndex", "jaeger-span").Return(test.returnErr) err := createIndexIfNotExist(indexClient, "jaeger-span") if test.containsError != "" { @@ -91,13 +91,13 @@ func TestIndexCreateIfNotExist(t *testing.T) { func TestRolloverAction(t *testing.T) { tests := []struct { name string - setupCallExpectations func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) + setupCallExpectations func(indexClient *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, ilmClient *mocks.IndexManagementLifecycleAPI) config Config expectedErr error }{ { name: "Unsupported version", - setupCallExpectations: func(_ *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, _ *mocks.MockILMAPI) { + setupCallExpectations: func(_ *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, _ *mocks.IndexManagementLifecycleAPI) { clusterClient.On("Version").Return(uint(5), nil) }, config: Config{ @@ -110,7 +110,7 @@ func TestRolloverAction(t *testing.T) { }, { name: "error getting version", - setupCallExpectations: func(_ *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, _ *mocks.MockILMAPI) { + setupCallExpectations: func(_ *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, _ *mocks.IndexManagementLifecycleAPI) { clusterClient.On("Version").Return(uint(0), errors.New("version error")) }, expectedErr: errors.New("version error"), @@ -123,7 +123,7 @@ func TestRolloverAction(t *testing.T) { }, { name: "ilm doesnt exist", - setupCallExpectations: func(_ *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) { + setupCallExpectations: func(_ *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, ilmClient *mocks.IndexManagementLifecycleAPI) { clusterClient.On("Version").Return(uint(7), nil) ilmClient.On("Exists", "myilmpolicy").Return(false, nil) }, @@ -138,7 +138,7 @@ func TestRolloverAction(t *testing.T) { }, { name: "fail get ilm policy", - setupCallExpectations: func(_ *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) { + setupCallExpectations: func(_ *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, ilmClient *mocks.IndexManagementLifecycleAPI) { clusterClient.On("Version").Return(uint(7), nil) ilmClient.On("Exists", "myilmpolicy").Return(false, errors.New("error getting ilm policy")) }, @@ -153,7 +153,7 @@ func TestRolloverAction(t *testing.T) { }, { name: "fail to create template", - setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, _ *mocks.MockILMAPI) { + setupCallExpectations: func(indexClient *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, _ *mocks.IndexManagementLifecycleAPI) { clusterClient.On("Version").Return(uint(7), nil) indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(errors.New("error creating template")) }, @@ -167,7 +167,7 @@ func TestRolloverAction(t *testing.T) { }, { name: "fail to get jaeger indices", - setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, _ *mocks.MockILMAPI) { + setupCallExpectations: func(indexClient *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, _ *mocks.IndexManagementLifecycleAPI) { clusterClient.On("Version").Return(uint(7), nil) indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil) indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil) @@ -183,7 +183,7 @@ func TestRolloverAction(t *testing.T) { }, { name: "fail to create alias", - setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, _ *mocks.MockILMAPI) { + setupCallExpectations: func(indexClient *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, _ *mocks.IndexManagementLifecycleAPI) { clusterClient.On("Version").Return(uint(7), nil) indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil) indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil) @@ -203,7 +203,7 @@ func TestRolloverAction(t *testing.T) { }, { name: "create rollover index", - setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, _ *mocks.MockILMAPI) { + setupCallExpectations: func(indexClient *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, _ *mocks.IndexManagementLifecycleAPI) { clusterClient.On("Version").Return(uint(7), nil) indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil) indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil) @@ -223,7 +223,7 @@ func TestRolloverAction(t *testing.T) { }, { name: "create rollover index with ilm", - setupCallExpectations: func(indexClient *mocks.MockIndexAPI, clusterClient *mocks.MockClusterAPI, ilmClient *mocks.MockILMAPI) { + setupCallExpectations: func(indexClient *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, ilmClient *mocks.IndexManagementLifecycleAPI) { clusterClient.On("Version").Return(uint(7), nil) indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil) indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil) @@ -247,9 +247,9 @@ func TestRolloverAction(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - indexClient := &mocks.MockIndexAPI{} - clusterClient := &mocks.MockClusterAPI{} - ilmClient := &mocks.MockILMAPI{} + indexClient := &mocks.IndexAPI{} + clusterClient := &mocks.ClusterAPI{} + ilmClient := &mocks.IndexManagementLifecycleAPI{} initAction := Action{ Config: test.config, IndicesClient: indexClient, diff --git a/cmd/es-rollover/app/lookback/action_test.go b/cmd/es-rollover/app/lookback/action_test.go index 79b786a74e7..2c8a57bf6ef 100644 --- a/cmd/es-rollover/app/lookback/action_test.go +++ b/cmd/es-rollover/app/lookback/action_test.go @@ -76,13 +76,13 @@ func TestLookBackAction(t *testing.T) { tests := []struct { name string - setupCallExpectations func(indexClient *mocks.MockIndexAPI) + setupCallExpectations func(indexClient *mocks.IndexAPI) config Config expectedErr error }{ { name: "success", - setupCallExpectations: func(indexClient *mocks.MockIndexAPI) { + setupCallExpectations: func(indexClient *mocks.IndexAPI) { indexClient.On("GetJaegerIndices", "").Return(indices, nil) indexClient.On("DeleteAlias", []client.Alias{ { @@ -103,7 +103,7 @@ func TestLookBackAction(t *testing.T) { }, { name: "get indices error", - setupCallExpectations: func(indexClient *mocks.MockIndexAPI) { + setupCallExpectations: func(indexClient *mocks.IndexAPI) { indexClient.On("GetJaegerIndices", "").Return(indices, errors.New("get indices error")) }, config: Config{ @@ -118,7 +118,7 @@ func TestLookBackAction(t *testing.T) { }, { name: "empty indices", - setupCallExpectations: func(indexClient *mocks.MockIndexAPI) { + setupCallExpectations: func(indexClient *mocks.IndexAPI) { indexClient.On("GetJaegerIndices", "").Return([]client.Index{}, nil) }, config: Config{ @@ -137,7 +137,7 @@ func TestLookBackAction(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - indexClient := &mocks.MockIndexAPI{} + indexClient := &mocks.IndexAPI{} lookbackAction := Action{ Config: test.config, IndicesClient: indexClient, diff --git a/cmd/es-rollover/app/rollover/action_test.go b/cmd/es-rollover/app/rollover/action_test.go index 3d092e464e5..be01978ef66 100644 --- a/cmd/es-rollover/app/rollover/action_test.go +++ b/cmd/es-rollover/app/rollover/action_test.go @@ -45,7 +45,7 @@ func TestRolloverAction(t *testing.T) { createAliasErr error expectedError bool indices []client.Index - setupCallExpectations func(indexClient *mocks.MockIndexAPI, t *testCase) + setupCallExpectations func(indexClient *mocks.IndexAPI, t *testCase) } tests := []testCase{ @@ -54,7 +54,7 @@ func TestRolloverAction(t *testing.T) { conditions: "{\"max_age\": \"2d\"}", expectedError: false, indices: readIndices, - setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) { + setupCallExpectations: func(indexClient *mocks.IndexAPI, test *testCase) { indexClient.On("GetJaegerIndices", "").Return(test.indices, test.getJaegerIndicesErr) indexClient.On("CreateAlias", aliasToCreate).Return(test.createAliasErr) indexClient.On("Rollover", "jaeger-span-archive-write", map[string]any{"max_age": "2d"}).Return(test.rolloverErr) @@ -72,7 +72,7 @@ func TestRolloverAction(t *testing.T) { }, }, }, - setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) { + setupCallExpectations: func(indexClient *mocks.IndexAPI, test *testCase) { indexClient.On("GetJaegerIndices", "").Return(test.indices, test.getJaegerIndicesErr) indexClient.On("Rollover", "jaeger-span-archive-write", map[string]any{"max_age": "2d"}).Return(test.rolloverErr) }, @@ -83,7 +83,7 @@ func TestRolloverAction(t *testing.T) { expectedError: true, getJaegerIndicesErr: errors.New("unable to get indices"), indices: readIndices, - setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) { + setupCallExpectations: func(indexClient *mocks.IndexAPI, test *testCase) { indexClient.On("Rollover", "jaeger-span-archive-write", map[string]any{"max_age": "2d"}).Return(test.rolloverErr) indexClient.On("GetJaegerIndices", "").Return(test.indices, test.getJaegerIndicesErr) }, @@ -94,7 +94,7 @@ func TestRolloverAction(t *testing.T) { expectedError: true, rolloverErr: errors.New("unable to rollover"), indices: readIndices, - setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) { + setupCallExpectations: func(indexClient *mocks.IndexAPI, test *testCase) { indexClient.On("Rollover", "jaeger-span-archive-write", map[string]any{"max_age": "2d"}).Return(test.rolloverErr) }, }, @@ -104,7 +104,7 @@ func TestRolloverAction(t *testing.T) { expectedError: true, createAliasErr: errors.New("unable to create alias"), indices: readIndices, - setupCallExpectations: func(indexClient *mocks.MockIndexAPI, test *testCase) { + setupCallExpectations: func(indexClient *mocks.IndexAPI, test *testCase) { indexClient.On("GetJaegerIndices", "").Return(test.indices, test.getJaegerIndicesErr) indexClient.On("CreateAlias", aliasToCreate).Return(test.createAliasErr) indexClient.On("Rollover", "jaeger-span-archive-write", map[string]any{"max_age": "2d"}).Return(test.rolloverErr) @@ -116,13 +116,13 @@ func TestRolloverAction(t *testing.T) { unmarshalErrExpected: true, createAliasErr: errors.New("unable to create alias"), indices: readIndices, - setupCallExpectations: func(_ *mocks.MockIndexAPI, _ *testCase) {}, + setupCallExpectations: func(_ *mocks.IndexAPI, _ *testCase) {}, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - indexClient := &mocks.MockIndexAPI{} + indexClient := &mocks.IndexAPI{} rolloverAction := Action{ Config: Config{ diff --git a/pkg/es/mocks/ClusterAPI.go b/pkg/es/client/mocks/ClusterAPI.go similarity index 100% rename from pkg/es/mocks/ClusterAPI.go rename to pkg/es/client/mocks/ClusterAPI.go diff --git a/pkg/es/mocks/IndexAPI.go b/pkg/es/client/mocks/IndexAPI.go similarity index 100% rename from pkg/es/mocks/IndexAPI.go rename to pkg/es/client/mocks/IndexAPI.go diff --git a/pkg/es/mocks/IndexManagementLifecycleAPI.go b/pkg/es/client/mocks/IndexManagementLifecycleAPI.go similarity index 100% rename from pkg/es/mocks/IndexManagementLifecycleAPI.go rename to pkg/es/client/mocks/IndexManagementLifecycleAPI.go diff --git a/pkg/es/client/mocks/cluster_client.go b/pkg/es/client/mocks/cluster_client.go deleted file mode 100644 index 37137b8ffde..00000000000 --- a/pkg/es/client/mocks/cluster_client.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2021 The Jaeger Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package mocks - -import ( - "github.com/stretchr/testify/mock" -) - -type MockClusterAPI struct { - mock.Mock -} - -func (c *MockClusterAPI) Version() (uint, error) { - ret := c.Called() - return ret.Get(0).(uint), ret.Error(1) -} diff --git a/pkg/es/client/mocks/ilm_client.go b/pkg/es/client/mocks/ilm_client.go deleted file mode 100644 index 0602f13a1bc..00000000000 --- a/pkg/es/client/mocks/ilm_client.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2021 The Jaeger Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package mocks - -import "github.com/stretchr/testify/mock" - -type MockILMAPI struct { - mock.Mock -} - -func (c *MockILMAPI) Exists(name string) (bool, error) { - ret := c.Called(name) - return ret.Get(0).(bool), ret.Error(1) -} diff --git a/pkg/es/client/mocks/index_client.go b/pkg/es/client/mocks/index_client.go deleted file mode 100644 index 1382b3e7f89..00000000000 --- a/pkg/es/client/mocks/index_client.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) 2021 The Jaeger Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package mocks - -import ( - "github.com/stretchr/testify/mock" - - "github.com/jaegertracing/jaeger/pkg/es/client" -) - -type MockIndexAPI struct { - mock.Mock -} - -func (c *MockIndexAPI) GetJaegerIndices(prefix string) ([]client.Index, error) { - ret := c.Called(prefix) - return ret.Get(0).([]client.Index), ret.Error(1) -} -func (c *MockIndexAPI) DeleteIndices(indices []client.Index) error { - ret := c.Called(indices) - return ret.Error(0) -} -func (c *MockIndexAPI) CreateIndex(index string) error { - ret := c.Called(index) - return ret.Error(0) -} -func (c *MockIndexAPI) CreateAlias(aliases []client.Alias) error { - ret := c.Called(aliases) - return ret.Error(0) -} -func (c *MockIndexAPI) DeleteAlias(aliases []client.Alias) error { - ret := c.Called(aliases) - return ret.Error(0) -} -func (c *MockIndexAPI) CreateTemplate(template, name string) error { - ret := c.Called(template, name) - return ret.Error(0) -} -func (c *MockIndexAPI) Rollover(rolloverTarget string, conditions map[string]interface{}) error { - ret := c.Called(rolloverTarget, conditions) - return ret.Error(0) -}