terraform: all tests passing

This commit is contained in:
Mitchell Hashimoto 2014-06-25 18:12:03 -07:00
parent e9d1be397c
commit 0f2d7f430c
4 changed files with 48 additions and 40 deletions

View File

@ -111,7 +111,8 @@ func graphAddConfigResources(
var state *ResourceState var state *ResourceState
if s != nil { if s != nil {
state = s.Resources[r.Id()] state = s.Resources[r.Id()]
} else { }
if state == nil {
state = &ResourceState{ state = &ResourceState{
Type: r.Type, Type: r.Type,
} }

View File

@ -89,14 +89,12 @@ func New(c *Config) (*Terraform, error) {
} }
func (t *Terraform) Apply(p *Plan) (*State, error) { func (t *Terraform) Apply(p *Plan) (*State, error) {
graph, err := t.Graph(p.Config, p.State) g, err := t.Graph(p.Config, p.State)
if err != nil { if err != nil {
return nil, err return nil, err
} }
result := new(State) return t.apply(g, p)
err = graph.Walk(t.applyWalkFn(p, result))
return result, err
} }
// Graph returns the dependency graph for the given configuration and // Graph returns the dependency graph for the given configuration and
@ -145,6 +143,14 @@ func (t *Terraform) Refresh(c *config.Config, s *State) (*State, error) {
return t.refresh(g) return t.refresh(g)
} }
func (t *Terraform) apply(
g *depgraph.Graph,
p *Plan) (*State, error) {
s := new(State)
err := g.Walk(t.applyWalkFn(s, p.Vars))
return s, err
}
func (t *Terraform) plan( func (t *Terraform) plan(
g *depgraph.Graph, g *depgraph.Graph,
c *config.Config, c *config.Config,
@ -187,8 +193,8 @@ func (t *Terraform) refreshWalkFn(result *State) depgraph.WalkFunc {
} }
func (t *Terraform) applyWalkFn( func (t *Terraform) applyWalkFn(
p *Plan, result *State,
result *State) depgraph.WalkFunc { vs map[string]string) depgraph.WalkFunc {
var l sync.Mutex var l sync.Mutex
// Initialize the result // Initialize the result
@ -246,7 +252,7 @@ func (t *Terraform) applyWalkFn(
return vars, err return vars, err
} }
return t.genericWalkFn(p.Vars, cb) return t.genericWalkFn(vs, cb)
} }
func (t *Terraform) planWalkFn( func (t *Terraform) planWalkFn(

View File

@ -13,12 +13,11 @@ import (
// This is the directory where our test fixtures are. // This is the directory where our test fixtures are.
const fixtureDir = "./test-fixtures" const fixtureDir = "./test-fixtures"
/*
func TestTerraformApply(t *testing.T) { func TestTerraformApply(t *testing.T) {
tf := testTerraform(t, "apply-good") c := testConfig(t, "apply-good")
tf := testTerraform2(t, nil)
s := &State{} p, err := tf.Plan(c, nil, nil)
p, err := tf.Plan(s)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -42,17 +41,15 @@ func TestTerraformApply(t *testing.T) {
func TestTerraformApply_compute(t *testing.T) { func TestTerraformApply_compute(t *testing.T) {
// This tests that computed variables are properly re-diffed // This tests that computed variables are properly re-diffed
// to get the value prior to application (Apply). // to get the value prior to application (Apply).
tf := testTerraform(t, "apply-compute") c := testConfig(t, "apply-compute")
tf := testTerraform2(t, nil)
s := &State{} p, err := tf.Plan(c, nil, nil)
p, err := tf.Plan(s)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
// Set meta to change behavior so that computed variables are filled p.Vars["value"] = "1"
testProviderMock(testProvider(tf, "aws_instance.foo")).Meta =
"compute"
state, err := tf.Apply(p) state, err := tf.Apply(p)
if err != nil { if err != nil {
@ -67,10 +64,10 @@ func TestTerraformApply_compute(t *testing.T) {
} }
func TestTerraformApply_unknownAttribute(t *testing.T) { func TestTerraformApply_unknownAttribute(t *testing.T) {
tf := testTerraform(t, "apply-unknown") c := testConfig(t, "apply-unknown")
tf := testTerraform2(t, nil)
s := &State{} p, err := tf.Plan(c, nil, nil)
p, err := tf.Plan(s)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -88,11 +85,10 @@ func TestTerraformApply_unknownAttribute(t *testing.T) {
} }
func TestTerraformApply_vars(t *testing.T) { func TestTerraformApply_vars(t *testing.T) {
tf := testTerraform(t, "apply-vars") c := testConfig(t, "apply-vars")
//tf.variables = map[string]string{"foo": "baz"} tf := testTerraform2(t, nil)
s := &State{} p, err := tf.Plan(c, nil, map[string]string{"foo": "baz"})
p, err := tf.Plan(s)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -111,7 +107,6 @@ func TestTerraformApply_vars(t *testing.T) {
t.Fatalf("bad: \n%s", actual) t.Fatalf("bad: \n%s", actual)
} }
} }
*/
func TestTerraformPlan(t *testing.T) { func TestTerraformPlan(t *testing.T) {
c := testConfig(t, "plan-good") c := testConfig(t, "plan-good")
@ -190,7 +185,7 @@ func TestTerraformRefresh(t *testing.T) {
if !rpAWS.RefreshCalled { if !rpAWS.RefreshCalled {
t.Fatal("refresh should be called") t.Fatal("refresh should be called")
} }
if rpAWS.RefreshState != nil { if rpAWS.RefreshState.ID != "" {
t.Fatalf("bad: %#v", rpAWS.RefreshState) t.Fatalf("bad: %#v", rpAWS.RefreshState)
} }
if !reflect.DeepEqual(s.Resources["aws_instance.web"], rpAWS.RefreshReturn) { if !reflect.DeepEqual(s.Resources["aws_instance.web"], rpAWS.RefreshReturn) {
@ -292,6 +287,11 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory {
return nil, nil return nil, nil
} }
// This key is used for other purposes
if k == "compute_value" {
continue
}
if k == "compute" { if k == "compute" {
attrDiff := &ResourceAttrDiff{ attrDiff := &ResourceAttrDiff{
Old: "", Old: "",
@ -299,11 +299,11 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory {
NewComputed: true, NewComputed: true,
} }
// If the value of Meta turns into "compute", then we if cv, ok := c.Config["compute_value"]; ok {
// fill the computed values. if cv.(string) == "1" {
if mv, ok := p.Meta.(string); ok && mv == "compute" { attrDiff.NewComputed = false
attrDiff.NewComputed = false attrDiff.New = fmt.Sprintf("computed_%s", v.(string))
attrDiff.New = fmt.Sprintf("computed_%s", v.(string)) }
} }
diff.Attributes[v.(string)] = attrDiff diff.Attributes[v.(string)] = attrDiff

View File

@ -1,8 +1,9 @@
resource "aws_instance" "foo" { resource "aws_instance" "foo" {
num = "2" num = "2"
compute = "id" compute = "id"
} compute_value = "${var.value}"
}
resource "aws_instance" "bar" {
foo = "${aws_instance.foo.id}" resource "aws_instance" "bar" {
} foo = "${aws_instance.foo.id}"
}