terraform: more tests

This commit is contained in:
Mitchell Hashimoto 2015-01-26 21:37:10 -08:00
parent 4f8152c28a
commit cb4e364aca
4 changed files with 44 additions and 87 deletions

View File

@ -56,7 +56,6 @@ func (g *Graph) Add(v dag.Vertex) dag.Vertex {
func (g *Graph) ConnectTo(source dag.Vertex, target []string) []string {
g.once.Do(g.init)
// TODO: test
var missing []string
for _, t := range target {
if dest := g.dependableMap[t]; dest != nil {
@ -78,3 +77,12 @@ func (g *Graph) init() {
g.dependableMap = make(map[string]dag.Vertex)
}
}
// GraphNodeDependable is an interface which says that a node can be
// depended on (an edge can be placed between this node and another) according
// to the well-known name returned by DependableName.
//
// DependableName can return multiple names it is known by.
type GraphNodeDependable interface {
DependableName() []string
}

View File

@ -1,43 +0,0 @@
package terraform
import (
"github.com/hashicorp/terraform/dag"
)
// GraphNodeDependable is an interface which says that a node can be
// depended on (an edge can be placed between this node and another) according
// to the well-known name returned by DependableName.
//
// DependableName can return multiple names it is known by.
type GraphNodeDependable interface {
DependableName() []string
}
// GraphConnectDeps is a helper to connect a Vertex to the proper dependencies
// in the graph based only on the names expected by DependableName.
//
// This function will return the number of dependencies found and connected.
func GraphConnectDeps(g *dag.Graph, source dag.Vertex, targets []string) int {
count := 0
// This is reasonably horrible. In the future, we should optimize this
// through some kind of metadata on the graph that can store all of
// this information in a look-aside table.
for _, v := range g.Vertices() {
if dv, ok := v.(GraphNodeDependable); ok {
for _, n := range dv.DependableName() {
for _, n2 := range targets {
if n == n2 {
count++
g.Connect(dag.BasicEdge(source, v))
goto NEXT
}
}
}
}
NEXT:
}
return count
}

View File

@ -1,43 +0,0 @@
package terraform
import (
"strings"
"testing"
"github.com/hashicorp/terraform/dag"
)
func TestGraphConnectDeps(t *testing.T) {
var g dag.Graph
g.Add(&testGraphDependable{VertexName: "a", Mock: []string{"a"}})
b := g.Add(&testGraphDependable{VertexName: "b"})
if n := GraphConnectDeps(&g, b, []string{"a"}); n != 1 {
t.Fatalf("bad: %d", n)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphConnectDepsStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
type testGraphDependable struct {
VertexName string
Mock []string
}
func (v *testGraphDependable) Name() string {
return v.VertexName
}
func (v *testGraphDependable) DependableName() []string {
return v.Mock
}
const testGraphConnectDepsStr = `
a
b
a
`

View File

@ -18,7 +18,42 @@ func TestGraphAdd(t *testing.T) {
}
}
func TestGraphConnectTo(t *testing.T) {
var g Graph
g.Add(&testGraphDependable{VertexName: "a", Mock: []string{"a"}})
b := g.Add(&testGraphDependable{VertexName: "b"})
if missing := g.ConnectTo(b, []string{"a"}); len(missing) > 0 {
t.Fatalf("bad: %#v", missing)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphConnectDepsStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
type testGraphDependable struct {
VertexName string
Mock []string
}
func (v *testGraphDependable) Name() string {
return v.VertexName
}
func (v *testGraphDependable) DependableName() []string {
return v.Mock
}
const testGraphAddStr = `
42
84
`
const testGraphConnectDepsStr = `
a
b
a
`