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