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: Creates mock generation infrastructure #1763

Merged
merged 27 commits into from
Dec 19, 2023
Merged

Conversation

lantoli
Copy link
Member

@lantoli lantoli commented Dec 17, 2023

Description

Creates mock generation infrastructure.

  • Uses Mockery to generate the mock skeletons for Testify Mock.
  • Integrates it in the Github Action workflow.
  • As an example it's used in transitions tests for search deployment resource.

Link to any related issue(s): https://jira.mongodb.org/browse/CLOUDP-218288

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contribution guidelines
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals, I defined an isolated PR with a relevant title as it will be used in the auto-generated changelog.

Further comments

- name: Build
run: make build
- name: Linter
run: make lint
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter here takes 2m instead of 3m and linter version is centralized in make file

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how it was before 🤔 I don't understand the advantages of adding these changes, maybe you could help me understand here. Could you clarify the current state without your changes and what your changes are trying to achieve? Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main advantage about the linter change is that we'll have the version only in one place, now it's in 2 places and we need to remember every time we upgrade it:
https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/GNUmakefile#L16
https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/.github/workflows/code-health.yml#L72

Copy link
Member Author

@lantoli lantoli Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with this change we use make lint from GH action so version is only in one place

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I see, thanks. I thought this was related somehow to the mock test but it is not.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI by moving out of the golangci lint action to the make file version you are loosing the action ability to comment on the line with the failure and you gonna have to parse the log files to figure out what broke

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gssbzn that's fair but we already have a pre-commit hook with linter so linter should rarely fail in GH

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gus made an interesting comment on that. And I guess your answer seems more like "this is a duplicate" - so my point is that either it's useful having linter here (hence we need to make the results easily actionable, hence this does not seem a good idea) or it's not.

Since it's not actually related to the change you are proposing in the PR I would suggest to remove this change the things as it is. Your pros mentioned above to Andrea as well does not look that strong to me compared to Gus's comment.

Copy link
Member Author

@lantoli lantoli Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed an offending commit (bypasing commit hooks) with current linter and the one in this PR to compare

Current linter: https://github.com/mongodb/terraform-provider-mongodbatlas/actions/runs/7258950919/job/19775168073

Running [/home/runner/golangci-lint-1.55.0-linux-amd64/golangci-lint run --out-format=github-actions --timeout 10m] in [] ...
  Error: test helper function should start from tb.Helper() (thelper)
  
  Error: issues found
  Ran golangci-lint in 169508ms

Linter in this PR: https://github.com/mongodb/terraform-provider-mongodbatlas/actions/runs/7259036317/job/19775411159?pr=1763

golangci-lint run
Error: internal/testutil/acc/skip.go:18:[6](https://github.com/mongodb/terraform-provider-mongodbatlas/actions/runs/7259036317/job/19775411159?pr=1763#step:8:7): test helper function should start from tb.Helper() (thelper)
func SkipIfTFAccNotDefined(tb testing.TB) {
     ^
make: *** [GNUmakefile:68: lint] Error 1
Error: Process completed with exit code 2.

So apart from having the linter version only in one place, here we have information about the offending file and line whereas in the current approach we only see the error not where it happens.

Please can you clarify why you think the current situation is better? Also I'm not sure I understand the part "the action ability to comment on the line with the failure ". thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2023-12-19 at 13 59 33

I am not gonna try to answer on behalf of Gus, but I just have a question: would we see this linter error on GH if we move the linter in this new place?

run: make tools
- name: Mock generation
run: mockery
- name: Check for uncommited files
Copy link
Member Author

@lantoli lantoli Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

taken from Atlas CLI.
We don't run mockery in each make build so it doesn't get slow, but we detect if it was not run when pushing the commits.

@@ -56,28 +68,11 @@ jobs:
with:
recreate: true
path: code-coverage-results.md
lint:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lint done in build to centralize version in make file

@@ -0,0 +1,10 @@
with-expecter: false
Copy link
Member Author

@lantoli lantoli Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go generate syntax is deprecated in favor of mockery config file

svc := mocksvc.NewDeploymentService(t)
ctx := context.Background()
for _, resp := range tc.mockResponses {
svc.On("GetAtlasSearchDeployment", ctx, dummyProjectID, clusterName).Return(resp.get()...).Once()
Copy link
Member Author

@lantoli lantoli Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mock programming

}

func (a *MockSearchDeploymentService) GetAtlasSearchDeployment(ctx context.Context, groupID, clusterName string) (*admin.ApiSearchDeploymentResponse, *http.Response, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the mock is auto-generated now

resp, err := searchdeployment.WaitSearchNodeStateTransition(ctx, dummyProjectID, "Cluster0", svc, testTimeoutConfig)
assert.Equal(t, tc.expectedError, err != nil)
assert.Equal(t, responseWithState(tc.expectedState), resp)
svc.AssertExpectations(t)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check mock expectations

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to point out that "AssertExpectations asserts that everything specified with On and Return was in fact called as expected. Calls may have occurred in any order." (link) In some tests we might want to avoid this call and instead use other methods such as AssertNumberOfCalls, AssertNotCalled, etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the clarification

}
resp, err := searchdeployment.WaitSearchNodeStateTransition(ctx, dummyProjectID, "Cluster0", svc, testTimeoutConfig)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we call the SUT (System Under Test) passing the mock collaborator

@lantoli lantoli marked this pull request as ready for review December 17, 2023 17:00
@oarbusi
Copy link
Collaborator

oarbusi commented Dec 18, 2023

@lantoli Not sure I understand why we use mockery in github actions. Shoudn't we generate the mock skeleton while developing to be able to create the tests?
edit: I see now that this is to make sure that we don't forget to run mockery when changing something in the mocks

@@ -89,11 +84,11 @@ jobs:
- name: Run ShellCheck
uses: bewuethr/shellcheck-action@v2
call-acceptance-tests-workflow:
needs: [build, lint, shellcheck, unit-test, website-lint]
needs: [build, shellcheck, unit-test, website-lint]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove lint here? if the lint fails we should not run the test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the lint is now running inside build, so threre is still a dependency on it

@@ -0,0 +1,71 @@
// Code generated by mockery. DO NOT EDIT.

package mocksvc
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have two mock packages?

Comment on lines -28 to -29
permissions:
pull-requests: write
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we weakening the permissions?

Copy link
Member Author

@lantoli lantoli Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gssbzn the intention was to simplify the action, what problems or risks do you see removing this? (I'm not sure why we're defining permissions in this job but not in others)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at some point we'll have to do some audit of gh actions permissions and be sure GH actions only use what they need, I'm not sure when was this permission added or why, a blame to understand the context would be great before trying to change it

I started some of this auditing on the CLI using https://github.com/ossf/scorecard but it's quite consuming and I haven't had the time to dig deeper on it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see, thanks for the info, it looks like we'll need to a focused effort on this and change all workflows.

GNUmakefile Outdated Show resolved Hide resolved
@@ -74,7 +75,9 @@ 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this pollute the go.mod file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhm, it didn' change anything in go.mod

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, double checking sometimes when doing go install and not using latest things get added to the go.mod

@@ -0,0 +1,11 @@
with-expecter: false
disable-version-string: true
dir: internal/testutil/mocksvc
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] since your interfaces are not centralized don't centralize the mocks, it leads to a lot of hooks if an interface happens to share a name and also to a lot of issues later on when you refactor your interfaces

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. I wanted to start simple, it's very easy to change at any moment to have mocks in their resource folders.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just be vigilant this doesn't become the permanent state, I'm currently trying to de-tangle some mock mess in the CLI and collocation would've made this easier

@lantoli
Copy link
Member Author

lantoli commented Dec 18, 2023

there is not mock file change detection in CLI pre-commit hooks

@lantoli lantoli changed the title feat: Creates mock generation infrastructure chore: Creates mock generation infrastructure Dec 19, 2023
@@ -70,14 +78,11 @@ jobs:
uses: golangci/[email protected]
with:
version: v1.55.0
args: --timeout 10m
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout is in config file now

Copy link
Contributor

Code Coverage

Package Line Rate Health
github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion 31%
github.com/mongodb/terraform-provider-mongodbatlas/internal/common/dsschema 100%
github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate 68%
github.com/mongodb/terraform-provider-mongodbatlas/internal/provider 7%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/accesslistapikey 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/alertconfiguration 33%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/apikey 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/atlasuser 0%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/auditing 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/backupcompliancepolicy 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cloudbackupschedule 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cloudbackupsnapshot 5%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cloudbackupsnapshotexportbucket 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cloudbackupsnapshotexportjob 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cloudbackupsnapshotrestorejob 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cloudprovideraccess 4%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cluster 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/clusteroutagesimulation 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/customdbrole 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/customdnsconfigurationclusteraws 5%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/databaseuser 30%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/datalakepipeline 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/encryptionatrest 31%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/eventtrigger 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/federateddatabaseinstance 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/federatedquerylimit 4%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/federatedsettingsidentityprovider 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/federatedsettingsorgconfig 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/federatedsettingsorgrolemapping 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/globalclusterconfig 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/ldapconfiguration 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/ldapverify 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/maintenancewindow 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/networkcontainer 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/networkpeering 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/onlinearchive 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/organization 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/orginvitation 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/privateendpointregionalmode 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/privatelinkendpoint 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/privatelinkendpointserverless 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/privatelinkendpointservice 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/privatelinkendpointservicedatafederationonlinearchive 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/privatelinkendpointserviceserverless 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/project 30%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/projectapikey 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/projectinvitation 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/projectipaccesslist 10%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/rolesorgid 7%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchdeployment 23%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchindex 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/serverlessinstance 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/streamconnection 26%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/streaminstance 14%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/teams 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/thirdpartyintegration 4%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/x509authenticationdatabaseuser 2%
github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc 4%
Summary 7% (765 / 11018)

@lantoli lantoli merged commit d8643b3 into master Dec 19, 2023
30 checks passed
@lantoli lantoli deleted the CLOUDP-218288_mocks branch December 19, 2023 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants