Skip to content

Commit 0f41bb0

Browse files
authored
helper/resource: Add terraform plan output to TRACE logging (#1058)
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 ... ```
1 parent a096f3a commit 0f41bb0

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

.changelog/1058.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
helper/resource: Added `terraform plan` output to `TRACE` logging
3+
```

internal/logging/keys.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ const (
4949
// The path to the Terraform CLI used for an acceptance test.
5050
KeyTestTerraformPath = "test_terraform_path"
5151

52+
// Terraform plan output generated during a TestStep.
53+
KeyTestTerraformPlan = "test_terraform_plan"
54+
5255
// The working directory of the acceptance test.
5356
KeyTestWorkingDirectory = "test_working_directory"
5457
)

internal/plugintest/working_dir.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,59 @@ func (wd *WorkingDir) planFilename() string {
174174
func (wd *WorkingDir) CreatePlan(ctx context.Context) error {
175175
logging.HelperResourceTrace(ctx, "Calling Terraform CLI plan command")
176176

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

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

181-
return err
181+
if err != nil {
182+
return err
183+
}
184+
185+
if !hasChanges {
186+
logging.HelperResourceTrace(ctx, "Created plan with no changes")
187+
188+
return nil
189+
}
190+
191+
stdout, err := wd.SavedPlanRawStdout(ctx)
192+
193+
if err != nil {
194+
return fmt.Errorf("error retrieving formatted plan output: %w", err)
195+
}
196+
197+
logging.HelperResourceTrace(ctx, "Created plan with changes", map[string]any{logging.KeyTestTerraformPlan: stdout})
198+
199+
return nil
182200
}
183201

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

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

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

193-
return err
211+
if err != nil {
212+
return err
213+
}
214+
215+
if !hasChanges {
216+
logging.HelperResourceTrace(ctx, "Created destroy plan with no changes")
217+
218+
return nil
219+
}
220+
221+
stdout, err := wd.SavedPlanRawStdout(ctx)
222+
223+
if err != nil {
224+
return fmt.Errorf("error retrieving formatted plan output: %w", err)
225+
}
226+
227+
logging.HelperResourceTrace(ctx, "Created destroy plan with changes", map[string]any{logging.KeyTestTerraformPlan: stdout})
228+
229+
return nil
194230
}
195231

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

0 commit comments

Comments
 (0)