Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion mmv1/api/resource/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -305,22 +315,26 @@ 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)
}

// 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")

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

// Reset the step
s.PrefixedVars = originalPrefixedVars
s.Vars = originalVars
}
15 changes: 14 additions & 1 deletion mmv1/templates/terraform/samples/base_configs/test_file.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
Loading