fix mock type conversion in tofu test (#2144)

Signed-off-by: ollevche <ollevche@gmail.com>
This commit is contained in:
Oleksandr Levchenkov 2024-11-20 17:47:13 +02:00 committed by GitHub
parent 2758f2cfbf
commit 7645992d9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 8 deletions

View File

@ -57,6 +57,7 @@ BUG FIXES:
* The `yamldecode` function's interpretation of scalars as numbers now conforms to the YAML 1.2 specification. In particular, the scalar value `+` is now interpreted as the string `"+"` rather than returning a parse error trying to interpret it as an integer. ([#2044](https://github.com/opentofu/opentofu/pull/2044)) * The `yamldecode` function's interpretation of scalars as numbers now conforms to the YAML 1.2 specification. In particular, the scalar value `+` is now interpreted as the string `"+"` rather than returning a parse error trying to interpret it as an integer. ([#2044](https://github.com/opentofu/opentofu/pull/2044))
* A `module` block's `version` argument now accepts prerelease version selections using a "v" prefix before the version number. Previously this was accepted only for non-prerelease selections. ([#2124])(https://github.com/opentofu/opentofu/issues/2124) * A `module` block's `version` argument now accepts prerelease version selections using a "v" prefix before the version number. Previously this was accepted only for non-prerelease selections. ([#2124])(https://github.com/opentofu/opentofu/issues/2124)
* The `tofu test` command doesn't try to validate mock provider definition by its underlying provider schema now. ([#2140](https://github.com/opentofu/opentofu/pull/2140)) * The `tofu test` command doesn't try to validate mock provider definition by its underlying provider schema now. ([#2140](https://github.com/opentofu/opentofu/pull/2140))
* Type validation for mocks and overrides are now less strict in `tofu test`. ([#2144](https://github.com/opentofu/opentofu/pull/2144))
INTERNAL CHANGES: INTERNAL CHANGES:

View File

@ -10,6 +10,7 @@ import (
"github.com/opentofu/opentofu/internal/configs/configschema" "github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/tfdiags" "github.com/opentofu/opentofu/internal/tfdiags"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
) )
// MockValueComposer provides different ways to generate mock values based on // MockValueComposer provides different ways to generate mock values based on
@ -111,19 +112,18 @@ func (mvc MockValueComposer) composeMockValueForAttributes(schema *configschema.
// If the attribute is computed and not configured, // If the attribute is computed and not configured,
// we use provided value from defaults. // we use provided value from defaults.
if ov, ok := defaults[k]; ok { if ov, ok := defaults[k]; ok {
typeConformanceErrs := ov.Type().TestConformance(attr.Type) converted, err := convert.Convert(ov, attr.Type)
if len(typeConformanceErrs) == 0 { if err != nil {
mockAttrs[k] = ov
continue
}
for _, err := range typeConformanceErrs {
diags = diags.Append(tfdiags.WholeContainingBody( diags = diags.Append(tfdiags.WholeContainingBody(
tfdiags.Warning, tfdiags.Warning,
fmt.Sprintf("Ignored mock/override field `%v`", k), fmt.Sprintf("Ignored mock/override field `%v`", k),
fmt.Sprintf("Values provided for override / mock must match resource fields types: %v.", err), fmt.Sprintf("Values provided for override / mock must match resource fields types: %v.", tfdiags.FormatError(err)),
)) ))
continue
} }
mockAttrs[k] = converted
continue
} }
// If there's no value in defaults, we generate our own. // If there's no value in defaults, we generate our own.

View File

@ -494,6 +494,26 @@ func TestComposeMockValueBySchema(t *testing.T) {
}), }),
wantWarning: true, // ignored value in defaults wantWarning: true, // ignored value in defaults
}, },
"type-conversion": {
schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"computed-list": {
Type: cty.List(cty.String),
Computed: true,
},
},
},
defaults: map[string]cty.Value{
"computed-list": cty.TupleVal([]cty.Value{
cty.StringVal("str"),
}),
},
wantVal: cty.ObjectVal(map[string]cty.Value{
"computed-list": cty.ListVal([]cty.Value{
cty.StringVal("str"),
}),
}),
},
} }
for name, test := range tests { for name, test := range tests {