command: tests pass

This commit is contained in:
Mitchell Hashimoto 2014-09-18 10:40:23 -07:00
parent 1dcdd7a336
commit 15564b04a5
4 changed files with 126 additions and 82 deletions

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -580,13 +581,16 @@ func TestApply_state(t *testing.T) {
} }
// Verify that the provider was called with the existing state // Verify that the provider was called with the existing state
expectedState := originalState.RootModule().Resources["test_instance.foo"] actual := strings.TrimSpace(p.DiffState.String())
if !reflect.DeepEqual(p.DiffState, expectedState) { expected := strings.TrimSpace(testApplyStateDiffStr)
t.Fatalf("bad: %#v", p.DiffState) if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
} }
if !reflect.DeepEqual(p.ApplyState, expectedState) { actual = strings.TrimSpace(p.ApplyState.String())
t.Fatalf("bad: %#v", p.ApplyState) expected = strings.TrimSpace(testApplyStateStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
} }
// Verify a new state exists // Verify a new state exists
@ -866,22 +870,7 @@ func TestApply_backup(t *testing.T) {
} }
func TestApply_disableBackup(t *testing.T) { func TestApply_disableBackup(t *testing.T) {
originalState := &terraform.State{ originalState := testState()
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",
},
},
},
},
},
}
statePath := testStateFile(t, originalState) statePath := testStateFile(t, originalState)
p := testProvider() p := testProvider()
@ -912,13 +901,16 @@ func TestApply_disableBackup(t *testing.T) {
} }
// Verify that the provider was called with the existing state // Verify that the provider was called with the existing state
expectedState := originalState.RootModule().Resources["test_instance.foo"] actual := strings.TrimSpace(p.DiffState.String())
if !reflect.DeepEqual(p.DiffState, expectedState) { expected := strings.TrimSpace(testApplyDisableBackupStr)
t.Fatalf("bad: %#v", p.DiffState) if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
} }
if !reflect.DeepEqual(p.ApplyState, expectedState) { actual = strings.TrimSpace(p.ApplyState.String())
t.Fatalf("bad: %#v", p.ApplyState) expected = strings.TrimSpace(testApplyDisableBackupStateStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
} }
// Verify a new state exists // Verify a new state exists
@ -950,3 +942,19 @@ func TestApply_disableBackup(t *testing.T) {
const applyVarFile = ` const applyVarFile = `
foo = "bar" foo = "bar"
` `
const testApplyDisableBackupStr = `
ID = bar
`
const testApplyDisableBackupStateStr = `
ID = bar
`
const testApplyStateStr = `
ID = bar
`
const testApplyStateDiffStr = `
ID = bar
`

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
@ -124,11 +125,10 @@ func TestPlan_noState(t *testing.T) {
} }
// Verify that the provider was called with the existing state // Verify that the provider was called with the existing state
expectedState := &terraform.ResourceState{ actual := strings.TrimSpace(p.DiffState.String())
Type: "test_instance", expected := strings.TrimSpace(testPlanNoStateStr)
} if actual != expected {
if !reflect.DeepEqual(p.DiffState, expectedState) { t.Fatalf("bad:\n\n%s", actual)
t.Fatalf("bad: %#v", p.DiffState)
} }
} }
@ -204,22 +204,7 @@ func TestPlan_state(t *testing.T) {
statePath := tf.Name() statePath := tf.Name()
defer os.Remove(tf.Name()) defer os.Remove(tf.Name())
originalState := &terraform.State{ originalState := testState()
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",
},
},
},
},
},
}
err = terraform.WriteState(originalState, tf) err = terraform.WriteState(originalState, tf)
tf.Close() tf.Close()
if err != nil { if err != nil {
@ -244,28 +229,15 @@ func TestPlan_state(t *testing.T) {
} }
// Verify that the provider was called with the existing state // Verify that the provider was called with the existing state
expectedState := originalState.RootModule().Resources["test_instance.foo"] actual := strings.TrimSpace(p.DiffState.String())
if !reflect.DeepEqual(p.DiffState, expectedState) { expected := strings.TrimSpace(testPlanStateStr)
t.Fatalf("bad: %#v", p.DiffState) if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
} }
} }
func TestPlan_stateDefault(t *testing.T) { func TestPlan_stateDefault(t *testing.T) {
originalState := &terraform.State{ originalState := testState()
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",
},
},
},
},
},
}
// Write the state file in a temporary directory with the // Write the state file in a temporary directory with the
// default filename. // default filename.
@ -312,9 +284,10 @@ func TestPlan_stateDefault(t *testing.T) {
} }
// Verify that the provider was called with the existing state // Verify that the provider was called with the existing state
expectedState := originalState.RootModule().Resources["test_instance.foo"] actual := strings.TrimSpace(p.DiffState.String())
if !reflect.DeepEqual(p.DiffState, expectedState) { expected := strings.TrimSpace(testPlanStateDefaultStr)
t.Fatalf("bad: %#v", p.DiffState) if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
} }
} }
@ -502,9 +475,10 @@ func TestPlan_backup(t *testing.T) {
} }
// Verify that the provider was called with the existing state // Verify that the provider was called with the existing state
expectedState := originalState.RootModule().Resources["test_instance.foo"] actual := strings.TrimSpace(p.DiffState.String())
if !reflect.DeepEqual(p.DiffState, expectedState) { expected := strings.TrimSpace(testPlanBackupStr)
t.Fatalf("bad: %#v", p.DiffState) if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
} }
// Verify the backup exist // Verify the backup exist
@ -574,9 +548,10 @@ func TestPlan_disableBackup(t *testing.T) {
} }
// Verify that the provider was called with the existing state // Verify that the provider was called with the existing state
expectedState := originalState.RootModule().Resources["test_instance.foo"] actual := strings.TrimSpace(p.DiffState.String())
if !reflect.DeepEqual(p.DiffState, expectedState) { expected := strings.TrimSpace(testPlanDisableBackupStr)
t.Fatalf("bad: %#v", p.DiffState) if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
} }
// Ensure there is no backup // Ensure there is no backup
@ -589,3 +564,23 @@ func TestPlan_disableBackup(t *testing.T) {
const planVarFile = ` const planVarFile = `
foo = "bar" foo = "bar"
` `
const testPlanBackupStr = `
ID = bar
`
const testPlanDisableBackupStr = `
ID = bar
`
const testPlanNoStateStr = `
<not created>
`
const testPlanStateStr = `
ID = bar
`
const testPlanStateDefaultStr = `
ID = bar
`

View File

@ -6,6 +6,7 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
"strings"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -50,10 +51,10 @@ func TestRefresh(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
actual := newState.RootModule().Resources["test_instance.foo"] actual := strings.TrimSpace(newState.String())
expected := p.RefreshReturn expected := strings.TrimSpace(testRefreshStr)
if !reflect.DeepEqual(actual, expected) { if actual != expected {
t.Fatalf("bad: %#v", actual) t.Fatalf("bad:\n\n%s", actual)
} }
} }
@ -123,10 +124,10 @@ func TestRefresh_cwd(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
actual := newState.RootModule().Resources["test_instance.foo"] actual := strings.TrimSpace(newState.String())
expected := p.RefreshReturn expected := strings.TrimSpace(testRefreshCwdStr)
if !reflect.DeepEqual(actual, expected) { if actual != expected {
t.Fatalf("bad: %#v", actual) t.Fatalf("bad:\n\n%s", actual)
} }
} }
@ -579,3 +580,12 @@ func TestRefresh_disableBackup(t *testing.T) {
const refreshVarFile = ` const refreshVarFile = `
foo = "bar" foo = "bar"
` `
const testRefreshStr = `
test_instance.foo:
ID = yes
`
const testRefreshCwdStr = `
test_instance.foo:
ID = yes
`

View File

@ -449,6 +449,37 @@ func (i *InstanceState) GoString() string {
return fmt.Sprintf("*%#v", *i) return fmt.Sprintf("*%#v", *i)
} }
func (i *InstanceState) String() string {
var buf bytes.Buffer
if i.ID == "" {
return "<not created>"
}
buf.WriteString(fmt.Sprintf("ID = %s\n", i.ID))
if i.Tainted {
buf.WriteString(fmt.Sprintf("Tainted = true"))
}
attributes := i.Attributes
attrKeys := make([]string, 0, len(attributes))
for ak, _ := range attributes {
if ak == "id" {
continue
}
attrKeys = append(attrKeys, ak)
}
sort.Strings(attrKeys)
for _, ak := range attrKeys {
av := attributes[ak]
buf.WriteString(fmt.Sprintf("%s = %s\n", ak, av))
}
return buf.String()
}
// EphemeralState is used for transient state that is only kept in-memory // EphemeralState is used for transient state that is only kept in-memory
type EphemeralState struct { type EphemeralState struct {
// ConnInfo is used for the providers to export information which is // ConnInfo is used for the providers to export information which is