Hello,
I’m fighting with my Terraform test code without any success in making it working, despite my reading of Using sensitive values in subsequent run blocks of terraform test not possible: value has marks · Issue #34185 · hashicorp/terraform · GitHub so requesting your kind help.
I don’t manage to pass a secret from a run block to another (2 different modules) in Terraform test
Basically my context is as follow (simplified):
Terraform v1.7.5 on windows_amd64
- provider Terraform Registry v2.47.0
serviceprincipal/main.tf
variable "owners" {
type = list(string)
}
variable "secret_keys" {
type = list(string)
}
resource "azuread_application" "app_registration" {
display_name = "myapp"
identifier_uris = ["http://${local.name}.example.com"]
owners = [ var.owners ]
sign_in_audience = "AzureADMyOrg"
}
resource "azuread_application_password" "app_secret" {
for_each = toset(var.secret_keys)
application_id = azuread_application.app_registration.id
display_name = each.key
}
output "app_secret_by_key" {
value = azuread_application_password.app_secret
sensitive = true
}
testedmodule/main.tf
variable "service_principal" {
type = object({
app_secret = object({
key_id = string
value = string # This is the sensitive data
})
})
sensitive = true
}
# ...
testedmodule/tests/unit.tftest.hcl
run "setup_serviceprincipal" {
command = apply
variables {
owners = [
# Useless stuff...
]
secret_purposes = [ "mysecret" ]
}
module {
source = "../serviceprincipal"
}
}
run "testedmodule" {
command = apply
variables {
service_principal = {
app_secret = run.setup_serviceprincipal.app_secret_by_key[ "mysecret" ]
}
}
# assert {...
When I run the “terraform init” then “terraform test”, it gives me:
$ terraform test
tests\unit.tftest.hcl... in progress
run "setup_serviceprincipal"... pass
run "testedmodule"... fail
╷
│ Error: Failed to prepare variable value for plan
│
│ The value for variable "service_principal" could not be serialized to store in the plan: value has marks, so it cannot be
│ serialized.
Am I doing something wrong ?
Note: If I’m changing the app_secret = run.setup_serviceprincipal.app_secret_by_key[ "mysecret" ]
part to an hardcoded value, it works