github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/command/init_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/copy" 10 "github.com/mitchellh/cli" 11 ) 12 13 func TestInit(t *testing.T) { 14 dir := tempDir(t) 15 16 ui := new(cli.MockUi) 17 c := &InitCommand{ 18 Meta: Meta{ 19 ContextOpts: testCtxConfig(testProvider()), 20 Ui: ui, 21 }, 22 } 23 24 args := []string{ 25 testFixturePath("init"), 26 dir, 27 } 28 if code := c.Run(args); code != 0 { 29 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 30 } 31 32 if _, err := os.Stat(filepath.Join(dir, "hello.tf")); err != nil { 33 t.Fatalf("err: %s", err) 34 } 35 } 36 37 func TestInit_cwd(t *testing.T) { 38 dir := tempDir(t) 39 if err := os.MkdirAll(dir, 0755); err != nil { 40 t.Fatalf("err: %s", err) 41 } 42 43 // Change to the temporary directory 44 cwd, err := os.Getwd() 45 if err != nil { 46 t.Fatalf("err: %s", err) 47 } 48 if err := os.Chdir(dir); err != nil { 49 t.Fatalf("err: %s", err) 50 } 51 defer os.Chdir(cwd) 52 53 ui := new(cli.MockUi) 54 c := &InitCommand{ 55 Meta: Meta{ 56 ContextOpts: testCtxConfig(testProvider()), 57 Ui: ui, 58 }, 59 } 60 61 args := []string{ 62 testFixturePath("init"), 63 } 64 if code := c.Run(args); code != 0 { 65 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 66 } 67 68 if _, err := os.Stat("hello.tf"); err != nil { 69 t.Fatalf("err: %s", err) 70 } 71 } 72 73 func TestInit_empty(t *testing.T) { 74 // Create a temporary working directory that is empty 75 td := tempDir(t) 76 os.MkdirAll(td, 0755) 77 defer os.RemoveAll(td) 78 defer testChdir(t, td)() 79 80 ui := new(cli.MockUi) 81 c := &InitCommand{ 82 Meta: Meta{ 83 ContextOpts: testCtxConfig(testProvider()), 84 Ui: ui, 85 }, 86 } 87 88 args := []string{} 89 if code := c.Run(args); code != 0 { 90 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 91 } 92 } 93 94 func TestInit_multipleArgs(t *testing.T) { 95 ui := new(cli.MockUi) 96 c := &InitCommand{ 97 Meta: Meta{ 98 ContextOpts: testCtxConfig(testProvider()), 99 Ui: ui, 100 }, 101 } 102 103 args := []string{ 104 "bad", 105 "bad", 106 } 107 if code := c.Run(args); code != 1 { 108 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 109 } 110 } 111 112 // https://github.com/hashicorp/terraform/issues/518 113 func TestInit_dstInSrc(t *testing.T) { 114 dir := tempDir(t) 115 if err := os.MkdirAll(dir, 0755); err != nil { 116 t.Fatalf("err: %s", err) 117 } 118 119 // Change to the temporary directory 120 cwd, err := os.Getwd() 121 if err != nil { 122 t.Fatalf("err: %s", err) 123 } 124 if err := os.Chdir(dir); err != nil { 125 t.Fatalf("err: %s", err) 126 } 127 defer os.Chdir(cwd) 128 129 if _, err := os.Create("issue518.tf"); err != nil { 130 t.Fatalf("err: %s", err) 131 } 132 133 ui := new(cli.MockUi) 134 c := &InitCommand{ 135 Meta: Meta{ 136 ContextOpts: testCtxConfig(testProvider()), 137 Ui: ui, 138 }, 139 } 140 141 args := []string{ 142 ".", 143 "foo", 144 } 145 if code := c.Run(args); code != 0 { 146 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 147 } 148 149 if _, err := os.Stat(filepath.Join(dir, "foo", "issue518.tf")); err != nil { 150 t.Fatalf("err: %s", err) 151 } 152 } 153 154 func TestInit_get(t *testing.T) { 155 // Create a temporary working directory that is empty 156 td := tempDir(t) 157 copy.CopyDir(testFixturePath("init-get"), td) 158 defer os.RemoveAll(td) 159 defer testChdir(t, td)() 160 161 ui := new(cli.MockUi) 162 c := &InitCommand{ 163 Meta: Meta{ 164 ContextOpts: testCtxConfig(testProvider()), 165 Ui: ui, 166 }, 167 } 168 169 args := []string{} 170 if code := c.Run(args); code != 0 { 171 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 172 } 173 174 // Check output 175 output := ui.OutputWriter.String() 176 if !strings.Contains(output, "Get: file://") { 177 t.Fatalf("doesn't look like get: %s", output) 178 } 179 } 180 181 func TestInit_copyGet(t *testing.T) { 182 // Create a temporary working directory that is empty 183 td := tempDir(t) 184 os.MkdirAll(td, 0755) 185 defer os.RemoveAll(td) 186 defer testChdir(t, td)() 187 188 ui := new(cli.MockUi) 189 c := &InitCommand{ 190 Meta: Meta{ 191 ContextOpts: testCtxConfig(testProvider()), 192 Ui: ui, 193 }, 194 } 195 196 args := []string{ 197 testFixturePath("init-get"), 198 } 199 if code := c.Run(args); code != 0 { 200 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 201 } 202 203 // Check copy 204 if _, err := os.Stat("main.tf"); err != nil { 205 t.Fatalf("err: %s", err) 206 } 207 208 output := ui.OutputWriter.String() 209 if !strings.Contains(output, "Get: file://") { 210 t.Fatalf("doesn't look like get: %s", output) 211 } 212 } 213 214 func TestInit_backend(t *testing.T) { 215 // Create a temporary working directory that is empty 216 td := tempDir(t) 217 copy.CopyDir(testFixturePath("init-backend"), td) 218 defer os.RemoveAll(td) 219 defer testChdir(t, td)() 220 221 ui := new(cli.MockUi) 222 c := &InitCommand{ 223 Meta: Meta{ 224 ContextOpts: testCtxConfig(testProvider()), 225 Ui: ui, 226 }, 227 } 228 229 args := []string{} 230 if code := c.Run(args); code != 0 { 231 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 232 } 233 234 if _, err := os.Stat(filepath.Join(DefaultDataDir, DefaultStateFilename)); err != nil { 235 t.Fatalf("err: %s", err) 236 } 237 } 238 239 func TestInit_backendConfigFile(t *testing.T) { 240 // Create a temporary working directory that is empty 241 td := tempDir(t) 242 copy.CopyDir(testFixturePath("init-backend-config-file"), td) 243 defer os.RemoveAll(td) 244 defer testChdir(t, td)() 245 246 ui := new(cli.MockUi) 247 c := &InitCommand{ 248 Meta: Meta{ 249 ContextOpts: testCtxConfig(testProvider()), 250 Ui: ui, 251 }, 252 } 253 254 args := []string{"-backend-config", "input.config"} 255 if code := c.Run(args); code != 0 { 256 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 257 } 258 259 // Read our saved backend config and verify we have our settings 260 state := testStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) 261 if v := state.Backend.Config["path"]; v != "hello" { 262 t.Fatalf("bad: %#v", v) 263 } 264 } 265 266 func TestInit_copyBackendDst(t *testing.T) { 267 // Create a temporary working directory that is empty 268 td := tempDir(t) 269 os.MkdirAll(td, 0755) 270 defer os.RemoveAll(td) 271 defer testChdir(t, td)() 272 273 ui := new(cli.MockUi) 274 c := &InitCommand{ 275 Meta: Meta{ 276 ContextOpts: testCtxConfig(testProvider()), 277 Ui: ui, 278 }, 279 } 280 281 args := []string{ 282 testFixturePath("init-backend"), 283 "dst", 284 } 285 if code := c.Run(args); code != 0 { 286 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 287 } 288 289 if _, err := os.Stat(filepath.Join( 290 "dst", DefaultDataDir, DefaultStateFilename)); err != nil { 291 t.Fatalf("err: %s", err) 292 } 293 } 294 295 /* 296 func TestInit_remoteState(t *testing.T) { 297 tmp, cwd := testCwd(t) 298 defer testFixCwd(t, tmp, cwd) 299 300 s := terraform.NewState() 301 conf, srv := testRemoteState(t, s, 200) 302 defer srv.Close() 303 304 ui := new(cli.MockUi) 305 c := &InitCommand{ 306 Meta: Meta{ 307 ContextOpts: testCtxConfig(testProvider()), 308 Ui: ui, 309 }, 310 } 311 312 args := []string{ 313 "-backend", "HTTP", 314 "-backend-config", "address=" + conf.Config["address"], 315 testFixturePath("init"), 316 tmp, 317 } 318 if code := c.Run(args); code != 0 { 319 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 320 } 321 322 if _, err := os.Stat(filepath.Join(tmp, "hello.tf")); err != nil { 323 t.Fatalf("err: %s", err) 324 } 325 326 if _, err := os.Stat(filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)); err != nil { 327 t.Fatalf("missing state: %s", err) 328 } 329 } 330 331 func TestInit_remoteStateSubdir(t *testing.T) { 332 tmp, cwd := testCwd(t) 333 defer testFixCwd(t, tmp, cwd) 334 subdir := filepath.Join(tmp, "subdir") 335 336 s := terraform.NewState() 337 conf, srv := testRemoteState(t, s, 200) 338 defer srv.Close() 339 340 ui := new(cli.MockUi) 341 c := &InitCommand{ 342 Meta: Meta{ 343 ContextOpts: testCtxConfig(testProvider()), 344 Ui: ui, 345 }, 346 } 347 348 args := []string{ 349 "-backend", "http", 350 "-backend-config", "address=" + conf.Config["address"], 351 testFixturePath("init"), 352 subdir, 353 } 354 if code := c.Run(args); code != 0 { 355 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 356 } 357 358 if _, err := os.Stat(filepath.Join(subdir, "hello.tf")); err != nil { 359 t.Fatalf("err: %s", err) 360 } 361 362 if _, err := os.Stat(filepath.Join(subdir, DefaultDataDir, DefaultStateFilename)); err != nil { 363 t.Fatalf("missing state: %s", err) 364 } 365 } 366 367 func TestInit_remoteStateWithLocal(t *testing.T) { 368 tmp, cwd := testCwd(t) 369 defer testFixCwd(t, tmp, cwd) 370 371 statePath := filepath.Join(tmp, DefaultStateFilename) 372 373 // Write some state 374 f, err := os.Create(statePath) 375 if err != nil { 376 t.Fatalf("err: %s", err) 377 } 378 err = terraform.WriteState(testState(), f) 379 f.Close() 380 if err != nil { 381 t.Fatalf("err: %s", err) 382 } 383 384 ui := new(cli.MockUi) 385 c := &InitCommand{ 386 Meta: Meta{ 387 ContextOpts: testCtxConfig(testProvider()), 388 Ui: ui, 389 }, 390 } 391 392 args := []string{ 393 "-backend", "http", 394 "-backend-config", "address=http://google.com", 395 testFixturePath("init"), 396 } 397 if code := c.Run(args); code == 0 { 398 t.Fatalf("should have failed: \n%s", ui.OutputWriter.String()) 399 } 400 } 401 402 func TestInit_remoteStateWithRemote(t *testing.T) { 403 tmp, cwd := testCwd(t) 404 defer testFixCwd(t, tmp, cwd) 405 406 statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename) 407 if err := os.MkdirAll(filepath.Dir(statePath), 0755); err != nil { 408 t.Fatalf("err: %s", err) 409 } 410 411 // Write some state 412 f, err := os.Create(statePath) 413 if err != nil { 414 t.Fatalf("err: %s", err) 415 } 416 err = terraform.WriteState(testState(), f) 417 f.Close() 418 if err != nil { 419 t.Fatalf("err: %s", err) 420 } 421 422 ui := new(cli.MockUi) 423 c := &InitCommand{ 424 Meta: Meta{ 425 ContextOpts: testCtxConfig(testProvider()), 426 Ui: ui, 427 }, 428 } 429 430 args := []string{ 431 "-backend", "http", 432 "-backend-config", "address=http://google.com", 433 testFixturePath("init"), 434 } 435 if code := c.Run(args); code == 0 { 436 t.Fatalf("should have failed: \n%s", ui.OutputWriter.String()) 437 } 438 } 439 */