diff --git a/.changelog/pending.txt b/.changelog/pending.txt new file mode 100644 index 00000000000..ee78ae50f52 --- /dev/null +++ b/.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 +``` diff --git a/internal/logging/keys.go b/internal/logging/keys.go index 2ba548f61d5..cb0e9876edf 100644 --- a/internal/logging/keys.go +++ b/internal/logging/keys.go @@ -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 diff --git a/internal/plugintest/environment_variables.go b/internal/plugintest/environment_variables.go index ac75151a4e8..6aaf205cba5 100644 --- a/internal/plugintest/environment_variables.go +++ b/internal/plugintest/environment_variables.go @@ -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 diff --git a/internal/plugintest/helper.go b/internal/plugintest/helper.go index d5aecd87bce..d3daa696097 100644 --- a/internal/plugintest/helper.go +++ b/internal/plugintest/helper.go @@ -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 != "" {