Skip to content

Commit

Permalink
test for lookup regression during validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jbardin committed Dec 15, 2021
1 parent 71b9682 commit 645fcc5
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions internal/terraform/context_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2031,3 +2031,60 @@ resource "test_object" "t" {
t.Fatal(diags.ErrWithWarnings())
}
}

func TestContext2Plan_lookupMismatchedObjectTypes(t *testing.T) {
p := new(MockProvider)
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
"test_instance": {
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
Computed: true,
},
"things": {
Type: cty.List(cty.String),
Optional: true,
},
},
},
},
})

m := testModuleInline(t, map[string]string{
"main.tf": `
variable "items" {
type = list(string)
default = []
}
resource "test_instance" "a" {
for_each = length(var.items) > 0 ? { default = {} } : {}
}
output "out" {
// Strictly speaking, this expression is incorrect because the map element
// type is a different type from the default value, and the lookup
// implementation expects to be able to convert the default to match the
// element type.
// There are two reasons this works which we need to maintain for
// compatibility. First during validation the 'test_instance.a' expression
// only returns a dynamic value, preventing any type comparison. Later during
// plan and apply 'test_instance.a' is an object and not a map, and the
// lookup implementation skips the type comparison when the keys are known
// statically.
value = lookup(test_instance.a, "default", { id = null })["id"]
}
`})

ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})

diags := ctx.Validate(m)
if diags.HasErrors() {
t.Fatal(diags.ErrWithWarnings())
}
}

0 comments on commit 645fcc5

Please sign in to comment.