terraform/terraform/state_test.go

136 lines
2.4 KiB
Go

package terraform
import (
"bytes"
"reflect"
"testing"
"github.com/hashicorp/terraform/config"
)
func TestResourceState_MergeDiff(t *testing.T) {
rs := ResourceState{
ID: "foo",
Attributes: map[string]string{
"foo": "bar",
"port": "8000",
},
}
diff := &ResourceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "bar",
New: "baz",
},
"bar": &ResourceAttrDiff{
Old: "",
New: "foo",
},
"baz": &ResourceAttrDiff{
Old: "",
New: "foo",
NewComputed: true,
},
"port": &ResourceAttrDiff{
NewRemoved: true,
},
},
}
rs2 := rs.MergeDiff(diff)
expected := map[string]string{
"foo": "baz",
"bar": "foo",
"baz": config.UnknownVariableValue,
}
if !reflect.DeepEqual(expected, rs2.Attributes) {
t.Fatalf("bad: %#v", rs2.Attributes)
}
}
func TestResourceState_MergeDiff_nil(t *testing.T) {
var rs *ResourceState = nil
diff := &ResourceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "",
New: "baz",
},
},
}
rs2 := rs.MergeDiff(diff)
expected := map[string]string{
"foo": "baz",
}
if !reflect.DeepEqual(expected, rs2.Attributes) {
t.Fatalf("bad: %#v", rs2.Attributes)
}
}
func TestResourceState_MergeDiff_nilDiff(t *testing.T) {
rs := ResourceState{
ID: "foo",
Attributes: map[string]string{
"foo": "bar",
},
}
rs2 := rs.MergeDiff(nil)
expected := map[string]string{
"foo": "bar",
}
if !reflect.DeepEqual(expected, rs2.Attributes) {
t.Fatalf("bad: %#v", rs2.Attributes)
}
}
func TestReadWriteState(t *testing.T) {
state := &State{
Resources: map[string]*ResourceState{
"foo": &ResourceState{
ID: "bar",
ConnInfo: map[string]string{
"type": "ssh",
"user": "root",
"password": "supersecret",
},
},
},
}
// Checksum before the write
chksum := checksumStruct(t, state)
buf := new(bytes.Buffer)
if err := WriteState(state, buf); err != nil {
t.Fatalf("err: %s", err)
}
// Checksum after the write
chksumAfter := checksumStruct(t, state)
if chksumAfter != chksum {
t.Fatalf("structure changed during serialization!")
}
actual, err := ReadState(buf)
if err != nil {
t.Fatalf("err: %s", err)
}
// ReadState should not restore sensitive information!
state.Resources["foo"].ConnInfo = nil
if !reflect.DeepEqual(actual, state) {
t.Fatalf("bad: %#v", actual)
}
}