2016-03-22 18:41:02 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-02-22 04:43:05 +01:00
|
|
|
"os"
|
2016-03-22 18:41:02 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStateList(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
2018-09-30 18:29:51 +02:00
|
|
|
ui := cli.NewMockUi()
|
2016-03-22 18:41:02 +01:00
|
|
|
c := &StateListCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-03-22 18:41:02 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that outputs were displayed
|
|
|
|
expected := strings.TrimSpace(testStateListOutput) + "\n"
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 19:54:48 +01:00
|
|
|
func TestStateListWithID(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
2018-09-30 18:29:51 +02:00
|
|
|
ui := cli.NewMockUi()
|
2018-02-26 19:54:48 +01:00
|
|
|
c := &StateListCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-id", "bar",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that outputs were displayed
|
|
|
|
expected := strings.TrimSpace(testStateListOutput) + "\n"
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateListWithNonExistentID(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
2018-09-30 18:29:51 +02:00
|
|
|
ui := cli.NewMockUi()
|
2018-02-26 19:54:48 +01:00
|
|
|
c := &StateListCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-id", "baz",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that output is empty
|
|
|
|
if ui.OutputWriter != nil {
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if actual != "" {
|
|
|
|
t.Fatalf("Expected an empty output but got: %q", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:01:15 +02:00
|
|
|
func TestStateList_backendDefaultState(t *testing.T) {
|
2017-02-22 04:43:05 +01:00
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("state-list-backend-default"), td)
|
2017-02-22 04:43:05 +01:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
2018-09-30 18:29:51 +02:00
|
|
|
ui := cli.NewMockUi()
|
2017-02-22 04:43:05 +01:00
|
|
|
c := &StateListCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-02-22 04:43:05 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that outputs were displayed
|
|
|
|
expected := "null_resource.a\n"
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:01:15 +02:00
|
|
|
func TestStateList_backendCustomState(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("state-list-backend-custom"), td)
|
2018-10-17 23:01:15 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := &StateListCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that outputs were displayed
|
|
|
|
expected := "null_resource.a\n"
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateList_backendOverrideState(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
2020-10-07 18:48:25 +02:00
|
|
|
testCopyDir(t, testFixturePath("state-list-backend-custom"), td)
|
2018-10-17 23:01:15 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := &StateListCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is configured to use a local backend that has
|
|
|
|
// a custom path defined. So we test if we can still pass
|
|
|
|
// is a user defined state file that will then override the
|
|
|
|
// one configured in the backend. As this file does not exist
|
|
|
|
// it should exit with a no state found error.
|
|
|
|
args := []string{"-state=" + DefaultStateFilename}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "No state file was found!") {
|
|
|
|
t.Fatalf("expected a no state file error, got: %s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 18:41:02 +01:00
|
|
|
func TestStateList_noState(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
|
|
|
p := testProvider()
|
2018-09-30 18:29:51 +02:00
|
|
|
ui := cli.NewMockUi()
|
2016-03-22 18:41:02 +01:00
|
|
|
c := &StateListCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-03-22 18:41:02 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 1 {
|
2018-10-17 23:01:15 +02:00
|
|
|
t.Fatalf("bad: %d", code)
|
2016-03-22 18:41:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 17:07:15 +01:00
|
|
|
func TestStateList_modules(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
|
|
|
td := tempDir(t)
|
|
|
|
testCopyDir(t, testFixturePath("state-list-nested-modules"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := &StateListCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("list resources in module and submodules", func(t *testing.T) {
|
|
|
|
args := []string{"module.nest"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// resources in the module and any submodules should be included in the outputs
|
|
|
|
expected := "module.nest.test_instance.nest\nmodule.nest.module.subnest.test_instance.subnest\n"
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("submodule has resources only", func(t *testing.T) {
|
|
|
|
// now get the state for a module that has no resources, only another nested module
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
args := []string{"module.nonexist"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
expected := "module.nonexist.module.child.test_instance.child\n"
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("expanded module", func(t *testing.T) {
|
|
|
|
// finally get the state for a module with an index
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
args := []string{"module.count"}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
expected := "module.count[0].test_instance.count\nmodule.count[1].test_instance.count\n"
|
|
|
|
actual := ui.OutputWriter.String()
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("completely nonexistent module", func(t *testing.T) {
|
|
|
|
// finally get the state for a module with an index
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
args := []string{"module.notevenalittlebit"}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-22 18:41:02 +01:00
|
|
|
const testStateListOutput = `
|
|
|
|
test_instance.foo
|
|
|
|
`
|