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