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

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