Skip to content

Commit ce2ad96

Browse files
authored
test: Supports running MacT tests at unit test stage (#3191)
* test: Support testing PlanModifier * fix broken unit test * test: Supports running MacT tests at unit test stage * Address PR comments * docs: Update testing best practices to include MipT for testing PlanModifier logic * update docs based on suggestions * pr suggestions * test: Remove test files after usage * add comment about signature * chore: increase timeout (believe the high default parallel factor 50 might lead to longer test time) * doc: Add docstrings and comments based on PR comments
1 parent fc63160 commit ce2ad96

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

.github/workflows/acceptance-tests-runner.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,6 @@ jobs:
403403
with:
404404
terraform_version: ${{ inputs.terraform_version }}
405405
terraform_wrapper: false
406-
- name: Unit tests
407-
run: go test -v ./internal/testutil/acc/advanced_cluster_preview_provider_v2_test.go
408-
- name: Mocked Acceptance Tests
409-
env:
410-
ACCTEST_REGEX_RUN: '^TestAccMockable'
411-
ACCTEST_PACKAGES: ./internal/service/advancedcluster
412-
run: make testmact
413406
- name: Acceptance Tests
414407
env:
415408
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ clean-atlas-org: ## Run a test to clean all projects and pending resources in an
4040

4141
.PHONY: test
4242
test: fmtcheck ## Run unit tests
43-
go test ./... -timeout=60s -parallel=$(PARALLEL_GO_TEST) -race
43+
@$(eval export HTTP_MOCKER_REPLAY?=true)
44+
@$(eval export MONGODB_ATLAS_ORG_ID?=111111111111111111111111)
45+
@$(eval export MONGODB_ATLAS_PROJECT_ID?=111111111111111111111111)
46+
@$(eval export MONGODB_ATLAS_CLUSTER_NAME?=mocked-cluster)
47+
go test ./... -timeout=120s -parallel=$(PARALLEL_GO_TEST) -race
4448

4549
.PHONY: testmact
4650
testmact: ## Run MacT tests (mocked acc tests)

internal/testutil/acc/shared_resource.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ func ProjectIDExecution(tb testing.TB) string {
7474
// When `MONGODB_ATLAS_PROJECT_ID` and `MONGODB_ATLAS_CLUSTER_NAME` are defined, they are used instead of creating a project and clusterName.
7575
func ProjectIDExecutionWithCluster(tb testing.TB, totalNodeCount int) (projectID, clusterName string) {
7676
tb.Helper()
77-
SkipInUnitTest(tb)
78-
require.True(tb, sharedInfo.init, "SetupSharedResources must called from TestMain test package")
79-
8077
if ExistingClusterUsed() {
8178
return existingProjectIDClusterName()
8279
}
80+
// Only skip after ExistingClusterUsed() to allow MacT (Mocked-Acceptance Tests) to return early instead of being skipped.
81+
SkipInUnitTest(tb)
82+
require.True(tb, sharedInfo.init, "SetupSharedResources must called from TestMain test package")
8383
return NextProjectIDClusterName(totalNodeCount, func(projectName string) string {
8484
return createProject(tb, projectName)
8585
})

internal/testutil/unit/http_mock_configs.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package unit
22

33
import (
4+
"sync"
45
"time"
56

67
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedclustertpf"
@@ -12,11 +13,15 @@ const (
1213

1314
var (
1415
MockConfigAdvancedClusterTPF = MockHTTPDataConfig{AllowMissingRequests: true, RunBeforeEach: shortenClusterTPFRetries, IsDiffMustSubstrings: []string{"/clusters"}, QueryVars: []string{"providerName"}}
16+
onceShortenClusterTPFRetries sync.Once
1517
)
1618

19+
// shortenClusterTPFRetries must meet the interface func() error as it is used in RunBeforeEach which runs as part of TestCase.PreCheck()
1720
func shortenClusterTPFRetries() error {
18-
advancedclustertpf.RetryMinTimeout = shortRefresh
19-
advancedclustertpf.RetryDelay = shortRefresh
20-
advancedclustertpf.RetryPollInterval = shortRefresh
21+
onceShortenClusterTPFRetries.Do(func() {
22+
advancedclustertpf.RetryMinTimeout = shortRefresh
23+
advancedclustertpf.RetryDelay = shortRefresh
24+
advancedclustertpf.RetryPollInterval = shortRefresh
25+
})
2126
return nil
2227
}

internal/testutil/unit/http_mocker.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ const (
2323
)
2424

2525
type MockHTTPDataConfig struct {
26-
RunBeforeEach func() error
27-
RequestHandler ManualRequestHandler
28-
FilePathOverride string
29-
IsDiffSkipSuffixes []string
30-
IsDiffMustSubstrings []string
31-
QueryVars []string
32-
AllowMissingRequests bool
33-
AllowOutOfOrder bool
26+
RunBeforeEach func() error // Run by TestCase.PreCheck. Useful for reducing retry timeouts.
27+
RequestHandler ManualRequestHandler // Allow inspecting or overriding mocking behavior. Can be used to return 404 when a test has completed.
28+
FilePathOverride string // Read mock data file from specific filepath, otherwise using the test name in `MockConfigFilePath` to find mocked responses.
29+
IsDiffSkipSuffixes []string // Can be used when a PATCH/POST request is creating noise for diffs, for example :validate endpoints.
30+
IsDiffMustSubstrings []string // Only include diff request for specific substrings, for example /clusters (avoids project create requests)
31+
QueryVars []string // Substitute this query vars. Useful when differentiating responses based on query args, for example ?providerName=AWS/AZURE returns different responses
32+
AllowMissingRequests bool // When false will require all API calls to be made.
33+
AllowOutOfOrder bool // When true will allow a GET request returned after a POST to be returned before the POST.
3434
}
3535

3636
func (c MockHTTPDataConfig) WithAllowOutOfOrder() MockHTTPDataConfig { //nolint: gocritic // Want each test run to have its own config (hugeParam: c is heavy (112 bytes); consider passing it by pointer)
@@ -151,10 +151,11 @@ func enableReplayForTestCase(t *testing.T, config *MockHTTPDataConfig, testCase
151151
}
152152
roundTripper, mockRoundTripper := NewMockRoundTripper(t, config, data)
153153
httpClientModifier := mockClientModifier{config: config, mockRoundTripper: roundTripper}
154+
testCase.IsUnitTest = true
154155
testCase.ProtoV6ProviderFactories = TestAccProviderV6FactoriesWithMock(t, &httpClientModifier)
155156
testCase.PreCheck = func() {
156157
if config.RunBeforeEach != nil {
157-
// Mock Configs can share SideEffect, using lock to avoid race conditions.
158+
// Mock Configs can share RunBeforeEach, using lock to avoid race conditions.
158159
accClientLock.Lock()
159160
defer accClientLock.Unlock()
160161
require.NoError(t, config.RunBeforeEach())

0 commit comments

Comments
 (0)