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

chore: Uses mocks for unit tests in Atlas Go SDK #2075

Merged
merged 3 commits into from
Mar 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
13 changes: 0 additions & 13 deletions .github/workflows/code-health.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ jobs:
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491
with:
go-version-file: 'go.mod'
- name: Mock generation
run: make tools generate-mocks
- name: Check for uncommited files
run: |
FILES=$(git ls-files -o -m --directory --exclude-standard --no-empty-directory)
LINES=$(echo "$FILES" | awk 'NF' | wc -l)
if [ "${LINES}" -ne 0 ]; then
echo "Detected files that need to be committed:"
echo "${FILES//^/ }"
echo ""
echo "Mock skeletons are not up-to-date, you may have forgotten to run mockery before committing your changes."
exit 1
fi
- name: Build
run: make build
unit-test:
Expand Down
15 changes: 0 additions & 15 deletions .mockery.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ To do this you can:
- Helper methods must have their own tests, e.g. `common_advanced_cluster_test.go` has tests for `common_advanced_cluster.go`.
- `internal/testutils/acc` contains helper test methods for Acceptance and Migration tests.
- Tests that need the provider binary like End-to-End tests don’t belong to the source code packages and go in `test/e2e`.
- [Testify Mock](https://pkg.go.dev/github.com/stretchr/testify/mock) and [Mockery](https://github.com/vektra/mockery) are used for test doubles in unit tests. Mocked interfaces are generated in folder `internal/testutil/mocksvc`.
- [Testify Mock](https://pkg.go.dev/github.com/stretchr/testify/mock) and [Mockery](https://github.com/vektra/mockery) are used for test doubles in Atlas Go SDK unit tests.


### Creating New Resource and Data Sources
Expand Down
6 changes: 0 additions & 6 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ VERSION=$(GITTAG:v%=%)
LINKER_FLAGS=-s -w -X 'github.com/mongodb/terraform-provider-mongodbatlas/version.ProviderVersion=${VERSION}'

GOLANGCI_VERSION=v1.56.2
MOCKERY_VERSION=v2.42.0

export PATH := $(shell go env GOPATH)/bin:$(PATH)
export SHELL := env PATH=$(PATH) /bin/bash
Expand Down Expand Up @@ -79,7 +78,6 @@ tools: ## Install dev tools
go install github.com/terraform-linters/[email protected]
go install github.com/rhysd/actionlint/cmd/actionlint@latest
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
go install github.com/vektra/mockery/v2@$(MOCKERY_VERSION)
go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@latest
go install github.com/hashicorp/terraform-plugin-codegen-openapi/cmd/tfplugingen-openapi@latest
go install github.com/hashicorp/terraform-plugin-codegen-framework/cmd/tfplugingen-framework@latest
Expand Down Expand Up @@ -112,10 +110,6 @@ link-git-hooks: ## Install git hooks
update-atlas-sdk: ## Update the atlas-sdk dependency
./scripts/update-sdk.sh

.PHONY: generate-mocks
generate-mocks: # uses mockery to generate mocks in folder `internal/testutil/mocksvc`
mockery

# e.g. run: make scaffold resource_name=streamInstance type=resource
# - type argument can have the values: `resource`, `data-source`, `plural-data-source`.
# details on usage can be found in CONTRIBUTING.md under "Scaffolding initial Code and File Structure"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mocksvc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
"go.mongodb.org/atlas-sdk/v20231115008/mockadmin"
)

var (
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestUpgradeRefreshFunc(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testObject := mocksvc.NewClustersApi(t)
testObject := mockadmin.NewClustersApi(t)

testObject.EXPECT().GetCluster(mock.Anything, mock.Anything, mock.Anything).Return(admin.GetClusterApiRequest{ApiService: testObject}).Once()
testObject.EXPECT().GetClusterExecute(mock.Anything).Return(tc.mockCluster, tc.mockResponse, tc.mockError).Once()
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestResourceListAdvancedRefreshFunc(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testObject := mocksvc.NewClustersApi(t)
testObject := mockadmin.NewClustersApi(t)

testObject.EXPECT().ListClusters(mock.Anything, mock.Anything).Return(admin.ListClustersApiRequest{ApiService: testObject}).Once()
testObject.EXPECT().ListClustersExecute(mock.Anything).Return(tc.mockCluster, tc.mockResponse, tc.mockError).Once()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/retrystrategy"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/encryptionatrest"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mocksvc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
"go.mongodb.org/atlas-sdk/v20231115008/mockadmin"
)

const (
Expand Down Expand Up @@ -546,7 +546,7 @@ func TestResourceMongoDBAtlasEncryptionAtRestCreateRefreshFunc(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
m := mocksvc.NewEncryptionAtRestUsingCustomerKeyManagementApi(t)
m := mockadmin.NewEncryptionAtRestUsingCustomerKeyManagementApi(t)

m.EXPECT().UpdateEncryptionAtRest(mock.Anything, mock.Anything, mock.Anything).Return(admin.UpdateEncryptionAtRestApiRequest{ApiService: m})
m.EXPECT().UpdateEncryptionAtRestExecute(mock.Anything).Return(tc.mockResponse, nil, tc.mockError).Once()
Expand Down
14 changes: 7 additions & 7 deletions internal/service/project/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/project"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mocksvc"
"github.com/stretchr/testify/mock"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
"go.mongodb.org/atlas-sdk/v20231115008/mockadmin"
)

var (
Expand Down Expand Up @@ -106,8 +106,8 @@ func TestGetProjectPropsFromAPI(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
teamsMock := mocksvc.NewTeamsApi(t)
projectsMock := mocksvc.NewProjectsApi(t)
teamsMock := mockadmin.NewTeamsApi(t)
projectsMock := mockadmin.NewProjectsApi(t)

teamsMock.EXPECT().ListProjectTeams(mock.Anything, mock.Anything).Return(admin.ListProjectTeamsApiRequest{ApiService: teamsMock})
teamsMock.EXPECT().ListProjectTeamsExecute(mock.Anything).Return(tc.teamRoleReponse.TeamRole, tc.teamRoleReponse.HTTPResponse, tc.teamRoleReponse.Err)
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestUpdateProject(t *testing.T) {

for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
svc := mocksvc.NewProjectsApi(t)
svc := mockadmin.NewProjectsApi(t)
svc.EXPECT().UpdateProject(mock.Anything, mock.Anything, mock.Anything).Return(admin.UpdateProjectApiRequest{ApiService: svc}).Maybe()

svc.EXPECT().UpdateProjectExecute(mock.Anything).Return(tc.mockResponses.Project, tc.mockResponses.HTTPResponse, tc.mockResponses.Err).Maybe()
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestUpdateProjectLimits(t *testing.T) {

for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
svc := mocksvc.NewProjectsApi(t)
svc := mockadmin.NewProjectsApi(t)

svc.EXPECT().DeleteProjectLimit(mock.Anything, mock.Anything, mock.Anything).Return(admin.DeleteProjectLimitApiRequest{ApiService: svc}).Maybe()
svc.EXPECT().DeleteProjectLimitExecute(mock.Anything).Return(tc.mockResponses.DeleteProjectLimit, tc.mockResponses.HTTPResponse, tc.mockResponses.Err).Maybe()
Expand Down Expand Up @@ -417,7 +417,7 @@ func TestUpdateProjectTeams(t *testing.T) {

for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
svc := mocksvc.NewTeamsApi(t)
svc := mockadmin.NewTeamsApi(t)

svc.EXPECT().AddAllTeamsToProject(mock.Anything, mock.Anything, mock.Anything).Return(admin.AddAllTeamsToProjectApiRequest{ApiService: svc}).Maybe()
svc.EXPECT().AddAllTeamsToProjectExecute(mock.Anything).Return(nil, nil, nil).Maybe()
Expand Down Expand Up @@ -477,7 +477,7 @@ func TestResourceProjectDependentsDeletingRefreshFunc(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
svc := mocksvc.NewClustersApi(t)
svc := mockadmin.NewClustersApi(t)

svc.EXPECT().ListClusters(mock.Anything, dummyProjectID).Return(admin.ListClustersApiRequest{ApiService: svc})
svc.EXPECT().ListClustersExecute(mock.Anything).Return(tc.mockResponses.AdvancedClusterDescription, tc.mockResponses.HTTPResponse, tc.mockResponses.Err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/retrystrategy"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchdeployment"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mocksvc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
"go.mongodb.org/atlas-sdk/v20231115008/mockadmin"
)

var (
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestSearchDeploymentStateTransition(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
m := mocksvc.NewAtlasSearchApi(t)
m := mockadmin.NewAtlasSearchApi(t)
m.EXPECT().GetAtlasSearchDeployment(mock.Anything, mock.Anything, mock.Anything).Return(admin.GetAtlasSearchDeploymentApiRequest{ApiService: m})

for _, resp := range tc.mockResponses {
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestSearchDeploymentStateTransitionForDelete(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
m := mocksvc.NewAtlasSearchApi(t)
m := mockadmin.NewAtlasSearchApi(t)
m.EXPECT().GetAtlasSearchDeployment(mock.Anything, mock.Anything, mock.Anything).Return(admin.GetAtlasSearchDeploymentApiRequest{ApiService: m})

for _, resp := range tc.mockResponses {
Expand Down
Loading