diff --git a/CHANGELOG.md b/CHANGELOG.md index b7107e968d..2b34be5162 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) * 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)) +* Type validation for mocks and overrides are now less strict in `tofu test`. ([#2144](https://github.com/opentofu/opentofu/pull/2144)) INTERNAL CHANGES: diff --git a/internal/configs/hcl2shim/mock_value_composer.go b/internal/configs/hcl2shim/mock_value_composer.go index bbd51e244b..fd0e32b750 100644 --- a/internal/configs/hcl2shim/mock_value_composer.go +++ b/internal/configs/hcl2shim/mock_value_composer.go @@ -10,6 +10,7 @@ import ( "github.com/opentofu/opentofu/internal/configs/configschema" "github.com/opentofu/opentofu/internal/tfdiags" "github.com/zclconf/go-cty/cty" + "github.com/zclconf/go-cty/cty/convert" ) // 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, // we use provided value from defaults. if ov, ok := defaults[k]; ok { - typeConformanceErrs := ov.Type().TestConformance(attr.Type) - if len(typeConformanceErrs) == 0 { - mockAttrs[k] = ov - continue - } - - for _, err := range typeConformanceErrs { + converted, err := convert.Convert(ov, attr.Type) + if err != nil { diags = diags.Append(tfdiags.WholeContainingBody( tfdiags.Warning, 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. diff --git a/internal/configs/hcl2shim/mock_value_composer_test.go b/internal/configs/hcl2shim/mock_value_composer_test.go index f4590506a6..a3725030db 100644 --- a/internal/configs/hcl2shim/mock_value_composer_test.go +++ b/internal/configs/hcl2shim/mock_value_composer_test.go @@ -494,6 +494,26 @@ func TestComposeMockValueBySchema(t *testing.T) { }), 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 {