command: Fix even more of the "terraform refresh" tests

This commit is contained in:
Martin Atkins 2018-10-14 16:07:42 -07:00
parent 5d1d6a95f9
commit 89d0944e5b
2 changed files with 242 additions and 297 deletions

View File

@ -242,7 +242,10 @@ func testState() *states.State {
Name: "foo", Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance), }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{ &states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"id":"bar"}`), // The weird whitespace here is reflective of how this would
// get written out in a real state file, due to the indentation
// of all of the containing wrapping objects and arrays.
AttrsJSON: []byte("{\n \"id\": \"bar\"\n }"),
Status: states.ObjectReady, Status: states.ObjectReady,
}, },
addrs.ProviderConfig{ addrs.ProviderConfig{

View File

@ -7,17 +7,21 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"strings" "strings"
"testing" "testing"
"github.com/davecgh/go-spew/spew"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/helper/copy" "github.com/hashicorp/terraform/helper/copy"
"github.com/hashicorp/terraform/providers" "github.com/hashicorp/terraform/providers"
"github.com/hashicorp/terraform/states" "github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/states/statefile" "github.com/hashicorp/terraform/states/statefile"
"github.com/hashicorp/terraform/states/statemgr"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -209,15 +213,13 @@ func TestRefresh_cwd(t *testing.T) {
} }
func TestRefresh_defaultState(t *testing.T) { func TestRefresh_defaultState(t *testing.T) {
t.Fatal("not yet updated for new provider types")
/*
originalState := testState() originalState := testState()
// Write the state file in a temporary directory with the // Write the state file in a temporary directory with the
// default filename. // default filename.
statePath := testStateFile(t, originalState) statePath := testStateFile(t, originalState)
localState := &state.LocalState{Path: statePath} localState := statemgr.NewFilesystem(statePath)
if err := localState.RefreshState(); err != nil { if err := localState.RefreshState(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -225,7 +227,6 @@ func TestRefresh_defaultState(t *testing.T) {
if s == nil { if s == nil {
t.Fatal("empty test state") t.Fatal("empty test state")
} }
serial := s.Serial
// Change to that directory // Change to that directory
cwd, err := os.Getwd() cwd, err := os.Getwd()
@ -247,8 +248,12 @@ func TestRefresh_defaultState(t *testing.T) {
} }
p.GetSchemaReturn = refreshFixtureSchema() p.GetSchemaReturn = refreshFixtureSchema()
p.RefreshFn = nil p.ReadResourceFn = nil
p.RefreshReturn = newInstanceState("yes") p.ReadResourceResponse = providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
}),
}
args := []string{ args := []string{
"-state", statePath, "-state", statePath,
@ -258,17 +263,20 @@ func TestRefresh_defaultState(t *testing.T) {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
} }
if !p.RefreshCalled { if !p.ReadResourceCalled {
t.Fatal("refresh should be called") t.Fatal("ReadResource should have been called")
} }
newState := testStateRead(t, statePath) newState := testStateRead(t, statePath)
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
expected := p.RefreshReturn expected := &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
Dependencies: []addrs.Referenceable{},
}
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {
t.Logf("expected:\n%#v", expected) t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
t.Fatalf("bad:\n%#v", actual)
} }
backupState := testStateRead(t, statePath+DefaultBackupExtension) backupState := testStateRead(t, statePath+DefaultBackupExtension)
@ -276,14 +284,11 @@ func TestRefresh_defaultState(t *testing.T) {
actual = backupState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current actual = backupState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
expected = originalState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current expected = originalState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual) t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
} }
*/
} }
func TestRefresh_outPath(t *testing.T) { func TestRefresh_outPath(t *testing.T) {
t.Fatal("not yet updated for new provider types")
/*
state := testState() state := testState()
statePath := testStateFile(t, state) statePath := testStateFile(t, state)
@ -306,8 +311,12 @@ func TestRefresh_outPath(t *testing.T) {
} }
p.GetSchemaReturn = refreshFixtureSchema() p.GetSchemaReturn = refreshFixtureSchema()
p.RefreshFn = nil p.ReadResourceFn = nil
p.RefreshReturn = newInstanceState("yes") p.ReadResourceResponse = providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
}),
}
args := []string{ args := []string{
"-state", statePath, "-state", statePath,
@ -318,55 +327,28 @@ func TestRefresh_outPath(t *testing.T) {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
} }
f, err := os.Open(statePath) newState := testStateRead(t, 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) { if !reflect.DeepEqual(newState, state) {
t.Fatalf("bad: %#v", newState) t.Fatalf("bad: %#v", newState)
} }
f, err = os.Open(outPath) newState = testStateRead(t, outPath)
if err != nil { actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
t.Fatalf("err: %s", err) expected := &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
Dependencies: []addrs.Referenceable{},
} }
newState, err = terraform.ReadState(f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := newState.RootModule().Resources["test_instance.foo"].Primary
expected := p.RefreshReturn
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual) t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
}
f, err = os.Open(outPath + DefaultBackupExtension)
if err != nil {
t.Fatalf("err: %s", err)
}
backupState, err := terraform.ReadState(f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
} }
backupState := testStateRead(t, outPath + DefaultBackupExtension)
actualStr := strings.TrimSpace(backupState.String()) actualStr := strings.TrimSpace(backupState.String())
expectedStr := strings.TrimSpace(state.String()) expectedStr := strings.TrimSpace(state.String())
if actualStr != expectedStr { if actualStr != expectedStr {
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr) t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
} }
*/
} }
func TestRefresh_var(t *testing.T) { func TestRefresh_var(t *testing.T) {
@ -520,8 +502,6 @@ func TestRefresh_varsUnset(t *testing.T) {
} }
func TestRefresh_backup(t *testing.T) { func TestRefresh_backup(t *testing.T) {
t.Fatal("not yet updated for new provider types")
/*
state := testState() state := testState()
statePath := testStateFile(t, state) statePath := testStateFile(t, state)
@ -553,8 +533,12 @@ func TestRefresh_backup(t *testing.T) {
} }
p.GetSchemaReturn = refreshFixtureSchema() p.GetSchemaReturn = refreshFixtureSchema()
p.RefreshFn = nil p.ReadResourceFn = nil
p.RefreshReturn = newInstanceState("yes") p.ReadResourceResponse = providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("changed"),
}),
}
args := []string{ args := []string{
"-state", statePath, "-state", statePath,
@ -566,60 +550,31 @@ func TestRefresh_backup(t *testing.T) {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
} }
f, err := os.Open(statePath) newState := testStateRead(t, 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) { if !reflect.DeepEqual(newState, state) {
t.Fatalf("bad: %#v", newState) t.Fatalf("bad: %#v", newState)
} }
f, err = os.Open(outPath) newState = testStateRead(t, outPath)
if err != nil { actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
t.Fatalf("err: %s", err) expected := &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"changed\"\n }"),
Dependencies: []addrs.Referenceable{},
} }
newState, err = terraform.ReadState(f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := newState.RootModule().Resources["test_instance.foo"].Primary
expected := p.RefreshReturn
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual) t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
}
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)
} }
backupState := testStateRead(t, outPath + DefaultBackupExtension)
actualStr := strings.TrimSpace(backupState.String()) actualStr := strings.TrimSpace(backupState.String())
expectedStr := strings.TrimSpace(state.String()) expectedStr := strings.TrimSpace(state.String())
if actualStr != expectedStr { if actualStr != expectedStr {
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr) t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
} }
*/
} }
func TestRefresh_disableBackup(t *testing.T) { func TestRefresh_disableBackup(t *testing.T) {
t.Fatal("not yet updated for new provider types")
/*
state := testState() state := testState()
statePath := testStateFile(t, state) statePath := testStateFile(t, state)
@ -642,8 +597,12 @@ func TestRefresh_disableBackup(t *testing.T) {
} }
p.GetSchemaReturn = refreshFixtureSchema() p.GetSchemaReturn = refreshFixtureSchema()
p.RefreshFn = nil p.ReadResourceFn = nil
p.RefreshReturn = newInstanceState("yes") p.ReadResourceResponse = providers.ReadResourceResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yes"),
}),
}
args := []string{ args := []string{
"-state", statePath, "-state", statePath,
@ -655,36 +614,20 @@ func TestRefresh_disableBackup(t *testing.T) {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
} }
f, err := os.Open(statePath) newState := testStateRead(t, 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) { if !reflect.DeepEqual(newState, state) {
t.Fatalf("bad: %#v", newState) t.Fatalf("bad: %#v", newState)
} }
f, err = os.Open(outPath) newState = testStateRead(t, outPath)
if err != nil { actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
t.Fatalf("err: %s", err) expected := &states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
Dependencies: []addrs.Referenceable{},
} }
newState, err = terraform.ReadState(f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := newState.RootModule().Resources["test_instance.foo"].Primary
expected := p.RefreshReturn
if !reflect.DeepEqual(actual, expected) { if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual) t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
} }
// Ensure there is no backup // Ensure there is no backup
@ -696,7 +639,6 @@ func TestRefresh_disableBackup(t *testing.T) {
if err == nil || !os.IsNotExist(err) { if err == nil || !os.IsNotExist(err) {
t.Fatalf("backup should not exist") t.Fatalf("backup should not exist")
} }
*/
} }
func TestRefresh_displaysOutputs(t *testing.T) { func TestRefresh_displaysOutputs(t *testing.T) {