Skip to content

Commit 193867f

Browse files
authored
test: Add E2E test coverage for inspecting a plan file using the terraform show command (#38020)
1 parent 13ccceb commit 193867f

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

internal/command/e2etest/pluggable_state_store_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func TestPrimary_stateStore_outputCmd(t *testing.T) {
276276
// Tests using the `terraform show` command in combination with pluggable state storage
277277
// > `terraform show`
278278
// > `terraform show <path-to-state-file>`
279-
// > `terraform show <path-to-plan-file>` // TODO
279+
// > `terraform show <path-to-plan-file>`
280280
func TestPrimary_stateStore_showCmd(t *testing.T) {
281281

282282
if !canRunGoBuild {
@@ -353,7 +353,44 @@ greeting = "hello world"
353353
t.Errorf("wrong result, diff:\n%s", diff)
354354
}
355355

356-
// TODO(SarahFrench/radeksimko): Show plan file: terraform show <path to plan file>
356+
//// Show state: terraform show <path to plan file>
357+
358+
// 1. Create a plan file via plan command
359+
newOutput := `output "replacement" {
360+
value = resource.terraform_data.my-data.output
361+
}`
362+
if err := os.WriteFile(filepath.Join(tf.WorkDir(), "outputs.tf"), []byte(newOutput), 0644); err != nil {
363+
t.Fatalf("err: %s", err)
364+
}
365+
366+
planFile := "tfplan"
367+
stdout, stderr, err = tf.Run("plan", fmt.Sprintf("-out=%s", planFile), "-no-color")
368+
if err != nil {
369+
t.Fatalf("unexpected error: %s\nstderr:\n%s", err, stderr)
370+
}
371+
expectedMsg = "Changes to Outputs"
372+
if !strings.Contains(stdout, expectedMsg) {
373+
t.Errorf("wrong result, expected the plan command to create a plan file but that hasn't happened, got:\n%s",
374+
stdout,
375+
)
376+
}
377+
378+
// 2. Inspect plan file
379+
stdout, stderr, err = tf.Run("show", planFile, "-no-color")
380+
if err != nil {
381+
t.Fatalf("unexpected error: %s\nstderr:\n%s", err, stderr)
382+
}
383+
expectedMsg = `
384+
Changes to Outputs:
385+
- greeting = "hello world" -> null
386+
+ replacement = "hello world"
387+
388+
You can apply this plan to save these new output values to the Terraform
389+
state, without changing any real infrastructure.
390+
`
391+
if diff := cmp.Diff(stdout, expectedMsg); diff != "" {
392+
t.Errorf("wrong result, diff:\n%s", diff)
393+
}
357394
}
358395

359396
// Tests using the `terraform provider` subcommands in combination with pluggable state storage:

internal/command/e2etest/testdata/initialized-directory-with-state-store-fs/main.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,3 @@ variable "name" {
1919
resource "terraform_data" "my-data" {
2020
input = "hello ${var.name}"
2121
}
22-
23-
output "greeting" {
24-
value = resource.terraform_data.my-data.output
25-
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "greeting" {
2+
value = resource.terraform_data.my-data.output
3+
}

0 commit comments

Comments
 (0)