terraform/command/plan_test.go

980 lines
25 KiB
Go
Raw Normal View History

2014-06-19 22:51:05 +02:00
package command
import (
"bytes"
2014-06-19 22:51:05 +02:00
"io/ioutil"
"os"
2014-07-12 06:03:56 +02:00
"path/filepath"
2014-09-18 19:40:23 +02:00
"strings"
"sync"
2014-06-19 22:51:05 +02:00
"testing"
"time"
2014-06-19 22:51:05 +02:00
"github.com/davecgh/go-spew/spew"
"github.com/mitchellh/cli"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/addrs"
backendinit "github.com/hashicorp/terraform/backend/init"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/plans"
"github.com/hashicorp/terraform/providers"
"github.com/hashicorp/terraform/states"
2014-06-19 22:51:05 +02:00
"github.com/hashicorp/terraform/terraform"
)
func TestPlan(t *testing.T) {
td := tempDir(t)
testCopyDir(t, testFixturePath("plan"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := planFixtureProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}
2017-02-03 22:02:22 +01:00
func TestPlan_lockedState(t *testing.T) {
td := tempDir(t)
testCopyDir(t, testFixturePath("plan"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
2017-02-03 22:02:22 +01:00
unlock, err := testLockState(testDataDir, filepath.Join(td, DefaultStateFilename))
2017-02-03 22:02:22 +01:00
if err != nil {
t.Fatal(err)
}
defer unlock()
p := planFixtureProvider()
2017-02-03 22:02:22 +01:00
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
2017-02-03 22:02:22 +01:00
},
}
args := []string{}
if code := c.Run(args); code == 0 {
t.Fatal("expected error")
}
output := ui.ErrorWriter.String()
if !strings.Contains(output, "lock") {
2017-02-03 22:02:22 +01:00
t.Fatal("command output does not look like a lock error:", output)
}
}
func TestPlan_plan(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
planPath := testPlanFileNoop(t)
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{planPath}
if code := c.Run(args); code != 1 {
t.Fatalf("wrong exit status %d; want 1\nstderr: %s", code, ui.ErrorWriter.String())
}
}
func TestPlan_destroy(t *testing.T) {
originalState := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"id":"bar"}`),
Status: states.ObjectReady,
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
outPath := testTempFile(t)
statePath := testStateFile(t, originalState)
p := planFixtureProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{
"-destroy",
"-out", outPath,
"-state", statePath,
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
plan := testReadPlan(t, outPath)
for _, rc := range plan.Changes.Resources {
if got, want := rc.Action, plans.Delete; got != want {
t.Fatalf("wrong action %s for %s; want %s\nplanned change: %s", got, rc.Addr, want, spew.Sdump(rc))
}
}
}
2014-09-19 02:16:09 +02:00
2014-06-20 20:47:02 +02:00
func TestPlan_noState(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
p := planFixtureProvider()
2014-06-19 22:51:05 +02:00
ui := new(cli.MockUi)
2014-06-20 20:47:02 +02:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
2014-06-19 22:51:05 +02:00
}
args := []string{
2014-06-20 20:47:02 +02:00
testFixturePath("plan"),
2014-06-19 22:51:05 +02:00
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
2014-06-26 18:56:29 +02:00
// Verify that refresh was called
if p.ReadResourceCalled {
t.Fatal("ReadResource should not be called")
2014-06-26 18:56:29 +02:00
}
2014-06-19 22:51:05 +02:00
// Verify that the provider was called with the existing state
actual := p.PlanResourceChangeRequest.PriorState
2021-01-12 22:13:10 +01:00
expected := cty.NullVal(p.GetSchemaResponse.ResourceTypes["test_instance"].Block.ImpliedType())
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state\ngot: %#v\nwant: %#v", actual, expected)
2014-06-19 22:51:05 +02:00
}
}
2014-06-19 22:51:28 +02:00
2014-06-27 07:23:51 +02:00
func TestPlan_outPath(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
td := testTempDir(t)
outPath := filepath.Join(td, "test.plan")
2014-06-27 07:23:51 +02:00
p := planFixtureProvider()
2014-06-27 07:23:51 +02:00
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
2014-06-27 07:23:51 +02:00
}
2021-01-12 22:13:10 +01:00
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.NullVal(cty.EmptyObject),
2014-06-27 07:23:51 +02:00
}
args := []string{
"-out", outPath,
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
testReadPlan(t, outPath) // will call t.Fatal itself if the file cannot be read
2014-06-27 07:23:51 +02:00
}
func TestPlan_outPathNoChange(t *testing.T) {
originalState := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
// Aside from "id" (which is computed) the values here must
// exactly match the values in the "plan" test fixture in order
// to produce the empty plan we need for this test.
AttrsJSON: []byte(`{"id":"bar","ami":"bar","network_interface":[{"description":"Main network interface","device_index":"0"}]}`),
Status: states.ObjectReady,
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
statePath := testStateFile(t, originalState)
td := testTempDir(t)
outPath := filepath.Join(td, "test.plan")
p := planFixtureProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{
"-out", outPath,
"-state", statePath,
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
plan := testReadPlan(t, outPath)
if !plan.Changes.Empty() {
t.Fatalf("Expected empty plan to be written to plan file, got: %s", spew.Sdump(plan))
}
}
2017-01-19 05:50:45 +01:00
// When using "-out" with a backend, the plan should encode the backend config
func TestPlan_outBackend(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("plan-out-backend"), td)
2017-01-19 05:50:45 +01:00
defer os.RemoveAll(td)
defer testChdir(t, td)()
originalState := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"id":"bar","ami":"bar"}`),
Status: states.ObjectReady,
2017-01-19 05:50:45 +01:00
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
2017-01-19 05:50:45 +01:00
// Set up our backend state
2017-01-19 05:50:45 +01:00
dataState, srv := testBackendState(t, originalState, 200)
defer srv.Close()
testStateFileRemote(t, dataState)
outPath := "foo"
p := testProvider()
2021-01-12 22:13:10 +01:00
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
Computed: true,
},
"ami": {
Type: cty.String,
Optional: true,
},
},
},
},
},
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
ui := cli.NewMockUi()
2017-01-19 05:50:45 +01:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
2017-01-19 05:50:45 +01:00
},
}
args := []string{
"-out", outPath,
}
if code := c.Run(args); code != 0 {
t.Logf("stdout: %s", ui.OutputWriter.String())
t.Fatalf("plan command failed with exit code %d\n\n%s", code, ui.ErrorWriter.String())
2017-01-19 05:50:45 +01:00
}
plan := testReadPlan(t, outPath)
if !plan.Changes.Empty() {
t.Fatalf("Expected empty plan to be written to plan file, got: %s", spew.Sdump(plan))
2017-01-19 05:50:45 +01:00
}
if got, want := plan.Backend.Type, "http"; got != want {
t.Errorf("wrong backend type %q; want %q", got, want)
}
if got, want := plan.Backend.Workspace, "default"; got != want {
t.Errorf("wrong backend workspace %q; want %q", got, want)
2017-01-19 05:50:45 +01:00
}
{
httpBackend := backendinit.Backend("http")()
schema := httpBackend.ConfigSchema()
got, err := plan.Backend.Config.Decode(schema.ImpliedType())
if err != nil {
t.Fatalf("failed to decode backend config in plan: %s", err)
}
want, err := dataState.Backend.Config(schema)
if err != nil {
t.Fatalf("failed to decode cached config: %s", err)
}
if !want.RawEquals(got) {
t.Errorf("wrong backend config\ngot: %#v\nwant: %#v", got, want)
}
2017-01-19 05:50:45 +01:00
}
}
func TestPlan_refreshFalse(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
p := planFixtureProvider()
2014-06-26 18:56:29 +02:00
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
2014-06-26 18:56:29 +02:00
}
args := []string{
"-refresh=false",
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
if p.ReadResourceCalled {
t.Fatal("ReadResource should not have been called")
2014-06-26 18:56:29 +02:00
}
}
2014-06-20 20:47:02 +02:00
func TestPlan_state(t *testing.T) {
2014-09-18 19:40:23 +02:00
originalState := testState()
statePath := testStateFile(t, originalState)
2014-06-19 22:51:05 +02:00
p := planFixtureProvider()
2014-06-19 22:51:05 +02:00
ui := new(cli.MockUi)
2014-06-20 20:47:02 +02:00
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
2014-06-19 22:51:05 +02:00
}
args := []string{
"-state", statePath,
2014-06-20 20:47:02 +02:00
testFixturePath("plan"),
2014-06-19 22:51:05 +02:00
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
// Verify that the provider was called with the existing state
actual := p.PlanResourceChangeRequest.PriorState
expected := cty.ObjectVal(map[string]cty.Value{
2018-10-15 00:36:19 +02:00
"id": cty.StringVal("bar"),
"ami": cty.NullVal(cty.String),
"network_interface": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
2018-10-15 00:36:19 +02:00
"device_index": cty.String,
"description": cty.String,
}))),
})
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state\ngot: %#v\nwant: %#v", actual, expected)
2014-06-19 22:51:05 +02:00
}
}
2014-07-12 06:03:56 +02:00
func TestPlan_stateDefault(t *testing.T) {
2014-09-18 19:40:23 +02:00
originalState := testState()
statePath := testStateFile(t, originalState)
2014-07-12 06:03:56 +02:00
// Change to that directory
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(filepath.Dir(statePath)); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := planFixtureProvider()
2014-07-12 06:03:56 +02:00
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
2014-07-12 06:03:56 +02:00
}
args := []string{
"-state", statePath,
2014-07-12 06:03:56 +02:00
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
// Verify that the provider was called with the existing state
actual := p.PlanResourceChangeRequest.PriorState
expected := cty.ObjectVal(map[string]cty.Value{
2018-10-15 00:36:19 +02:00
"id": cty.StringVal("bar"),
"ami": cty.NullVal(cty.String),
"network_interface": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
2018-10-15 00:36:19 +02:00
"device_index": cty.String,
"description": cty.String,
}))),
})
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state\ngot: %#v\nwant: %#v", actual, expected)
2014-07-12 06:03:56 +02:00
}
}
2014-07-18 20:37:27 +02:00
func TestPlan_validate(t *testing.T) {
// This is triggered by not asking for input so we have to set this to false
test = false
defer func() { test = true }()
td := tempDir(t)
testCopyDir(t, testFixturePath("plan-invalid"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := testProvider()
2021-01-12 22:13:10 +01:00
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
actual := ui.ErrorWriter.String()
if want := "Error: Invalid count argument"; !strings.Contains(actual, want) {
t.Fatalf("unexpected error output\ngot:\n%s\n\nshould contain: %s", actual, want)
}
}
2014-07-18 20:37:27 +02:00
func TestPlan_vars(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
p := planVarsFixtureProvider()
2014-07-18 20:37:27 +02:00
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
2014-07-18 20:37:27 +02:00
},
}
actual := ""
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
actual = req.ProposedNewState.GetAttr("value").AsString()
resp.PlannedState = req.ProposedNewState
return
2014-07-18 20:37:27 +02:00
}
args := []string{
"-var", "foo=bar",
testFixturePath("plan-vars"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
if actual != "bar" {
t.Fatal("didn't work")
}
}
2014-07-18 23:00:40 +02:00
func TestPlan_varsUnset(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
// The plan command will prompt for interactive input of var.foo.
// We'll answer "bar" to that prompt, which should then allow this
// configuration to apply even though var.foo doesn't have a
// default value and there are no -var arguments on our command line.
// This will (helpfully) panic if more than one variable is requested during plan:
// https://github.com/hashicorp/terraform/issues/26027
close := testInteractiveInput(t, []string{"bar"})
defer close()
p := planVarsFixtureProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{
testFixturePath("plan-vars"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}
// This test adds a required argument to the test provider to validate
// processing of user input:
// https://github.com/hashicorp/terraform/issues/26035
func TestPlan_providerArgumentUnset(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
// Disable test mode so input would be asked
test = false
defer func() { test = true }()
// The plan command will prompt for interactive input of provider.test.region
defaultInputReader = bytes.NewBufferString("us-east-1\n")
p := planFixtureProvider()
// override the planFixtureProvider schema to include a required provider argument
2021-01-12 22:13:10 +01:00
p.GetSchemaResponse = &providers.GetSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"region": {Type: cty.String, Required: true},
},
},
},
2021-01-12 22:13:10 +01:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true, Computed: true},
},
BlockTypes: map[string]*configschema.NestedBlock{
"network_interface": {
Nesting: configschema.NestingList,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"device_index": {Type: cty.String, Optional: true},
"description": {Type: cty.String, Optional: true},
},
},
},
},
},
},
},
}
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}
2014-07-18 23:00:40 +02:00
func TestPlan_varFile(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
2014-07-18 23:00:40 +02:00
varFilePath := testTempFile(t)
if err := ioutil.WriteFile(varFilePath, []byte(planVarFile), 0644); err != nil {
t.Fatalf("err: %s", err)
}
p := planVarsFixtureProvider()
2014-07-18 23:00:40 +02:00
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
2014-07-18 23:00:40 +02:00
},
}
actual := ""
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
actual = req.ProposedNewState.GetAttr("value").AsString()
resp.PlannedState = req.ProposedNewState
return
2014-07-18 23:00:40 +02:00
}
args := []string{
"-var-file", varFilePath,
testFixturePath("plan-vars"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
if actual != "bar" {
t.Fatal("didn't work")
}
}
func TestPlan_varFileDefault(t *testing.T) {
varFileDir := testTempDir(t)
varFilePath := filepath.Join(varFileDir, "terraform.tfvars")
if err := ioutil.WriteFile(varFilePath, []byte(planVarFile), 0644); err != nil {
t.Fatalf("err: %s", err)
}
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(varFileDir); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := planVarsFixtureProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
actual := ""
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
actual = req.ProposedNewState.GetAttr("value").AsString()
resp.PlannedState = req.ProposedNewState
return
}
args := []string{
testFixturePath("plan-vars"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
if actual != "bar" {
t.Fatal("didn't work")
}
}
func TestPlan_varFileWithDecls(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
varFilePath := testTempFile(t)
if err := ioutil.WriteFile(varFilePath, []byte(planVarFileWithDecl), 0644); err != nil {
t.Fatalf("err: %s", err)
}
p := planVarsFixtureProvider()
ui := cli.NewMockUi()
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{
"-var-file", varFilePath,
testFixturePath("plan-vars"),
}
if code := c.Run(args); code == 0 {
t.Fatalf("succeeded; want failure\n\n%s", ui.OutputWriter.String())
}
msg := ui.ErrorWriter.String()
if got, want := msg, "Variable declaration in .tfvars file"; !strings.Contains(got, want) {
t.Fatalf("missing expected error message\nwant message containing %q\ngot:\n%s", want, got)
}
}
func TestPlan_detailedExitcode(t *testing.T) {
td := tempDir(t)
testCopyDir(t, testFixturePath("plan"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := planFixtureProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{"-detailed-exitcode"}
if code := c.Run(args); code != 2 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}
func TestPlan_detailedExitcode_emptyDiff(t *testing.T) {
td := tempDir(t)
testCopyDir(t, testFixturePath("plan-emptydiff"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{"-detailed-exitcode"}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}
func TestPlan_shutdown(t *testing.T) {
cancelled := make(chan struct{})
shutdownCh := make(chan struct{})
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
ShutdownCh: shutdownCh,
},
}
p.StopFn = func() error {
close(cancelled)
return nil
}
var once sync.Once
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
once.Do(func() {
shutdownCh <- struct{}{}
})
// Because of the internal lock in the MockProvider, we can't
// coordinate directly with the calling of Stop, and making the
// MockProvider concurrent is disruptive to a lot of existing tests.
// Wait here a moment to help make sure the main goroutine gets to the
// Stop call before we exit, or the plan may finish before it can be
// canceled.
time.Sleep(200 * time.Millisecond)
s := req.ProposedNewState.AsValueMap()
s["ami"] = cty.StringVal("bar")
resp.PlannedState = cty.ObjectVal(s)
return
}
2021-01-12 22:13:10 +01:00
p.GetSchemaResponse = &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
code := c.Run([]string{
// Unfortunately it seems like this test can inadvertently pick up
// leftover state from other tests without this. Ideally we should
// find which test is leaving a terraform.tfstate behind and stop it
// doing that, but this will stop this test flapping for now.
"-state=nonexistent.tfstate",
testFixturePath("apply-shutdown"),
})
if code != 1 {
t.Errorf("wrong exit code %d; want 1\noutput:\n%s", code, ui.OutputWriter.String())
}
select {
case <-cancelled:
default:
t.Error("command not cancelled")
}
}
func TestPlan_init_required(t *testing.T) {
td := tempDir(t)
testCopyDir(t, testFixturePath("plan"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
// Running plan without setting testingOverrides is similar to plan without init
Ui: ui,
},
}
args := []string{}
if code := c.Run(args); code != 1 {
t.Fatalf("expected error, got success")
}
output := ui.ErrorWriter.String()
if !strings.Contains(output, `Plugin reinitialization required. Please run "terraform init".`) {
t.Fatal("wrong error message in output:", output)
}
}
// planFixtureSchema returns a schema suitable for processing the
// configuration in testdata/plan . This schema should be
// assigned to a mock provider named "test".
2021-01-12 22:13:10 +01:00
func planFixtureSchema() *providers.GetSchemaResponse {
return &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
},
BlockTypes: map[string]*configschema.NestedBlock{
"network_interface": {
Nesting: configschema.NestingList,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"device_index": {Type: cty.String, Optional: true},
"description": {Type: cty.String, Optional: true},
},
},
},
},
},
},
},
}
}
// planFixtureProvider returns a mock provider that is configured for basic
// operation with the configuration in testdata/plan. This mock has
2021-01-12 22:13:10 +01:00
// GetSchemaResponse and PlanResourceChangeFn populated, with the plan
// step just passing through the new object proposed by Terraform Core.
func planFixtureProvider() *terraform.MockProvider {
p := testProvider()
2021-01-12 22:13:10 +01:00
p.GetSchemaResponse = planFixtureSchema()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
return p
}
// planVarsFixtureSchema returns a schema suitable for processing the
// configuration in testdata/plan-vars . This schema should be
// assigned to a mock provider named "test".
2021-01-12 22:13:10 +01:00
func planVarsFixtureSchema() *providers.GetSchemaResponse {
return &providers.GetSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
}
// planVarsFixtureProvider returns a mock provider that is configured for basic
// operation with the configuration in testdata/plan-vars. This mock has
2021-01-12 22:13:10 +01:00
// GetSchemaResponse and PlanResourceChangeFn populated, with the plan
// step just passing through the new object proposed by Terraform Core.
func planVarsFixtureProvider() *terraform.MockProvider {
p := testProvider()
2021-01-12 22:13:10 +01:00
p.GetSchemaResponse = planVarsFixtureSchema()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
return p
}
2014-07-18 23:00:40 +02:00
const planVarFile = `
foo = "bar"
`
2014-09-18 19:40:23 +02:00
const planVarFileWithDecl = `
foo = "bar"
variable "nope" {
}
`