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
if s != nil {
state = s.Resources[r.Id()]
} else {
}
if state == nil {
state = &ResourceState{
Type: r.Type,
}

View File

@ -89,14 +89,12 @@ func New(c *Config) (*Terraform, 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 {
return nil, err
}
result := new(State)
err = graph.Walk(t.applyWalkFn(p, result))
return result, err
return t.apply(g, p)
}
// 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)
}
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(
g *depgraph.Graph,
c *config.Config,
@ -187,8 +193,8 @@ func (t *Terraform) refreshWalkFn(result *State) depgraph.WalkFunc {
}
func (t *Terraform) applyWalkFn(
p *Plan,
result *State) depgraph.WalkFunc {
result *State,
vs map[string]string) depgraph.WalkFunc {
var l sync.Mutex
// Initialize the result
@ -246,7 +252,7 @@ func (t *Terraform) applyWalkFn(
return vars, err
}
return t.genericWalkFn(p.Vars, cb)
return t.genericWalkFn(vs, cb)
}
func (t *Terraform) planWalkFn(

View File

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

View File

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