github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/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 := cli.NewMockUi()
    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 := cli.NewMockUi()
    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 := cli.NewMockUi()
    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_backendDefaultState(t *testing.T) {
   100  	// Create a temporary working directory that is empty
   101  	td := tempDir(t)
   102  	copy.CopyDir(testFixturePath("state-list-backend-default"), td)
   103  	defer os.RemoveAll(td)
   104  	defer testChdir(t, td)()
   105  
   106  	p := testProvider()
   107  	ui := cli.NewMockUi()
   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_backendCustomState(t *testing.T) {
   129  	// Create a temporary working directory that is empty
   130  	td := tempDir(t)
   131  	copy.CopyDir(testFixturePath("state-list-backend-custom"), td)
   132  	defer os.RemoveAll(td)
   133  	defer testChdir(t, td)()
   134  
   135  	p := testProvider()
   136  	ui := cli.NewMockUi()
   137  	c := &StateListCommand{
   138  		Meta: Meta{
   139  			testingOverrides: metaOverridesForProvider(p),
   140  			Ui:               ui,
   141  		},
   142  	}
   143  
   144  	args := []string{}
   145  	if code := c.Run(args); code != 0 {
   146  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   147  	}
   148  
   149  	// Test that outputs were displayed
   150  	expected := "null_resource.a\n"
   151  	actual := ui.OutputWriter.String()
   152  	if actual != expected {
   153  		t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
   154  	}
   155  }
   156  
   157  func TestStateList_backendOverrideState(t *testing.T) {
   158  	// Create a temporary working directory that is empty
   159  	td := tempDir(t)
   160  	copy.CopyDir(testFixturePath("state-list-backend-custom"), td)
   161  	defer os.RemoveAll(td)
   162  	defer testChdir(t, td)()
   163  
   164  	p := testProvider()
   165  	ui := cli.NewMockUi()
   166  	c := &StateListCommand{
   167  		Meta: Meta{
   168  			testingOverrides: metaOverridesForProvider(p),
   169  			Ui:               ui,
   170  		},
   171  	}
   172  
   173  	// This test is configured to use a local backend that has
   174  	// a custom path defined. So we test if we can still pass
   175  	// is a user defined state file that will then override the
   176  	// one configured in the backend. As this file does not exist
   177  	// it should exit with a no state found error.
   178  	args := []string{"-state=" + DefaultStateFilename}
   179  	if code := c.Run(args); code != 1 {
   180  		t.Fatalf("bad: %d", code)
   181  	}
   182  	if !strings.Contains(ui.ErrorWriter.String(), "No state file was found!") {
   183  		t.Fatalf("expected a no state file error, got: %s", ui.ErrorWriter.String())
   184  	}
   185  }
   186  
   187  func TestStateList_noState(t *testing.T) {
   188  	tmp, cwd := testCwd(t)
   189  	defer testFixCwd(t, tmp, cwd)
   190  
   191  	p := testProvider()
   192  	ui := cli.NewMockUi()
   193  	c := &StateListCommand{
   194  		Meta: Meta{
   195  			testingOverrides: metaOverridesForProvider(p),
   196  			Ui:               ui,
   197  		},
   198  	}
   199  
   200  	args := []string{}
   201  	if code := c.Run(args); code != 1 {
   202  		t.Fatalf("bad: %d", code)
   203  	}
   204  }
   205  
   206  const testStateListOutput = `
   207  test_instance.foo
   208  `