2014-06-27 20:09:01 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-11-27 16:10:42 +01:00
|
|
|
"bytes"
|
2014-06-27 20:09:01 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-07-12 06:56:43 +02:00
|
|
|
"path/filepath"
|
2014-06-27 20:09:01 +02:00
|
|
|
"reflect"
|
2014-09-18 19:40:23 +02:00
|
|
|
"strings"
|
2014-09-21 18:42:48 +02:00
|
|
|
"testing"
|
2014-06-27 20:09:01 +02:00
|
|
|
|
2017-03-16 20:05:11 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2017-07-06 00:17:07 +02:00
|
|
|
"github.com/hashicorp/terraform/state"
|
2014-06-27 20:09:01 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRefresh(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-08 06:12:12 +02:00
|
|
|
statePath := testStateFile(t, state)
|
2014-06-27 20:09:01 +02:00
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-27 20:09:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = nil
|
2014-09-17 20:15:07 +02:00
|
|
|
p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
|
2014-06-27 20:09:01 +02:00
|
|
|
|
|
|
|
args := []string{
|
2014-07-12 06:56:43 +02:00
|
|
|
"-state", statePath,
|
2014-06-27 20:09:01 +02:00
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-09-18 19:40:23 +02:00
|
|
|
actual := strings.TrimSpace(newState.String())
|
|
|
|
expected := strings.TrimSpace(testRefreshStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
2014-06-27 20:09:01 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 20:12:45 +02:00
|
|
|
|
2017-03-16 20:05:11 +01:00
|
|
|
func TestRefresh_empty(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("refresh-empty"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-03-16 20:05:11 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = nil
|
|
|
|
p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
td,
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:32:40 +01:00
|
|
|
func TestRefresh_lockedState(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
2017-02-06 19:26:03 +01:00
|
|
|
unlock, err := testLockState("./testdata", statePath)
|
2017-02-03 21:32:40 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-02-03 21:32:40 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = nil
|
|
|
|
p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.ErrorWriter.String()
|
2017-02-15 20:01:18 +01:00
|
|
|
if !strings.Contains(output, "lock") {
|
2017-02-03 21:32:40 +01:00
|
|
|
t.Fatal("command output does not look like a lock error:", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 06:56:43 +02:00
|
|
|
func TestRefresh_cwd(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(testFixturePath("refresh")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-12 06:56:43 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-12 06:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = nil
|
2014-09-17 20:15:07 +02:00
|
|
|
p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
|
2014-07-12 06:56:43 +02:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-09-18 19:40:23 +02:00
|
|
|
actual := strings.TrimSpace(newState.String())
|
|
|
|
expected := strings.TrimSpace(testRefreshCwdStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
2014-07-12 06:56:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefresh_defaultState(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
originalState := testState()
|
2014-07-12 06:56:43 +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)
|
|
|
|
|
2017-07-06 00:17:07 +02:00
|
|
|
localState := &state.LocalState{Path: statePath}
|
|
|
|
if err := localState.WriteState(originalState); err != nil {
|
|
|
|
t.Fatal(err)
|
2014-07-12 06:56:43 +02:00
|
|
|
}
|
2017-07-06 00:17:07 +02:00
|
|
|
serial := localState.State().Serial
|
2014-07-12 06:56:43 +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 := &RefreshCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-12 06:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = nil
|
2016-08-10 21:47:25 +02:00
|
|
|
p.RefreshReturn = newInstanceState("yes")
|
2014-07-12 06:56:43 +02:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2017-07-06 00:17:07 +02:00
|
|
|
newState := testStateRead(t, statePath)
|
2014-07-12 06:56:43 +02:00
|
|
|
|
2014-09-17 20:15:07 +02:00
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Primary
|
2014-07-12 06:56:43 +02:00
|
|
|
expected := p.RefreshReturn
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2016-08-10 21:47:25 +02:00
|
|
|
t.Logf("expected:\n%#v", expected)
|
|
|
|
t.Fatalf("bad:\n%#v", actual)
|
2014-07-12 06:56:43 +02:00
|
|
|
}
|
2014-07-28 04:58:12 +02:00
|
|
|
|
2017-07-06 00:17:07 +02:00
|
|
|
if newState.Serial <= serial {
|
|
|
|
t.Fatalf("serial not incremented during refresh. previous:%d, current:%d", serial, newState.Serial)
|
2014-07-28 04:58:12 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 00:17:07 +02:00
|
|
|
backupState := testStateRead(t, statePath+DefaultBackupExtension)
|
2014-07-28 04:58:12 +02:00
|
|
|
|
2014-09-17 20:15:07 +02:00
|
|
|
actual = backupState.RootModule().Resources["test_instance.foo"].Primary
|
|
|
|
expected = originalState.RootModule().Resources["test_instance.foo"].Primary
|
2014-07-28 04:58:12 +02:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
2014-07-12 06:56:43 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 20:07:54 +01:00
|
|
|
func TestRefresh_futureState(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(testFixturePath("refresh")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
state := testState()
|
|
|
|
state.TFVersion = "99.99.99"
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-03-11 20:07:54 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.RefreshCalled {
|
|
|
|
t.Fatal("refresh should not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(newState.String())
|
|
|
|
expected := strings.TrimSpace(state.String())
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefresh_pastState(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
state.TFVersion = "0.1.0"
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-03-11 20:07:54 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = nil
|
|
|
|
p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(newState.String())
|
|
|
|
expected := strings.TrimSpace(testRefreshStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if newState.TFVersion != terraform.Version {
|
|
|
|
t.Fatalf("bad:\n\n%s", newState.TFVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 20:12:45 +02:00
|
|
|
func TestRefresh_outPath(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-08 06:12:12 +02:00
|
|
|
statePath := testStateFile(t, state)
|
2014-06-27 20:12:45 +02:00
|
|
|
|
|
|
|
// Output path
|
|
|
|
outf, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
outPath := outf.Name()
|
|
|
|
outf.Close()
|
|
|
|
os.Remove(outPath)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-27 20:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = nil
|
2016-08-10 21:47:25 +02:00
|
|
|
p.RefreshReturn = newInstanceState("yes")
|
2014-06-27 20:12:45 +02:00
|
|
|
|
|
|
|
args := []string{
|
2014-07-12 06:56:43 +02:00
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", outPath,
|
2014-06-27 20:12:45 +02:00
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
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 !reflect.DeepEqual(newState, state) {
|
|
|
|
t.Fatalf("bad: %#v", newState)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err = os.Open(outPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newState, err = terraform.ReadState(f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-17 20:15:07 +02:00
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Primary
|
2014-06-27 20:12:45 +02:00
|
|
|
expected := p.RefreshReturn
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
2014-07-28 04:58:12 +02:00
|
|
|
|
2015-09-11 20:56:20 +02:00
|
|
|
f, err = os.Open(outPath + DefaultBackupExtension)
|
2014-07-28 04:58:12 +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(state.String())
|
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
2014-07-28 04:58:12 +02:00
|
|
|
}
|
2014-06-27 20:12:45 +02:00
|
|
|
}
|
2014-07-18 20:37:27 +02:00
|
|
|
|
|
|
|
func TestRefresh_var(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-18 20:37:27 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-18 20:37:27 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var", "foo=bar",
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh-var"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ConfigureCalled {
|
|
|
|
t.Fatal("configure should be called")
|
|
|
|
}
|
|
|
|
if p.ConfigureConfig.Config["value"].(string) != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", p.ConfigureConfig.Config)
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 23:00:40 +02:00
|
|
|
|
|
|
|
func TestRefresh_varFile(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-18 23:00:40 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-18 23:00:40 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
varFilePath := testTempFile(t)
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(refreshVarFile), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var-file", varFilePath,
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh-var"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ConfigureCalled {
|
|
|
|
t.Fatal("configure should be called")
|
|
|
|
}
|
|
|
|
if p.ConfigureConfig.Config["value"].(string) != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", p.ConfigureConfig.Config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:01 +02:00
|
|
|
func TestRefresh_varFileDefault(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-08-05 18:32:01 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-08-05 18:32:01 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
varFileDir := testTempDir(t)
|
|
|
|
varFilePath := filepath.Join(varFileDir, "terraform.tfvars")
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(refreshVarFile), 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)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh-var"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ConfigureCalled {
|
|
|
|
t.Fatal("configure should be called")
|
|
|
|
}
|
|
|
|
if p.ConfigureConfig.Config["value"].(string) != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", p.ConfigureConfig.Config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 09:32:46 +01:00
|
|
|
func TestRefresh_varsUnset(t *testing.T) {
|
|
|
|
// Disable test mode so input would be asked
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
defaultInputReader = bytes.NewBufferString("bar\n")
|
|
|
|
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-11-23 09:32:46 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh-unset-var"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 04:58:12 +02:00
|
|
|
func TestRefresh_backup(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-28 04:58:12 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
// Output path
|
|
|
|
outf, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
outPath := outf.Name()
|
|
|
|
outf.Close()
|
|
|
|
os.Remove(outPath)
|
|
|
|
|
|
|
|
// Backup path
|
|
|
|
backupf, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
backupPath := backupf.Name()
|
|
|
|
backupf.Close()
|
|
|
|
os.Remove(backupPath)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-28 04:58:12 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = nil
|
2016-08-10 21:47:25 +02:00
|
|
|
p.RefreshReturn = newInstanceState("yes")
|
2014-07-28 04:58:12 +02:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", outPath,
|
|
|
|
"-backup", backupPath,
|
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
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 !reflect.DeepEqual(newState, state) {
|
|
|
|
t.Fatalf("bad: %#v", newState)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err = os.Open(outPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newState, err = terraform.ReadState(f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-17 20:15:07 +02:00
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Primary
|
2014-07-28 04:58:12 +02:00
|
|
|
expected := p.RefreshReturn
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
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(state.String())
|
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
2014-07-28 04:58:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 05:38:41 +02:00
|
|
|
func TestRefresh_disableBackup(t *testing.T) {
|
2014-09-17 20:15:07 +02:00
|
|
|
state := testState()
|
2014-07-28 05:38:41 +02:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
// Output path
|
|
|
|
outf, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
outPath := outf.Name()
|
|
|
|
outf.Close()
|
|
|
|
os.Remove(outPath)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-28 05:38:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.RefreshFn = nil
|
2016-08-10 21:47:25 +02:00
|
|
|
p.RefreshReturn = newInstanceState("yes")
|
2014-07-28 05:38:41 +02:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", outPath,
|
|
|
|
"-backup", "-",
|
|
|
|
testFixturePath("refresh"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
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 !reflect.DeepEqual(newState, state) {
|
|
|
|
t.Fatalf("bad: %#v", newState)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err = os.Open(outPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newState, err = terraform.ReadState(f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-17 20:15:07 +02:00
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Primary
|
2014-07-28 05:38:41 +02:00
|
|
|
expected := p.RefreshReturn
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure there is no backup
|
2015-09-11 20:56:20 +02:00
|
|
|
_, err = os.Stat(outPath + DefaultBackupExtension)
|
2014-07-28 05:38:41 +02:00
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("backup should not exist")
|
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
_, err = os.Stat("-")
|
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("backup should not exist")
|
|
|
|
}
|
2014-07-28 05:38:41 +02:00
|
|
|
}
|
|
|
|
|
2015-06-15 17:40:56 +02:00
|
|
|
func TestRefresh_displaysOutputs(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-06-15 17:40:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("refresh-output"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that outputs were displayed
|
|
|
|
outputValue := "foo.example.com"
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(actual, outputValue) {
|
|
|
|
t.Fatalf("Expected:\n%s\n\nTo include: %q", actual, outputValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:47:25 +02:00
|
|
|
// When creating an InstaneState for direct comparison to one contained in
|
2016-08-16 16:48:21 +02:00
|
|
|
// terraform.State, all fields must be initialized (duplicating the
|
2016-08-10 21:47:25 +02:00
|
|
|
// InstanceState.init() method)
|
|
|
|
func newInstanceState(id string) *terraform.InstanceState {
|
|
|
|
return &terraform.InstanceState{
|
|
|
|
ID: id,
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
Ephemeral: terraform.EphemeralState{
|
|
|
|
ConnInfo: make(map[string]string),
|
|
|
|
},
|
2017-02-23 19:51:29 +01:00
|
|
|
Meta: make(map[string]interface{}),
|
2016-08-10 21:47:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 23:00:40 +02:00
|
|
|
const refreshVarFile = `
|
|
|
|
foo = "bar"
|
|
|
|
`
|
2014-09-18 19:40:23 +02:00
|
|
|
|
|
|
|
const testRefreshStr = `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = yes
|
|
|
|
`
|
|
|
|
const testRefreshCwdStr = `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = yes
|
|
|
|
`
|