github.com/mehmetalisavas/terraform@v0.7.10/command/init_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/mitchellh/cli" 10 ) 11 12 func TestInit(t *testing.T) { 13 dir := tempDir(t) 14 15 ui := new(cli.MockUi) 16 c := &InitCommand{ 17 Meta: Meta{ 18 ContextOpts: testCtxConfig(testProvider()), 19 Ui: ui, 20 }, 21 } 22 23 args := []string{ 24 testFixturePath("init"), 25 dir, 26 } 27 if code := c.Run(args); code != 0 { 28 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 29 } 30 31 if _, err := os.Stat(filepath.Join(dir, "hello.tf")); err != nil { 32 t.Fatalf("err: %s", err) 33 } 34 } 35 36 func TestInit_cwd(t *testing.T) { 37 dir := tempDir(t) 38 if err := os.MkdirAll(dir, 0755); err != nil { 39 t.Fatalf("err: %s", err) 40 } 41 42 // Change to the temporary directory 43 cwd, err := os.Getwd() 44 if err != nil { 45 t.Fatalf("err: %s", err) 46 } 47 if err := os.Chdir(dir); err != nil { 48 t.Fatalf("err: %s", err) 49 } 50 defer os.Chdir(cwd) 51 52 ui := new(cli.MockUi) 53 c := &InitCommand{ 54 Meta: Meta{ 55 ContextOpts: testCtxConfig(testProvider()), 56 Ui: ui, 57 }, 58 } 59 60 args := []string{ 61 testFixturePath("init"), 62 } 63 if code := c.Run(args); code != 0 { 64 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 65 } 66 67 if _, err := os.Stat("hello.tf"); err != nil { 68 t.Fatalf("err: %s", err) 69 } 70 } 71 72 func TestInit_multipleArgs(t *testing.T) { 73 ui := new(cli.MockUi) 74 c := &InitCommand{ 75 Meta: Meta{ 76 ContextOpts: testCtxConfig(testProvider()), 77 Ui: ui, 78 }, 79 } 80 81 args := []string{ 82 "bad", 83 "bad", 84 } 85 if code := c.Run(args); code != 1 { 86 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 87 } 88 } 89 90 func TestInit_noArgs(t *testing.T) { 91 ui := new(cli.MockUi) 92 c := &InitCommand{ 93 Meta: Meta{ 94 ContextOpts: testCtxConfig(testProvider()), 95 Ui: ui, 96 }, 97 } 98 99 args := []string{} 100 if code := c.Run(args); code != 1 { 101 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 102 } 103 } 104 105 // https://github.com/hashicorp/terraform/issues/518 106 func TestInit_dstInSrc(t *testing.T) { 107 dir := tempDir(t) 108 if err := os.MkdirAll(dir, 0755); err != nil { 109 t.Fatalf("err: %s", err) 110 } 111 112 // Change to the temporary directory 113 cwd, err := os.Getwd() 114 if err != nil { 115 t.Fatalf("err: %s", err) 116 } 117 if err := os.Chdir(dir); err != nil { 118 t.Fatalf("err: %s", err) 119 } 120 defer os.Chdir(cwd) 121 122 if _, err := os.Create("issue518.tf"); err != nil { 123 t.Fatalf("err: %s", err) 124 } 125 126 ui := new(cli.MockUi) 127 c := &InitCommand{ 128 Meta: Meta{ 129 ContextOpts: testCtxConfig(testProvider()), 130 Ui: ui, 131 }, 132 } 133 134 args := []string{ 135 ".", 136 "foo", 137 } 138 if code := c.Run(args); code != 0 { 139 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 140 } 141 142 if _, err := os.Stat(filepath.Join(dir, "foo", "issue518.tf")); err != nil { 143 t.Fatalf("err: %s", err) 144 } 145 } 146 147 func TestInit_remoteState(t *testing.T) { 148 tmp, cwd := testCwd(t) 149 defer testFixCwd(t, tmp, cwd) 150 151 s := terraform.NewState() 152 conf, srv := testRemoteState(t, s, 200) 153 defer srv.Close() 154 155 ui := new(cli.MockUi) 156 c := &InitCommand{ 157 Meta: Meta{ 158 ContextOpts: testCtxConfig(testProvider()), 159 Ui: ui, 160 }, 161 } 162 163 args := []string{ 164 "-backend", "HTTP", 165 "-backend-config", "address=" + conf.Config["address"], 166 testFixturePath("init"), 167 tmp, 168 } 169 if code := c.Run(args); code != 0 { 170 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 171 } 172 173 if _, err := os.Stat(filepath.Join(tmp, "hello.tf")); err != nil { 174 t.Fatalf("err: %s", err) 175 } 176 177 if _, err := os.Stat(filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)); err != nil { 178 t.Fatalf("missing state: %s", err) 179 } 180 } 181 182 func TestInit_remoteStateSubdir(t *testing.T) { 183 tmp, cwd := testCwd(t) 184 defer testFixCwd(t, tmp, cwd) 185 subdir := filepath.Join(tmp, "subdir") 186 187 s := terraform.NewState() 188 conf, srv := testRemoteState(t, s, 200) 189 defer srv.Close() 190 191 ui := new(cli.MockUi) 192 c := &InitCommand{ 193 Meta: Meta{ 194 ContextOpts: testCtxConfig(testProvider()), 195 Ui: ui, 196 }, 197 } 198 199 args := []string{ 200 "-backend", "http", 201 "-backend-config", "address=" + conf.Config["address"], 202 testFixturePath("init"), 203 subdir, 204 } 205 if code := c.Run(args); code != 0 { 206 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 207 } 208 209 if _, err := os.Stat(filepath.Join(subdir, "hello.tf")); err != nil { 210 t.Fatalf("err: %s", err) 211 } 212 213 if _, err := os.Stat(filepath.Join(subdir, DefaultDataDir, DefaultStateFilename)); err != nil { 214 t.Fatalf("missing state: %s", err) 215 } 216 } 217 218 func TestInit_remoteStateWithLocal(t *testing.T) { 219 tmp, cwd := testCwd(t) 220 defer testFixCwd(t, tmp, cwd) 221 222 statePath := filepath.Join(tmp, DefaultStateFilename) 223 224 // Write some state 225 f, err := os.Create(statePath) 226 if err != nil { 227 t.Fatalf("err: %s", err) 228 } 229 err = terraform.WriteState(testState(), f) 230 f.Close() 231 if err != nil { 232 t.Fatalf("err: %s", err) 233 } 234 235 ui := new(cli.MockUi) 236 c := &InitCommand{ 237 Meta: Meta{ 238 ContextOpts: testCtxConfig(testProvider()), 239 Ui: ui, 240 }, 241 } 242 243 args := []string{ 244 "-backend", "http", 245 "-backend-config", "address=http://google.com", 246 testFixturePath("init"), 247 } 248 if code := c.Run(args); code == 0 { 249 t.Fatalf("should have failed: \n%s", ui.OutputWriter.String()) 250 } 251 } 252 253 func TestInit_remoteStateWithRemote(t *testing.T) { 254 tmp, cwd := testCwd(t) 255 defer testFixCwd(t, tmp, cwd) 256 257 statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename) 258 if err := os.MkdirAll(filepath.Dir(statePath), 0755); err != nil { 259 t.Fatalf("err: %s", err) 260 } 261 262 // Write some state 263 f, err := os.Create(statePath) 264 if err != nil { 265 t.Fatalf("err: %s", err) 266 } 267 err = terraform.WriteState(testState(), f) 268 f.Close() 269 if err != nil { 270 t.Fatalf("err: %s", err) 271 } 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 "-backend", "http", 283 "-backend-config", "address=http://google.com", 284 testFixturePath("init"), 285 } 286 if code := c.Run(args); code == 0 { 287 t.Fatalf("should have failed: \n%s", ui.OutputWriter.String()) 288 } 289 }