github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/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 267 resource.Test(t, resource.TestCase{ 268 PreCheck: func() { testAccPreCheck(t) }, 269 Providers: testAccProviders, 270 CheckDestroy: testCheckTritonMachineDestroy, 271 Steps: []resource.TestStep{ 272 resource.TestStep{ 273 Config: basic, 274 Check: resource.ComposeTestCheckFunc( 275 testCheckTritonMachineExists("triton_machine.test"), 276 ), 277 }, 278 resource.TestStep{ 279 Config: add_metadata, 280 Check: resource.ComposeTestCheckFunc( 281 testCheckTritonMachineExists("triton_machine.test"), 282 resource.TestCheckResourceAttr( 283 "triton_machine.test", "user_data", "hello"), 284 ), 285 }, 286 }, 287 }) 288 } 289 290 var testAccTritonMachine_basic = ` 291 provider "triton" { 292 url = "https://us-west-1.api.joyentcloud.com" 293 } 294 295 resource "triton_machine" "test" { 296 name = "%s" 297 package = "g3-standard-0.25-smartos" 298 image = "c20b4b7c-e1a6-11e5-9a4d-ef590901732e" 299 300 tags = { 301 test = "hello!" 302 } 303 } 304 ` 305 306 var testAccTritonMachine_firewall_0 = ` 307 provider "triton" { 308 url = "https://us-west-1.api.joyentcloud.com" 309 } 310 311 resource "triton_machine" "test" { 312 name = "%s" 313 package = "g3-standard-0.25-smartos" 314 image = "c20b4b7c-e1a6-11e5-9a4d-ef590901732e" 315 316 firewall_enabled = 0 317 } 318 ` 319 var testAccTritonMachine_firewall_1 = ` 320 provider "triton" { 321 url = "https://us-west-1.api.joyentcloud.com" 322 } 323 324 resource "triton_machine" "test" { 325 name = "%s" 326 package = "g3-standard-0.25-smartos" 327 image = "c20b4b7c-e1a6-11e5-9a4d-ef590901732e" 328 329 firewall_enabled = 1 330 } 331 ` 332 333 var testAccTritonMachine_metadata_1 = ` 334 provider "triton" { 335 url = "https://us-west-1.api.joyentcloud.com" 336 } 337 338 resource "triton_machine" "test" { 339 name = "%s" 340 package = "g3-standard-0.25-smartos" 341 image = "c20b4b7c-e1a6-11e5-9a4d-ef590901732e" 342 343 user_data = "hello" 344 345 tags = { 346 test = "hello!" 347 } 348 } 349 ` 350 351 var testAccTritonMachine_withnic = ` 352 resource "triton_fabric" "test" { 353 name = "%s-network" 354 description = "test network" 355 vlan_id = 2 # every DC seems to have a vlan 2 available 356 357 subnet = "10.0.0.0/22" 358 gateway = "10.0.0.1" 359 provision_start_ip = "10.0.0.5" 360 provision_end_ip = "10.0.3.250" 361 362 resolvers = ["8.8.8.8", "8.8.4.4"] 363 } 364 365 resource "triton_machine" "test" { 366 name = "%s" 367 package = "g3-standard-0.25-smartos" 368 image = "842e6fa6-6e9b-11e5-8402-1b490459e334" 369 370 tags = { 371 test = "hello!" 372 } 373 374 nic { network = "${triton_fabric.test.id}" } 375 } 376 ` 377 378 var testAccTritonMachine_withoutnic = ` 379 resource "triton_fabric" "test" { 380 name = "%s-network" 381 description = "test network" 382 vlan_id = 2 # every DC seems to have a vlan 2 available 383 384 subnet = "10.0.0.0/22" 385 gateway = "10.0.0.1" 386 provision_start_ip = "10.0.0.5" 387 provision_end_ip = "10.0.3.250" 388 389 resolvers = ["8.8.8.8", "8.8.4.4"] 390 } 391 392 resource "triton_machine" "test" { 393 name = "%s" 394 package = "g3-standard-0.25-smartos" 395 image = "842e6fa6-6e9b-11e5-8402-1b490459e334" 396 397 tags = { 398 test = "hello!" 399 } 400 } 401 ` 402 403 var testAccTritonMachine_dns = ` 404 provider "triton" { 405 } 406 407 resource "triton_machine" "test" { 408 name = "%s" 409 package = "g4-highcpu-128M" 410 image = "e1faace4-e19b-11e5-928b-83849e2fd94a" 411 } 412 output "domain_names" { 413 value = "${join(", ", triton_machine.test.domain_names)}" 414 } 415 `