2015-01-26 23:23:27 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2016-12-14 23:39:13 -06:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
2015-01-26 23:23:27 -06:00
|
|
|
)
|
|
|
|
|
2017-01-20 20:07:51 -06:00
|
|
|
// testGraphContains is an assertion helper that tests that a node is
|
|
|
|
// contained in the graph.
|
|
|
|
func testGraphContains(t *testing.T, g *Graph, name string) {
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if dag.VertexName(v) == name {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf(
|
|
|
|
"Expected %q in:\n\n%s",
|
|
|
|
name, g.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// testGraphnotContains is an assertion helper that tests that a node is
|
|
|
|
// NOT contained in the graph.
|
|
|
|
func testGraphNotContains(t *testing.T, g *Graph, name string) {
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if dag.VertexName(v) == name {
|
|
|
|
t.Fatalf(
|
|
|
|
"Expected %q to NOT be in:\n\n%s",
|
|
|
|
name, g.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:39:13 -06:00
|
|
|
// testGraphHappensBefore is an assertion helper that tests that node
|
|
|
|
// A (dag.VertexName value) happens before node B.
|
|
|
|
func testGraphHappensBefore(t *testing.T, g *Graph, A, B string) {
|
2018-09-24 18:25:40 -05:00
|
|
|
t.Helper()
|
2016-12-14 23:39:13 -06:00
|
|
|
// Find the B vertex
|
|
|
|
var vertexB dag.Vertex
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if dag.VertexName(v) == B {
|
|
|
|
vertexB = v
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if vertexB == nil {
|
|
|
|
t.Fatalf(
|
|
|
|
"Expected %q before %q. Couldn't find %q in:\n\n%s",
|
|
|
|
A, B, B, g.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look at ancestors
|
|
|
|
deps, err := g.Ancestors(vertexB)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error: %s in graph:\n\n%s", err, g.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure B is in there
|
|
|
|
for _, v := range deps.List() {
|
|
|
|
if dag.VertexName(v) == A {
|
|
|
|
// Success
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf(
|
|
|
|
"Expected %q before %q in:\n\n%s",
|
|
|
|
A, B, g.String())
|
|
|
|
}
|
|
|
|
|
2016-11-03 14:04:04 -05:00
|
|
|
type testGraphSubPath struct {
|
|
|
|
PathFn func() []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *testGraphSubPath) Path() []string { return v.PathFn() }
|
|
|
|
|
2015-01-26 23:37:10 -06:00
|
|
|
type testGraphDependable struct {
|
2015-01-27 16:56:01 -06:00
|
|
|
VertexName string
|
|
|
|
DependentOnMock []string
|
2015-01-26 23:37:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *testGraphDependable) Name() string {
|
|
|
|
return v.VertexName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *testGraphDependable) DependableName() []string {
|
2015-08-10 15:04:48 -05:00
|
|
|
return []string{v.VertexName}
|
2015-01-26 23:37:10 -06:00
|
|
|
}
|
|
|
|
|
2015-01-27 16:56:01 -06:00
|
|
|
func (v *testGraphDependable) DependentOn() []string {
|
|
|
|
return v.DependentOnMock
|
|
|
|
}
|
|
|
|
|
2015-01-26 23:23:27 -06:00
|
|
|
const testGraphAddStr = `
|
|
|
|
42
|
|
|
|
84
|
|
|
|
`
|
2015-01-26 23:37:10 -06:00
|
|
|
|
|
|
|
const testGraphConnectDepsStr = `
|
|
|
|
a
|
|
|
|
b
|
|
|
|
a
|
|
|
|
`
|