From cdad3036aedf77be067ef6a6a73927d6a1b6406d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 17 Sep 2014 11:15:07 -0700 Subject: [PATCH] command: closer to compiling --- command/apply.go | 12 +++-- command/apply_test.go | 107 +++++++++++++++++++++++++-------------- command/command_test.go | 22 +++++++- command/format_state.go | 43 +++++++++------- command/hook_count.go | 4 +- command/hook_ui.go | 8 +-- command/output.go | 4 +- command/output_test.go | 43 ++++++++++++---- command/plan_test.go | 92 +++++++++++++++++++++++---------- command/refresh_test.go | 109 +++++++++------------------------------- command/show_test.go | 10 +--- 11 files changed, 254 insertions(+), 200 deletions(-) diff --git a/command/apply.go b/command/apply.go index a3d6f204e..611f2f250 100644 --- a/command/apply.go +++ b/command/apply.go @@ -182,14 +182,18 @@ func (c *ApplyCommand) Run(args []string) int { } // If we have outputs, then output those at the end. - if state != nil && len(state.Outputs) > 0 { + var outputs map[string]string + if state != nil { + outputs = state.RootModule().Outputs + } + if len(outputs) > 0 { outputBuf := new(bytes.Buffer) outputBuf.WriteString("[reset][bold][green]\nOutputs:\n\n") // Output the outputs in alphabetical order keyLen := 0 - keys := make([]string, 0, len(state.Outputs)) - for key, _ := range state.Outputs { + keys := make([]string, 0, len(outputs)) + for key, _ := range outputs { keys = append(keys, key) if len(key) > keyLen { keyLen = len(key) @@ -198,7 +202,7 @@ func (c *ApplyCommand) Run(args []string) int { sort.Strings(keys) for _, k := range keys { - v := state.Outputs[k] + v := outputs[k] outputBuf.WriteString(fmt.Sprintf( " %s%s = %s\n", diff --git a/command/apply_test.go b/command/apply_test.go index 5cd7b7da0..f45737c59 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -140,8 +140,9 @@ func TestApply_error(t *testing.T) { var lock sync.Mutex errored := false p.ApplyFn = func( - s *terraform.ResourceState, - d *terraform.ResourceDiff) (*terraform.ResourceState, error) { + info *terraform.InstanceInfo, + s *terraform.InstanceState, + d *terraform.ResourceDiff) (*terraform.InstanceState, error) { lock.Lock() defer lock.Unlock() @@ -150,10 +151,11 @@ func TestApply_error(t *testing.T) { return nil, fmt.Errorf("error") } - return &terraform.ResourceState{ID: "foo"}, nil + return &terraform.InstanceState{ID: "foo"}, nil } p.DiffFn = func( - *terraform.ResourceState, + *terraform.InstanceInfo, + *terraform.InstanceState, *terraform.ResourceConfig) (*terraform.ResourceDiff, error) { return &terraform.ResourceDiff{ Attributes: map[string]*terraform.ResourceAttrDiff{ @@ -189,7 +191,7 @@ func TestApply_error(t *testing.T) { if state == nil { t.Fatal("state should not be nil") } - if len(state.Resources) == 0 { + if len(state.RootModule().Resources) == 0 { t.Fatal("no resources in state") } } @@ -367,10 +369,17 @@ func TestApply_planVars(t *testing.T) { func TestApply_refresh(t *testing.T) { originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", + 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", + }, + }, + }, }, }, } @@ -453,7 +462,8 @@ func TestApply_shutdown(t *testing.T) { } p.DiffFn = func( - *terraform.ResourceState, + *terraform.InstanceInfo, + *terraform.InstanceState, *terraform.ResourceConfig) (*terraform.ResourceDiff, error) { return &terraform.ResourceDiff{ Attributes: map[string]*terraform.ResourceAttrDiff{ @@ -464,15 +474,16 @@ func TestApply_shutdown(t *testing.T) { }, nil } p.ApplyFn = func( - *terraform.ResourceState, - *terraform.ResourceDiff) (*terraform.ResourceState, error) { + *terraform.InstanceInfo, + *terraform.InstanceState, + *terraform.ResourceDiff) (*terraform.InstanceState, error) { if !stopped { stopped = true close(stopCh) <-stopReplyCh } - return &terraform.ResourceState{ + return &terraform.InstanceState{ ID: "foo", Attributes: map[string]string{ "ami": "2", @@ -518,18 +529,24 @@ func TestApply_shutdown(t *testing.T) { t.Fatal("state should not be nil") } - if len(state.Resources) != 1 { - t.Fatalf("bad: %d", len(state.Resources)) + if len(state.RootModule().Resources) != 1 { + t.Fatalf("bad: %d", len(state.RootModule().Resources)) } } func TestApply_state(t *testing.T) { originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - ConnInfo: make(map[string]string), + 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", + }, + }, + }, }, }, } @@ -563,7 +580,7 @@ func TestApply_state(t *testing.T) { } // Verify that the provider was called with the existing state - expectedState := originalState.Resources["test_instance.foo"] + expectedState := originalState.RootModule().Resources["test_instance.foo"] if !reflect.DeepEqual(p.DiffState, expectedState) { t.Fatalf("bad: %#v", p.DiffState) } @@ -604,7 +621,7 @@ func TestApply_state(t *testing.T) { } // nil out the ConnInfo since that should not be restored - originalState.Resources["test_instance.foo"].ConnInfo = nil + originalState.RootModule().Resources["test_instance.foo"].Primary.Ephemeral.ConnInfo = nil if !reflect.DeepEqual(backupState, originalState) { t.Fatalf("bad: %#v", backupState) @@ -644,7 +661,8 @@ func TestApply_vars(t *testing.T) { actual := "" p.DiffFn = func( - s *terraform.ResourceState, + info *terraform.InstanceInfo, + s *terraform.InstanceState, c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) { if v, ok := c.Config["value"]; ok { actual = v.(string) @@ -686,7 +704,8 @@ func TestApply_varFile(t *testing.T) { actual := "" p.DiffFn = func( - s *terraform.ResourceState, + info *terraform.InstanceInfo, + s *terraform.InstanceState, c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) { if v, ok := c.Config["value"]; ok { actual = v.(string) @@ -738,7 +757,8 @@ func TestApply_varFileDefault(t *testing.T) { actual := "" p.DiffFn = func( - s *terraform.ResourceState, + info *terraform.InstanceInfo, + s *terraform.InstanceState, c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) { if v, ok := c.Config["value"]; ok { actual = v.(string) @@ -762,10 +782,17 @@ func TestApply_varFileDefault(t *testing.T) { func TestApply_backup(t *testing.T) { originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", + 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", + }, + }, + }, }, }, } @@ -831,8 +858,8 @@ func TestApply_backup(t *testing.T) { t.Fatalf("err: %s", err) } - actual := backupState.Resources["test_instance.foo"] - expected := originalState.Resources["test_instance.foo"] + actual := backupState.RootModule().Resources["test_instance.foo"] + expected := originalState.RootModule().Resources["test_instance.foo"] if !reflect.DeepEqual(actual, expected) { t.Fatalf("bad: %#v %#v", actual, expected) } @@ -840,11 +867,17 @@ func TestApply_backup(t *testing.T) { func TestApply_disableBackup(t *testing.T) { originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - ConnInfo: make(map[string]string), + 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", + }, + }, + }, }, }, } @@ -879,7 +912,7 @@ func TestApply_disableBackup(t *testing.T) { } // Verify that the provider was called with the existing state - expectedState := originalState.Resources["test_instance.foo"] + expectedState := originalState.RootModule().Resources["test_instance.foo"] if !reflect.DeepEqual(p.DiffState, expectedState) { t.Fatalf("bad: %#v", p.DiffState) } diff --git a/command/command_test.go b/command/command_test.go index 09f8022f2..0acfa9f78 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -67,6 +67,25 @@ func testReadPlan(t *testing.T, path string) *terraform.Plan { return p } +// testState returns a test State structure that we use for a lot of tests. +func testState() *terraform.State { + return &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", + }, + }, + }, + }, + }, + } +} + func testStateFile(t *testing.T, s *terraform.State) string { path := testTempFile(t) @@ -87,7 +106,8 @@ func testProvider() *terraform.MockResourceProvider { p := new(terraform.MockResourceProvider) p.DiffReturn = &terraform.ResourceDiff{} p.RefreshFn = func( - s *terraform.ResourceState) (*terraform.ResourceState, error) { + info *terraform.InstanceInfo, + s *terraform.InstanceState) (*terraform.InstanceState, error) { return s, nil } p.ResourcesReturn = []terraform.ResourceType{ diff --git a/command/format_state.go b/command/format_state.go index d21d036e9..90405042b 100644 --- a/command/format_state.go +++ b/command/format_state.go @@ -16,42 +16,49 @@ func FormatState(s *terraform.State, c *colorstring.Colorize) string { panic("colorize not given") } - if len(s.Resources) == 0 { + if len(s.Modules) == 0 { return "The state file is empty. No resources are represented." } - buf := new(bytes.Buffer) + var buf bytes.Buffer + for _, m := range s.Modules { + formatStateModule(&buf, m, c) + } + + return c.Color(strings.TrimSpace(buf.String())) +} + +func formatStateModule(buf *bytes.Buffer, m *terraform.ModuleState, c *colorstring.Colorize) { buf.WriteString("[reset]") // First get the names of all the resources so we can show them // in alphabetical order. - names := make([]string, 0, len(s.Resources)) - for name, _ := range s.Resources { + names := make([]string, 0, len(m.Resources)) + for name, _ := range m.Resources { names = append(names, name) } sort.Strings(names) // Go through each resource and begin building up the output. for _, k := range names { - rs := s.Resources[k] - id := rs.ID + rs := m.Resources[k] + is := rs.Primary + id := is.ID if id == "" { id = "" } taintStr := "" - if s.Tainted != nil { - if _, ok := s.Tainted[k]; ok { - taintStr = " (tainted)" - } + if len(rs.Tainted) > 0 { + taintStr = " (tainted)" } buf.WriteString(fmt.Sprintf("%s:%s\n", k, taintStr)) buf.WriteString(fmt.Sprintf(" id = %s\n", id)) // Sort the attributes - attrKeys := make([]string, 0, len(rs.Attributes)) - for ak, _ := range rs.Attributes { + attrKeys := make([]string, 0, len(is.Attributes)) + for ak, _ := range is.Attributes { // Skip the id attribute since we just show the id directly if ak == "id" { continue @@ -63,27 +70,25 @@ func FormatState(s *terraform.State, c *colorstring.Colorize) string { // Output each attribute for _, ak := range attrKeys { - av := rs.Attributes[ak] + av := is.Attributes[ak] buf.WriteString(fmt.Sprintf(" %s = %s\n", ak, av)) } } - if len(s.Outputs) > 0 { + if len(m.Outputs) > 0 { buf.WriteString("\nOutputs:\n\n") // Sort the outputs - ks := make([]string, 0, len(s.Outputs)) - for k, _ := range s.Outputs { + ks := make([]string, 0, len(m.Outputs)) + for k, _ := range m.Outputs { ks = append(ks, k) } sort.Strings(ks) // Output each output k/v pair for _, k := range ks { - v := s.Outputs[k] + v := m.Outputs[k] buf.WriteString(fmt.Sprintf("%s = %s\n", k, v)) } } - - return c.Color(strings.TrimSpace(buf.String())) } diff --git a/command/hook_count.go b/command/hook_count.go index 152d3170e..bbebcc78c 100644 --- a/command/hook_count.go +++ b/command/hook_count.go @@ -39,7 +39,7 @@ func (h *CountHook) Reset() { func (h *CountHook) PreApply( id string, - s *terraform.ResourceState, + s *terraform.InstanceState, d *terraform.ResourceDiff) (terraform.HookAction, error) { h.Lock() defer h.Unlock() @@ -62,7 +62,7 @@ func (h *CountHook) PreApply( func (h *CountHook) PostApply( id string, - s *terraform.ResourceState, + s *terraform.InstanceState, e error) (terraform.HookAction, error) { h.Lock() defer h.Unlock() diff --git a/command/hook_ui.go b/command/hook_ui.go index 3b0771c80..8fe6e7c63 100644 --- a/command/hook_ui.go +++ b/command/hook_ui.go @@ -35,7 +35,7 @@ const ( func (h *UiHook) PreApply( id string, - s *terraform.ResourceState, + s *terraform.InstanceState, d *terraform.ResourceDiff) (terraform.HookAction, error) { h.once.Do(h.init) @@ -114,7 +114,7 @@ func (h *UiHook) PreApply( func (h *UiHook) PostApply( id string, - s *terraform.ResourceState, + s *terraform.InstanceState, applyerr error) (terraform.HookAction, error) { h.l.Lock() op := h.resources[id] @@ -145,7 +145,7 @@ func (h *UiHook) PostApply( } func (h *UiHook) PreDiff( - id string, s *terraform.ResourceState) (terraform.HookAction, error) { + id string, s *terraform.InstanceState) (terraform.HookAction, error) { return terraform.HookActionContinue, nil } @@ -157,7 +157,7 @@ func (h *UiHook) PreProvision(id, provId string) (terraform.HookAction, error) { } func (h *UiHook) PreRefresh( - id string, s *terraform.ResourceState) (terraform.HookAction, error) { + id string, s *terraform.InstanceState) (terraform.HookAction, error) { h.once.Do(h.init) h.ui.Output(h.Colorize.Color(fmt.Sprintf( diff --git a/command/output.go b/command/output.go index ab7292dd6..8e783d923 100644 --- a/command/output.go +++ b/command/output.go @@ -50,14 +50,14 @@ func (c *OutputCommand) Run(args []string) int { return 1 } - if len(state.Outputs) == 0 { + if len(state.RootModule().Outputs) == 0 { c.Ui.Error(fmt.Sprintf( "The state file has no outputs defined. Define an output\n" + "in your configuration with the `output` directive and re-run\n" + "`terraform apply` for it to become available.")) return 1 } - v, ok := state.Outputs[name] + v, ok := state.RootModule().Outputs[name] if !ok { c.Ui.Error(fmt.Sprintf( "The output variable requested could not be found in the state\n" + diff --git a/command/output_test.go b/command/output_test.go index f177198c5..5714ac20d 100644 --- a/command/output_test.go +++ b/command/output_test.go @@ -13,8 +13,13 @@ import ( func TestOutput(t *testing.T) { originalState := &terraform.State{ - Outputs: map[string]string{ - "foo": "bar", + Modules: []*terraform.ModuleState{ + &terraform.ModuleState{ + Path: []string{"root"}, + Outputs: map[string]string{ + "foo": "bar", + }, + }, }, } @@ -44,8 +49,13 @@ func TestOutput(t *testing.T) { func TestOutput_badVar(t *testing.T) { originalState := &terraform.State{ - Outputs: map[string]string{ - "foo": "bar", + Modules: []*terraform.ModuleState{ + &terraform.ModuleState{ + Path: []string{"root"}, + Outputs: map[string]string{ + "foo": "bar", + }, + }, }, } @@ -70,8 +80,13 @@ func TestOutput_badVar(t *testing.T) { func TestOutput_blank(t *testing.T) { originalState := &terraform.State{ - Outputs: map[string]string{ - "foo": "bar", + Modules: []*terraform.ModuleState{ + &terraform.ModuleState{ + Path: []string{"root"}, + Outputs: map[string]string{ + "foo": "bar", + }, + }, }, } @@ -129,7 +144,12 @@ func TestOutput_noArgs(t *testing.T) { func TestOutput_noVars(t *testing.T) { originalState := &terraform.State{ - Outputs: map[string]string{}, + Modules: []*terraform.ModuleState{ + &terraform.ModuleState{ + Path: []string{"root"}, + Outputs: map[string]string{}, + }, + }, } statePath := testStateFile(t, originalState) @@ -153,8 +173,13 @@ func TestOutput_noVars(t *testing.T) { func TestOutput_stateDefault(t *testing.T) { originalState := &terraform.State{ - Outputs: map[string]string{ - "foo": "bar", + Modules: []*terraform.ModuleState{ + &terraform.ModuleState{ + Path: []string{"root"}, + Outputs: map[string]string{ + "foo": "bar", + }, + }, }, } diff --git a/command/plan_test.go b/command/plan_test.go index af211cd2f..af4ab8cca 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -38,10 +38,17 @@ func TestPlan(t *testing.T) { func TestPlan_destroy(t *testing.T) { originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", + 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", + }, + }, + }, }, }, } @@ -198,10 +205,17 @@ func TestPlan_state(t *testing.T) { defer os.Remove(tf.Name()) originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", + 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", + }, + }, + }, }, }, } @@ -230,7 +244,7 @@ func TestPlan_state(t *testing.T) { } // Verify that the provider was called with the existing state - expectedState := originalState.Resources["test_instance.foo"] + expectedState := originalState.RootModule().Resources["test_instance.foo"] if !reflect.DeepEqual(p.DiffState, expectedState) { t.Fatalf("bad: %#v", p.DiffState) } @@ -238,10 +252,17 @@ func TestPlan_state(t *testing.T) { func TestPlan_stateDefault(t *testing.T) { originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", + 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", + }, + }, + }, }, }, } @@ -291,7 +312,7 @@ func TestPlan_stateDefault(t *testing.T) { } // Verify that the provider was called with the existing state - expectedState := originalState.Resources["test_instance.foo"] + expectedState := originalState.RootModule().Resources["test_instance.foo"] if !reflect.DeepEqual(p.DiffState, expectedState) { t.Fatalf("bad: %#v", p.DiffState) } @@ -309,7 +330,8 @@ func TestPlan_vars(t *testing.T) { actual := "" p.DiffFn = func( - s *terraform.ResourceState, + info *terraform.InstanceInfo, + s *terraform.InstanceState, c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) { if v, ok := c.Config["value"]; ok { actual = v.(string) @@ -348,7 +370,8 @@ func TestPlan_varFile(t *testing.T) { actual := "" p.DiffFn = func( - s *terraform.ResourceState, + info *terraform.InstanceInfo, + s *terraform.InstanceState, c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) { if v, ok := c.Config["value"]; ok { actual = v.(string) @@ -397,7 +420,8 @@ func TestPlan_varFileDefault(t *testing.T) { actual := "" p.DiffFn = func( - s *terraform.ResourceState, + info *terraform.InstanceInfo, + s *terraform.InstanceState, c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) { if v, ok := c.Config["value"]; ok { actual = v.(string) @@ -438,10 +462,17 @@ func TestPlan_backup(t *testing.T) { defer os.Remove(backupPath) originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", + 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", + }, + }, + }, }, }, } @@ -471,7 +502,7 @@ func TestPlan_backup(t *testing.T) { } // Verify that the provider was called with the existing state - expectedState := originalState.Resources["test_instance.foo"] + expectedState := originalState.RootModule().Resources["test_instance.foo"] if !reflect.DeepEqual(p.DiffState, expectedState) { t.Fatalf("bad: %#v", p.DiffState) } @@ -503,10 +534,17 @@ func TestPlan_disableBackup(t *testing.T) { defer os.Remove(tf.Name()) originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", + 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", + }, + }, + }, }, }, } @@ -536,7 +574,7 @@ func TestPlan_disableBackup(t *testing.T) { } // Verify that the provider was called with the existing state - expectedState := originalState.Resources["test_instance.foo"] + expectedState := originalState.RootModule().Resources["test_instance.foo"] if !reflect.DeepEqual(p.DiffState, expectedState) { t.Fatalf("bad: %#v", p.DiffState) } diff --git a/command/refresh_test.go b/command/refresh_test.go index 145c4699a..073be20cd 100644 --- a/command/refresh_test.go +++ b/command/refresh_test.go @@ -12,14 +12,7 @@ import ( ) func TestRefresh(t *testing.T) { - state := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - }, - }, - } + state := testState() statePath := testStateFile(t, state) p := testProvider() @@ -32,7 +25,7 @@ func TestRefresh(t *testing.T) { } p.RefreshFn = nil - p.RefreshReturn = &terraform.ResourceState{ID: "yes"} + p.RefreshReturn = &terraform.InstanceState{ID: "yes"} args := []string{ "-state", statePath, @@ -57,7 +50,7 @@ func TestRefresh(t *testing.T) { t.Fatalf("err: %s", err) } - actual := newState.Resources["test_instance.foo"] + actual := newState.RootModule().Resources["test_instance.foo"] expected := p.RefreshReturn if !reflect.DeepEqual(actual, expected) { t.Fatalf("bad: %#v", actual) @@ -93,14 +86,7 @@ func TestRefresh_cwd(t *testing.T) { } defer os.Chdir(cwd) - state := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - }, - }, - } + state := testState() statePath := testStateFile(t, state) p := testProvider() @@ -113,7 +99,7 @@ func TestRefresh_cwd(t *testing.T) { } p.RefreshFn = nil - p.RefreshReturn = &terraform.ResourceState{ID: "yes"} + p.RefreshReturn = &terraform.InstanceState{ID: "yes"} args := []string{ "-state", statePath, @@ -137,7 +123,7 @@ func TestRefresh_cwd(t *testing.T) { t.Fatalf("err: %s", err) } - actual := newState.Resources["test_instance.foo"] + actual := newState.RootModule().Resources["test_instance.foo"] expected := p.RefreshReturn if !reflect.DeepEqual(actual, expected) { t.Fatalf("bad: %#v", actual) @@ -145,14 +131,7 @@ func TestRefresh_cwd(t *testing.T) { } func TestRefresh_defaultState(t *testing.T) { - originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - }, - }, - } + originalState := testState() // Write the state file in a temporary directory with the // default filename. @@ -192,7 +171,7 @@ func TestRefresh_defaultState(t *testing.T) { } p.RefreshFn = nil - p.RefreshReturn = &terraform.ResourceState{ID: "yes"} + p.RefreshReturn = &terraform.InstanceState{ID: "yes"} args := []string{ testFixturePath("refresh"), @@ -216,7 +195,7 @@ func TestRefresh_defaultState(t *testing.T) { t.Fatalf("err: %s", err) } - actual := newState.Resources["test_instance.foo"] + actual := newState.RootModule().Resources["test_instance.foo"].Primary expected := p.RefreshReturn if !reflect.DeepEqual(actual, expected) { t.Fatalf("bad: %#v", actual) @@ -233,22 +212,15 @@ func TestRefresh_defaultState(t *testing.T) { t.Fatalf("err: %s", err) } - actual = backupState.Resources["test_instance.foo"] - expected = originalState.Resources["test_instance.foo"] + actual = backupState.RootModule().Resources["test_instance.foo"].Primary + expected = originalState.RootModule().Resources["test_instance.foo"].Primary if !reflect.DeepEqual(actual, expected) { t.Fatalf("bad: %#v", actual) } } func TestRefresh_outPath(t *testing.T) { - state := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - }, - }, - } + state := testState() statePath := testStateFile(t, state) // Output path @@ -270,7 +242,7 @@ func TestRefresh_outPath(t *testing.T) { } p.RefreshFn = nil - p.RefreshReturn = &terraform.ResourceState{ID: "yes"} + p.RefreshReturn = &terraform.InstanceState{ID: "yes"} args := []string{ "-state", statePath, @@ -307,7 +279,7 @@ func TestRefresh_outPath(t *testing.T) { t.Fatalf("err: %s", err) } - actual := newState.Resources["test_instance.foo"] + actual := newState.RootModule().Resources["test_instance.foo"].Primary expected := p.RefreshReturn if !reflect.DeepEqual(actual, expected) { t.Fatalf("bad: %#v", actual) @@ -330,14 +302,7 @@ func TestRefresh_outPath(t *testing.T) { } func TestRefresh_var(t *testing.T) { - state := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - }, - }, - } + state := testState() statePath := testStateFile(t, state) p := testProvider() @@ -367,14 +332,7 @@ func TestRefresh_var(t *testing.T) { } func TestRefresh_varFile(t *testing.T) { - state := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - }, - }, - } + state := testState() statePath := testStateFile(t, state) p := testProvider() @@ -409,14 +367,7 @@ func TestRefresh_varFile(t *testing.T) { } func TestRefresh_varFileDefault(t *testing.T) { - state := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - }, - }, - } + state := testState() statePath := testStateFile(t, state) p := testProvider() @@ -460,14 +411,7 @@ func TestRefresh_varFileDefault(t *testing.T) { } func TestRefresh_backup(t *testing.T) { - state := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - }, - }, - } + state := testState() statePath := testStateFile(t, state) // Output path @@ -498,7 +442,7 @@ func TestRefresh_backup(t *testing.T) { } p.RefreshFn = nil - p.RefreshReturn = &terraform.ResourceState{ID: "yes"} + p.RefreshReturn = &terraform.InstanceState{ID: "yes"} args := []string{ "-state", statePath, @@ -536,7 +480,7 @@ func TestRefresh_backup(t *testing.T) { t.Fatalf("err: %s", err) } - actual := newState.Resources["test_instance.foo"] + actual := newState.RootModule().Resources["test_instance.foo"].Primary expected := p.RefreshReturn if !reflect.DeepEqual(actual, expected) { t.Fatalf("bad: %#v", actual) @@ -559,14 +503,7 @@ func TestRefresh_backup(t *testing.T) { } func TestRefresh_disableBackup(t *testing.T) { - state := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - }, - }, - } + state := testState() statePath := testStateFile(t, state) // Output path @@ -588,7 +525,7 @@ func TestRefresh_disableBackup(t *testing.T) { } p.RefreshFn = nil - p.RefreshReturn = &terraform.ResourceState{ID: "yes"} + p.RefreshReturn = &terraform.InstanceState{ID: "yes"} args := []string{ "-state", statePath, @@ -626,7 +563,7 @@ func TestRefresh_disableBackup(t *testing.T) { t.Fatalf("err: %s", err) } - actual := newState.Resources["test_instance.foo"] + actual := newState.RootModule().Resources["test_instance.foo"].Primary expected := p.RefreshReturn if !reflect.DeepEqual(actual, expected) { t.Fatalf("bad: %#v", actual) diff --git a/command/show_test.go b/command/show_test.go index c1bebf247..3025f258e 100644 --- a/command/show_test.go +++ b/command/show_test.go @@ -63,15 +63,7 @@ func TestShow_plan(t *testing.T) { } func TestShow_state(t *testing.T) { - originalState := &terraform.State{ - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - ID: "bar", - Type: "test_instance", - }, - }, - } - + originalState := testState() statePath := testStateFile(t, originalState) ui := new(cli.MockUi)