-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathhttp_mocker_plan_checks_test.go
127 lines (117 loc) · 4.18 KB
/
http_mocker_plan_checks_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package unit_test
import (
"fmt"
"os"
"path"
"strings"
"testing"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/unit"
"github.com/stretchr/testify/require"
)
const (
pkgAdvancedCluster = "advancedcluster"
pkgAdvancedClusterTPF = "advancedclustertpf"
pkgRelPath = "internal/service"
)
type importNameConfig struct {
VariableReplacments map[string]string
TestName string
SrcPackage string
DestPackage string
Step int
}
// Manual test meant for creating the data needed for a MockPlanChecks Test.
func TestConvertMockableTests(t *testing.T) {
if os.Getenv("CONVERT_MOCKABLE_TESTS") == "" {
t.Skip("CONVERT_MOCKABLE_TESTS is not set, avoid running this in CI and by accident")
}
for importName, config := range map[string]importNameConfig{
unit.ImportNameClusterTwoRepSpecsWithAutoScalingAndSpecs: {
TestName: "TestAccMockableAdvancedCluster_removeBlocksFromConfig",
Step: 1,
VariableReplacments: map[string]string{
"clusterName": unit.MockedClusterName,
"groupId": unit.MockedProjectID,
},
SrcPackage: pkgAdvancedCluster,
DestPackage: pkgAdvancedClusterTPF,
},
} {
srcTestdata := unit.RepoPath(path.Join(pkgRelPath, config.SrcPackage, "testdata"))
destTestdata := unit.RepoPath(path.Join(pkgRelPath, config.DestPackage, "testdata"))
ensureDir(t, destTestdata)
srcTestdataPath := path.Join(srcTestdata, config.TestName+".yaml")
destTestdataPath := path.Join(destTestdata, importName+".tmpl.yaml")
t.Logf("Converting %s step %d to %s", srcTestdataPath, config.Step, destTestdataPath)
createImportData(t, srcTestdataPath, destTestdataPath, importName, config.Step, config.VariableReplacments)
}
}
func createImportData(t *testing.T, srcMockFile, destMockFile, importName string, stepNr int, newVars map[string]string) {
t.Helper()
destTestData, _ := path.Split(destMockFile)
require.True(t, strings.HasSuffix(destTestData, "/testdata/"))
ensureDir(t, destTestData)
destOutputDir := ensureDir(t, path.Join(destTestData, importName))
templateMockHTTPData := createImportMockData(t, srcMockFile, destOutputDir, stepNr, newVars)
require.NoError(t, templateMockHTTPData.UpdateVariablesIgnoreChanges(t, newVars))
templateYaml, err := unit.ConfigYaml(templateMockHTTPData)
require.NoError(t, err)
require.NoError(t, os.WriteFile(destMockFile, []byte(templateYaml), 0o600))
}
func createImportMockData(t *testing.T, srcMockFile, destOutputDir string, stepNr int, newVars map[string]string) *unit.MockHTTPData {
t.Helper()
data, err := unit.ParseTestDataConfigYAML(srcMockFile)
require.NoError(t, err)
relevantStep := data.Steps[stepNr-1]
getRequestsInStep := []unit.RequestInfo{}
for _, req := range relevantStep.RequestResponses {
if req.Method == "GET" {
getRequestsInStep = append(getRequestsInStep, req)
}
}
replaceVarsInConfig := func(config string) unit.Literal {
for key, oldValue := range data.Variables {
newValue, ok := newVars[key]
require.True(t, ok, "Missing variable %s in newVars", key)
config = strings.ReplaceAll(config, oldValue, newValue)
}
return unit.Literal(config)
}
templateMockHTTPData := unit.MockHTTPData{
Steps: []unit.StepRequests{
{
Config: replaceVarsInConfig(string(relevantStep.Config)),
},
},
Variables: newVars,
}
for _, req := range getRequestsInStep {
lastResponse := req.Responses[len(req.Responses)-1]
jsonFileName := strings.ReplaceAll(fmt.Sprintf("import_%s.json", req.IDShort()), "/", "_")
jsonFilePath := path.Join(destOutputDir, jsonFileName)
err = os.WriteFile(jsonFilePath, []byte(lastResponse.Text), 0o600)
require.NoError(t, err)
templateReqResponse := unit.RequestInfo{
Path: req.Path,
Method: req.Method,
Version: req.Version,
Text: req.Text,
Responses: []unit.StatusText{
{
Status: lastResponse.Status,
Text: jsonFileName,
},
},
}
templateMockHTTPData.Steps[0].RequestResponses = append(templateMockHTTPData.Steps[0].RequestResponses, templateReqResponse)
}
return &templateMockHTTPData
}
func ensureDir(t *testing.T, dir string) string {
t.Helper()
if !unit.FileExist(dir) {
err := os.Mkdir(dir, 0o755)
require.NoError(t, err)
}
return dir
}