mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
command: Fix even more of the "terraform refresh" tests
This commit is contained in:
parent
5d1d6a95f9
commit
89d0944e5b
@ -242,7 +242,10 @@ func testState() *states.State {
|
||||
Name: "foo",
|
||||
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
||||
&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,
|
||||
},
|
||||
addrs.ProviderConfig{
|
||||
|
@ -7,17 +7,21 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
|
||||
"github.com/hashicorp/terraform/addrs"
|
||||
"github.com/hashicorp/terraform/configs/configschema"
|
||||
"github.com/hashicorp/terraform/helper/copy"
|
||||
"github.com/hashicorp/terraform/providers"
|
||||
"github.com/hashicorp/terraform/states"
|
||||
"github.com/hashicorp/terraform/states/statefile"
|
||||
"github.com/hashicorp/terraform/states/statemgr"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
@ -209,15 +213,13 @@ func TestRefresh_cwd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRefresh_defaultState(t *testing.T) {
|
||||
t.Fatal("not yet updated for new provider types")
|
||||
/*
|
||||
originalState := testState()
|
||||
|
||||
// Write the state file in a temporary directory with the
|
||||
// default filename.
|
||||
statePath := testStateFile(t, originalState)
|
||||
|
||||
localState := &state.LocalState{Path: statePath}
|
||||
localState := statemgr.NewFilesystem(statePath)
|
||||
if err := localState.RefreshState(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -225,7 +227,6 @@ func TestRefresh_defaultState(t *testing.T) {
|
||||
if s == nil {
|
||||
t.Fatal("empty test state")
|
||||
}
|
||||
serial := s.Serial
|
||||
|
||||
// Change to that directory
|
||||
cwd, err := os.Getwd()
|
||||
@ -247,8 +248,12 @@ func TestRefresh_defaultState(t *testing.T) {
|
||||
}
|
||||
|
||||
p.GetSchemaReturn = refreshFixtureSchema()
|
||||
p.RefreshFn = nil
|
||||
p.RefreshReturn = newInstanceState("yes")
|
||||
p.ReadResourceFn = nil
|
||||
p.ReadResourceResponse = providers.ReadResourceResponse{
|
||||
NewState: cty.ObjectVal(map[string]cty.Value{
|
||||
"id": cty.StringVal("yes"),
|
||||
}),
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state", statePath,
|
||||
@ -258,17 +263,20 @@ func TestRefresh_defaultState(t *testing.T) {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
if !p.RefreshCalled {
|
||||
t.Fatal("refresh should be called")
|
||||
if !p.ReadResourceCalled {
|
||||
t.Fatal("ReadResource should have been called")
|
||||
}
|
||||
|
||||
newState := testStateRead(t, statePath)
|
||||
|
||||
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) {
|
||||
t.Logf("expected:\n%#v", expected)
|
||||
t.Fatalf("bad:\n%#v", actual)
|
||||
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
||||
}
|
||||
|
||||
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
|
||||
expected = originalState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
||||
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) {
|
||||
t.Fatal("not yet updated for new provider types")
|
||||
/*
|
||||
state := testState()
|
||||
statePath := testStateFile(t, state)
|
||||
|
||||
@ -306,8 +311,12 @@ func TestRefresh_outPath(t *testing.T) {
|
||||
}
|
||||
|
||||
p.GetSchemaReturn = refreshFixtureSchema()
|
||||
p.RefreshFn = nil
|
||||
p.RefreshReturn = newInstanceState("yes")
|
||||
p.ReadResourceFn = nil
|
||||
p.ReadResourceResponse = providers.ReadResourceResponse{
|
||||
NewState: cty.ObjectVal(map[string]cty.Value{
|
||||
"id": cty.StringVal("yes"),
|
||||
}),
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state", statePath,
|
||||
@ -318,55 +327,28 @@ func TestRefresh_outPath(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
|
||||
newState := testStateRead(t, statePath)
|
||||
if !reflect.DeepEqual(newState, state) {
|
||||
t.Fatalf("bad: %#v", newState)
|
||||
}
|
||||
|
||||
f, err = os.Open(outPath)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
newState = testStateRead(t, outPath)
|
||||
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
||||
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) {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
|
||||
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)
|
||||
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
||||
}
|
||||
|
||||
backupState := testStateRead(t, outPath + DefaultBackupExtension)
|
||||
actualStr := strings.TrimSpace(backupState.String())
|
||||
expectedStr := strings.TrimSpace(state.String())
|
||||
if actualStr != expectedStr {
|
||||
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func TestRefresh_var(t *testing.T) {
|
||||
@ -520,8 +502,6 @@ func TestRefresh_varsUnset(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRefresh_backup(t *testing.T) {
|
||||
t.Fatal("not yet updated for new provider types")
|
||||
/*
|
||||
state := testState()
|
||||
statePath := testStateFile(t, state)
|
||||
|
||||
@ -553,8 +533,12 @@ func TestRefresh_backup(t *testing.T) {
|
||||
}
|
||||
|
||||
p.GetSchemaReturn = refreshFixtureSchema()
|
||||
p.RefreshFn = nil
|
||||
p.RefreshReturn = newInstanceState("yes")
|
||||
p.ReadResourceFn = nil
|
||||
p.ReadResourceResponse = providers.ReadResourceResponse{
|
||||
NewState: cty.ObjectVal(map[string]cty.Value{
|
||||
"id": cty.StringVal("changed"),
|
||||
}),
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state", statePath,
|
||||
@ -566,60 +550,31 @@ func TestRefresh_backup(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
|
||||
newState := testStateRead(t, statePath)
|
||||
if !reflect.DeepEqual(newState, state) {
|
||||
t.Fatalf("bad: %#v", newState)
|
||||
}
|
||||
|
||||
f, err = os.Open(outPath)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
newState = testStateRead(t, outPath)
|
||||
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
||||
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) {
|
||||
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)
|
||||
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
||||
}
|
||||
|
||||
backupState := testStateRead(t, outPath + DefaultBackupExtension)
|
||||
actualStr := strings.TrimSpace(backupState.String())
|
||||
expectedStr := strings.TrimSpace(state.String())
|
||||
if actualStr != expectedStr {
|
||||
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func TestRefresh_disableBackup(t *testing.T) {
|
||||
t.Fatal("not yet updated for new provider types")
|
||||
/*
|
||||
state := testState()
|
||||
statePath := testStateFile(t, state)
|
||||
|
||||
@ -642,8 +597,12 @@ func TestRefresh_disableBackup(t *testing.T) {
|
||||
}
|
||||
|
||||
p.GetSchemaReturn = refreshFixtureSchema()
|
||||
p.RefreshFn = nil
|
||||
p.RefreshReturn = newInstanceState("yes")
|
||||
p.ReadResourceFn = nil
|
||||
p.ReadResourceResponse = providers.ReadResourceResponse{
|
||||
NewState: cty.ObjectVal(map[string]cty.Value{
|
||||
"id": cty.StringVal("yes"),
|
||||
}),
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state", statePath,
|
||||
@ -655,36 +614,20 @@ func TestRefresh_disableBackup(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
|
||||
newState := testStateRead(t, statePath)
|
||||
if !reflect.DeepEqual(newState, state) {
|
||||
t.Fatalf("bad: %#v", newState)
|
||||
}
|
||||
|
||||
f, err = os.Open(outPath)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
newState = testStateRead(t, outPath)
|
||||
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
||||
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) {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
||||
}
|
||||
|
||||
// Ensure there is no backup
|
||||
@ -696,7 +639,6 @@ func TestRefresh_disableBackup(t *testing.T) {
|
||||
if err == nil || !os.IsNotExist(err) {
|
||||
t.Fatalf("backup should not exist")
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func TestRefresh_displaysOutputs(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user