Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to manage values for variables #55

Open
baolsen opened this issue Oct 14, 2022 · 1 comment
Open

Ability to manage values for variables #55

baolsen opened this issue Oct 14, 2022 · 1 comment

Comments

@baolsen
Copy link
Contributor

baolsen commented Oct 14, 2022

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 the tests/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.

@kapilt
Copy link
Contributor

kapilt commented Dec 20, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants