github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/terraform/graph_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/dag"
     7  )
     8  
     9  func TestGraphWalk_panicWrap(t *testing.T) {
    10  	var g Graph
    11  
    12  	// Add our crasher
    13  	v := &testGraphSubPath{
    14  		PathFn: func() []string {
    15  			panic("yo")
    16  		},
    17  	}
    18  	g.Add(v)
    19  
    20  	err := g.Walk(GraphWalkerPanicwrap(new(NullGraphWalker)))
    21  	if err == nil {
    22  		t.Fatal("should error")
    23  	}
    24  }
    25  
    26  // testGraphContains is an assertion helper that tests that a node is
    27  // contained in the graph.
    28  func testGraphContains(t *testing.T, g *Graph, name string) {
    29  	for _, v := range g.Vertices() {
    30  		if dag.VertexName(v) == name {
    31  			return
    32  		}
    33  	}
    34  
    35  	t.Fatalf(
    36  		"Expected %q in:\n\n%s",
    37  		name, g.String())
    38  }
    39  
    40  // testGraphnotContains is an assertion helper that tests that a node is
    41  // NOT contained in the graph.
    42  func testGraphNotContains(t *testing.T, g *Graph, name string) {
    43  	for _, v := range g.Vertices() {
    44  		if dag.VertexName(v) == name {
    45  			t.Fatalf(
    46  				"Expected %q to NOT be in:\n\n%s",
    47  				name, g.String())
    48  		}
    49  	}
    50  }
    51  
    52  // testGraphHappensBefore is an assertion helper that tests that node
    53  // A (dag.VertexName value) happens before node B.
    54  func testGraphHappensBefore(t *testing.T, g *Graph, A, B string) {
    55  	// Find the B vertex
    56  	var vertexB dag.Vertex
    57  	for _, v := range g.Vertices() {
    58  		if dag.VertexName(v) == B {
    59  			vertexB = v
    60  			break
    61  		}
    62  	}
    63  	if vertexB == nil {
    64  		t.Fatalf(
    65  			"Expected %q before %q. Couldn't find %q in:\n\n%s",
    66  			A, B, B, g.String())
    67  	}
    68  
    69  	// Look at ancestors
    70  	deps, err := g.Ancestors(vertexB)
    71  	if err != nil {
    72  		t.Fatalf("Error: %s in graph:\n\n%s", err, g.String())
    73  	}
    74  
    75  	// Make sure B is in there
    76  	for _, v := range deps.List() {
    77  		if dag.VertexName(v) == A {
    78  			// Success
    79  			return
    80  		}
    81  	}
    82  
    83  	t.Fatalf(
    84  		"Expected %q before %q in:\n\n%s",
    85  		A, B, g.String())
    86  }
    87  
    88  type testGraphSubPath struct {
    89  	PathFn func() []string
    90  }
    91  
    92  func (v *testGraphSubPath) Path() []string { return v.PathFn() }
    93  
    94  type testGraphDependable struct {
    95  	VertexName      string
    96  	DependentOnMock []string
    97  }
    98  
    99  func (v *testGraphDependable) Name() string {
   100  	return v.VertexName
   101  }
   102  
   103  func (v *testGraphDependable) DependableName() []string {
   104  	return []string{v.VertexName}
   105  }
   106  
   107  func (v *testGraphDependable) DependentOn() []string {
   108  	return v.DependentOnMock
   109  }
   110  
   111  const testGraphAddStr = `
   112  42
   113  84
   114  `
   115  
   116  const testGraphConnectDepsStr = `
   117  a
   118  b
   119    a
   120  `