github.com/opsidian/terraform@v0.7.8-0.20161104123224-27c39cdfba5b/terraform/graph_test.go (about) 1 package terraform 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 ) 8 9 func TestGraphAdd(t *testing.T) { 10 // Test Add since we override it and want to make sure we don't break it. 11 var g Graph 12 g.Add(42) 13 g.Add(84) 14 15 actual := strings.TrimSpace(g.String()) 16 expected := strings.TrimSpace(testGraphAddStr) 17 if actual != expected { 18 t.Fatalf("bad: %s", actual) 19 } 20 } 21 22 func TestGraphConnectDependent(t *testing.T) { 23 var g Graph 24 g.Add(&testGraphDependable{VertexName: "a"}) 25 b := g.Add(&testGraphDependable{ 26 VertexName: "b", 27 DependentOnMock: []string{"a"}, 28 }) 29 30 if missing := g.ConnectDependent(b); len(missing) > 0 { 31 t.Fatalf("bad: %#v", missing) 32 } 33 34 actual := strings.TrimSpace(g.String()) 35 expected := strings.TrimSpace(testGraphConnectDepsStr) 36 if actual != expected { 37 t.Fatalf("bad: %s", actual) 38 } 39 } 40 41 func TestGraphReplace_DependableWithNonDependable(t *testing.T) { 42 var g Graph 43 a := g.Add(&testGraphDependable{VertexName: "a"}) 44 b := g.Add(&testGraphDependable{ 45 VertexName: "b", 46 DependentOnMock: []string{"a"}, 47 }) 48 newA := "non-dependable-a" 49 50 if missing := g.ConnectDependent(b); len(missing) > 0 { 51 t.Fatalf("bad: %#v", missing) 52 } 53 54 if !g.Replace(a, newA) { 55 t.Fatalf("failed to replace") 56 } 57 58 c := g.Add(&testGraphDependable{ 59 VertexName: "c", 60 DependentOnMock: []string{"a"}, 61 }) 62 63 // This should fail by reporting missing, since a node with dependable 64 // name "a" is no longer in the graph. 65 missing := g.ConnectDependent(c) 66 expected := []string{"a"} 67 if !reflect.DeepEqual(expected, missing) { 68 t.Fatalf("expected: %#v, got: %#v", expected, missing) 69 } 70 } 71 72 func TestGraphWalk_panicWrap(t *testing.T) { 73 var g Graph 74 75 // Add our crasher 76 v := &testGraphSubPath{ 77 PathFn: func() []string { 78 panic("yo") 79 }, 80 } 81 g.Add(v) 82 83 err := g.Walk(GraphWalkerPanicwrap(new(NullGraphWalker))) 84 if err == nil { 85 t.Fatal("should error") 86 } 87 } 88 89 type testGraphSubPath struct { 90 PathFn func() []string 91 } 92 93 func (v *testGraphSubPath) Path() []string { return v.PathFn() } 94 95 type testGraphDependable struct { 96 VertexName string 97 DependentOnMock []string 98 } 99 100 func (v *testGraphDependable) Name() string { 101 return v.VertexName 102 } 103 104 func (v *testGraphDependable) DependableName() []string { 105 return []string{v.VertexName} 106 } 107 108 func (v *testGraphDependable) DependentOn() []string { 109 return v.DependentOnMock 110 } 111 112 const testGraphAddStr = ` 113 42 114 84 115 ` 116 117 const testGraphConnectDepsStr = ` 118 a 119 b 120 a 121 `