github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/command/state_list_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/mitchellh/cli"
     9  )
    10  
    11  func TestStateList(t *testing.T) {
    12  	state := testState()
    13  	statePath := testStateFile(t, state)
    14  
    15  	p := testProvider()
    16  	ui := cli.NewMockUi()
    17  	c := &StateListCommand{
    18  		Meta: Meta{
    19  			testingOverrides: metaOverridesForProvider(p),
    20  			Ui:               ui,
    21  		},
    22  	}
    23  
    24  	args := []string{
    25  		"-state", statePath,
    26  	}
    27  	if code := c.Run(args); code != 0 {
    28  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    29  	}
    30  
    31  	// Test that outputs were displayed
    32  	expected := strings.TrimSpace(testStateListOutput) + "\n"
    33  	actual := ui.OutputWriter.String()
    34  	if actual != expected {
    35  		t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
    36  	}
    37  }
    38  
    39  func TestStateListWithID(t *testing.T) {
    40  	state := testState()
    41  	statePath := testStateFile(t, state)
    42  
    43  	p := testProvider()
    44  	ui := cli.NewMockUi()
    45  	c := &StateListCommand{
    46  		Meta: Meta{
    47  			testingOverrides: metaOverridesForProvider(p),
    48  			Ui:               ui,
    49  		},
    50  	}
    51  
    52  	args := []string{
    53  		"-state", statePath,
    54  		"-id", "bar",
    55  	}
    56  	if code := c.Run(args); code != 0 {
    57  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    58  	}
    59  
    60  	// Test that outputs were displayed
    61  	expected := strings.TrimSpace(testStateListOutput) + "\n"
    62  	actual := ui.OutputWriter.String()
    63  	if actual != expected {
    64  		t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
    65  	}
    66  }
    67  
    68  func TestStateListWithNonExistentID(t *testing.T) {
    69  	state := testState()
    70  	statePath := testStateFile(t, state)
    71  
    72  	p := testProvider()
    73  	ui := cli.NewMockUi()
    74  	c := &StateListCommand{
    75  		Meta: Meta{
    76  			testingOverrides: metaOverridesForProvider(p),
    77  			Ui:               ui,
    78  		},
    79  	}
    80  
    81  	args := []string{
    82  		"-state", statePath,
    83  		"-id", "baz",
    84  	}
    85  	if code := c.Run(args); code != 0 {
    86  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    87  	}
    88  
    89  	// Test that output is empty
    90  	if ui.OutputWriter != nil {
    91  		actual := ui.OutputWriter.String()
    92  		if actual != "" {
    93  			t.Fatalf("Expected an empty output but got: %q", actual)
    94  		}
    95  	}
    96  }
    97  
    98  func TestStateList_backendDefaultState(t *testing.T) {
    99  	// Create a temporary working directory that is empty
   100  	td := tempDir(t)
   101  	testCopyDir(t, testFixturePath("state-list-backend-default"), td)
   102  	defer os.RemoveAll(td)
   103  	defer testChdir(t, td)()
   104  
   105  	p := testProvider()
   106  	ui := cli.NewMockUi()
   107  	c := &StateListCommand{
   108  		Meta: Meta{
   109  			testingOverrides: metaOverridesForProvider(p),
   110  			Ui:               ui,
   111  		},
   112  	}
   113  
   114  	args := []string{}
   115  	if code := c.Run(args); code != 0 {
   116  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   117  	}
   118  
   119  	// Test that outputs were displayed
   120  	expected := "null_resource.a\n"
   121  	actual := ui.OutputWriter.String()
   122  	if actual != expected {
   123  		t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
   124  	}
   125  }
   126  
   127  func TestStateList_backendCustomState(t *testing.T) {
   128  	// Create a temporary working directory that is empty
   129  	td := tempDir(t)
   130  	testCopyDir(t, testFixturePath("state-list-backend-custom"), td)
   131  	defer os.RemoveAll(td)
   132  	defer testChdir(t, td)()
   133  
   134  	p := testProvider()
   135  	ui := cli.NewMockUi()
   136  	c := &StateListCommand{
   137  		Meta: Meta{
   138  			testingOverrides: metaOverridesForProvider(p),
   139  			Ui:               ui,
   140  		},
   141  	}
   142  
   143  	args := []string{}
   144  	if code := c.Run(args); code != 0 {
   145  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   146  	}
   147  
   148  	// Test that outputs were displayed
   149  	expected := "null_resource.a\n"
   150  	actual := ui.OutputWriter.String()
   151  	if actual != expected {
   152  		t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
   153  	}
   154  }
   155  
   156  func TestStateList_backendOverrideState(t *testing.T) {
   157  	// Create a temporary working directory that is empty
   158  	td := tempDir(t)
   159  	testCopyDir(t, testFixturePath("state-list-backend-custom"), td)
   160  	defer os.RemoveAll(td)
   161  	defer testChdir(t, td)()
   162  
   163  	p := testProvider()
   164  	ui := cli.NewMockUi()
   165  	c := &StateListCommand{
   166  		Meta: Meta{
   167  			testingOverrides: metaOverridesForProvider(p),
   168  			Ui:               ui,
   169  		},
   170  	}
   171  
   172  	// This test is configured to use a local backend that has
   173  	// a custom path defined. So we test if we can still pass
   174  	// is a user defined state file that will then override the
   175  	// one configured in the backend. As this file does not exist
   176  	// it should exit with a no state found error.
   177  	args := []string{"-state=" + DefaultStateFilename}
   178  	if code := c.Run(args); code != 1 {
   179  		t.Fatalf("bad: %d", code)
   180  	}
   181  	if !strings.Contains(ui.ErrorWriter.String(), "No state file was found!") {
   182  		t.Fatalf("expected a no state file error, got: %s", ui.ErrorWriter.String())
   183  	}
   184  }
   185  
   186  func TestStateList_noState(t *testing.T) {
   187  	tmp, cwd := testCwd(t)
   188  	defer testFixCwd(t, tmp, cwd)
   189  
   190  	p := testProvider()
   191  	ui := cli.NewMockUi()
   192  	c := &StateListCommand{
   193  		Meta: Meta{
   194  			testingOverrides: metaOverridesForProvider(p),
   195  			Ui:               ui,
   196  		},
   197  	}
   198  
   199  	args := []string{}
   200  	if code := c.Run(args); code != 1 {
   201  		t.Fatalf("bad: %d", code)
   202  	}
   203  }
   204  
   205  func TestStateList_modules(t *testing.T) {
   206  	// Create a temporary working directory that is empty
   207  	td := tempDir(t)
   208  	testCopyDir(t, testFixturePath("state-list-nested-modules"), td)
   209  	defer os.RemoveAll(td)
   210  	defer testChdir(t, td)()
   211  
   212  	p := testProvider()
   213  	ui := cli.NewMockUi()
   214  	c := &StateListCommand{
   215  		Meta: Meta{
   216  			testingOverrides: metaOverridesForProvider(p),
   217  			Ui:               ui,
   218  		},
   219  	}
   220  
   221  	t.Run("list resources in module and submodules", func(t *testing.T) {
   222  		args := []string{"module.nest"}
   223  		if code := c.Run(args); code != 0 {
   224  			t.Fatalf("bad: %d", code)
   225  		}
   226  
   227  		// resources in the module and any submodules should be included in the outputs
   228  		expected := "module.nest.test_instance.nest\nmodule.nest.module.subnest.test_instance.subnest\n"
   229  		actual := ui.OutputWriter.String()
   230  		if actual != expected {
   231  			t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
   232  		}
   233  	})
   234  
   235  	t.Run("submodule has resources only", func(t *testing.T) {
   236  		// now get the state for a module that has no resources, only another nested module
   237  		ui.OutputWriter.Reset()
   238  		args := []string{"module.nonexist"}
   239  		if code := c.Run(args); code != 0 {
   240  			t.Fatalf("bad: %d", code)
   241  		}
   242  		expected := "module.nonexist.module.child.test_instance.child\n"
   243  		actual := ui.OutputWriter.String()
   244  		if actual != expected {
   245  			t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
   246  		}
   247  	})
   248  
   249  	t.Run("expanded module", func(t *testing.T) {
   250  		// finally get the state for a module with an index
   251  		ui.OutputWriter.Reset()
   252  		args := []string{"module.count"}
   253  		if code := c.Run(args); code != 0 {
   254  			t.Fatalf("bad: %d", code)
   255  		}
   256  		expected := "module.count[0].test_instance.count\nmodule.count[1].test_instance.count\n"
   257  		actual := ui.OutputWriter.String()
   258  		if actual != expected {
   259  			t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
   260  		}
   261  	})
   262  
   263  	t.Run("completely nonexistent module", func(t *testing.T) {
   264  		// finally get the state for a module with an index
   265  		ui.OutputWriter.Reset()
   266  		args := []string{"module.notevenalittlebit"}
   267  		if code := c.Run(args); code != 1 {
   268  			t.Fatalf("bad: %d", code)
   269  		}
   270  	})
   271  
   272  }
   273  
   274  const testStateListOutput = `
   275  test_instance.foo
   276  `