make tofu test generate dynamic mock values (#2245)

Signed-off-by: ollevche <ollevche@gmail.com>
This commit is contained in:
Oleksandr Levchenkov 2024-12-04 16:22:54 +02:00 committed by GitHub
parent da1ad73f6e
commit e5d26f9a71
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 29 deletions

View File

@ -65,6 +65,7 @@ BUG FIXES:
* Type validation for mocks and overrides are now less strict in `tofu test`. ([#2144](https://github.com/opentofu/opentofu/pull/2144)) * Type validation for mocks and overrides are now less strict in `tofu test`. ([#2144](https://github.com/opentofu/opentofu/pull/2144))
* Skip imports blocks logic on `tofu destroy` ([#2214](https://github.com/opentofu/opentofu/pull/2214)) * Skip imports blocks logic on `tofu destroy` ([#2214](https://github.com/opentofu/opentofu/pull/2214))
* Updated github.com/golang-jwt/jwt/v4 from 4.4.2 to 4.5.1 to make security scanners happy (no vulnerability, see [#2179](https://github.com/opentofu/opentofu/pull/2179)) * Updated github.com/golang-jwt/jwt/v4 from 4.4.2 to 4.5.1 to make security scanners happy (no vulnerability, see [#2179](https://github.com/opentofu/opentofu/pull/2179))
* `tofu test` is now setting `null`s for dynamic type when generating mock values. ([#2245](https://github.com/opentofu/opentofu/pull/2245))
INTERNAL CHANGES: INTERNAL CHANGES:

View File

@ -129,18 +129,7 @@ func (mvc MockValueComposer) composeMockValueForAttributes(schema *configschema.
continue continue
} }
// If there's no value in defaults, we generate our own. mockAttrs[k] = mvc.getMockValueByType(impliedTypes[k])
v, ok := mvc.getMockValueByType(impliedTypes[k])
if !ok {
diags = diags.Append(tfdiags.WholeContainingBody(
tfdiags.Error,
"Failed to generate mock value",
fmt.Sprintf("Mock value cannot be generated for dynamic type. Please specify the `%v` field explicitly in the configuration.", k),
))
continue
}
mockAttrs[k] = v
} }
return mockAttrs, diags return mockAttrs, diags
@ -289,16 +278,10 @@ func (mvc MockValueComposer) getMockValueForBlock(targetType cty.Type, configVal
} }
} }
// getMockValueByType tries to generate mock cty.Value based on provided cty.Type. // getMockValueByType generates mock cty.Value based on provided cty.Type.
// It will return non-ok response if it encounters dynamic type. func (mvc MockValueComposer) getMockValueByType(t cty.Type) cty.Value {
func (mvc MockValueComposer) getMockValueByType(t cty.Type) (cty.Value, bool) {
var v cty.Value var v cty.Value
// just to be sure for cases when the logic below misses something
if t.HasDynamicTypes() {
return cty.Value{}, false
}
switch { switch {
// primitives // primitives
case t.Equals(cty.Number): case t.Equals(cty.Number):
@ -329,24 +312,19 @@ func (mvc MockValueComposer) getMockValueByType(t cty.Type) (cty.Value, bool) {
continue continue
} }
objV, ok := mvc.getMockValueByType(at) objVals[k] = mvc.getMockValueByType(at)
if !ok {
return cty.Value{}, false
}
objVals[k] = objV
} }
v = cty.ObjectVal(objVals) v = cty.ObjectVal(objVals)
case t.IsTupleType(): case t.IsTupleType():
v = cty.EmptyTupleVal v = cty.EmptyTupleVal
// dynamically typed values are not supported // dynamically typed values
default: default:
return cty.Value{}, false v = cty.NullVal(cty.DynamicPseudoType)
} }
return v, true return v
} }
func (mvc MockValueComposer) getMockString() string { func (mvc MockValueComposer) getMockString() string {

View File

@ -528,6 +528,19 @@ func TestComposeMockValueBySchema(t *testing.T) {
}, },
wantError: true, wantError: true,
}, },
"dynamically-typed-values": {
schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"dynamic-field": {
Type: cty.DynamicPseudoType,
Optional: true,
},
},
},
wantVal: cty.ObjectVal(map[string]cty.Value{
"dynamic-field": cty.NullVal(cty.DynamicPseudoType),
}),
},
} }
for name, test := range tests { for name, test := range tests {