github.com/magodo/terraform@v0.11.12-beta1/builtin/provisioners/salt-masterless/resource_provisioner_test.go (about) 1 package saltmasterless 2 3 import ( 4 "io/ioutil" 5 "os" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/config" 10 "github.com/hashicorp/terraform/helper/schema" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig { 15 r, err := config.NewRawConfig(c) 16 if err != nil { 17 t.Fatalf("bad: %s", err) 18 } 19 20 return terraform.NewResourceConfig(r) 21 } 22 23 func TestResourceProvisioner_impl(t *testing.T) { 24 var _ terraform.ResourceProvisioner = Provisioner() 25 } 26 27 func TestProvisioner(t *testing.T) { 28 if err := Provisioner().(*schema.Provisioner).InternalValidate(); err != nil { 29 t.Fatalf("err: %s", err) 30 } 31 } 32 33 func TestResourceProvisioner_Validate_good(t *testing.T) { 34 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 35 if err != nil { 36 t.Fatalf("Error when creating temp dir: %v", err) 37 } 38 39 defer os.RemoveAll(dir) // clean up 40 41 c := testConfig(t, map[string]interface{}{ 42 "local_state_tree": dir, 43 }) 44 warn, errs := Provisioner().Validate(c) 45 if len(warn) > 0 { 46 t.Fatalf("Warnings: %v", warn) 47 } 48 if len(errs) > 0 { 49 t.Fatalf("Errors: %v", errs) 50 } 51 } 52 53 func TestResourceProvider_Validate_missing_required(t *testing.T) { 54 c := testConfig(t, map[string]interface{}{ 55 "remote_state_tree": "_default", 56 }) 57 warn, errs := Provisioner().Validate(c) 58 if len(warn) > 0 { 59 t.Fatalf("Warnings: %v", warn) 60 } 61 if len(errs) == 0 { 62 t.Fatalf("Should have errors") 63 } 64 } 65 66 func TestResourceProvider_Validate_LocalStateTree_doesnt_exist(t *testing.T) { 67 c := testConfig(t, map[string]interface{}{ 68 "local_state_tree": "/i/dont/exist", 69 }) 70 warn, errs := Provisioner().Validate(c) 71 if len(warn) > 0 { 72 t.Fatalf("Warnings: %v", warn) 73 } 74 if len(errs) == 0 { 75 t.Fatalf("Should have errors") 76 } 77 } 78 79 func TestResourceProvisioner_Validate_invalid(t *testing.T) { 80 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 81 if err != nil { 82 t.Fatalf("Error when creating temp dir: %v", err) 83 } 84 85 defer os.RemoveAll(dir) // clean up 86 87 c := testConfig(t, map[string]interface{}{ 88 "local_state_tree": dir, 89 "i_am_not_valid": "_invalid", 90 }) 91 92 warn, errs := Provisioner().Validate(c) 93 if len(warn) > 0 { 94 t.Fatalf("Warnings: %v", warn) 95 } 96 if len(errs) == 0 { 97 t.Fatalf("Should have errors") 98 } 99 } 100 101 func TestProvisionerPrepare_CustomState(t *testing.T) { 102 c := map[string]interface{}{ 103 "local_state_tree": "/tmp/local_state_tree", 104 } 105 106 p, err := decodeConfig( 107 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 108 ) 109 110 if err != nil { 111 t.Fatalf("Error: %v", err) 112 } 113 114 if !strings.Contains(p.CmdArgs, "state.highstate") { 115 t.Fatal("CmdArgs should contain state.highstate") 116 } 117 118 if err != nil { 119 t.Fatalf("err: %s", err) 120 } 121 122 c = map[string]interface{}{ 123 "local_state_tree": "/tmp/local_state_tree", 124 "custom_state": "custom", 125 } 126 127 p, err = decodeConfig( 128 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 129 ) 130 131 if err != nil { 132 t.Fatalf("Error: %v", err) 133 } 134 135 if !strings.Contains(p.CmdArgs, "state.sls custom") { 136 t.Fatal("CmdArgs should contain state.sls custom") 137 } 138 139 if err != nil { 140 t.Fatalf("err: %s", err) 141 } 142 } 143 144 func TestProvisionerPrepare_MinionConfig(t *testing.T) { 145 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 146 if err != nil { 147 t.Fatalf("Error when creating temp dir: %v", err) 148 } 149 150 defer os.RemoveAll(dir) // clean up 151 152 c := testConfig(t, map[string]interface{}{ 153 "local_state_tree": dir, 154 "minion_config_file": "i/dont/exist", 155 }) 156 157 warns, errs := Provisioner().Validate(c) 158 159 if len(warns) > 0 { 160 t.Fatalf("Warnings: %v", warns) 161 } 162 if len(errs) == 0 { 163 t.Fatalf("Should have error") 164 } 165 166 tf, err := ioutil.TempFile("", "minion") 167 if err != nil { 168 t.Fatalf("error tempfile: %s", err) 169 } 170 171 defer os.Remove(tf.Name()) 172 173 c = testConfig(t, map[string]interface{}{ 174 "local_state_tree": dir, 175 "minion_config_file": tf.Name(), 176 }) 177 178 warns, errs = Provisioner().Validate(c) 179 180 if len(warns) > 0 { 181 t.Fatalf("Warnings: %v", warns) 182 } 183 if len(errs) > 0 { 184 t.Fatalf("errs: %s", errs) 185 } 186 } 187 188 func TestProvisionerPrepare_MinionConfig_RemoteStateTree(t *testing.T) { 189 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 190 if err != nil { 191 t.Fatalf("Error when creating temp dir: %v", err) 192 } 193 194 c := testConfig(t, map[string]interface{}{ 195 "local_state_tree": dir, 196 "minion_config_file": "i/dont/exist", 197 "remote_state_tree": "i/dont/exist/remote_state_tree", 198 }) 199 200 warns, errs := Provisioner().Validate(c) 201 if len(warns) > 0 { 202 t.Fatalf("Warnings: %v", warns) 203 } 204 if len(errs) == 0 { 205 t.Fatalf("Should be error") 206 } 207 } 208 209 func TestProvisionerPrepare_MinionConfig_RemotePillarRoots(t *testing.T) { 210 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 211 if err != nil { 212 t.Fatalf("Error when creating temp dir: %v", err) 213 } 214 215 c := testConfig(t, map[string]interface{}{ 216 "local_state_tree": dir, 217 "minion_config_file": "i/dont/exist", 218 "remote_pillar_roots": "i/dont/exist/remote_pillar_roots", 219 }) 220 221 warns, errs := Provisioner().Validate(c) 222 if len(warns) > 0 { 223 t.Fatalf("Warnings: %v", warns) 224 } 225 if len(errs) == 0 { 226 t.Fatalf("Should be error") 227 } 228 } 229 230 func TestProvisionerPrepare_LocalPillarRoots(t *testing.T) { 231 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 232 if err != nil { 233 t.Fatalf("Error when creating temp dir: %v", err) 234 } 235 236 c := testConfig(t, map[string]interface{}{ 237 "local_state_tree": dir, 238 "minion_config_file": "i/dont/exist", 239 "local_pillar_roots": "i/dont/exist/local_pillar_roots", 240 }) 241 242 warns, errs := Provisioner().Validate(c) 243 if len(warns) > 0 { 244 t.Fatalf("Warnings: %v", warns) 245 } 246 if len(errs) == 0 { 247 t.Fatalf("Should be error") 248 } 249 } 250 251 func TestProvisionerSudo(t *testing.T) { 252 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 253 if err != nil { 254 t.Fatalf("Error when creating temp dir: %v", err) 255 } 256 257 c := map[string]interface{}{ 258 "local_state_tree": dir, 259 } 260 261 p, err := decodeConfig( 262 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 263 ) 264 265 withSudo := p.sudo("echo hello") 266 if withSudo != "sudo echo hello" { 267 t.Fatalf("sudo command not generated correctly") 268 } 269 270 c = map[string]interface{}{ 271 "local_state_tree": dir, 272 "disable_sudo": "true", 273 } 274 275 p, err = decodeConfig( 276 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 277 ) 278 279 if err != nil { 280 t.Fatalf("err: %s", err) 281 } 282 withoutSudo := p.sudo("echo hello") 283 if withoutSudo != "echo hello" { 284 t.Fatalf("sudo-less command not generated correctly") 285 } 286 } 287 288 func TestProvisionerPrepare_RemoteStateTree(t *testing.T) { 289 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 290 if err != nil { 291 t.Fatalf("Error when creating temp dir: %v", err) 292 } 293 294 c := map[string]interface{}{ 295 "local_state_tree": dir, 296 "remote_state_tree": "/remote_state_tree", 297 } 298 299 p, err := decodeConfig( 300 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 301 ) 302 303 if err != nil { 304 t.Fatalf("err: %s", err) 305 } 306 307 if !strings.Contains(p.CmdArgs, "--file-root=/remote_state_tree") { 308 t.Fatal("--file-root should be set in CmdArgs") 309 } 310 } 311 312 func TestProvisionerPrepare_RemotePillarRoots(t *testing.T) { 313 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 314 if err != nil { 315 t.Fatalf("Error when creating temp dir: %v", err) 316 } 317 318 c := map[string]interface{}{ 319 "local_state_tree": dir, 320 "remote_pillar_roots": "/remote_pillar_roots", 321 } 322 323 p, err := decodeConfig( 324 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 325 ) 326 327 if err != nil { 328 t.Fatalf("err: %s", err) 329 } 330 331 if !strings.Contains(p.CmdArgs, "--pillar-root=/remote_pillar_roots") { 332 t.Fatal("--pillar-root should be set in CmdArgs") 333 } 334 } 335 336 func TestProvisionerPrepare_RemoteStateTree_Default(t *testing.T) { 337 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 338 if err != nil { 339 t.Fatalf("Error when creating temp dir: %v", err) 340 } 341 342 c := map[string]interface{}{ 343 "local_state_tree": dir, 344 } 345 346 p, err := decodeConfig( 347 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 348 ) 349 350 if err != nil { 351 t.Fatalf("err: %s", err) 352 } 353 354 if !strings.Contains(p.CmdArgs, "--file-root=/srv/salt") { 355 t.Fatal("--file-root should be set in CmdArgs") 356 } 357 } 358 359 func TestProvisionerPrepare_RemotePillarRoots_Default(t *testing.T) { 360 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 361 if err != nil { 362 t.Fatalf("Error when creating temp dir: %v", err) 363 } 364 365 c := map[string]interface{}{ 366 "local_state_tree": dir, 367 } 368 369 p, err := decodeConfig( 370 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 371 ) 372 373 if err != nil { 374 t.Fatalf("err: %s", err) 375 } 376 377 if !strings.Contains(p.CmdArgs, "--pillar-root=/srv/pillar") { 378 t.Fatal("--pillar-root should be set in CmdArgs") 379 } 380 } 381 382 func TestProvisionerPrepare_NoExitOnFailure(t *testing.T) { 383 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 384 if err != nil { 385 t.Fatalf("Error when creating temp dir: %v", err) 386 } 387 388 c := map[string]interface{}{ 389 "local_state_tree": dir, 390 } 391 392 p, err := decodeConfig( 393 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 394 ) 395 396 if err != nil { 397 t.Fatalf("err: %s", err) 398 } 399 400 if !strings.Contains(p.CmdArgs, "--retcode-passthrough") { 401 t.Fatal("--retcode-passthrough should be set in CmdArgs") 402 } 403 404 c = map[string]interface{}{ 405 "no_exit_on_failure": true, 406 } 407 408 p, err = decodeConfig( 409 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 410 ) 411 412 if err != nil { 413 t.Fatalf("err: %s", err) 414 } 415 416 if strings.Contains(p.CmdArgs, "--retcode-passthrough") { 417 t.Fatal("--retcode-passthrough should not be set in CmdArgs") 418 } 419 } 420 421 func TestProvisionerPrepare_LogLevel(t *testing.T) { 422 dir, err := ioutil.TempDir("", "_terraform_saltmasterless_test") 423 if err != nil { 424 t.Fatalf("Error when creating temp dir: %v", err) 425 } 426 427 c := map[string]interface{}{ 428 "local_state_tree": dir, 429 } 430 431 p, err := decodeConfig( 432 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 433 ) 434 435 if err != nil { 436 t.Fatalf("err: %s", err) 437 } 438 439 if !strings.Contains(p.CmdArgs, "-l info") { 440 t.Fatal("-l info should be set in CmdArgs") 441 } 442 443 c = map[string]interface{}{ 444 "log_level": "debug", 445 } 446 447 p, err = decodeConfig( 448 schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, c), 449 ) 450 451 if err != nil { 452 t.Fatalf("err: %s", err) 453 } 454 455 if !strings.Contains(p.CmdArgs, "-l debug") { 456 t.Fatal("-l debug should be set in CmdArgs") 457 } 458 }