2017-02-17 00:29:19 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/backend/local"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEnv_createAndChange(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)()
|
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
newCmd := &EnvNewCommand{}
|
2017-02-17 00:29:19 +01:00
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
current := newCmd.Env()
|
2017-02-17 00:29:19 +01:00
|
|
|
if current != backend.DefaultStateName {
|
|
|
|
t.Fatal("current env should be 'default'")
|
|
|
|
}
|
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
args := []string{"test"}
|
2017-02-17 00:29:19 +01:00
|
|
|
ui := new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
newCmd.Meta = Meta{Ui: ui}
|
|
|
|
if code := newCmd.Run(args); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
current = newCmd.Env()
|
2017-02-17 00:29:19 +01:00
|
|
|
if current != "test" {
|
2017-02-28 19:13:03 +01:00
|
|
|
t.Fatalf("current env should be 'test', got %q", current)
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
selCmd := &EnvSelectCommand{}
|
2017-02-17 00:29:19 +01:00
|
|
|
args = []string{backend.DefaultStateName}
|
|
|
|
ui = new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
selCmd.Meta = Meta{Ui: ui}
|
|
|
|
if code := selCmd.Run(args); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
current = newCmd.Env()
|
2017-02-17 00:29:19 +01:00
|
|
|
if current != backend.DefaultStateName {
|
|
|
|
t.Fatal("current env should be 'default'")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create some environments and test the list output.
|
|
|
|
// This also ensures we switch to the correct env after each call
|
|
|
|
func TestEnv_createAndList(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)()
|
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
newCmd := &EnvNewCommand{}
|
2017-02-17 00:29:19 +01:00
|
|
|
|
|
|
|
envs := []string{"test_a", "test_b", "test_c"}
|
|
|
|
|
|
|
|
// create multiple envs
|
|
|
|
for _, env := range envs {
|
|
|
|
ui := new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
newCmd.Meta = Meta{Ui: ui}
|
|
|
|
if code := newCmd.Run([]string{env}); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
listCmd := &EnvListCommand{}
|
2017-02-17 00:29:19 +01:00
|
|
|
ui := new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
listCmd.Meta = Meta{Ui: ui}
|
2017-02-17 00:29:19 +01:00
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
if code := listCmd.Run(nil); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(ui.OutputWriter.String())
|
2017-02-23 19:13:28 +01:00
|
|
|
expected := "default\n test_a\n test_b\n* test_c"
|
|
|
|
|
2017-02-17 00:29:19 +01:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("\nexpcted: %q\nactual: %q", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnv_createWithState(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// create a non-empty 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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := (&state.LocalState{Path: "test.tfstate"}).WriteState(originalState)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
args := []string{"-state", "test.tfstate", "test"}
|
2017-02-17 00:29:19 +01:00
|
|
|
ui := new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
newCmd := &EnvNewCommand{
|
2017-02-17 00:29:19 +01:00
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
2017-02-23 19:13:28 +01:00
|
|
|
if code := newCmd.Run(args); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
newPath := filepath.Join(local.DefaultEnvDir, "test", DefaultStateFilename)
|
|
|
|
envState := state.LocalState{Path: newPath}
|
|
|
|
err = envState.RefreshState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newState := envState.State()
|
|
|
|
if !originalState.Equal(newState) {
|
|
|
|
t.Fatalf("states not equal\norig: %s\nnew: %s", originalState, newState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnv_delete(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// create the env directories
|
|
|
|
if err := os.MkdirAll(filepath.Join(local.DefaultEnvDir, "test"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the environment file
|
|
|
|
if err := os.MkdirAll(DefaultDataDir, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(DefaultDataDir, local.DefaultEnvFile), []byte("test"), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
delCmd := &EnvDeleteCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
current := delCmd.Env()
|
2017-02-17 00:29:19 +01:00
|
|
|
if current != "test" {
|
|
|
|
t.Fatal("wrong env:", current)
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
// we can't delete out current environment
|
2017-02-23 19:13:28 +01:00
|
|
|
args := []string{"test"}
|
2017-02-28 19:13:03 +01:00
|
|
|
if code := delCmd.Run(args); code == 0 {
|
|
|
|
t.Fatal("expected error deleting current env")
|
2017-02-17 00:29:19 +01:00
|
|
|
}
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
// change back to default
|
|
|
|
if err := delCmd.SetEnv(backend.DefaultStateName); err != nil {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:13:03 +01:00
|
|
|
// try the delete again
|
|
|
|
ui = new(cli.MockUi)
|
|
|
|
delCmd.Meta.Ui = ui
|
|
|
|
if code := delCmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("error deleting env: %s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
current = delCmd.Env()
|
2017-02-17 00:29:19 +01:00
|
|
|
if current != backend.DefaultStateName {
|
|
|
|
t.Fatalf("wrong env: %q", current)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestEnv_deleteWithState(t *testing.T) {
|
|
|
|
td := tempDir(t)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// create the env directories
|
|
|
|
if err := os.MkdirAll(filepath.Join(local.DefaultEnvDir, "test"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a non-empty 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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
envStatePath := filepath.Join(local.DefaultEnvDir, "test", DefaultStateFilename)
|
|
|
|
err := (&state.LocalState{Path: envStatePath}).WriteState(originalState)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
delCmd := &EnvDeleteCommand{
|
2017-02-17 00:29:19 +01:00
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
2017-02-23 19:13:28 +01:00
|
|
|
args := []string{"test"}
|
|
|
|
if code := delCmd.Run(args); code == 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("expected failure without -force.\noutput: %s", ui.OutputWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui = new(cli.MockUi)
|
2017-02-23 19:13:28 +01:00
|
|
|
delCmd.Meta.Ui = ui
|
2017-02-17 00:29:19 +01:00
|
|
|
|
2017-02-23 19:13:28 +01:00
|
|
|
args = []string{"-force", "test"}
|
|
|
|
if code := delCmd.Run(args); code != 0 {
|
2017-02-17 00:29:19 +01:00
|
|
|
t.Fatalf("failure: %s", ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(local.DefaultEnvDir, "test")); !os.IsNotExist(err) {
|
|
|
|
t.Fatal("env 'test' still exists!")
|
|
|
|
}
|
|
|
|
}
|