2014-06-19 22:51:05 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-07-12 06:03:56 +02:00
|
|
|
"path/filepath"
|
2014-09-18 19:40:23 +02:00
|
|
|
"strings"
|
2014-06-19 22:51:05 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2014-07-12 05:51:26 +02:00
|
|
|
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{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
2014-07-12 05:51:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 18:12:05 +02:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-07-01 18:12:05 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
outPath := testTempFile(t)
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
2014-07-01 18:12:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2014-07-08 06:12:12 +02:00
|
|
|
if !p.RefreshCalled {
|
|
|
|
t.Fatal("refresh should be called")
|
|
|
|
}
|
|
|
|
|
2014-07-01 18:12:05 +02:00
|
|
|
plan := testReadPlan(t, outPath)
|
2014-09-24 23:37:24 +02:00
|
|
|
for _, m := range plan.Diff.Modules {
|
|
|
|
for _, r := range m.Resources {
|
|
|
|
if !r.Destroy {
|
|
|
|
t.Fatalf("bad: %#v", r)
|
|
|
|
}
|
2014-07-01 18:12:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-28 05:38:41 +02:00
|
|
|
|
2015-09-11 20:56:20 +02:00
|
|
|
f, err := os.Open(statePath + DefaultBackupExtension)
|
2014-07-28 05:38:41 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
backupState, err := terraform.ReadState(f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-19 02:16:09 +02:00
|
|
|
actualStr := strings.TrimSpace(backupState.String())
|
|
|
|
expectedStr := strings.TrimSpace(originalState.String())
|
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
2014-07-28 05:38:41 +02:00
|
|
|
}
|
2014-07-01 18:12:05 +02:00
|
|
|
}
|
2014-09-19 02:16:09 +02:00
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func TestPlan_noState(t *testing.T) {
|
2014-06-19 22:51:05 +02:00
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2014-06-20 20:47:02 +02:00
|
|
|
c := &PlanCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(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
|
2014-07-08 06:12:12 +02:00
|
|
|
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) {
|
|
|
|
tf, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
outPath := tf.Name()
|
|
|
|
os.Remove(tf.Name())
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:56:29 +02:00
|
|
|
func TestPlan_refresh(t *testing.T) {
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(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-06-19 22:51:05 +02:00
|
|
|
// Write out some prior state
|
|
|
|
tf, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
statePath := tf.Name()
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
2014-09-18 19:40:23 +02:00
|
|
|
originalState := testState()
|
2014-06-19 22:51:05 +02:00
|
|
|
err = terraform.WriteState(originalState, tf)
|
|
|
|
tf.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2014-06-20 20:47:02 +02:00
|
|
|
c := &PlanCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(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()
|
2014-07-12 06:03:56 +02:00
|
|
|
|
|
|
|
// Write the state file in a temporary directory with the
|
|
|
|
// default filename.
|
|
|
|
td, err := ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
statePath := filepath.Join(td, DefaultStateFilename)
|
|
|
|
|
|
|
|
f, err := os.Create(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
err = terraform.WriteState(originalState, f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
2014-07-12 06:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
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_vars(t *testing.T) {
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(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) {
|
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_varFile(t *testing.T) {
|
|
|
|
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{
|
|
|
|
ContextOpts: testCtxConfig(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) {
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:01 +02:00
|
|
|
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{
|
|
|
|
ContextOpts: testCtxConfig(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) {
|
2014-08-05 18:32:01 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 05:38:41 +02:00
|
|
|
func TestPlan_backup(t *testing.T) {
|
|
|
|
// Write out some prior state
|
|
|
|
tf, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
statePath := tf.Name()
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
// Write out some prior state
|
|
|
|
backupf, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
backupPath := backupf.Name()
|
|
|
|
backupf.Close()
|
|
|
|
os.Remove(backupPath)
|
|
|
|
defer os.Remove(backupPath)
|
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-07-28 05:38:41 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = terraform.WriteState(originalState, tf)
|
|
|
|
tf.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-backup", backupPath,
|
|
|
|
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(testPlanBackupStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
2014-07-28 05:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the backup exist
|
|
|
|
f, err := os.Open(backupPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
backupState, err := terraform.ReadState(f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-19 02:16:09 +02:00
|
|
|
actualStr := strings.TrimSpace(backupState.String())
|
|
|
|
expectedStr := strings.TrimSpace(originalState.String())
|
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
2014-07-28 05:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlan_disableBackup(t *testing.T) {
|
|
|
|
// Write out some prior state
|
|
|
|
tf, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
statePath := tf.Name()
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-07-28 05:38:41 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = terraform.WriteState(originalState, tf)
|
|
|
|
tf.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PlanCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-backup", "-",
|
|
|
|
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(testPlanDisableBackupStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
2014-07-28 05:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure there is no backup
|
2015-09-11 20:56:20 +02:00
|
|
|
_, err = os.Stat(statePath + DefaultBackupExtension)
|
2014-07-28 05:38:41 +02:00
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("backup should not exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 18:38:19 +02:00
|
|
|
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{
|
|
|
|
ContextOpts: testCtxConfig(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{
|
|
|
|
ContextOpts: testCtxConfig(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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 23:00:40 +02:00
|
|
|
const planVarFile = `
|
|
|
|
foo = "bar"
|
|
|
|
`
|
2014-09-18 19:40:23 +02:00
|
|
|
|
|
|
|
const testPlanBackupStr = `
|
|
|
|
ID = bar
|
|
|
|
`
|
|
|
|
|
|
|
|
const testPlanDisableBackupStr = `
|
|
|
|
ID = bar
|
|
|
|
`
|
|
|
|
|
|
|
|
const testPlanNoStateStr = `
|
|
|
|
<not created>
|
|
|
|
`
|
|
|
|
|
|
|
|
const testPlanStateStr = `
|
|
|
|
ID = bar
|
|
|
|
`
|
|
|
|
|
|
|
|
const testPlanStateDefaultStr = `
|
|
|
|
ID = bar
|
|
|
|
`
|