terraform/command/plan_test.go

877 lines
18 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"
2017-01-19 05:50:45 +01:00
"reflect"
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
2017-01-19 05:50:45 +01:00
"github.com/hashicorp/terraform/helper/copy"
2014-06-19 22:51:05 +02:00
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
func TestPlan(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(testFixturePath("plan")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := testProvider()
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) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
testPath := testFixturePath("plan")
unlock, err := testLockState("./testdata", filepath.Join(testPath, DefaultStateFilename))
2017-02-03 22:02:22 +01:00
if err != nil {
t.Fatal(err)
}
defer unlock()
if err := os.Chdir(testPath); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := testProvider()
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 := testPlanFile(t, &terraform.Plan{
Config: testModule(t, "apply"),
})
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{planPath}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
if p.RefreshCalled {
t.Fatal("refresh should not be called")
}
}
func TestPlan_destroy(t *testing.T) {
originalState := &terraform.State{
2014-09-17 20:15:07 +02:00
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
},
},
},
},
},
}
outPath := testTempFile(t)
statePath := testStateFile(t, originalState)
p := testProvider()
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())
}
if !p.RefreshCalled {
t.Fatal("refresh should be called")
}
plan := testReadPlan(t, outPath)
for _, m := range plan.Diff.Modules {
for _, r := range m.Resources {
if !r.Destroy {
t.Fatalf("bad: %#v", r)
}
}
}
}
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)
2014-06-19 22:51:05 +02:00
p := testProvider()
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.RefreshCalled {
t.Fatal("refresh 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
2014-09-18 19:40:23 +02:00
actual := strings.TrimSpace(p.DiffState.String())
expected := strings.TrimSpace(testPlanNoStateStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
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 := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
2014-06-27 07:23:51 +02:00
}
2014-09-18 01:33:24 +02:00
p.DiffReturn = &terraform.InstanceDiff{
2014-06-27 07:23:51 +02:00
Destroy: true,
}
args := []string{
"-out", outPath,
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
f, err := os.Open(outPath)
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
if _, err := terraform.ReadPlan(f); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestPlan_outPathNoChange(t *testing.T) {
originalState := &terraform.State{
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
},
},
},
},
},
}
statePath := testStateFile(t, originalState)
td := testTempDir(t)
outPath := filepath.Join(td, "test.plan")
p := testProvider()
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.Diff.Empty() {
t.Fatalf("Expected empty plan to be written to plan file, got: %s", 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)
copy.CopyDir(testFixturePath("plan-out-backend"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
// Our state
originalState := &terraform.State{
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
},
},
},
},
},
}
originalState.Init()
// Setup our backend state
dataState, srv := testBackendState(t, originalState, 200)
defer srv.Close()
testStateFileRemote(t, dataState)
outPath := "foo"
p := testProvider()
ui := new(cli.MockUi)
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.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
plan := testReadPlan(t, outPath)
if !plan.Diff.Empty() {
t.Fatalf("Expected empty plan to be written to plan file, got: %s", plan)
}
if plan.Backend.Empty() {
t.Fatal("should have backend info")
}
if !reflect.DeepEqual(plan.Backend, dataState.Backend) {
t.Fatalf("bad: %#v", plan.Backend)
}
}
// When using "-out" with a legacy remote state, the plan should encode
// the backend config
func TestPlan_outBackendLegacy(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
copy.CopyDir(testFixturePath("plan-out-backend-legacy"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
// Our state
originalState := &terraform.State{
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
},
},
},
},
},
}
originalState.Init()
// Setup our legacy state
remoteState, srv := testRemoteState(t, originalState, 200)
defer srv.Close()
dataState := terraform.NewState()
dataState.Remote = remoteState
testStateFileRemote(t, dataState)
outPath := "foo"
p := testProvider()
ui := new(cli.MockUi)
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.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
plan := testReadPlan(t, outPath)
if !plan.Diff.Empty() {
t.Fatalf("Expected empty plan to be written to plan file, got: %s", plan)
}
if plan.State.Remote.Empty() {
t.Fatal("should have remote info")
}
}
2014-06-26 18:56:29 +02:00
func TestPlan_refresh(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
2014-06-26 18:56:29 +02:00
p := testProvider()
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.RefreshCalled {
t.Fatal("refresh should not be called")
}
}
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 := testProvider()
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
2014-09-18 19:40:23 +02:00
actual := strings.TrimSpace(p.DiffState.String())
expected := strings.TrimSpace(testPlanStateStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
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 := testProvider()
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
2014-09-18 19:40:23 +02:00
actual := strings.TrimSpace(p.DiffState.String())
expected := strings.TrimSpace(testPlanStateDefaultStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
2014-07-12 06:03:56 +02:00
}
}
2014-07-18 20:37:27 +02:00
func TestPlan_stateFuture(t *testing.T) {
originalState := testState()
originalState.TFVersion = "99.99.99"
statePath := testStateFile(t, originalState)
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{
"-state", statePath,
testFixturePath("plan"),
}
if code := c.Run(args); code == 0 {
t.Fatal("should fail")
}
f, err := os.Open(statePath)
if err != nil {
t.Fatalf("err: %s", err)
}
newState, err := terraform.ReadState(f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
if !newState.Equal(originalState) {
t.Fatalf("bad: %#v", newState)
}
if newState.TFVersion != originalState.TFVersion {
t.Fatalf("bad: %#v", newState)
}
}
func TestPlan_statePast(t *testing.T) {
originalState := testState()
originalState.TFVersion = "0.1.0"
statePath := testStateFile(t, originalState)
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
args := []string{
"-state", statePath,
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}
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 }()
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(testFixturePath("plan-invalid")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := testProvider()
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 !strings.Contains(actual, "cannot be computed") {
t.Fatalf("bad: %s", actual)
}
}
2014-07-18 20:37:27 +02:00
func TestPlan_vars(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
2014-07-18 20:37:27 +02:00
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
2014-07-18 20:37:27 +02:00
},
}
actual := ""
p.DiffFn = func(
2014-09-17 20:15:07 +02:00
info *terraform.InstanceInfo,
s *terraform.InstanceState,
2014-09-18 01:33:24 +02:00
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
2014-07-18 20:37:27 +02:00
if v, ok := c.Config["value"]; ok {
actual = v.(string)
}
return nil, nil
}
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)
// Disable test mode so input would be asked
test = false
defer func() { test = true }()
defaultInputReader = bytes.NewBufferString("bar\n")
p := testProvider()
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())
}
}
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 := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
2014-07-18 23:00:40 +02:00
},
}
actual := ""
p.DiffFn = func(
2014-09-17 20:15:07 +02:00
info *terraform.InstanceInfo,
s *terraform.InstanceState,
2014-09-18 01:33:24 +02:00
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
2014-07-18 23:00:40 +02:00
if v, ok := c.Config["value"]; ok {
actual = v.(string)
}
return nil, nil
}
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 := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
}
actual := ""
p.DiffFn = func(
2014-09-17 20:15:07 +02:00
info *terraform.InstanceInfo,
s *terraform.InstanceState,
2014-09-18 01:33:24 +02:00
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
if v, ok := c.Config["value"]; ok {
actual = v.(string)
}
return nil, nil
}
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_detailedExitcode(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(testFixturePath("plan")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
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 != 2 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}
func TestPlan_detailedExitcode_emptyDiff(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(testFixturePath("plan-emptydiff")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
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.DiffFn = func(
*terraform.InstanceInfo,
*terraform.InstanceState,
*terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
once.Do(func() {
shutdownCh <- struct{}{}
})
// Because of the internal lock in the MockProvider, we can't
// coordiante 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)
return &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ami": &terraform.ResourceAttrDiff{
New: "bar",
},
},
}, nil
}
if code := c.Run([]string{testFixturePath("apply-shutdown")}); code != 1 {
// FIXME: we should be able to avoid the error during evaluation
// the early exit isn't caught before the interpolation is evaluated
t.Fatal(ui.OutputWriter.String())
}
select {
case <-cancelled:
default:
t.Fatal("command not cancelled")
}
}
2014-07-18 23:00:40 +02:00
const planVarFile = `
foo = "bar"
`
2014-09-18 19:40:23 +02:00
const testPlanNoStateStr = `
<not created>
`
const testPlanStateStr = `
ID = bar
2016-04-21 21:59:10 +02:00
Tainted = false
2014-09-18 19:40:23 +02:00
`
const testPlanStateDefaultStr = `
ID = bar
2016-04-21 21:59:10 +02:00
Tainted = false
2014-09-18 19:40:23 +02:00
`