github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/azure/resource_azure_instance_test.go (about) 1 package azure 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 "time" 8 9 "github.com/Azure/azure-sdk-for-go/management" 10 "github.com/Azure/azure-sdk-for-go/management/virtualmachine" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 var randInt = rand.New(rand.NewSource(time.Now().UnixNano())).Int() 17 var instanceName = fmt.Sprintf("terraform-test-%d", randInt) 18 19 func TestAccAzureInstance_basic(t *testing.T) { 20 var dpmt virtualmachine.DeploymentResponse 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckAzureInstanceDestroyed(""), 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccAzureInstance_basic, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAzureInstanceExists( 31 "azure_instance.foo", "", &dpmt), 32 testAccCheckAzureInstanceBasicAttributes(&dpmt), 33 resource.TestCheckResourceAttr( 34 "azure_instance.foo", "name", instanceName), 35 resource.TestCheckResourceAttr( 36 "azure_instance.foo", "hosted_service_name", instanceName), 37 resource.TestCheckResourceAttr( 38 "azure_instance.foo", "location", "West US"), 39 resource.TestCheckResourceAttr( 40 "azure_instance.foo", "endpoint.2462817782.public_port", "22"), 41 resource.TestCheckResourceAttr( 42 "azure_instance.foo", "custom_data", "0ea0f28b0c42d6bef7d0c7ab4886324feaa8b5e1"), 43 ), 44 }, 45 }, 46 }) 47 } 48 49 func TestAccAzureInstance_separateHostedService(t *testing.T) { 50 var dpmt virtualmachine.DeploymentResponse 51 52 hostedServiceName := fmt.Sprintf("terraform-testing-service%d", acctest.RandInt()) 53 54 config := fmt.Sprintf(testAccAzureInstance_separateHostedService, hostedServiceName, instanceName, testAccStorageServiceName) 55 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 Providers: testAccProviders, 59 CheckDestroy: testAccCheckAzureInstanceDestroyed(hostedServiceName), 60 Steps: []resource.TestStep{ 61 resource.TestStep{ 62 Config: config, 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckAzureInstanceExists( 65 "azure_instance.foo", hostedServiceName, &dpmt), 66 testAccCheckAzureInstanceBasicAttributes(&dpmt), 67 resource.TestCheckResourceAttr( 68 "azure_instance.foo", "name", instanceName), 69 resource.TestCheckResourceAttr( 70 "azure_instance.foo", "hosted_service_name", hostedServiceName), 71 resource.TestCheckResourceAttr( 72 "azure_instance.foo", "location", "West US"), 73 resource.TestCheckResourceAttr( 74 "azure_instance.foo", "endpoint.2462817782.public_port", "22"), 75 ), 76 }, 77 }, 78 }) 79 } 80 81 func TestAccAzureInstance_advanced(t *testing.T) { 82 var dpmt virtualmachine.DeploymentResponse 83 84 resource.Test(t, resource.TestCase{ 85 PreCheck: func() { testAccPreCheck(t) }, 86 Providers: testAccProviders, 87 CheckDestroy: testAccCheckAzureInstanceDestroyed(""), 88 Steps: []resource.TestStep{ 89 resource.TestStep{ 90 Config: testAccAzureInstance_advanced, 91 Check: resource.ComposeTestCheckFunc( 92 testAccCheckAzureInstanceExists( 93 "azure_instance.foo", "", &dpmt), 94 testAccCheckAzureInstanceAdvancedAttributes(&dpmt), 95 resource.TestCheckResourceAttr( 96 "azure_instance.foo", "name", "terraform-test1"), 97 resource.TestCheckResourceAttr( 98 "azure_instance.foo", "hosted_service_name", "terraform-test1"), 99 resource.TestCheckResourceAttr( 100 "azure_instance.foo", "size", "Basic_A1"), 101 resource.TestCheckResourceAttr( 102 "azure_instance.foo", "subnet", "subnet1"), 103 resource.TestCheckResourceAttr( 104 "azure_instance.foo", "virtual_network", "terraform-vnet-advanced-test"), 105 resource.TestCheckResourceAttr( 106 "azure_instance.foo", "security_group", "terraform-security-group1"), 107 resource.TestCheckResourceAttr( 108 "azure_instance.foo", "endpoint.1814039778.public_port", "3389"), 109 resource.TestCheckResourceAttr( 110 "azure_instance.foo", "custom_data", "04c589e0edaa5ffe185d1e5532e77d1b2ac4b948"), 111 ), 112 }, 113 }, 114 }) 115 } 116 117 func TestAccAzureInstance_update(t *testing.T) { 118 var dpmt virtualmachine.DeploymentResponse 119 120 resource.Test(t, resource.TestCase{ 121 PreCheck: func() { testAccPreCheck(t) }, 122 Providers: testAccProviders, 123 CheckDestroy: testAccCheckAzureInstanceDestroyed(""), 124 Steps: []resource.TestStep{ 125 resource.TestStep{ 126 Config: testAccAzureInstance_advanced, 127 Check: resource.ComposeTestCheckFunc( 128 testAccCheckAzureInstanceExists( 129 "azure_instance.foo", "", &dpmt), 130 testAccCheckAzureInstanceAdvancedAttributes(&dpmt), 131 resource.TestCheckResourceAttr( 132 "azure_instance.foo", "name", "terraform-test1"), 133 resource.TestCheckResourceAttr( 134 "azure_instance.foo", "hosted_service_name", "terraform-test1"), 135 resource.TestCheckResourceAttr( 136 "azure_instance.foo", "size", "Basic_A1"), 137 resource.TestCheckResourceAttr( 138 "azure_instance.foo", "subnet", "subnet1"), 139 resource.TestCheckResourceAttr( 140 "azure_instance.foo", "virtual_network", "terraform-vnet-advanced-test"), 141 resource.TestCheckResourceAttr( 142 "azure_instance.foo", "security_group", "terraform-security-group1"), 143 resource.TestCheckResourceAttr( 144 "azure_instance.foo", "endpoint.1814039778.public_port", "3389"), 145 ), 146 }, 147 148 resource.TestStep{ 149 Config: testAccAzureInstance_update, 150 Check: resource.ComposeTestCheckFunc( 151 testAccCheckAzureInstanceExists( 152 "azure_instance.foo", "", &dpmt), 153 testAccCheckAzureInstanceUpdatedAttributes(&dpmt), 154 resource.TestCheckResourceAttr( 155 "azure_instance.foo", "size", "Basic_A2"), 156 resource.TestCheckResourceAttr( 157 "azure_instance.foo", "security_group", "terraform-security-update-group2"), 158 resource.TestCheckResourceAttr( 159 "azure_instance.foo", "endpoint.1814039778.public_port", "3389"), 160 resource.TestCheckResourceAttr( 161 "azure_instance.foo", "endpoint.3713350066.public_port", "5985"), 162 ), 163 }, 164 }, 165 }) 166 } 167 168 func testAccCheckAzureInstanceExists( 169 n string, 170 hostedServiceName string, 171 dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc { 172 return func(s *terraform.State) error { 173 rs, ok := s.RootModule().Resources[n] 174 if !ok { 175 return fmt.Errorf("Not found: %s", n) 176 } 177 178 if rs.Primary.ID == "" { 179 return fmt.Errorf("No instance ID is set") 180 } 181 182 // if not hosted service was provided; it means that we expect it 183 // to be identical with the name of the instance; which is in the ID. 184 var serviceName string 185 if hostedServiceName == "" { 186 serviceName = rs.Primary.ID 187 } else { 188 serviceName = hostedServiceName 189 } 190 191 vmClient := testAccProvider.Meta().(*Client).vmClient 192 vm, err := vmClient.GetDeployment(serviceName, rs.Primary.ID) 193 if err != nil { 194 return err 195 } 196 197 if vm.Name != rs.Primary.ID { 198 return fmt.Errorf("Instance not found") 199 } 200 201 *dpmt = vm 202 203 return nil 204 } 205 } 206 207 func testAccCheckAzureInstanceBasicAttributes( 208 dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc { 209 return func(s *terraform.State) error { 210 211 if dpmt.Name != instanceName { 212 return fmt.Errorf("Bad name: %s", dpmt.Name) 213 } 214 215 if len(dpmt.RoleList) != 1 { 216 return fmt.Errorf( 217 "Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList)) 218 } 219 220 if dpmt.RoleList[0].RoleSize != "Basic_A1" { 221 return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize) 222 } 223 224 return nil 225 } 226 } 227 228 func testAccCheckAzureInstanceAdvancedAttributes( 229 dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc { 230 return func(s *terraform.State) error { 231 232 if dpmt.Name != "terraform-test1" { 233 return fmt.Errorf("Bad name: %s", dpmt.Name) 234 } 235 236 if dpmt.VirtualNetworkName != "terraform-vnet-advanced-test" { 237 return fmt.Errorf("Bad virtual network: %s", dpmt.VirtualNetworkName) 238 } 239 240 if len(dpmt.RoleList) != 1 { 241 return fmt.Errorf( 242 "Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList)) 243 } 244 245 if dpmt.RoleList[0].RoleSize != "Basic_A1" { 246 return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize) 247 } 248 249 for _, c := range dpmt.RoleList[0].ConfigurationSets { 250 if c.ConfigurationSetType == virtualmachine.ConfigurationSetTypeNetwork { 251 if len(c.InputEndpoints) != 1 { 252 return fmt.Errorf( 253 "Instance %s has an unexpected number of endpoints %d", 254 dpmt.Name, len(c.InputEndpoints)) 255 } 256 257 if c.InputEndpoints[0].Name != "RDP" { 258 return fmt.Errorf("Bad endpoint name: %s", c.InputEndpoints[0].Name) 259 } 260 261 if c.InputEndpoints[0].Port != 3389 { 262 return fmt.Errorf("Bad endpoint port: %d", c.InputEndpoints[0].Port) 263 } 264 265 if len(c.SubnetNames) != 1 { 266 return fmt.Errorf( 267 "Instance %s has an unexpected number of associated subnets %d", 268 dpmt.Name, len(c.SubnetNames)) 269 } 270 271 if c.SubnetNames[0] != "subnet1" { 272 return fmt.Errorf("Bad subnet: %s", c.SubnetNames[0]) 273 } 274 275 if c.NetworkSecurityGroup != "terraform-security-group1" { 276 return fmt.Errorf("Bad security group: %s", c.NetworkSecurityGroup) 277 } 278 } 279 } 280 281 return nil 282 } 283 } 284 285 func testAccCheckAzureInstanceAdvancedUpdatedAttributes( 286 dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc { 287 return func(s *terraform.State) error { 288 289 if dpmt.Name != "terraform-test1" { 290 return fmt.Errorf("Bad name: %s", dpmt.Name) 291 } 292 293 if dpmt.VirtualNetworkName != "terraform-vnet-update-test" { 294 return fmt.Errorf("Bad virtual network: %s", dpmt.VirtualNetworkName) 295 } 296 297 if len(dpmt.RoleList) != 1 { 298 return fmt.Errorf( 299 "Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList)) 300 } 301 302 if dpmt.RoleList[0].RoleSize != "Basic_A1" { 303 return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize) 304 } 305 306 for _, c := range dpmt.RoleList[0].ConfigurationSets { 307 if c.ConfigurationSetType == virtualmachine.ConfigurationSetTypeNetwork { 308 if len(c.InputEndpoints) != 1 { 309 return fmt.Errorf( 310 "Instance %s has an unexpected number of endpoints %d", 311 dpmt.Name, len(c.InputEndpoints)) 312 } 313 314 if c.InputEndpoints[0].Name != "RDP" { 315 return fmt.Errorf("Bad endpoint name: %s", c.InputEndpoints[0].Name) 316 } 317 318 if c.InputEndpoints[0].Port != 3389 { 319 return fmt.Errorf("Bad endpoint port: %d", c.InputEndpoints[0].Port) 320 } 321 322 if len(c.SubnetNames) != 1 { 323 return fmt.Errorf( 324 "Instance %s has an unexpected number of associated subnets %d", 325 dpmt.Name, len(c.SubnetNames)) 326 } 327 328 if c.SubnetNames[0] != "subnet1" { 329 return fmt.Errorf("Bad subnet: %s", c.SubnetNames[0]) 330 } 331 332 if c.NetworkSecurityGroup != "terraform-security-group1" { 333 return fmt.Errorf("Bad security group: %s", c.NetworkSecurityGroup) 334 } 335 } 336 } 337 338 return nil 339 } 340 } 341 342 func testAccCheckAzureInstanceUpdatedAttributes( 343 dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc { 344 return func(s *terraform.State) error { 345 346 if dpmt.Name != "terraform-test1" { 347 return fmt.Errorf("Bad name: %s", dpmt.Name) 348 } 349 350 if dpmt.VirtualNetworkName != "terraform-vnet-update-test" { 351 return fmt.Errorf("Bad virtual network: %s", dpmt.VirtualNetworkName) 352 } 353 354 if len(dpmt.RoleList) != 1 { 355 return fmt.Errorf( 356 "Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList)) 357 } 358 359 if dpmt.RoleList[0].RoleSize != "Basic_A2" { 360 return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize) 361 } 362 363 for _, c := range dpmt.RoleList[0].ConfigurationSets { 364 if c.ConfigurationSetType == virtualmachine.ConfigurationSetTypeNetwork { 365 if len(c.InputEndpoints) != 2 { 366 return fmt.Errorf( 367 "Instance %s has an unexpected number of endpoints %d", 368 dpmt.Name, len(c.InputEndpoints)) 369 } 370 371 if c.InputEndpoints[1].Name != "WINRM" { 372 return fmt.Errorf("Bad endpoint name: %s", c.InputEndpoints[1].Name) 373 } 374 375 if c.InputEndpoints[1].Port != 5985 { 376 return fmt.Errorf("Bad endpoint port: %d", c.InputEndpoints[1].Port) 377 } 378 379 if len(c.SubnetNames) != 1 { 380 return fmt.Errorf( 381 "Instance %s has an unexpected number of associated subnets %d", 382 dpmt.Name, len(c.SubnetNames)) 383 } 384 385 if c.SubnetNames[0] != "subnet1" { 386 return fmt.Errorf("Bad subnet: %s", c.SubnetNames[0]) 387 } 388 389 if c.NetworkSecurityGroup != "terraform-security-update-group2" { 390 return fmt.Errorf("Bad security group: %s", c.NetworkSecurityGroup) 391 } 392 } 393 } 394 395 return nil 396 } 397 } 398 399 func testAccCheckAzureInstanceDestroyed(hostedServiceName string) resource.TestCheckFunc { 400 return func(s *terraform.State) error { 401 hostedServiceClient := testAccProvider.Meta().(*Client).hostedServiceClient 402 403 for _, rs := range s.RootModule().Resources { 404 if rs.Type != "azure_instance" { 405 continue 406 } 407 408 if rs.Primary.ID == "" { 409 return fmt.Errorf("No instance ID is set") 410 } 411 412 // if not hosted service was provided; it means that we expect it 413 // to be identical with the name of the instance; which is in the ID. 414 var serviceName string 415 if hostedServiceName == "" { 416 serviceName = rs.Primary.ID 417 } else { 418 serviceName = hostedServiceName 419 } 420 421 _, err := hostedServiceClient.GetHostedService(serviceName) 422 if err == nil { 423 return fmt.Errorf("Instance %s still exists", rs.Primary.ID) 424 } 425 426 if !management.IsResourceNotFoundError(err) { 427 return err 428 } 429 } 430 431 return nil 432 } 433 } 434 435 var testAccAzureInstance_basic = fmt.Sprintf(` 436 resource "azure_instance" "foo" { 437 name = "%s" 438 image = "Ubuntu Server 14.04 LTS" 439 size = "Basic_A1" 440 storage_service_name = "%s" 441 location = "West US" 442 username = "terraform" 443 password = "Pass!admin123" 444 custom_data = "# Hello world" 445 446 endpoint { 447 name = "SSH" 448 protocol = "tcp" 449 public_port = 22 450 private_port = 22 451 } 452 }`, instanceName, testAccStorageServiceName) 453 454 var testAccAzureInstance_separateHostedService = ` 455 resource "azure_hosted_service" "foo" { 456 name = "%s" 457 location = "West US" 458 ephemeral_contents = true 459 } 460 461 resource "azure_instance" "foo" { 462 name = "%s" 463 hosted_service_name = "${azure_hosted_service.foo.name}" 464 image = "Ubuntu Server 14.04 LTS" 465 size = "Basic_A1" 466 storage_service_name = "%s" 467 location = "West US" 468 username = "terraform" 469 password = "Pass!admin123" 470 471 endpoint { 472 name = "SSH" 473 protocol = "tcp" 474 public_port = 22 475 private_port = 22 476 } 477 }` 478 479 var testAccAzureInstance_advanced = fmt.Sprintf(` 480 resource "azure_virtual_network" "foo" { 481 name = "terraform-vnet-advanced-test" 482 address_space = ["10.1.2.0/24"] 483 location = "West US" 484 485 subnet { 486 name = "subnet1" 487 address_prefix = "10.1.2.0/25" 488 } 489 490 subnet { 491 name = "subnet2" 492 address_prefix = "10.1.2.128/25" 493 } 494 } 495 496 resource "azure_security_group" "foo" { 497 name = "terraform-security-group1" 498 location = "West US" 499 } 500 501 resource "azure_security_group_rule" "foo" { 502 name = "rdp" 503 security_group_names = ["${azure_security_group.foo.name}"] 504 priority = 101 505 source_address_prefix = "*" 506 source_port_range = "*" 507 destination_address_prefix = "*" 508 destination_port_range = "3389" 509 action = "Deny" 510 type = "Inbound" 511 protocol = "TCP" 512 } 513 514 resource "azure_instance" "foo" { 515 name = "terraform-test1" 516 image = "Windows Server 2012 R2 Datacenter, January 2016" 517 size = "Basic_A1" 518 storage_service_name = "%s" 519 location = "West US" 520 time_zone = "America/Los_Angeles" 521 subnet = "subnet1" 522 virtual_network = "${azure_virtual_network.foo.name}" 523 security_group = "${azure_security_group.foo.name}" 524 username = "terraform" 525 password = "Pass!admin123" 526 custom_data = "IyBIZWxsbyB3b3JsZA==" 527 528 endpoint { 529 name = "RDP" 530 protocol = "tcp" 531 public_port = 3389 532 private_port = 3389 533 } 534 }`, testAccStorageServiceName) 535 536 var testAccAzureInstance_update = fmt.Sprintf(` 537 resource "azure_virtual_network" "foo" { 538 name = "terraform-vnet-update-test" 539 address_space = ["10.1.2.0/24"] 540 location = "West US" 541 542 subnet { 543 name = "subnet1" 544 address_prefix = "10.1.2.0/25" 545 } 546 547 subnet { 548 name = "subnet2" 549 address_prefix = "10.1.2.128/25" 550 } 551 } 552 553 resource "azure_security_group" "foo" { 554 name = "terraform-security-group1" 555 location = "West US" 556 } 557 558 resource "azure_security_group_rule" "foo" { 559 name = "rdp" 560 security_group_names = ["${azure_security_group.foo.name}"] 561 priority = 101 562 source_address_prefix = "*" 563 source_port_range = "*" 564 destination_address_prefix = "*" 565 destination_port_range = "3389" 566 type = "Inbound" 567 action = "Deny" 568 protocol = "TCP" 569 } 570 571 resource "azure_security_group" "bar" { 572 name = "terraform-security-update-group2" 573 location = "West US" 574 } 575 576 resource "azure_security_group_rule" "bar" { 577 name = "rdp" 578 security_group_names = ["${azure_security_group.bar.name}"] 579 priority = 101 580 source_address_prefix = "192.168.0.0/24" 581 source_port_range = "*" 582 destination_address_prefix = "*" 583 destination_port_range = "3389" 584 type = "Inbound" 585 action = "Deny" 586 protocol = "TCP" 587 } 588 589 resource "azure_instance" "foo" { 590 name = "terraform-test1" 591 image = "Windows Server 2012 R2 Datacenter, January 2016" 592 size = "Basic_A2" 593 storage_service_name = "%s" 594 location = "West US" 595 time_zone = "America/Los_Angeles" 596 subnet = "subnet1" 597 virtual_network = "${azure_virtual_network.foo.name}" 598 security_group = "${azure_security_group.bar.name}" 599 username = "terraform" 600 password = "Pass!admin123" 601 602 endpoint { 603 name = "RDP" 604 protocol = "tcp" 605 public_port = 3389 606 private_port = 3389 607 } 608 609 endpoint { 610 name = "WINRM" 611 protocol = "tcp" 612 public_port = 5985 613 private_port = 5985 614 } 615 }`, testAccStorageServiceName)