diff --git a/mmv1/api/resource/step.go b/mmv1/api/resource/step.go index dc6d4d5ccc44..c4c1ff7e19e3 100644 --- a/mmv1/api/resource/step.go +++ b/mmv1/api/resource/step.go @@ -87,10 +87,12 @@ type Step struct { // "network": nameOfVpc // ... // } + // NOTE: Keys in TestVarsOverrides will apply to a matching key in EITHER Vars or PrefixedVars. TestVarsOverrides map[string]string `yaml:"test_vars_overrides,omitempty"` // Hash to provider custom override values for generating oics config // See test_vars_overrides for more details + // NOTE: Keys in OicsVarsOverrides will apply to a matching key in EITHER Vars or PrefixedVars for OiCS generation. OicsVarsOverrides map[string]string `yaml:"oics_vars_overrides,omitempty"` // 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 { } func (s *Step) Validate(rName, sName string) (es []error) { - // TODO: Add check identifier when it's implemented + for k := range s.Vars { + if _, exists := s.PrefixedVars[k]; exists { + 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)) + } + } if s.Name == "" { es = append(es, fmt.Errorf("missing `name` for one step in test sample %s in resource %s", sName, rName)) } @@ -204,6 +210,10 @@ func (s *Step) SetHCLText(sysfs fs.FS) { testPrefixedVars[key] = fmt.Sprintf("%s%%{random_suffix}", newVal) } + for key := range originalVars { + testVars[key] = fmt.Sprintf("%%{%s}", key) + } + // Apply overrides from YAML for key := range s.TestVarsOverrides { testPrefixedVars[key] = fmt.Sprintf("%%{%s}", key) @@ -305,12 +315,14 @@ func SubstituteTestPaths(config string) string { // Executes step configuration templates for documentation and tests func (s *Step) SetOiCSHCLText(sysfs fs.FS) { originalPrefixedVars := s.PrefixedVars + originalVars := s.Vars // // Remove region tags re1 := regexp.MustCompile(`# \[[a-zA-Z_ ]+\]\n`) re2 := regexp.MustCompile(`\n# \[[a-zA-Z_ ]+\]`) testPrefixedVars := make(map[string]string) + testVars := make(map[string]string) for key, value := range originalPrefixedVars { testPrefixedVars[key] = fmt.Sprintf("%s-${local.name_suffix}", value) } @@ -318,9 +330,11 @@ func (s *Step) SetOiCSHCLText(sysfs fs.FS) { // Apply overrides from YAML for key, value := range s.OicsVarsOverrides { testPrefixedVars[key] = value + testVars[key] = value } s.PrefixedVars = testPrefixedVars + s.Vars = testVars s.OicsHCLText = s.ExecuteTemplate(sysfs) s.OicsHCLText = regexp.MustCompile(`\n\n$`).ReplaceAllString(s.OicsHCLText, "\n") @@ -331,4 +345,5 @@ func (s *Step) SetOiCSHCLText(sysfs fs.FS) { // Reset the step s.PrefixedVars = originalPrefixedVars + s.Vars = originalVars } diff --git a/mmv1/templates/terraform/samples/base_configs/test_file.go.tmpl b/mmv1/templates/terraform/samples/base_configs/test_file.go.tmpl index 86ac50bcaa31..0a35cacc785c 100644 --- a/mmv1/templates/terraform/samples/base_configs/test_file.go.tmpl +++ b/mmv1/templates/terraform/samples/base_configs/test_file.go.tmpl @@ -86,18 +86,31 @@ func TestAcc{{ $s.TestSampleSlug $.Res.ProductMetadata.Name $.Res.Name }}(t *tes }) {{- end }} + {{- $stepCount := len $s.TestSteps }} + {{- if gt $stepCount 1 }} + randomSuffix := acctest.RandString(t, 10) + {{- end }} + {{- range $i, $st := $s.TestSteps }} {{- if eq $i 0 }} context := map[string]interface{}{ {{- else }} + context_{{ $i }} := map[string]interface{}{ {{- end }} {{- template "EnvVarContext" dict "TestEnvVars" $st.TestEnvVars "HasNewLine" false}} + {{- range $varKey, $varVal := $st.Vars }} + "{{$varKey}}": {{ $varVal }}, + {{- end }} {{- range $varKey, $varVal := $st.TestVarsOverrides }} - "{{$varKey}}": {{$varVal}}, + "{{$varKey}}": {{ $varVal }}, {{- end }} + {{- if gt $stepCount 1 }} + "random_suffix": randomSuffix, + {{- else }} "random_suffix": acctest.RandString(t, 10), + {{- end }} } {{- end }}