diff --git a/command/command_test.go b/command/command_test.go index 2f3207aa7..e2165bafa 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -13,7 +13,7 @@ import ( const fixtureDir = "./test-fixtures" func testFixturePath(name string) string { - return filepath.Join(fixtureDir, name, "main.tf") + return filepath.Join(fixtureDir, name) } func testCtxConfig(p terraform.ResourceProvider) *terraform.ContextOpts { diff --git a/command/graph.go b/command/graph.go index 0d36166cd..02ff969de 100644 --- a/command/graph.go +++ b/command/graph.go @@ -1,9 +1,9 @@ package command import ( + "bytes" "flag" "fmt" - "os" "strings" "github.com/hashicorp/terraform/config" @@ -33,7 +33,9 @@ func (c *GraphCommand) Run(args []string) int { return 1 } - conf, err := config.Load(args[0]) + path := args[0] + + conf, err := config.LoadDir(path) if err != nil { c.Ui.Error(fmt.Sprintf("Error loading config: %s", err)) return 1 @@ -48,11 +50,14 @@ func (c *GraphCommand) Run(args []string) int { return 1 } + buf := new(bytes.Buffer) nodes := make([]digraph.Node, len(g.Nouns)) for i, n := range g.Nouns { nodes[i] = n } - digraph.GenerateDot(nodes, os.Stdout) + digraph.GenerateDot(nodes, buf) + + c.Ui.Output(buf.String()) return 0 } @@ -66,6 +71,10 @@ Usage: terraform graph [options] PATH shown. If the path is a plan file, then the dependency graph of the plan itself is shown. + The graph is outputted in DOT format. The typical program that can + read this format is GraphViz, but many web services are also available + to read this format. + ` return strings.TrimSpace(helpText) } diff --git a/command/graph_test.go b/command/graph_test.go new file mode 100644 index 000000000..382292340 --- /dev/null +++ b/command/graph_test.go @@ -0,0 +1,57 @@ +package command + +import ( + "strings" + "testing" + + "github.com/mitchellh/cli" +) + +func TestGraph(t *testing.T) { + ui := new(cli.MockUi) + c := &GraphCommand{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + } + + args := []string{ + testFixturePath("graph"), + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } + + output := ui.OutputWriter.String() + if !strings.Contains(output, "digraph {") { + t.Fatalf("doesn't look like digraph: %s", output) + } +} + +func TestGraph_noArgs(t *testing.T) { + ui := new(cli.MockUi) + c := &ApplyCommand{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + } + + args := []string{} + if code := c.Run(args); code != 1 { + t.Fatalf("bad: \n%s", ui.OutputWriter.String()) + } +} + +func TestGraph_multipleArgs(t *testing.T) { + ui := new(cli.MockUi) + c := &ApplyCommand{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + } + + args := []string{ + "bad", + "bad", + } + if code := c.Run(args); code != 1 { + t.Fatalf("bad: \n%s", ui.OutputWriter.String()) + } +} diff --git a/command/test-fixtures/graph/main.tf b/command/test-fixtures/graph/main.tf new file mode 100644 index 000000000..5794f94d9 --- /dev/null +++ b/command/test-fixtures/graph/main.tf @@ -0,0 +1,3 @@ +resource "test_instance" "foo" { + ami = "bar" +}