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

Environment variables wiped out #935

Closed
pmenglund opened this issue Apr 11, 2022 · 3 comments · Fixed by #937
Closed

Environment variables wiped out #935

pmenglund opened this issue Apr 11, 2022 · 3 comments · Fixed by #937
Assignees
Labels
bug Something isn't working subsystem/tests Issues and feature requests related to the testing framework.
Milestone

Comments

@pmenglund
Copy link

In 1c932cf this change was made:
internal/plugintest/helper.go:

	err = tf.SetEnv(map[string]string{
 		EnvCheckpointDisable: "1",
 	})

Which now causes the environment to be wiped, as once tf.SetEnv() is called it no longer copies os.Environ

	tf := Terraform{
		execPath:   execPath,
		workingDir: workingDir,
		env:        nil, // explicit nil means copy os.Environ
		logger:     log.New(ioutil.Discard, "", 0),
	}

Reverting back to v2.12.0 fixes the issue.

SDK version

v2.13.0

Relevant provider source code

n/a

Terraform Configuration Files

Set the environment variable TF_VAR_TESTING and use

variable TESTING {
  type = string
}

Debug Output

    resource_gcs_integration_test.go:20: Step 1/1 error: Error running pre-apply refresh: variable "TESTING" was required but not supplied

Expected Behavior

The environment variable TF_VAR_TESTING should be available

Actual Behavior

    resource_xxx_integration_test.go:20: Step 1/1 error: Error running pre-apply refresh: variable "TESTING" was required but not supplied

Steps to Reproduce

  1. create a test which uses an environment variable2.
  2. run the test and the environment it wiped

References

Introduced in 1c932cf

@pmenglund pmenglund added the bug Something isn't working label Apr 11, 2022
@bflad bflad self-assigned this Apr 11, 2022
@bflad bflad added this to the v2.14.0 milestone Apr 11, 2022
@bflad
Copy link
Contributor

bflad commented Apr 11, 2022

Hi @pmenglund 👋 Thank you for raising this and sorry you ran into trouble here. Your assessment here is correct where previously all environment variables were copied to Terraform CLI during acceptance testing. While using TF_VARS_ environment variables can potentially be problematic in parallel testing scenarios, there is not currently a good alternative short of fmt.Sprintf() with the configuration and removing this capability was certainly a regression introduced in 2.13.0 which will be fixed.

@bflad bflad added the subsystem/tests Issues and feature requests related to the testing framework. label Apr 11, 2022
@pmenglund
Copy link
Author

I'm using it to inject GCP credentials into the provider. Is the recommended way to templatize it and use something like this instead

variable TESTING {
  type = string
  default = "{{ .Testing }}"
}

bflad added a commit that referenced this issue Apr 12, 2022
…terraform-exec

Reference: #935

When using `(tfexec.Terraform).SetEnv()`, it will no longer copy `os.Environ()`. It is also not possible for the code to call `tfexec.CleanEnv(os.Environ())` with the additional environment variable as it would strip previously accepted `TF_VAR_` environment variables. There are no plans to change this behavior in terraform-exec.
bflad added a commit that referenced this issue Apr 12, 2022
…terraform-exec

Reference: #935

When using `(tfexec.Terraform).SetEnv()`, it will no longer copy `os.Environ()`. It is also not possible for the code to call `tfexec.CleanEnv(os.Environ())` with the additional environment variable as it would strip previously accepted `TF_VAR_` environment variables. There are no plans to change this behavior in terraform-exec.
bflad added a commit that referenced this issue Apr 12, 2022
…terraform-exec (#937)

Reference: #935

When using `(tfexec.Terraform).SetEnv()`, it will no longer copy `os.Environ()`. It is also not possible for the code to call `tfexec.CleanEnv(os.Environ())` with the additional environment variable as it would strip previously accepted `TF_VAR_` environment variables. There are no plans to change this behavior in terraform-exec.
bflad added a commit to hashicorp/terraform-provider-corner that referenced this issue Apr 13, 2022
Reference: hashicorp/terraform-plugin-sdk#935

With github.com/hashicorp/terraform-plugin-sdk/v2@v2.13.0:

```console
$ TF_ACC=1 go test -count=1 -run='TestAccFrameworkResourceUser_TF_VAR_Environment_Variable' -v ./internal/frameworkprovider/
=== RUN   TestAccFrameworkResourceUser_TF_VAR_Environment_Variable
    resource_user_test.go:117: Step 1/1 error: Error running pre-apply refresh: variable "framework_user_name" was required but not supplied
--- FAIL: TestAccFrameworkResourceUser_TF_VAR_Environment_Variable (0.56s)
FAIL
FAIL    github.com/hashicorp/terraform-provider-corner/internal/frameworkprovider       1.324s
FAIL
```

Passes with hashicorp/terraform-plugin-sdk#937.
bflad added a commit to hashicorp/terraform-provider-corner that referenced this issue Apr 15, 2022
Reference: hashicorp/terraform-plugin-sdk#935

With github.com/hashicorp/terraform-plugin-sdk/v2@v2.13.0:

```console
$ TF_ACC=1 go test -count=1 -run='TestAccFrameworkResourceUser_TF_VAR_Environment_Variable' -v ./internal/frameworkprovider/
=== RUN   TestAccFrameworkResourceUser_TF_VAR_Environment_Variable
    resource_user_test.go:117: Step 1/1 error: Error running pre-apply refresh: variable "framework_user_name" was required but not supplied
--- FAIL: TestAccFrameworkResourceUser_TF_VAR_Environment_Variable (0.56s)
FAIL
FAIL    github.com/hashicorp/terraform-provider-corner/internal/frameworkprovider       1.324s
FAIL
```

Passes with hashicorp/terraform-plugin-sdk#937.
bflad added a commit to hashicorp/terraform-provider-corner that referenced this issue Apr 15, 2022
Reference: hashicorp/terraform-plugin-sdk#935

With github.com/hashicorp/terraform-plugin-sdk/v2@v2.13.0:

```console
$ TF_ACC=1 go test -count=1 -run='TestAccFrameworkResourceUser_TF_VAR_Environment_Variable' -v ./internal/frameworkprovider/
=== RUN   TestAccFrameworkResourceUser_TF_VAR_Environment_Variable
    resource_user_test.go:117: Step 1/1 error: Error running pre-apply refresh: variable "framework_user_name" was required but not supplied
--- FAIL: TestAccFrameworkResourceUser_TF_VAR_Environment_Variable (0.56s)
FAIL
FAIL    github.com/hashicorp/terraform-provider-corner/internal/frameworkprovider       1.324s
FAIL
```
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working subsystem/tests Issues and feature requests related to the testing framework.
Projects
None yet
2 participants