command: Fix TestApply_destroyTargeted for new provider and state types

This commit is contained in:
Martin Atkins 2018-09-30 08:52:29 -07:00
parent ec2e6cb06f
commit 5021e0e098
1 changed files with 16 additions and 9 deletions

View File

@ -5,11 +5,13 @@ import (
"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/addrs"
"github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/configs/configschema"
"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/terraform" "github.com/hashicorp/terraform/terraform"
@ -219,11 +221,17 @@ func TestApply_destroyTargeted(t *testing.T) {
}, },
"test_load_balancer": { "test_load_balancer": {
Attributes: map[string]*configschema.Attribute{ Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
"instances": {Type: cty.List(cty.String), Optional: true}, "instances": {Type: cty.List(cty.String), Optional: true},
}, },
}, },
}, },
} }
p.PlanResourceChangeFn = func (req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &ApplyCommand{ c := &ApplyCommand{
@ -256,18 +264,17 @@ func TestApply_destroyTargeted(t *testing.T) {
} }
defer f.Close() defer f.Close()
state, err := terraform.ReadState(f) stateFile, err := statefile.Read(f)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if state == nil { if stateFile == nil || stateFile.State == nil {
t.Fatal("state should not be nil") t.Fatal("state should not be nil")
} }
actualStr := strings.TrimSpace(state.String()) spew.Config.DisableMethods = true
expectedStr := strings.TrimSpace(testApplyDestroyStr) if !stateFile.State.Empty() {
if actualStr != expectedStr { t.Fatalf("unexpected final state\ngot: %s\nwant: empty state", spew.Sdump(stateFile.State))
t.Fatalf("bad:\n\n%s\n\nexpected:\n\n%s", actualStr, expectedStr)
} }
// Should have a backup file // Should have a backup file
@ -276,14 +283,14 @@ func TestApply_destroyTargeted(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
backupState, err := terraform.ReadState(f) backupStateFile, err := statefile.Read(f)
f.Close() f.Close()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
actualStr = strings.TrimSpace(backupState.String()) actualStr := strings.TrimSpace(backupStateFile.State.String())
expectedStr = strings.TrimSpace(originalState.String()) expectedStr := strings.TrimSpace(originalState.String())
if actualStr != expectedStr { if actualStr != expectedStr {
t.Fatalf("bad:\n\nactual:\n%s\n\nexpected:\nb%s", actualStr, expectedStr) t.Fatalf("bad:\n\nactual:\n%s\n\nexpected:\nb%s", actualStr, expectedStr)
} }