github.com/svenhamers/terraform@v0.11.12-beta1/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 TestStateListWithID(t *testing.T) {
    41  	state := testState()
    42  	statePath := testStateFile(t, state)
    43  
    44  	p := testProvider()
    45  	ui := new(cli.MockUi)
    46  	c := &StateListCommand{
    47  		Meta: Meta{
    48  			testingOverrides: metaOverridesForProvider(p),
    49  			Ui:               ui,
    50  		},
    51  	}
    52  
    53  	args := []string{
    54  		"-state", statePath,
    55  		"-id", "bar",
    56  	}
    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 := strings.TrimSpace(testStateListOutput) + "\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 TestStateListWithNonExistentID(t *testing.T) {
    70  	state := testState()
    71  	statePath := testStateFile(t, state)
    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  		"-state", statePath,
    84  		"-id", "baz",
    85  	}
    86  	if code := c.Run(args); code != 0 {
    87  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    88  	}
    89  
    90  	// Test that output is empty
    91  	if ui.OutputWriter != nil {
    92  		actual := ui.OutputWriter.String()
    93  		if actual != "" {
    94  			t.Fatalf("Expected an empty output but got: %q", actual)
    95  		}
    96  	}
    97  }
    98  
    99  func TestStateList_backendState(t *testing.T) {
   100  	// Create a temporary working directory that is empty
   101  	td := tempDir(t)
   102  	copy.CopyDir(testFixturePath("state-list-backend"), td)
   103  	defer os.RemoveAll(td)
   104  	defer testChdir(t, td)()
   105  
   106  	p := testProvider()
   107  	ui := new(cli.MockUi)
   108  	c := &StateListCommand{
   109  		Meta: Meta{
   110  			testingOverrides: metaOverridesForProvider(p),
   111  			Ui:               ui,
   112  		},
   113  	}
   114  
   115  	args := []string{}
   116  	if code := c.Run(args); code != 0 {
   117  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   118  	}
   119  
   120  	// Test that outputs were displayed
   121  	expected := "null_resource.a\n"
   122  	actual := ui.OutputWriter.String()
   123  	if actual != expected {
   124  		t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
   125  	}
   126  }
   127  
   128  func TestStateList_noState(t *testing.T) {
   129  	tmp, cwd := testCwd(t)
   130  	defer testFixCwd(t, tmp, cwd)
   131  
   132  	p := testProvider()
   133  	ui := new(cli.MockUi)
   134  	c := &StateListCommand{
   135  		Meta: Meta{
   136  			testingOverrides: metaOverridesForProvider(p),
   137  			Ui:               ui,
   138  		},
   139  	}
   140  
   141  	args := []string{}
   142  	if code := c.Run(args); code != 1 {
   143  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   144  	}
   145  }
   146  
   147  const testStateListOutput = `
   148  test_instance.foo
   149  `