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