github.com/hartzell/terraform@v0.8.6-0.20180503104400-0cc9e050ecd4/command/show_test.go (about)

     1  package command
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/config/module"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func TestShow(t *testing.T) {
    14  	ui := new(cli.MockUi)
    15  	c := &ShowCommand{
    16  		Meta: Meta{
    17  			testingOverrides: metaOverridesForProvider(testProvider()),
    18  			Ui:               ui,
    19  		},
    20  	}
    21  
    22  	args := []string{
    23  		"bad",
    24  		"bad",
    25  	}
    26  	if code := c.Run(args); code != 1 {
    27  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    28  	}
    29  }
    30  
    31  func TestShow_noArgs(t *testing.T) {
    32  	// Create the default state
    33  	statePath := testStateFile(t, testState())
    34  	defer testChdir(t, filepath.Dir(statePath))()
    35  
    36  	ui := new(cli.MockUi)
    37  	c := &ShowCommand{
    38  		Meta: Meta{
    39  			testingOverrides: metaOverridesForProvider(testProvider()),
    40  			Ui:               ui,
    41  		},
    42  	}
    43  
    44  	args := []string{}
    45  	if code := c.Run(args); code != 0 {
    46  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    47  	}
    48  }
    49  
    50  func TestShow_noArgsNoState(t *testing.T) {
    51  	// Create the default state
    52  	statePath := testStateFile(t, testState())
    53  	defer testChdir(t, filepath.Dir(statePath))()
    54  
    55  	ui := new(cli.MockUi)
    56  	c := &ShowCommand{
    57  		Meta: Meta{
    58  			testingOverrides: metaOverridesForProvider(testProvider()),
    59  			Ui:               ui,
    60  		},
    61  	}
    62  
    63  	args := []string{}
    64  	if code := c.Run(args); code != 0 {
    65  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    66  	}
    67  }
    68  
    69  func TestShow_plan(t *testing.T) {
    70  	planPath := testPlanFile(t, &terraform.Plan{
    71  		Module: new(module.Tree),
    72  	})
    73  
    74  	ui := new(cli.MockUi)
    75  	c := &ShowCommand{
    76  		Meta: Meta{
    77  			testingOverrides: metaOverridesForProvider(testProvider()),
    78  			Ui:               ui,
    79  		},
    80  	}
    81  
    82  	args := []string{
    83  		planPath,
    84  	}
    85  	if code := c.Run(args); code != 0 {
    86  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    87  	}
    88  }
    89  
    90  func TestShow_noArgsRemoteState(t *testing.T) {
    91  	tmp, cwd := testCwd(t)
    92  	defer testFixCwd(t, tmp, cwd)
    93  
    94  	// Create some legacy remote state
    95  	legacyState := testState()
    96  	_, srv := testRemoteState(t, legacyState, 200)
    97  	defer srv.Close()
    98  	testStateFileRemote(t, legacyState)
    99  
   100  	ui := new(cli.MockUi)
   101  	c := &ShowCommand{
   102  		Meta: Meta{
   103  			testingOverrides: metaOverridesForProvider(testProvider()),
   104  			Ui:               ui,
   105  		},
   106  	}
   107  
   108  	args := []string{}
   109  	if code := c.Run(args); code != 0 {
   110  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
   111  	}
   112  
   113  	expected := "test_instance.foo"
   114  	actual := ui.OutputWriter.String()
   115  	if !strings.Contains(actual, expected) {
   116  		t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
   117  	}
   118  }
   119  
   120  func TestShow_state(t *testing.T) {
   121  	originalState := testState()
   122  	statePath := testStateFile(t, originalState)
   123  
   124  	ui := new(cli.MockUi)
   125  	c := &ShowCommand{
   126  		Meta: Meta{
   127  			testingOverrides: metaOverridesForProvider(testProvider()),
   128  			Ui:               ui,
   129  		},
   130  	}
   131  
   132  	args := []string{
   133  		statePath,
   134  	}
   135  	if code := c.Run(args); code != 0 {
   136  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   137  	}
   138  }