github.com/r3labs/terraform@v0.8.4/command/graph_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/mitchellh/cli" 10 ) 11 12 func TestGraph(t *testing.T) { 13 ui := new(cli.MockUi) 14 c := &GraphCommand{ 15 Meta: Meta{ 16 ContextOpts: testCtxConfig(testProvider()), 17 Ui: ui, 18 }, 19 } 20 21 args := []string{ 22 testFixturePath("graph"), 23 } 24 if code := c.Run(args); code != 0 { 25 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 26 } 27 28 output := ui.OutputWriter.String() 29 if !strings.Contains(output, "provider.test") { 30 t.Fatalf("doesn't look like digraph: %s", output) 31 } 32 } 33 34 func TestGraph_multipleArgs(t *testing.T) { 35 ui := new(cli.MockUi) 36 c := &GraphCommand{ 37 Meta: Meta{ 38 ContextOpts: testCtxConfig(testProvider()), 39 Ui: ui, 40 }, 41 } 42 43 args := []string{ 44 "bad", 45 "bad", 46 } 47 if code := c.Run(args); code != 1 { 48 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 49 } 50 } 51 52 func TestGraph_noArgs(t *testing.T) { 53 cwd, err := os.Getwd() 54 if err != nil { 55 t.Fatalf("err: %s", err) 56 } 57 if err := os.Chdir(testFixturePath("graph")); err != nil { 58 t.Fatalf("err: %s", err) 59 } 60 defer os.Chdir(cwd) 61 62 ui := new(cli.MockUi) 63 c := &GraphCommand{ 64 Meta: Meta{ 65 ContextOpts: testCtxConfig(testProvider()), 66 Ui: ui, 67 }, 68 } 69 70 args := []string{} 71 if code := c.Run(args); code != 0 { 72 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 73 } 74 75 output := ui.OutputWriter.String() 76 if !strings.Contains(output, "provider.test") { 77 t.Fatalf("doesn't look like digraph: %s", output) 78 } 79 } 80 81 func TestGraph_plan(t *testing.T) { 82 planPath := testPlanFile(t, &terraform.Plan{ 83 Diff: &terraform.Diff{ 84 Modules: []*terraform.ModuleDiff{ 85 &terraform.ModuleDiff{ 86 Path: []string{"root"}, 87 Resources: map[string]*terraform.InstanceDiff{ 88 "test_instance.bar": &terraform.InstanceDiff{ 89 Destroy: true, 90 }, 91 }, 92 }, 93 }, 94 }, 95 96 Module: testModule(t, "graph"), 97 }) 98 99 ui := new(cli.MockUi) 100 c := &GraphCommand{ 101 Meta: Meta{ 102 ContextOpts: testCtxConfig(testProvider()), 103 Ui: ui, 104 }, 105 } 106 107 args := []string{ 108 planPath, 109 } 110 if code := c.Run(args); code != 0 { 111 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 112 } 113 114 output := ui.OutputWriter.String() 115 if !strings.Contains(output, "provider.test") { 116 t.Fatalf("doesn't look like digraph: %s", output) 117 } 118 }