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