github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/triton/resource_machine_test.go (about) 1 package triton 2 3 import ( 4 "fmt" 5 "log" 6 "regexp" 7 "testing" 8 "time" 9 10 "github.com/davecgh/go-spew/spew" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 "github.com/joyent/triton-go" 15 ) 16 17 func TestAccTritonMachine_basic(t *testing.T) { 18 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 19 config := fmt.Sprintf(testAccTritonMachine_basic, machineName) 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 Providers: testAccProviders, 24 CheckDestroy: testCheckTritonMachineDestroy, 25 Steps: []resource.TestStep{ 26 { 27 Config: config, 28 Check: resource.ComposeTestCheckFunc( 29 testCheckTritonMachineExists("triton_machine.test"), 30 func(*terraform.State) error { 31 time.Sleep(10 * time.Second) 32 return nil 33 }, 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func TestAccTritonMachine_dns(t *testing.T) { 41 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 42 dns_output := fmt.Sprintf(testAccTritonMachine_dns, machineName) 43 44 resource.Test(t, resource.TestCase{ 45 PreCheck: func() { testAccPreCheck(t) }, 46 Providers: testAccProviders, 47 CheckDestroy: testCheckTritonMachineDestroy, 48 Steps: []resource.TestStep{ 49 { 50 Config: dns_output, 51 Check: resource.ComposeTestCheckFunc( 52 testCheckTritonMachineExists("triton_machine.test"), 53 func(state *terraform.State) error { 54 time.Sleep(10 * time.Second) 55 log.Printf("[DEBUG] %s", spew.Sdump(state)) 56 return nil 57 }, 58 resource.TestMatchOutput("domain_names", regexp.MustCompile(".*acctest-.*")), 59 ), 60 }, 61 }, 62 }) 63 } 64 65 func TestAccTritonMachine_nic(t *testing.T) { 66 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 67 config := testAccTritonMachine_singleNIC(machineName, acctest.RandIntRange(1024, 2048), acctest.RandIntRange(0, 256)) 68 69 resource.Test(t, resource.TestCase{ 70 PreCheck: func() { testAccPreCheck(t) }, 71 Providers: testAccProviders, 72 CheckDestroy: testCheckTritonMachineDestroy, 73 Steps: []resource.TestStep{ 74 { 75 Config: config, 76 Check: resource.ComposeTestCheckFunc( 77 testCheckTritonMachineExists("triton_machine.test"), 78 func(*terraform.State) error { 79 time.Sleep(10 * time.Second) 80 return nil 81 }, 82 testCheckTritonMachineHasFabric("triton_machine.test", "triton_fabric.test"), 83 ), 84 }, 85 }, 86 }) 87 } 88 89 func TestAccTritonMachine_addNIC(t *testing.T) { 90 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 91 vlanNumber := acctest.RandIntRange(1024, 2048) 92 subnetNumber := acctest.RandIntRange(0, 256) 93 94 singleNICConfig := testAccTritonMachine_singleNIC(machineName, vlanNumber, subnetNumber) 95 dualNICConfig := testAccTritonMachine_dualNIC(machineName, vlanNumber, subnetNumber) 96 97 resource.Test(t, resource.TestCase{ 98 PreCheck: func() { testAccPreCheck(t) }, 99 Providers: testAccProviders, 100 CheckDestroy: testCheckTritonMachineDestroy, 101 Steps: []resource.TestStep{ 102 { 103 Config: singleNICConfig, 104 Check: resource.ComposeTestCheckFunc( 105 testCheckTritonMachineExists("triton_machine.test"), 106 func(*terraform.State) error { 107 time.Sleep(10 * time.Second) 108 return nil 109 }, 110 ), 111 }, 112 { 113 Config: dualNICConfig, 114 Check: resource.ComposeTestCheckFunc( 115 testCheckTritonMachineExists("triton_machine.test"), 116 testCheckTritonMachineHasFabric("triton_machine.test", "triton_fabric.test_add"), 117 ), 118 }, 119 }, 120 }) 121 } 122 123 func testCheckTritonMachineExists(name string) resource.TestCheckFunc { 124 return func(s *terraform.State) error { 125 // Ensure we have enough information in state to look up in API 126 rs, ok := s.RootModule().Resources[name] 127 if !ok { 128 return fmt.Errorf("Not found: %s", name) 129 } 130 conn := testAccProvider.Meta().(*triton.Client) 131 132 machine, err := conn.Machines().GetMachine(&triton.GetMachineInput{ 133 ID: rs.Primary.ID, 134 }) 135 if err != nil { 136 return fmt.Errorf("Bad: Check Machine Exists: %s", err) 137 } 138 139 if machine == nil { 140 return fmt.Errorf("Bad: Machine %q does not exist", rs.Primary.ID) 141 } 142 143 return nil 144 } 145 } 146 147 func testCheckTritonMachineHasFabric(name, fabricName string) resource.TestCheckFunc { 148 return func(s *terraform.State) error { 149 // Ensure we have enough information in state to look up in API 150 machine, ok := s.RootModule().Resources[name] 151 if !ok { 152 return fmt.Errorf("Not found: %s", name) 153 } 154 155 network, ok := s.RootModule().Resources[fabricName] 156 if !ok { 157 return fmt.Errorf("Not found: %s", fabricName) 158 } 159 conn := testAccProvider.Meta().(*triton.Client) 160 161 nics, err := conn.Machines().ListNICs(&triton.ListNICsInput{ 162 MachineID: machine.Primary.ID, 163 }) 164 if err != nil { 165 return fmt.Errorf("Bad: Check NICs Exist: %s", err) 166 } 167 168 for _, nic := range nics { 169 if nic.Network == network.Primary.ID { 170 return nil 171 } 172 } 173 174 return fmt.Errorf("Bad: Machine %q does not have Fabric %q", machine.Primary.ID, network.Primary.ID) 175 } 176 } 177 178 func testCheckTritonMachineDestroy(s *terraform.State) error { 179 conn := testAccProvider.Meta().(*triton.Client) 180 181 for _, rs := range s.RootModule().Resources { 182 if rs.Type != "triton_machine" { 183 continue 184 } 185 186 resp, err := conn.Machines().GetMachine(&triton.GetMachineInput{ 187 ID: rs.Primary.ID, 188 }) 189 if err != nil { 190 if triton.IsResourceNotFound(err) { 191 return nil 192 } 193 return err 194 } 195 196 if resp != nil && resp.State != machineStateDeleted { 197 return fmt.Errorf("Bad: Machine %q still exists", rs.Primary.ID) 198 } 199 } 200 201 return nil 202 } 203 204 func TestAccTritonMachine_firewall(t *testing.T) { 205 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 206 disabled_config := fmt.Sprintf(testAccTritonMachine_firewall_0, machineName) 207 enabled_config := fmt.Sprintf(testAccTritonMachine_firewall_1, machineName) 208 209 resource.Test(t, resource.TestCase{ 210 PreCheck: func() { testAccPreCheck(t) }, 211 Providers: testAccProviders, 212 CheckDestroy: testCheckTritonMachineDestroy, 213 Steps: []resource.TestStep{ 214 { 215 Config: enabled_config, 216 Check: resource.ComposeTestCheckFunc( 217 testCheckTritonMachineExists("triton_machine.test"), 218 resource.TestCheckResourceAttr( 219 "triton_machine.test", "firewall_enabled", "true"), 220 ), 221 }, 222 { 223 Config: disabled_config, 224 Check: resource.ComposeTestCheckFunc( 225 testCheckTritonMachineExists("triton_machine.test"), 226 resource.TestCheckResourceAttr( 227 "triton_machine.test", "firewall_enabled", "false"), 228 ), 229 }, 230 { 231 Config: enabled_config, 232 Check: resource.ComposeTestCheckFunc( 233 testCheckTritonMachineExists("triton_machine.test"), 234 resource.TestCheckResourceAttr( 235 "triton_machine.test", "firewall_enabled", "true"), 236 ), 237 }, 238 }, 239 }) 240 } 241 242 func TestAccTritonMachine_metadata(t *testing.T) { 243 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 244 basic := fmt.Sprintf(testAccTritonMachine_metadata_1, machineName) 245 add_metadata := fmt.Sprintf(testAccTritonMachine_metadata_1, machineName) 246 add_metadata_2 := fmt.Sprintf(testAccTritonMachine_metadata_2, machineName) 247 add_metadata_3 := fmt.Sprintf(testAccTritonMachine_metadata_3, machineName) 248 249 resource.Test(t, resource.TestCase{ 250 PreCheck: func() { testAccPreCheck(t) }, 251 Providers: testAccProviders, 252 CheckDestroy: testCheckTritonMachineDestroy, 253 Steps: []resource.TestStep{ 254 { 255 Config: basic, 256 Check: resource.ComposeTestCheckFunc( 257 testCheckTritonMachineExists("triton_machine.test"), 258 ), 259 }, 260 { 261 Config: add_metadata, 262 Check: resource.ComposeTestCheckFunc( 263 testCheckTritonMachineExists("triton_machine.test"), 264 resource.TestCheckResourceAttr( 265 "triton_machine.test", "user_data", "hello"), 266 ), 267 }, 268 { 269 Config: add_metadata_2, 270 Check: resource.ComposeTestCheckFunc( 271 testCheckTritonMachineExists("triton_machine.test"), 272 resource.TestCheckResourceAttr( 273 "triton_machine.test", 274 "tags.triton.cns.services", "test-cns-service"), 275 ), 276 }, 277 { 278 Config: add_metadata_3, 279 Check: resource.ComposeTestCheckFunc( 280 testCheckTritonMachineExists("triton_machine.test"), 281 resource.TestCheckResourceAttr( 282 "triton_machine.test", 283 "tags.triton.cns.services", "test-cns-service"), 284 ), 285 }, 286 }, 287 }) 288 } 289 290 var testAccTritonMachine_basic = ` 291 resource "triton_machine" "test" { 292 name = "%s" 293 package = "g4-general-4G" 294 image = "fb5fe970-e6e4-11e6-9820-4b51be190db9" 295 296 tags = { 297 test = "hello!" 298 } 299 } 300 ` 301 302 var testAccTritonMachine_firewall_0 = ` 303 resource "triton_machine" "test" { 304 name = "%s" 305 package = "g4-general-4G" 306 image = "c20b4b7c-e1a6-11e5-9a4d-ef590901732e" 307 308 firewall_enabled = 0 309 } 310 ` 311 var testAccTritonMachine_firewall_1 = ` 312 resource "triton_machine" "test" { 313 name = "%s" 314 package = "g4-general-4G" 315 image = "fb5fe970-e6e4-11e6-9820-4b51be190db9" 316 317 firewall_enabled = 1 318 } 319 ` 320 321 var testAccTritonMachine_metadata_1 = ` 322 resource "triton_machine" "test" { 323 name = "%s" 324 package = "g4-general-4G" 325 image = "c20b4b7c-e1a6-11e5-9a4d-ef590901732e" 326 327 user_data = "hello" 328 329 tags = { 330 test = "hello!" 331 } 332 } 333 ` 334 var testAccTritonMachine_metadata_2 = ` 335 variable "tags" { 336 default = { 337 test = "hello!" 338 triton.cns.services = "test-cns-service" 339 } 340 } 341 resource "triton_machine" "test" { 342 name = "%s" 343 package = "g4-highcpu-128M" 344 image = "fb5fe970-e6e4-11e6-9820-4b51be190db9" 345 346 user_data = "hello" 347 348 tags = "${var.tags}" 349 } 350 ` 351 var testAccTritonMachine_metadata_3 = ` 352 resource "triton_machine" "test" { 353 name = "%s" 354 package = "g4-highcpu-128M" 355 image = "fb5fe970-e6e4-11e6-9820-4b51be190db9" 356 357 user_data = "hello" 358 359 tags = { 360 test = "hello!" 361 triton.cns.services = "test-cns-service" 362 } 363 } 364 ` 365 var testAccTritonMachine_singleNIC = func(name string, vlanNumber int, subnetNumber int) string { 366 return fmt.Sprintf(`resource "triton_vlan" "test" { 367 vlan_id = %d 368 name = "%s-vlan" 369 description = "test vlan" 370 } 371 372 resource "triton_fabric" "test" { 373 name = "%s-network" 374 description = "test network" 375 vlan_id = "${triton_vlan.test.vlan_id}" 376 377 subnet = "10.%d.0.0/24" 378 gateway = "10.%d.0.1" 379 provision_start_ip = "10.%d.0.10" 380 provision_end_ip = "10.%d.0.250" 381 382 resolvers = ["8.8.8.8", "8.8.4.4"] 383 } 384 385 resource "triton_machine" "test" { 386 name = "%s-instance" 387 package = "g4-highcpu-128M" 388 image = "fb5fe970-e6e4-11e6-9820-4b51be190db9" 389 390 tags = { 391 test = "Test" 392 } 393 394 nic { 395 network = "${triton_fabric.test.id}" 396 } 397 }`, vlanNumber, name, name, subnetNumber, subnetNumber, subnetNumber, subnetNumber, name) 398 } 399 400 var testAccTritonMachine_dualNIC = func(name string, vlanNumber, subnetNumber int) string { 401 return fmt.Sprintf(`resource "triton_vlan" "test" { 402 vlan_id = %d 403 name = "%s-vlan" 404 description = "test vlan" 405 } 406 407 resource "triton_fabric" "test" { 408 name = "%s-network" 409 description = "test network" 410 vlan_id = "${triton_vlan.test.vlan_id}" 411 412 subnet = "10.%d.0.0/24" 413 gateway = "10.%d.0.1" 414 provision_start_ip = "10.%d.0.10" 415 provision_end_ip = "10.%d.0.250" 416 417 resolvers = ["8.8.8.8", "8.8.4.4"] 418 } 419 420 resource "triton_fabric" "test_add" { 421 name = "%s-network-2" 422 description = "test network 2" 423 vlan_id = "${triton_vlan.test.vlan_id}" 424 425 subnet = "172.23.%d.0/24" 426 gateway = "172.23.%d.1" 427 provision_start_ip = "172.23.%d.10" 428 provision_end_ip = "172.23.%d.250" 429 430 resolvers = ["8.8.8.8", "8.8.4.4"] 431 } 432 433 resource "triton_machine" "test" { 434 name = "%s-instance" 435 package = "g4-highcpu-128M" 436 image = "fb5fe970-e6e4-11e6-9820-4b51be190db9" 437 438 tags = { 439 test = "Test" 440 } 441 442 nic { 443 network = "${triton_fabric.test.id}" 444 } 445 nic { 446 network = "${triton_fabric.test_add.id}" 447 } 448 }`, vlanNumber, name, name, subnetNumber, subnetNumber, subnetNumber, subnetNumber, name, subnetNumber, subnetNumber, subnetNumber, subnetNumber, name) 449 } 450 451 var testAccTritonMachine_dns = ` 452 provider "triton" { 453 } 454 455 resource "triton_machine" "test" { 456 name = "%s" 457 package = "g4-highcpu-128M" 458 image = "fb5fe970-e6e4-11e6-9820-4b51be190db9" 459 } 460 461 output "domain_names" { 462 value = "${join(", ", triton_machine.test.domain_names)}" 463 } 464 `