github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/debug_json2dot_test.go (about)

     1  package command
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/dag"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func TestDebugJSON2Dot(t *testing.T) {
    14  	// create the graph JSON output
    15  	logFile, err := ioutil.TempFile("", "tf")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	defer os.Remove(logFile.Name())
    20  
    21  	var g dag.Graph
    22  	g.SetDebugWriter(logFile)
    23  
    24  	g.Add(1)
    25  	g.Add(2)
    26  	g.Add(3)
    27  	g.Connect(dag.BasicEdge(1, 2))
    28  	g.Connect(dag.BasicEdge(2, 3))
    29  
    30  	ui := new(cli.MockUi)
    31  	c := &DebugJSON2DotCommand{
    32  		Meta: Meta{
    33  			testingOverrides: metaOverridesForProvider(testProvider()),
    34  			Ui:               ui,
    35  		},
    36  	}
    37  
    38  	args := []string{
    39  		logFile.Name(),
    40  	}
    41  	if code := c.Run(args); code != 0 {
    42  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    43  	}
    44  
    45  	output := ui.OutputWriter.String()
    46  	if !strings.HasPrefix(output, "digraph {") {
    47  		t.Fatalf("doesn't look like digraph: %s", output)
    48  	}
    49  
    50  	if !strings.Contains(output, `subgraph "root" {`) {
    51  		t.Fatalf("doesn't contains root subgraph: %s", output)
    52  	}
    53  }