Skip to content

Commit

Permalink
helper/resource: Add terraform plan output to TRACE logging (#1058)
Browse files Browse the repository at this point in the history
Reference: #698

For example:

```console
$ TF_ACC=1 TF_LOG=TRACE go test -count=1 -run='TestTest_TestStep_ExternalProviders_DifferentVersions' -v ./helper/resource
...
2022-09-13T22:27:17.924-0400 [TRACE] sdk.helper_resource: Created plan with changes:
  test_terraform_plan=
  |
  | Terraform used the selected providers to generate the following execution
  | plan. Resource actions are indicated with the following symbols:
  |   + create
  |
  | Terraform will perform the following actions:
  |
  |   # null_resource.test will be created
  |   + resource "null_resource" "test" {
  |       + id = (known after apply)
  |     }
  |
  | Plan: 1 to add, 0 to change, 0 to destroy.
   test_name=TestTest_TestStep_ExternalProviders_DifferentVersions test_step_number=1 test_terraform_path=/opt/homebrew/bin/terraform test_working_directory=/var/folders/f3/2mhr8hkx72z9dllv0ry81zm40000gq/T/plugintest2492851432
...
2022-09-13T22:27:18.816-0400 [TRACE] sdk.helper_resource: Created plan with no changes: test_working_directory=/var/folders/f3/2mhr8hkx72z9dllv0ry81zm40000gq/T/plugintest2492851432 test_name=TestTest_TestStep_ExternalProviders_DifferentVersions test_step_number=2 test_terraform_path=/opt/homebrew/bin/terraform
...
```
  • Loading branch information
bflad committed Sep 14, 2022
1 parent a096f3a commit 0f41bb0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/1058.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
helper/resource: Added `terraform plan` output to `TRACE` logging
```
3 changes: 3 additions & 0 deletions internal/logging/keys.go
Expand Up @@ -49,6 +49,9 @@ const (
// The path to the Terraform CLI used for an acceptance test.
KeyTestTerraformPath = "test_terraform_path"

// Terraform plan output generated during a TestStep.
KeyTestTerraformPlan = "test_terraform_plan"

// The working directory of the acceptance test.
KeyTestWorkingDirectory = "test_working_directory"
)
44 changes: 40 additions & 4 deletions internal/plugintest/working_dir.go
Expand Up @@ -174,23 +174,59 @@ func (wd *WorkingDir) planFilename() string {
func (wd *WorkingDir) CreatePlan(ctx context.Context) error {
logging.HelperResourceTrace(ctx, "Calling Terraform CLI plan command")

_, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out(PlanFileName))
hasChanges, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out(PlanFileName))

logging.HelperResourceTrace(ctx, "Called Terraform CLI plan command")

return err
if err != nil {
return err
}

if !hasChanges {
logging.HelperResourceTrace(ctx, "Created plan with no changes")

return nil
}

stdout, err := wd.SavedPlanRawStdout(ctx)

if err != nil {
return fmt.Errorf("error retrieving formatted plan output: %w", err)
}

logging.HelperResourceTrace(ctx, "Created plan with changes", map[string]any{logging.KeyTestTerraformPlan: stdout})

return nil
}

// CreateDestroyPlan runs "terraform plan -destroy" to create a saved plan
// file, which if successful will then be used for the next call to Apply.
func (wd *WorkingDir) CreateDestroyPlan(ctx context.Context) error {
logging.HelperResourceTrace(ctx, "Calling Terraform CLI plan -destroy command")

_, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out(PlanFileName), tfexec.Destroy(true))
hasChanges, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out(PlanFileName), tfexec.Destroy(true))

logging.HelperResourceTrace(ctx, "Called Terraform CLI plan -destroy command")

return err
if err != nil {
return err
}

if !hasChanges {
logging.HelperResourceTrace(ctx, "Created destroy plan with no changes")

return nil
}

stdout, err := wd.SavedPlanRawStdout(ctx)

if err != nil {
return fmt.Errorf("error retrieving formatted plan output: %w", err)
}

logging.HelperResourceTrace(ctx, "Created destroy plan with changes", map[string]any{logging.KeyTestTerraformPlan: stdout})

return nil
}

// Apply runs "terraform apply". If CreatePlan has previously completed
Expand Down

0 comments on commit 0f41bb0

Please sign in to comment.