github.com/r3labs/terraform@v0.8.4/terraform/graph_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/dag"
     9  )
    10  
    11  func TestGraphAdd(t *testing.T) {
    12  	// Test Add since we override it and want to make sure we don't break it.
    13  	var g Graph
    14  	g.Add(42)
    15  	g.Add(84)
    16  
    17  	actual := strings.TrimSpace(g.String())
    18  	expected := strings.TrimSpace(testGraphAddStr)
    19  	if actual != expected {
    20  		t.Fatalf("bad: %s", actual)
    21  	}
    22  }
    23  
    24  func TestGraphConnectDependent(t *testing.T) {
    25  	var g Graph
    26  	g.Add(&testGraphDependable{VertexName: "a"})
    27  	b := g.Add(&testGraphDependable{
    28  		VertexName:      "b",
    29  		DependentOnMock: []string{"a"},
    30  	})
    31  
    32  	if missing := g.ConnectDependent(b); len(missing) > 0 {
    33  		t.Fatalf("bad: %#v", missing)
    34  	}
    35  
    36  	actual := strings.TrimSpace(g.String())
    37  	expected := strings.TrimSpace(testGraphConnectDepsStr)
    38  	if actual != expected {
    39  		t.Fatalf("bad: %s", actual)
    40  	}
    41  }
    42  
    43  func TestGraphReplace_DependableWithNonDependable(t *testing.T) {
    44  	var g Graph
    45  	a := g.Add(&testGraphDependable{VertexName: "a"})
    46  	b := g.Add(&testGraphDependable{
    47  		VertexName:      "b",
    48  		DependentOnMock: []string{"a"},
    49  	})
    50  	newA := "non-dependable-a"
    51  
    52  	if missing := g.ConnectDependent(b); len(missing) > 0 {
    53  		t.Fatalf("bad: %#v", missing)
    54  	}
    55  
    56  	if !g.Replace(a, newA) {
    57  		t.Fatalf("failed to replace")
    58  	}
    59  
    60  	c := g.Add(&testGraphDependable{
    61  		VertexName:      "c",
    62  		DependentOnMock: []string{"a"},
    63  	})
    64  
    65  	// This should fail by reporting missing, since a node with dependable
    66  	// name "a" is no longer in the graph.
    67  	missing := g.ConnectDependent(c)
    68  	expected := []string{"a"}
    69  	if !reflect.DeepEqual(expected, missing) {
    70  		t.Fatalf("expected: %#v, got: %#v", expected, missing)
    71  	}
    72  }
    73  
    74  func TestGraphWalk_panicWrap(t *testing.T) {
    75  	var g Graph
    76  
    77  	// Add our crasher
    78  	v := &testGraphSubPath{
    79  		PathFn: func() []string {
    80  			panic("yo")
    81  		},
    82  	}
    83  	g.Add(v)
    84  
    85  	err := g.Walk(GraphWalkerPanicwrap(new(NullGraphWalker)))
    86  	if err == nil {
    87  		t.Fatal("should error")
    88  	}
    89  }
    90  
    91  // testGraphHappensBefore is an assertion helper that tests that node
    92  // A (dag.VertexName value) happens before node B.
    93  func testGraphHappensBefore(t *testing.T, g *Graph, A, B string) {
    94  	// Find the B vertex
    95  	var vertexB dag.Vertex
    96  	for _, v := range g.Vertices() {
    97  		if dag.VertexName(v) == B {
    98  			vertexB = v
    99  			break
   100  		}
   101  	}
   102  	if vertexB == nil {
   103  		t.Fatalf(
   104  			"Expected %q before %q. Couldn't find %q in:\n\n%s",
   105  			A, B, B, g.String())
   106  	}
   107  
   108  	// Look at ancestors
   109  	deps, err := g.Ancestors(vertexB)
   110  	if err != nil {
   111  		t.Fatalf("Error: %s in graph:\n\n%s", err, g.String())
   112  	}
   113  
   114  	// Make sure B is in there
   115  	for _, v := range deps.List() {
   116  		if dag.VertexName(v) == A {
   117  			// Success
   118  			return
   119  		}
   120  	}
   121  
   122  	t.Fatalf(
   123  		"Expected %q before %q in:\n\n%s",
   124  		A, B, g.String())
   125  }
   126  
   127  type testGraphSubPath struct {
   128  	PathFn func() []string
   129  }
   130  
   131  func (v *testGraphSubPath) Path() []string { return v.PathFn() }
   132  
   133  type testGraphDependable struct {
   134  	VertexName      string
   135  	DependentOnMock []string
   136  }
   137  
   138  func (v *testGraphDependable) Name() string {
   139  	return v.VertexName
   140  }
   141  
   142  func (v *testGraphDependable) DependableName() []string {
   143  	return []string{v.VertexName}
   144  }
   145  
   146  func (v *testGraphDependable) DependentOn() []string {
   147  	return v.DependentOnMock
   148  }
   149  
   150  const testGraphAddStr = `
   151  42
   152  84
   153  `
   154  
   155  const testGraphConnectDepsStr = `
   156  a
   157  b
   158    a
   159  `