2014-06-18 03:40:32 +02:00
|
|
|
package diff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2014-06-20 21:12:24 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2014-06-18 03:40:32 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2014-07-11 20:46:21 +02:00
|
|
|
func TestResourceBuilder_attrSetComputed(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"foo": AttrTypeCreate,
|
|
|
|
},
|
|
|
|
ComputedAttrs: []string{
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{}
|
2014-07-11 20:46:21 +02:00
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("diff shold not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testResourceDiffStr(diff)
|
|
|
|
expected := testRBAttrSetComputedDiff
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 00:21:01 +02:00
|
|
|
func TestResourceBuilder_attrSetComputedComplex(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"foo": AttrTypeCreate,
|
|
|
|
},
|
|
|
|
ComputedAttrs: []string{
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{
|
2014-07-17 00:21:01 +02:00
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo.#": "0",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c := testConfig(t, map[string]interface{}{}, nil)
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff != nil {
|
2014-08-25 06:50:35 +02:00
|
|
|
t.Fatalf("diff shold be nil: %#v", diff)
|
2014-07-17 00:21:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 20:36:44 +02:00
|
|
|
func TestResourceBuilder_replaceComputed(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"foo": AttrTypeCreate,
|
|
|
|
},
|
|
|
|
ComputedAttrs: []string{
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{
|
2014-07-11 20:36:44 +02:00
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c := testConfig(t, nil, nil)
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff != nil {
|
|
|
|
t.Fatalf("should be nil: %#v", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 01:06:06 +02:00
|
|
|
func TestResourceBuilder_complex(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"listener": AttrTypeUpdate,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{
|
2014-07-02 01:06:06 +02:00
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"ignore": "1",
|
|
|
|
"listener.#": "1",
|
|
|
|
"listener.0.port": "80",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"listener": []interface{}{
|
|
|
|
map[interface{}]interface{}{
|
|
|
|
"port": 3000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testResourceDiffStr(diff)
|
|
|
|
expected := testRBComplexDiff
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 18:47:21 +02:00
|
|
|
func TestResourceBuilder_complexReplace(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"listener": AttrTypeUpdate,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{
|
2014-07-09 18:47:21 +02:00
|
|
|
ID: "foo",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"ignore": "1",
|
|
|
|
"listener.#": "1",
|
|
|
|
"listener.0.port": "80",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"listener": []interface{}{
|
|
|
|
map[interface{}]interface{}{
|
|
|
|
"value": "50",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testResourceDiffStr(diff)
|
|
|
|
expected := testRBComplexReplaceDiff
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 05:24:40 +02:00
|
|
|
func TestResourceBuilder_computedAttrsUpdate(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"foo": AttrTypeUpdate,
|
|
|
|
},
|
|
|
|
ComputedAttrsUpdate: []string{
|
|
|
|
"bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{
|
2014-07-25 05:24:40 +02:00
|
|
|
Attributes: map[string]string{"foo": "foo"},
|
|
|
|
}
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("diff shold not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testResourceDiffStr(diff)
|
|
|
|
expected := testRBComputedAttrUpdate
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 03:40:32 +02:00
|
|
|
func TestResourceBuilder_new(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
2014-07-02 01:06:06 +02:00
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"foo": AttrTypeUpdate,
|
|
|
|
},
|
|
|
|
ComputedAttrs: []string{"private_ip"},
|
2014-06-18 03:40:32 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{}
|
2014-06-18 03:40:32 +02:00
|
|
|
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testResourceDiffStr(diff)
|
|
|
|
expected := testRBNewDiff
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 00:58:47 +02:00
|
|
|
func TestResourceBuilder_preProcess(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"foo": AttrTypeCreate,
|
|
|
|
},
|
|
|
|
|
|
|
|
PreProcess: map[string]PreProcessFunc{
|
2014-07-17 01:40:54 +02:00
|
|
|
"foo": func(v string) string {
|
|
|
|
return "bar" + v
|
2014-07-17 00:58:47 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{}
|
2014-07-17 00:58:47 +02:00
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "foo",
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("diff shold not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testResourceDiffStr(diff)
|
|
|
|
expected := testRBPreProcessDiff
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = diff.Attributes["foo"].NewExtra.(string)
|
|
|
|
expected = "foo"
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceBuilder_preProcessUnknown(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"foo": AttrTypeCreate,
|
|
|
|
},
|
|
|
|
|
|
|
|
PreProcess: map[string]PreProcessFunc{
|
|
|
|
"foo": func(string) string {
|
|
|
|
return "bar"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{}
|
2014-07-17 00:58:47 +02:00
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "${var.unknown}",
|
|
|
|
}, map[string]string{
|
|
|
|
"var.unknown": config.UnknownVariableValue,
|
|
|
|
})
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("diff shold not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testResourceDiffStr(diff)
|
|
|
|
expected := testRBPreProcessUnknownDiff
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 03:40:32 +02:00
|
|
|
func TestResourceBuilder_requiresNew(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
2014-07-02 01:06:06 +02:00
|
|
|
ComputedAttrs: []string{"private_ip"},
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"ami": AttrTypeCreate,
|
|
|
|
},
|
2014-06-18 03:40:32 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{
|
2014-06-18 03:40:32 +02:00
|
|
|
ID: "1",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"ami": "foo",
|
|
|
|
"private_ip": "127.0.0.1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"ami": "bar",
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testResourceDiffStr(diff)
|
|
|
|
expected := testRBRequiresNewDiff
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceBuilder_same(t *testing.T) {
|
|
|
|
rb := &ResourceBuilder{
|
2014-07-02 01:06:06 +02:00
|
|
|
ComputedAttrs: []string{"private_ip"},
|
2014-06-18 03:40:32 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{
|
2014-06-18 03:40:32 +02:00
|
|
|
ID: "1",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff != nil {
|
2014-08-25 06:50:35 +02:00
|
|
|
t.Fatalf("should not diff: %#v", diff)
|
2014-06-18 03:40:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:12:24 +02:00
|
|
|
func TestResourceBuilder_unknown(t *testing.T) {
|
2014-07-02 01:06:06 +02:00
|
|
|
rb := &ResourceBuilder{
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"foo": AttrTypeUpdate,
|
|
|
|
},
|
|
|
|
}
|
2014-06-20 21:12:24 +02:00
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{}
|
2014-06-20 21:12:24 +02:00
|
|
|
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "${var.unknown}",
|
|
|
|
}, map[string]string{
|
|
|
|
"var.foo": "bar",
|
|
|
|
"var.unknown": config.UnknownVariableValue,
|
|
|
|
})
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testResourceDiffStr(diff)
|
|
|
|
expected := testRBUnknownDiff
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceBuilder_vars(t *testing.T) {
|
2014-07-02 01:06:06 +02:00
|
|
|
rb := &ResourceBuilder{
|
|
|
|
Attrs: map[string]AttrType{
|
|
|
|
"foo": AttrTypeUpdate,
|
|
|
|
},
|
|
|
|
}
|
2014-06-20 21:12:24 +02:00
|
|
|
|
2014-09-17 01:56:14 +02:00
|
|
|
state := &terraform.InstanceState{}
|
2014-06-20 21:12:24 +02:00
|
|
|
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"foo": "${var.foo}",
|
|
|
|
}, map[string]string{
|
|
|
|
"var.foo": "bar",
|
|
|
|
})
|
|
|
|
|
|
|
|
diff, err := rb.Diff(state, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := testResourceDiffStr(diff)
|
|
|
|
expected := testRBVarsDiff
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 20:46:21 +02:00
|
|
|
const testRBAttrSetComputedDiff = `CREATE
|
|
|
|
IN foo: "" => "bar" (forces new resource)
|
|
|
|
`
|
|
|
|
|
2014-07-02 01:06:06 +02:00
|
|
|
const testRBComplexDiff = `UPDATE
|
|
|
|
IN listener.0.port: "80" => "3000"
|
|
|
|
`
|
|
|
|
|
2014-07-09 18:47:21 +02:00
|
|
|
const testRBComplexReplaceDiff = `UPDATE
|
2014-07-09 18:51:36 +02:00
|
|
|
IN listener.0.port: "80" => "<removed>"
|
2014-07-09 18:47:21 +02:00
|
|
|
IN listener.0.value: "" => "50"
|
|
|
|
`
|
|
|
|
|
2014-07-25 05:24:40 +02:00
|
|
|
const testRBComputedAttrUpdate = `UPDATE
|
|
|
|
OUT bar: "" => "<computed>"
|
|
|
|
IN foo: "foo" => "bar"
|
|
|
|
`
|
|
|
|
|
2014-06-23 22:14:08 +02:00
|
|
|
const testRBNewDiff = `UPDATE
|
2014-06-23 21:49:30 +02:00
|
|
|
IN foo: "" => "bar"
|
|
|
|
OUT private_ip: "" => "<computed>"
|
2014-06-18 03:40:32 +02:00
|
|
|
`
|
|
|
|
|
2014-07-17 00:58:47 +02:00
|
|
|
const testRBPreProcessDiff = `CREATE
|
2014-07-17 01:40:54 +02:00
|
|
|
IN foo: "" => "barfoo" (forces new resource)
|
2014-07-17 00:58:47 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const testRBPreProcessUnknownDiff = `CREATE
|
|
|
|
IN foo: "" => "${var.unknown}" (forces new resource)
|
|
|
|
`
|
|
|
|
|
2014-06-18 03:40:32 +02:00
|
|
|
const testRBRequiresNewDiff = `CREATE
|
2014-06-23 21:49:30 +02:00
|
|
|
IN ami: "foo" => "bar" (forces new resource)
|
|
|
|
OUT private_ip: "127.0.0.1" => "<computed>"
|
2014-06-18 03:40:32 +02:00
|
|
|
`
|
2014-06-20 21:12:24 +02:00
|
|
|
|
2014-06-23 22:14:08 +02:00
|
|
|
const testRBUnknownDiff = `UPDATE
|
2014-06-23 21:49:30 +02:00
|
|
|
IN foo: "" => "${var.unknown}"
|
2014-06-20 21:12:24 +02:00
|
|
|
`
|
|
|
|
|
2014-06-23 22:14:08 +02:00
|
|
|
const testRBVarsDiff = `UPDATE
|
2014-06-23 21:49:30 +02:00
|
|
|
IN foo: "" => "bar"
|
2014-06-20 21:12:24 +02:00
|
|
|
`
|