From d475fc29a82368eafe43cae3a2f82b0c18c0595c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 13 Mar 2017 16:25:27 -0700 Subject: [PATCH] command: test that terraform meta information is passed through --- command/apply_test.go | 89 ++++++++++++++++++- command/meta.go | 5 ++ .../test-fixtures/apply-terraform-env/main.tf | 1 + 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 command/test-fixtures/apply-terraform-env/main.tf diff --git a/command/apply_test.go b/command/apply_test.go index 7f21aa1c2..65d341293 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -603,9 +603,7 @@ func TestApply_plan_backup(t *testing.T) { if err != nil { t.Fatal(err) } - - args := []string{ - "-state-out", statePath, + args := []string{"-state-out", statePath, "-backup", backupPath, planPath, } @@ -1531,6 +1529,91 @@ func TestApply_disableBackup(t *testing.T) { } } +// Test that the Terraform env is passed through +func TestApply_terraformEnv(t *testing.T) { + statePath := testTempFile(t) + + p := testProvider() + ui := new(cli.MockUi) + c := &ApplyCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, + }, + } + + args := []string{ + "-state", statePath, + testFixturePath("apply-terraform-env"), + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + expected := strings.TrimSpace(` + +Outputs: + +output = default + `) + testStateOutput(t, statePath, expected) +} + +// Test that the Terraform env is passed through +func TestApply_terraformEnvNonDefault(t *testing.T) { + // Create a temporary working directory that is empty + td := tempDir(t) + os.MkdirAll(td, 0755) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + // Create new env + { + ui := new(cli.MockUi) + newCmd := &EnvNewCommand{} + newCmd.Meta = Meta{Ui: ui} + if code := newCmd.Run([]string{"test"}); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter) + } + } + + // Switch to it + { + args := []string{"test"} + ui := new(cli.MockUi) + selCmd := &EnvSelectCommand{} + selCmd.Meta = Meta{Ui: ui} + if code := selCmd.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter) + } + } + + p := testProvider() + ui := new(cli.MockUi) + c := &ApplyCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, + }, + } + + args := []string{ + testFixturePath("apply-terraform-env"), + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + statePath := filepath.Join("terraform.tfstate.d", "test", "terraform.tfstate") + expected := strings.TrimSpace(` + +Outputs: + +output = test + `) + testStateOutput(t, statePath, expected) +} + func testHttpServer(t *testing.T) net.Listener { ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { diff --git a/command/meta.go b/command/meta.go index c18c77fc7..0780c544f 100644 --- a/command/meta.go +++ b/command/meta.go @@ -208,11 +208,16 @@ func (m *Meta) contextOpts() *terraform.ContextOpts { vs[k] = v } opts.Variables = vs + opts.Targets = m.targets opts.UIInput = m.UIInput() opts.Parallelism = m.parallelism opts.Shadow = m.shadow + opts.Meta = &terraform.ContextMeta{ + Env: m.Env(), + } + return &opts } diff --git a/command/test-fixtures/apply-terraform-env/main.tf b/command/test-fixtures/apply-terraform-env/main.tf new file mode 100644 index 000000000..6fc63dbc5 --- /dev/null +++ b/command/test-fixtures/apply-terraform-env/main.tf @@ -0,0 +1 @@ +output "output" { value = "${terraform.env}" }