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

helper/resource: Add TF_ACC_LOG, TF_LOG_CORE, and TF_LOG_PROVIDER environment variable handling for Terraform versions 0.15 and later #993

Merged
merged 6 commits into from Jul 5, 2022

Conversation

bflad
Copy link
Member

@bflad bflad commented Jun 30, 2022

Closes #992

This enables provider developers to have full control over Terraform CLI logging environment variables with the acceptance testing framework in helper/resource.

In Terraform CLI 0.15 and later, the logging environment variables work as the following:

Environment Variable Description
TF_LOG Global logging level for core and providers. Overrides TF_LOG_CORE (but not TF_LOG_PROVIDER) if set.
TF_LOG_CORE Logging level for core functionality. No effect if TF_LOG is set.
TF_LOG_PROVIDER Logging level for provider logging sent over plugin file descriptors.

To execute Terraform CLI during acceptance testing, it uses terraform-exec for all interactions. terraform-exec has certain environment variable handling rules associated with it, to prevent issues with reading stdout/stderr output from calling Terraform CLI since it needs to be able to parse that information. It was recently updated to support setting TF_LOG, TF_LOG_CORE, and TF_LOG_PROVIDER environment variables when calling Terraform, in addition to the prior support for setting TF_LOG_PATH, which defaults to always setting TF_LOG to TRACE if unset.

This enhancement allows the acceptance testing framework to accept the TF_ACC_LOG environment variable to appropriately set the TF_LOG environment variable in terraform-exec, if Terraform is version 0.15 or later. We could not change the behavior of the existing TF_LOG environment variable as it would cause undesirable side effects with the existing testing framework handling for the Go standard library log package. The TF_ACC_LOG naming follows the convention of TF_ACC_LOG_PATH setting TF_LOG_PATH. The TF_LOG value defaults to TRACE, similar to before, when TF_ACC_LOG_PATH or TF_LOG_PATH_MASK environment variables are set.

This also enables the passthrough of TF_LOG_CORE and TF_LOG_PROVIDER environment variables to terraform-exec. This is required because terraform-exec will otherwise default TF_LOG to TRACE if TF_LOG_PATH is used, meaning that without this, provider developers could not disable Terraform core logging. This enhancement opts to not prefix the variables with TF_ACC_ as there's no prior behaviors to protect and the differing environment variable names for acceptance testing is already confusing enough for everyone. They could still be prefixed for consistency with TF_ACC_LOG though, if desired.

To summarize the state of logging environment variable with the acceptance testing framework after this change:

Environment Variable Description
TF_ACC_LOG Controls the Terraform CLI TF_LOG environment variable
TF_ACC_LOG_PATH Controls the Terraform CLI TF_LOG_PATH environment variable and where terraform-plugin-log SDK/provider loggers will write logs
TF_LOG Controls the Go standard library log package log level; Any level enables terraform-plugin-log SDK/provider loggers at TRACE currently (they have to be tuned down individually at the moment)
TF_LOG_CORE Controls the Terraform CLI TF_LOG_CORE environment variable
TF_LOG_PATH_MASK Act similar to TF_ACC_LOG_PATH, but replaces a %s placeholder in the value with individual test names ((testing.T).Name())
TF_LOG_PROVIDER Controls the Terraform CLI TF_LOG_PROVIDER environment variable

@bflad bflad added enhancement New feature or request subsystem/tests Issues and feature requests related to the testing framework. labels Jun 30, 2022
@bflad bflad added this to the v2.18.0 milestone Jun 30, 2022
bflad added 2 commits July 1, 2022 10:25
…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.
@bflad bflad changed the title helper/resource: Added TF_LOG environment variable passthrough to Terraform during acceptance testing on versions 0.15 and later helper/resource: Add TF_ACC_LOG, TF_LOG_CORE, and TF_LOG_PROVIDER environment variable handling for Terraform versions 0.15 and later Jul 1, 2022
@bflad bflad marked this pull request as ready for review July 1, 2022 19:28
@bflad bflad requested review from a team as code owners July 1, 2022 19:28
Copy link
Contributor

@bendbennett bendbennett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some typos, LGTM

internal/logging/keys.go Outdated Show resolved Hide resolved
Co-authored-by: Ivan De Marino <ivan.demarino@hashicorp.com>
@bflad bflad merged commit a9d04ec into main Jul 5, 2022
@bflad bflad deleted the bflad-tf-log branch July 5, 2022 18:33
@github-actions
Copy link

github-actions bot commented Aug 5, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, 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 Aug 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request subsystem/tests Issues and feature requests related to the testing framework.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow TF_LOG/TF_LOG_CORE/TF_LOG_PROVIDER to be set correctly in Acceptance Tests
3 participants