github.com/jd3nn1s/terraform@v0.9.6-0.20170906225847-13878347b7a1/command/state_list_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/copy"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  func TestStateList(t *testing.T) {
    13  	state := testState()
    14  	statePath := testStateFile(t, state)
    15  
    16  	p := testProvider()
    17  	ui := new(cli.MockUi)
    18  	c := &StateListCommand{
    19  		Meta: Meta{
    20  			testingOverrides: metaOverridesForProvider(p),
    21  			Ui:               ui,
    22  		},
    23  	}
    24  
    25  	args := []string{
    26  		"-state", statePath,
    27  	}
    28  	if code := c.Run(args); code != 0 {
    29  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    30  	}
    31  
    32  	// Test that outputs were displayed
    33  	expected := strings.TrimSpace(testStateListOutput) + "\n"
    34  	actual := ui.OutputWriter.String()
    35  	if actual != expected {
    36  		t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
    37  	}
    38  }
    39  
    40  func TestStateList_backendState(t *testing.T) {
    41  	// Create a temporary working directory that is empty
    42  	td := tempDir(t)
    43  	copy.CopyDir(testFixturePath("state-list-backend"), td)
    44  	defer os.RemoveAll(td)
    45  	defer testChdir(t, td)()
    46  
    47  	p := testProvider()
    48  	ui := new(cli.MockUi)
    49  	c := &StateListCommand{
    50  		Meta: Meta{
    51  			testingOverrides: metaOverridesForProvider(p),
    52  			Ui:               ui,
    53  		},
    54  	}
    55  
    56  	args := []string{}
    57  	if code := c.Run(args); code != 0 {
    58  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    59  	}
    60  
    61  	// Test that outputs were displayed
    62  	expected := "null_resource.a\n"
    63  	actual := ui.OutputWriter.String()
    64  	if actual != expected {
    65  		t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
    66  	}
    67  }
    68  
    69  func TestStateList_noState(t *testing.T) {
    70  	tmp, cwd := testCwd(t)
    71  	defer testFixCwd(t, tmp, cwd)
    72  
    73  	p := testProvider()
    74  	ui := new(cli.MockUi)
    75  	c := &StateListCommand{
    76  		Meta: Meta{
    77  			testingOverrides: metaOverridesForProvider(p),
    78  			Ui:               ui,
    79  		},
    80  	}
    81  
    82  	args := []string{}
    83  	if code := c.Run(args); code != 1 {
    84  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    85  	}
    86  }
    87  
    88  const testStateListOutput = `
    89  test_instance.foo
    90  `