Skip to content

Commit

Permalink
helper/resource: Added TF_LOG environment variable passthrough to T…
Browse files Browse the repository at this point in the history
…erraform during acceptance testing on versions 0.15 and later

Reference: hashicorp/terraform-exec#291
Reference: #992

This allows the acceptance testing framework to appropriately set the `TF_LOG` environment variable via `terraform-exec`, if Terraform is version 0.15 or later. Otherwise, defaults to `TRACE` similar to before when `TF_ACC_LOG_PATH` or `TF_LOG_PATH_MASK` environment variables are set.
  • Loading branch information
bflad committed Jun 30, 2022
1 parent 8486195 commit af72fc0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/pending.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
helper/resource: Added `TF_LOG` environment variable passthrough to Terraform during acceptance testing on versions 0.15 and later
```
3 changes: 3 additions & 0 deletions internal/logging/keys.go
Expand Up @@ -31,6 +31,9 @@ const (
// The TestStep number of the test being executed. Starts at 1.
KeyTestStepNumber = "test_step_number"

// The Terraform CLI logging level (TF_LOG) for used for an acceptance test.
KeyTestTerraformLogLevel = "test_terraform_log_level"

// The path to the Terraform CLI logging file used for an acceptance test.
//
// This should match where the rest of the acceptance test logs are going
Expand Down
12 changes: 12 additions & 0 deletions internal/plugintest/environment_variables.go
Expand Up @@ -18,6 +18,18 @@ const (
// If TF_LOG_PATH_MASK is set, it takes precedence over this value.
EnvTfAccLogPath = "TF_ACC_LOG_PATH"

// Environment variable with level to filter Terraform logs during
// acceptance testing. This value sets TF_LOG in a safe manner when
// executing Terraform CLI commands, which would otherwise be ignored
// since it could interfere with how the underlying execution is performed.
//
// If not set, but TF_ACC_LOG_PATH or TF_LOG_PATH_MASK is set, it defaults
// to TRACE. If Terraform CLI is version 0.14 or earlier, it will have no
// separate affect from the TF_ACC_LOG_PATH or TF_LOG_PATH_MASK behavior,
// as those earlier versions of Terraform are unreliable with the logging
// level being outside TRACE.
EnvTfLog = "TF_LOG"

// Environment variable with path containing the string %s, which is
// replaced with the test name, to save separate Terraform logs during
// acceptance testing. This value sets TF_LOG_PATH in a safe manner when
Expand Down
22 changes: 22 additions & 0 deletions internal/plugintest/helper.go
Expand Up @@ -139,6 +139,28 @@ func (h *Helper) NewWorkingDir(ctx context.Context, t TestControl) (*WorkingDir,
return nil, fmt.Errorf("unable to disable terraform-exec provider verification: %w", err)
}

if tfLog := os.Getenv(EnvTfLog); tfLog != "" {
logging.HelperResourceTrace(
ctx,
fmt.Sprintf("Setting terraform-exec log level via %s environment variable, if Terraform CLI is version 0.15 or later", EnvTfLog),
map[string]interface{}{logging.KeyTestTerraformLogLevel: tfLog},
)

err := tf.SetLog(tfLog)

if err != nil {
if !errors.As(err, new(*tfexec.ErrVersionMismatch)) {
return nil, fmt.Errorf("unable to set terraform-exec log level (%s): %w", tfLog, err)
}

logging.HelperResourceWarn(
ctx,
fmt.Sprintf("Unable to set terraform-exec log level via %s environment variable, as Terraform CLI is version 0.14 or earlier. It will default to TRACE.", EnvTfLog),
map[string]interface{}{logging.KeyTestTerraformLogLevel: "TRACE"},
)
}
}

var logPath, logPathEnvVar string

if tfAccLogPath := os.Getenv(EnvTfAccLogPath); tfAccLogPath != "" {
Expand Down

0 comments on commit af72fc0

Please sign in to comment.