github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/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_noConfig(t *testing.T) {
    85  	td := tempDir(t)
    86  	os.MkdirAll(td, 0755)
    87  	defer os.RemoveAll(td)
    88  	defer testChdir(t, td)()
    89  
    90  	ui := new(cli.MockUi)
    91  	c := &GraphCommand{
    92  		Meta: Meta{
    93  			testingOverrides: metaOverridesForProvider(testProvider()),
    94  			Ui:               ui,
    95  		},
    96  	}
    97  
    98  	// Running the graph command without a config should not panic,
    99  	// but this may be an error at some point in the future.
   100  	args := []string{"-type", "apply"}
   101  	if code := c.Run(args); code != 0 {
   102  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   103  	}
   104  }
   105  
   106  func TestGraph_plan(t *testing.T) {
   107  	tmp, cwd := testCwd(t)
   108  	defer testFixCwd(t, tmp, cwd)
   109  
   110  	planPath := testPlanFile(t, &terraform.Plan{
   111  		Diff: &terraform.Diff{
   112  			Modules: []*terraform.ModuleDiff{
   113  				&terraform.ModuleDiff{
   114  					Path: []string{"root"},
   115  					Resources: map[string]*terraform.InstanceDiff{
   116  						"test_instance.bar": &terraform.InstanceDiff{
   117  							Destroy: true,
   118  						},
   119  					},
   120  				},
   121  			},
   122  		},
   123  
   124  		Module: testModule(t, "graph"),
   125  	})
   126  
   127  	ui := new(cli.MockUi)
   128  	c := &GraphCommand{
   129  		Meta: Meta{
   130  			testingOverrides: metaOverridesForProvider(testProvider()),
   131  			Ui:               ui,
   132  		},
   133  	}
   134  
   135  	args := []string{
   136  		planPath,
   137  	}
   138  	if code := c.Run(args); code != 0 {
   139  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   140  	}
   141  
   142  	output := ui.OutputWriter.String()
   143  	if !strings.Contains(output, "provider.test") {
   144  		t.Fatalf("doesn't look like digraph: %s", output)
   145  	}
   146  }