terraform: more tests
This commit is contained in:
parent
4f8152c28a
commit
cb4e364aca
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
`
|
|
@ -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
|
||||
`
|
||||
|
|
Loading…
Reference in New Issue