github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/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 type testGraphDependable struct { 73 VertexName string 74 DependentOnMock []string 75 } 76 77 func (v *testGraphDependable) Name() string { 78 return v.VertexName 79 } 80 81 func (v *testGraphDependable) DependableName() []string { 82 return []string{v.VertexName} 83 } 84 85 func (v *testGraphDependable) DependentOn() []string { 86 return v.DependentOnMock 87 } 88 89 const testGraphAddStr = ` 90 42 91 84 92 ` 93 94 const testGraphConnectDepsStr = ` 95 a 96 b 97 a 98 `