terraform: basic apply tests

This commit is contained in:
Mitchell Hashimoto 2014-06-18 16:38:08 -07:00
parent 9f5b6cc40f
commit 830ddf0c8e
3 changed files with 39 additions and 2 deletions

View File

@ -1,6 +1,8 @@
package terraform package terraform
import ( import (
"bytes"
"fmt"
"sync" "sync"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
@ -21,6 +23,21 @@ func (s *State) init() {
}) })
} }
func (s *State) String() string {
var buf bytes.Buffer
for k, rs := range s.Resources {
buf.WriteString(fmt.Sprintf("%s:\n", k))
buf.WriteString(fmt.Sprintf(" ID = %s\n", rs.ID))
for ak, av := range rs.Attributes {
buf.WriteString(fmt.Sprintf(" %s = %s\n", ak, av))
}
}
return buf.String()
}
// ResourceState holds the state of a resource that is used so that // ResourceState holds the state of a resource that is used so that
// a provider can find and manage an existing resource as well as for // a provider can find and manage an existing resource as well as for
// storing attributes that are uesd to populate variables of child // storing attributes that are uesd to populate variables of child

View File

@ -207,7 +207,10 @@ func TestTerraformApply(t *testing.T) {
tf := testTerraform(t, "apply-good") tf := testTerraform(t, "apply-good")
s := &State{} s := &State{}
d := &Diff{} d, err := tf.Diff(s)
if err != nil {
t.Fatalf("err: %s", err)
}
state, err := tf.Apply(s, d) state, err := tf.Apply(s, d)
if err != nil { if err != nil {
@ -217,6 +220,12 @@ func TestTerraformApply(t *testing.T) {
if len(state.Resources) < 2 { if len(state.Resources) < 2 {
t.Fatalf("bad: %#v", state.Resources) t.Fatalf("bad: %#v", state.Resources)
} }
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
} }
func TestTerraformDiff(t *testing.T) { func TestTerraformDiff(t *testing.T) {
@ -456,6 +465,17 @@ func testTerraformProvider(tf *Terraform, n string) *terraformProvider {
return nil return nil
} }
const testTerraformApplyStr = `
aws_instance.bar:
ID = foo
type = aws_instance
foo = bar
aws_instance.foo:
ID = foo
type = aws_instance
num = 2
`
const testTerraformDiffStr = ` const testTerraformDiffStr = `
UPDATE: aws_instance.bar UPDATE: aws_instance.bar
foo: "" => "2" foo: "" => "2"

View File

@ -3,5 +3,5 @@ resource "aws_instance" "foo" {
} }
resource "aws_instance" "bar" { resource "aws_instance" "bar" {
foo = "${aws_instance.foo.num}" foo = "bar"
} }