2014-07-13 05:21:46 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-10-01 17:37:57 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-07-13 05:21:46 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-10-08 19:29:54 +02:00
|
|
|
|
2014-10-12 03:05:23 +02:00
|
|
|
"github.com/hashicorp/terraform/remote"
|
2014-10-08 19:29:54 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-07-13 05:21:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMetaColorize(t *testing.T) {
|
|
|
|
var m *Meta
|
|
|
|
var args, args2 []string
|
|
|
|
|
2014-07-17 18:34:32 +02:00
|
|
|
// Test basic, color
|
2014-07-13 05:21:46 +02:00
|
|
|
m = new(Meta)
|
2014-07-17 18:34:32 +02:00
|
|
|
m.Color = true
|
2014-07-13 05:21:46 +02:00
|
|
|
args = []string{"foo", "bar"}
|
|
|
|
args2 = []string{"foo", "bar"}
|
2014-08-05 18:32:01 +02:00
|
|
|
args = m.process(args, false)
|
2014-07-13 05:21:46 +02:00
|
|
|
if !reflect.DeepEqual(args, args2) {
|
|
|
|
t.Fatalf("bad: %#v", args)
|
|
|
|
}
|
|
|
|
if m.Colorize().Disable {
|
|
|
|
t.Fatal("should not be disabled")
|
|
|
|
}
|
|
|
|
|
2014-07-17 18:34:32 +02:00
|
|
|
// Test basic, no change
|
|
|
|
m = new(Meta)
|
|
|
|
args = []string{"foo", "bar"}
|
|
|
|
args2 = []string{"foo", "bar"}
|
2014-08-05 18:32:01 +02:00
|
|
|
args = m.process(args, false)
|
2014-07-17 18:34:32 +02:00
|
|
|
if !reflect.DeepEqual(args, args2) {
|
|
|
|
t.Fatalf("bad: %#v", args)
|
|
|
|
}
|
|
|
|
if !m.Colorize().Disable {
|
|
|
|
t.Fatal("should be disabled")
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
// Test disable #1
|
|
|
|
m = new(Meta)
|
2014-09-09 05:41:10 +02:00
|
|
|
m.Color = true
|
2014-07-13 05:21:46 +02:00
|
|
|
args = []string{"foo", "-no-color", "bar"}
|
|
|
|
args2 = []string{"foo", "bar"}
|
2014-08-05 18:32:01 +02:00
|
|
|
args = m.process(args, false)
|
2014-07-13 05:21:46 +02:00
|
|
|
if !reflect.DeepEqual(args, args2) {
|
|
|
|
t.Fatalf("bad: %#v", args)
|
|
|
|
}
|
|
|
|
if !m.Colorize().Disable {
|
|
|
|
t.Fatal("should be disabled")
|
|
|
|
}
|
|
|
|
}
|
2014-09-29 20:11:35 +02:00
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
func TestMetaInputMode(t *testing.T) {
|
2014-09-29 20:24:16 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
m := new(Meta)
|
|
|
|
args := []string{}
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
if m.InputMode() != terraform.InputModeStd {
|
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-09-29 20:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
func TestMetaInputMode_disable(t *testing.T) {
|
2014-09-29 20:24:16 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
m := new(Meta)
|
|
|
|
args := []string{"-input=false"}
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
if m.InputMode() > 0 {
|
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-09-29 20:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
func TestMetaInputMode_defaultVars(t *testing.T) {
|
2014-10-01 17:37:57 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
// Create a temporary directory for our cwd
|
|
|
|
d := tempDir(t)
|
|
|
|
if err := os.MkdirAll(d, 0755); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(d); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
// Create the default vars file
|
|
|
|
err = ioutil.WriteFile(
|
|
|
|
filepath.Join(d, DefaultVarsFilename),
|
|
|
|
[]byte(""),
|
|
|
|
0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(Meta)
|
|
|
|
args := []string{}
|
|
|
|
args = m.process(args, true)
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
if m.InputMode()&terraform.InputModeVar != 0 {
|
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-10-01 17:37:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-08 19:29:54 +02:00
|
|
|
|
|
|
|
func TestMetaInputMode_vars(t *testing.T) {
|
2014-09-29 20:24:16 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
m := new(Meta)
|
|
|
|
args := []string{"-var", "foo=bar"}
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
if m.InputMode()&terraform.InputModeVar != 0 {
|
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-09-29 20:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-12 03:05:23 +02:00
|
|
|
|
|
|
|
func TestMeta_initStatePaths(t *testing.T) {
|
|
|
|
m := new(Meta)
|
|
|
|
m.initStatePaths()
|
|
|
|
|
|
|
|
if m.statePath != DefaultStateFilename {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
if m.stateOutPath != DefaultStateFilename {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
if m.backupPath != DefaultStateFilename+DefaultBackupExtention {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
m = new(Meta)
|
|
|
|
m.statePath = "foo"
|
|
|
|
m.initStatePaths()
|
|
|
|
|
|
|
|
if m.stateOutPath != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
if m.backupPath != "foo"+DefaultBackupExtention {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
m = new(Meta)
|
|
|
|
m.stateOutPath = "foo"
|
|
|
|
m.initStatePaths()
|
|
|
|
|
|
|
|
if m.statePath != DefaultStateFilename {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
if m.backupPath != "foo"+DefaultBackupExtention {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMeta_persistLocal(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
m := new(Meta)
|
|
|
|
s := terraform.NewState()
|
|
|
|
if err := m.persistLocalState(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := remote.ExistsFile(m.stateOutPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
t.Fatalf("state should exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write again, shoudl backup
|
|
|
|
if err := m.persistLocalState(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err = remote.ExistsFile(m.backupPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
t.Fatalf("backup should exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMeta_persistRemote(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
err := remote.EnsureDirectory()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := terraform.NewState()
|
|
|
|
conf, srv := testRemoteState(t, s, 200)
|
|
|
|
s.Remote = conf
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
m := new(Meta)
|
|
|
|
if err := m.persistRemoteState(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
local, _, err := remote.ReadLocalState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if local == nil {
|
|
|
|
t.Fatalf("state should exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.persistRemoteState(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
backup := remote.LocalDirectory + "/" + remote.BackupHiddenStateFile
|
|
|
|
exists, err := remote.ExistsFile(backup)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
t.Fatalf("backup should exist")
|
|
|
|
}
|
|
|
|
}
|
2014-10-12 03:21:20 +02:00
|
|
|
|
|
|
|
func TestMeta_loadState_remote(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
err := remote.EnsureDirectory()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 1000
|
|
|
|
if err := remote.PersistState(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(Meta)
|
|
|
|
s1, err := m.loadState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s1.Serial < 1000 {
|
|
|
|
t.Fatalf("Bad: %#v", s1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !m.useRemoteState {
|
|
|
|
t.Fatalf("should enable remote")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMeta_loadState_statePath(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
m := new(Meta)
|
|
|
|
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 1000
|
|
|
|
if err := m.persistLocalState(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s1, err := m.loadState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s1.Serial < 1000 {
|
|
|
|
t.Fatalf("Bad: %#v", s1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMeta_loadState_conflict(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
err := remote.EnsureDirectory()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(Meta)
|
|
|
|
|
|
|
|
s := terraform.NewState()
|
|
|
|
if err := remote.PersistState(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := m.persistLocalState(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.loadState()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error with conflict")
|
|
|
|
}
|
|
|
|
}
|