test that module outputs maintain sensitive marks

This commit is contained in:
James Bardin 2023-03-20 13:46:15 -04:00
parent d33e627514
commit defd7f0cde

View File

@ -1887,3 +1887,70 @@ output "null_module_test" {
_, diags = ctx.Apply(plan, m)
assertNoErrors(t, diags)
}
func TestContext2Apply_moduleOutputWithSensitiveAttrs(t *testing.T) {
// Ensure that nested sensitive marks are stored when accessing non-root
// module outputs, and that they do not cause the entire output value to
// become sensitive.
m := testModuleInline(t, map[string]string{
"main.tf": `
module "mod" {
source = "./mod"
}
resource "test_resource" "b" {
// if the module output were wholly sensitive it would not be valid to use in
// for_each
for_each = module.mod.resources
value = each.value.output
}
output "root_output" {
// The root output cannot contain any sensitive marks at all.
// Applying nonsensitive would fail here if the nested sensitive mark were
// not maintained through the output.
value = [ for k, v in module.mod.resources : nonsensitive(v.output) ]
}
`,
"./mod/main.tf": `
resource "test_resource" "a" {
for_each = {"key": "value"}
value = each.key
}
output "resources" {
value = test_resource.a
}
`,
})
p := testProvider("test")
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
"test_resource": {
Attributes: map[string]*configschema.Attribute{
"value": {
Type: cty.String,
Required: true,
},
"output": {
Type: cty.String,
Sensitive: true,
Computed: true,
},
},
},
},
})
ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})
plan, diags := ctx.Plan(m, states.NewState(), &PlanOpts{
Mode: plans.NormalMode,
})
assertNoErrors(t, diags)
_, diags = ctx.Apply(plan, m)
assertNoErrors(t, diags)
}