Skip to content

Commit 1952662

Browse files
authored
Update Test Generation: fix RandString + vars overrides (GoogleCloudPlatform#16587)
1 parent ccb380e commit 1952662

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

mmv1/api/resource/step.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ type Step struct {
8787
// "network": nameOfVpc
8888
// ...
8989
// }
90+
// NOTE: Keys in TestVarsOverrides will apply to a matching key in EITHER Vars or PrefixedVars.
9091
TestVarsOverrides map[string]string `yaml:"test_vars_overrides,omitempty"`
9192

9293
// Hash to provider custom override values for generating oics config
9394
// See test_vars_overrides for more details
95+
// NOTE: Keys in OicsVarsOverrides will apply to a matching key in EITHER Vars or PrefixedVars for OiCS generation.
9496
OicsVarsOverrides map[string]string `yaml:"oics_vars_overrides,omitempty"`
9597

9698
// The version name of the test step's version if it's different than the
@@ -119,7 +121,11 @@ func (s *Step) TestStepSlug(productName, resourceName string) string {
119121
}
120122

121123
func (s *Step) Validate(rName, sName string) (es []error) {
122-
// TODO: Add check identifier when it's implemented
124+
for k := range s.Vars {
125+
if _, exists := s.PrefixedVars[k]; exists {
126+
es = append(es, fmt.Errorf("variable key '%s' cannot exist in both 'vars' and 'prefixed_vars' for step '%s' in sample '%s' of resource '%s'", k, s.Name, sName, rName))
127+
}
128+
}
123129
if s.Name == "" {
124130
es = append(es, fmt.Errorf("missing `name` for one step in test sample %s in resource %s", sName, rName))
125131
}
@@ -204,6 +210,10 @@ func (s *Step) SetHCLText(sysfs fs.FS) {
204210
testPrefixedVars[key] = fmt.Sprintf("%s%%{random_suffix}", newVal)
205211
}
206212

213+
for key := range originalVars {
214+
testVars[key] = fmt.Sprintf("%%{%s}", key)
215+
}
216+
207217
// Apply overrides from YAML
208218
for key := range s.TestVarsOverrides {
209219
testPrefixedVars[key] = fmt.Sprintf("%%{%s}", key)
@@ -305,22 +315,26 @@ func SubstituteTestPaths(config string) string {
305315
// Executes step configuration templates for documentation and tests
306316
func (s *Step) SetOiCSHCLText(sysfs fs.FS) {
307317
originalPrefixedVars := s.PrefixedVars
318+
originalVars := s.Vars
308319

309320
// // Remove region tags
310321
re1 := regexp.MustCompile(`# \[[a-zA-Z_ ]+\]\n`)
311322
re2 := regexp.MustCompile(`\n# \[[a-zA-Z_ ]+\]`)
312323

313324
testPrefixedVars := make(map[string]string)
325+
testVars := make(map[string]string)
314326
for key, value := range originalPrefixedVars {
315327
testPrefixedVars[key] = fmt.Sprintf("%s-${local.name_suffix}", value)
316328
}
317329

318330
// Apply overrides from YAML
319331
for key, value := range s.OicsVarsOverrides {
320332
testPrefixedVars[key] = value
333+
testVars[key] = value
321334
}
322335

323336
s.PrefixedVars = testPrefixedVars
337+
s.Vars = testVars
324338
s.OicsHCLText = s.ExecuteTemplate(sysfs)
325339
s.OicsHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(s.OicsHCLText, "\n")
326340

@@ -331,4 +345,5 @@ func (s *Step) SetOiCSHCLText(sysfs fs.FS) {
331345

332346
// Reset the step
333347
s.PrefixedVars = originalPrefixedVars
348+
s.Vars = originalVars
334349
}

mmv1/templates/terraform/samples/base_configs/test_file.go.tmpl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,31 @@ func TestAcc{{ $s.TestSampleSlug $.Res.ProductMetadata.Name $.Res.Name }}(t *tes
8686
})
8787
{{- end }}
8888

89+
{{- $stepCount := len $s.TestSteps }}
90+
{{- if gt $stepCount 1 }}
91+
randomSuffix := acctest.RandString(t, 10)
92+
{{- end }}
93+
8994
{{- range $i, $st := $s.TestSteps }}
9095
{{- if eq $i 0 }}
9196

9297
context := map[string]interface{}{
9398
{{- else }}
99+
94100
context_{{ $i }} := map[string]interface{}{
95101
{{- end }}
96102
{{- template "EnvVarContext" dict "TestEnvVars" $st.TestEnvVars "HasNewLine" false}}
103+
{{- range $varKey, $varVal := $st.Vars }}
104+
"{{$varKey}}": {{ $varVal }},
105+
{{- end }}
97106
{{- range $varKey, $varVal := $st.TestVarsOverrides }}
98-
"{{$varKey}}": {{$varVal}},
107+
"{{$varKey}}": {{ $varVal }},
99108
{{- end }}
109+
{{- if gt $stepCount 1 }}
110+
"random_suffix": randomSuffix,
111+
{{- else }}
100112
"random_suffix": acctest.RandString(t, 10),
113+
{{- end }}
101114
}
102115
{{- end }}
103116

0 commit comments

Comments
 (0)