Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mocktest module #255

Merged
merged 32 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ issues:
- path: controller/suite_test.go
linters:
- gci
# Exclude goimports check for controller tests that import both mocktest and ginkgo/gomega as dot imports.
# goimports wants mocktest as a dot import in a separate group, but gci wants them in the same group.
- path: controller/.*_controller_test.go
linters:
- goimports

# These are performance optimisations rather than style issues per se.
# They warn when function arguments or range values copy a lot of memory
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ docs:

.PHONY: test
test: generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(CACHE_BIN) -p path)" go test -race -timeout 60s `go list ./... | grep -v ./mock` -coverprofile cover.out.tmp
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(CACHE_BIN) -p path)" go test -race -timeout 60s `go list ./... | grep -v ./mock$$` -coverprofile cover.out.tmp
grep -v "zz_generated.deepcopy.go" cover.out.tmp > cover.out
rm cover.out.tmp

Expand Down
8 changes: 1 addition & 7 deletions cloud/scope/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ type LinodeObjectStorageClient interface {
DeleteObjectStorageKey(ctx context.Context, keyID int) error
}

type LinodeObjectStorageClientBuilder func(apiKey string) (LinodeObjectStorageClient, error)

func CreateLinodeObjectStorageClient(apiKey string) (LinodeObjectStorageClient, error) {
return CreateLinodeClient(apiKey)
}

type k8sClient interface {
type K8sClient interface {
client.Client
}
4 changes: 2 additions & 2 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

// ClusterScopeParams defines the input parameters used to create a new Scope.
type ClusterScopeParams struct {
Client k8sClient
Client K8sClient
Cluster *clusterv1.Cluster
LinodeCluster *infrav1alpha1.LinodeCluster
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func NewClusterScope(ctx context.Context, apiKey string, params ClusterScopePara

// ClusterScope defines the basic context for an actuator to operate upon.
type ClusterScope struct {
client k8sClient
client K8sClient
PatchHelper *patch.Helper
LinodeClient LinodeNodeBalancerClient
Cluster *clusterv1.Cluster
Expand Down
24 changes: 12 additions & 12 deletions cloud/scope/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestClusterScopeMethods(t *testing.T) {
tests := []struct {
name string
fields fields
expects func(mock *mock.Mockk8sClient)
expects func(mock *mock.MockK8sClient)
}{
{
name: "Success - finalizer should be added to the Linode Cluster object",
Expand All @@ -114,7 +114,7 @@ func TestClusterScopeMethods(t *testing.T) {
},
},
},
expects: func(mock *mock.Mockk8sClient) {
expects: func(mock *mock.MockK8sClient) {
mock.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
Expand All @@ -134,7 +134,7 @@ func TestClusterScopeMethods(t *testing.T) {
},
},
},
expects: func(mock *mock.Mockk8sClient) {
expects: func(mock *mock.MockK8sClient) {
mock.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
Expand All @@ -151,7 +151,7 @@ func TestClusterScopeMethods(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockK8sClient := mock.NewMockk8sClient(ctrl)
mockK8sClient := mock.NewMockK8sClient(ctrl)

testcase.expects(mockK8sClient)

Expand Down Expand Up @@ -188,7 +188,7 @@ func TestNewClusterScope(t *testing.T) {
name string
args args
expectedError error
expects func(mock *mock.Mockk8sClient)
expects func(mock *mock.MockK8sClient)
}{
{
name: "Success - Pass in valid args and get a valid ClusterScope",
Expand All @@ -200,7 +200,7 @@ func TestNewClusterScope(t *testing.T) {
},
},
expectedError: nil,
expects: func(mock *mock.Mockk8sClient) {
expects: func(mock *mock.MockK8sClient) {
mock.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
Expand All @@ -226,7 +226,7 @@ func TestNewClusterScope(t *testing.T) {
},
},
expectedError: nil,
expects: func(mock *mock.Mockk8sClient) {
expects: func(mock *mock.MockK8sClient) {
mock.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
Expand All @@ -250,7 +250,7 @@ func TestNewClusterScope(t *testing.T) {
params: ClusterScopeParams{},
},
expectedError: fmt.Errorf("cluster is required when creating a ClusterScope"),
expects: func(mock *mock.Mockk8sClient) {},
expects: func(mock *mock.MockK8sClient) {},
},
{
name: "Error - patchHelper returns error. Checking error handle for when new patchHelper is invoked",
Expand All @@ -262,7 +262,7 @@ func TestNewClusterScope(t *testing.T) {
},
},
expectedError: fmt.Errorf("failed to init patch helper:"),
expects: func(mock *mock.Mockk8sClient) {
expects: func(mock *mock.MockK8sClient) {
mock.EXPECT().Scheme().Return(runtime.NewScheme())
},
},
Expand All @@ -284,7 +284,7 @@ func TestNewClusterScope(t *testing.T) {
},
},
expectedError: fmt.Errorf("credentials from secret ref: get credentials secret test/example: failed to get secret"),
expects: func(mock *mock.Mockk8sClient) {
expects: func(mock *mock.MockK8sClient) {
mock.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("failed to get secret"))
},
},
Expand All @@ -298,7 +298,7 @@ func TestNewClusterScope(t *testing.T) {
},
},
expectedError: fmt.Errorf("failed to create linode client: missing Linode API key"),
expects: func(mock *mock.Mockk8sClient) {},
expects: func(mock *mock.MockK8sClient) {},
},
}

Expand All @@ -310,7 +310,7 @@ func TestNewClusterScope(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockK8sClient := mock.NewMockk8sClient(ctrl)
mockK8sClient := mock.NewMockK8sClient(ctrl)

testcase.expects(mockK8sClient)

Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func CreateLinodeClient(apiKey string) (*linodego.Client, error) {
return &linodeClient, nil
}

func getCredentialDataFromRef(ctx context.Context, crClient k8sClient, credentialsRef corev1.SecretReference, defaultNamespace string) ([]byte, error) {
func getCredentialDataFromRef(ctx context.Context, crClient K8sClient, credentialsRef corev1.SecretReference, defaultNamespace string) ([]byte, error) {
secretRef := client.ObjectKey{
Name: credentialsRef.Name,
Namespace: credentialsRef.Namespace,
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestGetCredentialDataFromRef(t *testing.T) {
defer ctrl.Finish()

// Create an instance of the mock K8sClient
mockClient := mock.NewMockk8sClient(ctrl)
mockClient := mock.NewMockK8sClient(ctrl)

// Setup Expected behaviour
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(testCase.args.funcBehavior)
Expand Down
8 changes: 4 additions & 4 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import (
)

type MachineScopeParams struct {
Client k8sClient
Client K8sClient
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
LinodeCluster *infrav1alpha1.LinodeCluster
LinodeMachine *infrav1alpha1.LinodeMachine
}

type MachineScope struct {
Client k8sClient
Client K8sClient
PatchHelper *patch.Helper
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
Expand All @@ -34,7 +34,7 @@ type MachineScope struct {

func validateMachineScopeParams(params MachineScopeParams) error {
if params.Cluster == nil {
return errors.New("custer is required when creating a MachineScope")
return errors.New("cluster is required when creating a MachineScope")
}
if params.Machine == nil {
return errors.New("machine is required when creating a MachineScope")
Expand Down Expand Up @@ -77,7 +77,7 @@ func NewMachineScope(ctx context.Context, apiKey string, params MachineScopePara
if credentialRef != nil {
data, err := getCredentialDataFromRef(ctx, params.Client, *credentialRef, defaultNamespace)
if err != nil {
return nil, fmt.Errorf("credentials from cluster secret ref: %w", err)
return nil, fmt.Errorf("credentials from secret ref: %w", err)
}
apiKey = string(data)
}
Expand Down
Loading
Loading