pytest-terraform
pytest-terraform copied to clipboard
Ability to manage values for variables
Can be assigned to me, I would just like some feedback on the issue / approach first.
It would be good to specify and override values for Terraform variables when testing.
Currently we don't see a good way to do this with pytest-terraform
.
Apparent options:
- Setting
os.environ[TF_VAR_x] = "foo"
before the test seems to work in some cases and not in others. It's a bit flaky. - Writing
terraform.tfvars
to thetests/terraform/my_test
folder and exclude it from source control. This works but is a bit clunky as we need to ensure variable values are written correctly to the file, eg if they are of complex type.
The specific use case we have involves providing at least a value for the AWS profile that the Terraform AWS provider needs to use at run time, so that we can authenticate to the correct environment.
pytest --profile my_ci_account_profile
=> We add a fixture called "profile" to read in this value
=> We write terraform.tfvars
with profile = "my_ci_account_profile"
Our terraform test code:
# variables.tf:
variable "profile" {
description = "AWS profile"
type = string
}
# providers.tf:
provider "aws" {
region = "us-east-1"
profile = var.profile # Dynamically use an AWS profile at runtime
}
My particular use case is complicated because the profile is only known at runtime.
Perhaps something like this could work, for our use case:
@terraform("main", scope="session", variables={profile=os.environ.get["profile"]})
Perhaps this feature could even use pytest parameterize to iteratively test values of variables under different scenarios.
variables support sounds great. re options listed, env vars should work, we typically straight pass through with some augmentation (plugin cache, data dir, in automation TF env vars) on each subprocess. I'm curious what your seeing here wrt to flakey. wrt to write out of variable files, we do keep a separate work dir for state that we could drop variables file into rather than dealing with source code exclusion.
re first steps mod'ing tf runner to accept variables for apply & plan with var file write out to temp dir seems like a good option. and then updating the decorator to support passing in values sounds good. re parameterize support, sounds cool, although there might be a rabbit hole there.