github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/builtin/providers/azure/resource_azure_instance_test.go (about) 1 package azure 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/Azure/azure-sdk-for-go/management" 8 "github.com/Azure/azure-sdk-for-go/management/virtualmachine" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAzureInstance_basic(t *testing.T) { 14 var dpmt virtualmachine.DeploymentResponse 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckAzureInstanceDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAzureInstance_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckAzureInstanceExists( 25 "azure_instance.foo", &dpmt), 26 testAccCheckAzureInstanceBasicAttributes(&dpmt), 27 resource.TestCheckResourceAttr( 28 "azure_instance.foo", "name", "terraform-test"), 29 resource.TestCheckResourceAttr( 30 "azure_instance.foo", "location", "West US"), 31 resource.TestCheckResourceAttr( 32 "azure_instance.foo", "endpoint.2462817782.public_port", "22"), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func TestAccAzureInstance_advanced(t *testing.T) { 40 var dpmt virtualmachine.DeploymentResponse 41 42 resource.Test(t, resource.TestCase{ 43 PreCheck: func() { testAccPreCheck(t) }, 44 Providers: testAccProviders, 45 CheckDestroy: testAccCheckAzureInstanceDestroy, 46 Steps: []resource.TestStep{ 47 resource.TestStep{ 48 Config: testAccAzureInstance_advanced, 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckAzureInstanceExists( 51 "azure_instance.foo", &dpmt), 52 testAccCheckAzureInstanceAdvancedAttributes(&dpmt), 53 resource.TestCheckResourceAttr( 54 "azure_instance.foo", "name", "terraform-test1"), 55 resource.TestCheckResourceAttr( 56 "azure_instance.foo", "size", "Basic_A1"), 57 resource.TestCheckResourceAttr( 58 "azure_instance.foo", "subnet", "subnet1"), 59 resource.TestCheckResourceAttr( 60 "azure_instance.foo", "virtual_network", "terraform-vnet"), 61 resource.TestCheckResourceAttr( 62 "azure_instance.foo", "security_group", "terraform-security-group1"), 63 resource.TestCheckResourceAttr( 64 "azure_instance.foo", "endpoint.1814039778.public_port", "3389"), 65 ), 66 }, 67 }, 68 }) 69 } 70 71 func TestAccAzureInstance_update(t *testing.T) { 72 var dpmt virtualmachine.DeploymentResponse 73 74 resource.Test(t, resource.TestCase{ 75 PreCheck: func() { testAccPreCheck(t) }, 76 Providers: testAccProviders, 77 CheckDestroy: testAccCheckAzureInstanceDestroy, 78 Steps: []resource.TestStep{ 79 resource.TestStep{ 80 Config: testAccAzureInstance_advanced, 81 Check: resource.ComposeTestCheckFunc( 82 testAccCheckAzureInstanceExists( 83 "azure_instance.foo", &dpmt), 84 testAccCheckAzureInstanceAdvancedAttributes(&dpmt), 85 resource.TestCheckResourceAttr( 86 "azure_instance.foo", "name", "terraform-test1"), 87 resource.TestCheckResourceAttr( 88 "azure_instance.foo", "size", "Basic_A1"), 89 resource.TestCheckResourceAttr( 90 "azure_instance.foo", "subnet", "subnet1"), 91 resource.TestCheckResourceAttr( 92 "azure_instance.foo", "virtual_network", "terraform-vnet"), 93 resource.TestCheckResourceAttr( 94 "azure_instance.foo", "security_group", "terraform-security-group1"), 95 resource.TestCheckResourceAttr( 96 "azure_instance.foo", "endpoint.1814039778.public_port", "3389"), 97 ), 98 }, 99 100 resource.TestStep{ 101 Config: testAccAzureInstance_update, 102 Check: resource.ComposeTestCheckFunc( 103 testAccCheckAzureInstanceExists( 104 "azure_instance.foo", &dpmt), 105 testAccCheckAzureInstanceUpdatedAttributes(&dpmt), 106 resource.TestCheckResourceAttr( 107 "azure_instance.foo", "size", "Basic_A2"), 108 resource.TestCheckResourceAttr( 109 "azure_instance.foo", "security_group", "terraform-security-group2"), 110 resource.TestCheckResourceAttr( 111 "azure_instance.foo", "endpoint.1814039778.public_port", "3389"), 112 resource.TestCheckResourceAttr( 113 "azure_instance.foo", "endpoint.3713350066.public_port", "5985"), 114 ), 115 }, 116 }, 117 }) 118 } 119 120 func testAccCheckAzureInstanceExists( 121 n string, 122 dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc { 123 return func(s *terraform.State) error { 124 rs, ok := s.RootModule().Resources[n] 125 if !ok { 126 return fmt.Errorf("Not found: %s", n) 127 } 128 129 if rs.Primary.ID == "" { 130 return fmt.Errorf("No instance ID is set") 131 } 132 133 vmClient := testAccProvider.Meta().(*Client).vmClient 134 vm, err := vmClient.GetDeployment(rs.Primary.ID, rs.Primary.ID) 135 if err != nil { 136 return err 137 } 138 139 if vm.Name != rs.Primary.ID { 140 return fmt.Errorf("Instance not found") 141 } 142 143 *dpmt = vm 144 145 return nil 146 } 147 } 148 149 func testAccCheckAzureInstanceBasicAttributes( 150 dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc { 151 return func(s *terraform.State) error { 152 153 if dpmt.Name != "terraform-test" { 154 return fmt.Errorf("Bad name: %s", dpmt.Name) 155 } 156 157 if len(dpmt.RoleList) != 1 { 158 return fmt.Errorf( 159 "Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList)) 160 } 161 162 if dpmt.RoleList[0].RoleSize != "Basic_A1" { 163 return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize) 164 } 165 166 return nil 167 } 168 } 169 170 func testAccCheckAzureInstanceAdvancedAttributes( 171 dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc { 172 return func(s *terraform.State) error { 173 174 if dpmt.Name != "terraform-test1" { 175 return fmt.Errorf("Bad name: %s", dpmt.Name) 176 } 177 178 if dpmt.VirtualNetworkName != "terraform-vnet" { 179 return fmt.Errorf("Bad virtual network: %s", dpmt.VirtualNetworkName) 180 } 181 182 if len(dpmt.RoleList) != 1 { 183 return fmt.Errorf( 184 "Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList)) 185 } 186 187 if dpmt.RoleList[0].RoleSize != "Basic_A1" { 188 return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize) 189 } 190 191 for _, c := range dpmt.RoleList[0].ConfigurationSets { 192 if c.ConfigurationSetType == virtualmachine.ConfigurationSetTypeNetwork { 193 if len(c.InputEndpoints) != 1 { 194 return fmt.Errorf( 195 "Instance %s has an unexpected number of endpoints %d", 196 dpmt.Name, len(c.InputEndpoints)) 197 } 198 199 if c.InputEndpoints[0].Name != "RDP" { 200 return fmt.Errorf("Bad endpoint name: %s", c.InputEndpoints[0].Name) 201 } 202 203 if c.InputEndpoints[0].Port != 3389 { 204 return fmt.Errorf("Bad endpoint port: %d", c.InputEndpoints[0].Port) 205 } 206 207 if len(c.SubnetNames) != 1 { 208 return fmt.Errorf( 209 "Instance %s has an unexpected number of associated subnets %d", 210 dpmt.Name, len(c.SubnetNames)) 211 } 212 213 if c.SubnetNames[0] != "subnet1" { 214 return fmt.Errorf("Bad subnet: %s", c.SubnetNames[0]) 215 } 216 217 if c.NetworkSecurityGroup != "terraform-security-group1" { 218 return fmt.Errorf("Bad security group: %s", c.NetworkSecurityGroup) 219 } 220 } 221 } 222 223 return nil 224 } 225 } 226 227 func testAccCheckAzureInstanceUpdatedAttributes( 228 dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc { 229 return func(s *terraform.State) error { 230 231 if dpmt.Name != "terraform-test1" { 232 return fmt.Errorf("Bad name: %s", dpmt.Name) 233 } 234 235 if dpmt.VirtualNetworkName != "terraform-vnet" { 236 return fmt.Errorf("Bad virtual network: %s", dpmt.VirtualNetworkName) 237 } 238 239 if len(dpmt.RoleList) != 1 { 240 return fmt.Errorf( 241 "Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList)) 242 } 243 244 if dpmt.RoleList[0].RoleSize != "Basic_A2" { 245 return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize) 246 } 247 248 for _, c := range dpmt.RoleList[0].ConfigurationSets { 249 if c.ConfigurationSetType == virtualmachine.ConfigurationSetTypeNetwork { 250 if len(c.InputEndpoints) != 2 { 251 return fmt.Errorf( 252 "Instance %s has an unexpected number of endpoints %d", 253 dpmt.Name, len(c.InputEndpoints)) 254 } 255 256 if c.InputEndpoints[1].Name != "WINRM" { 257 return fmt.Errorf("Bad endpoint name: %s", c.InputEndpoints[1].Name) 258 } 259 260 if c.InputEndpoints[1].Port != 5985 { 261 return fmt.Errorf("Bad endpoint port: %d", c.InputEndpoints[1].Port) 262 } 263 264 if len(c.SubnetNames) != 1 { 265 return fmt.Errorf( 266 "Instance %s has an unexpected number of associated subnets %d", 267 dpmt.Name, len(c.SubnetNames)) 268 } 269 270 if c.SubnetNames[0] != "subnet1" { 271 return fmt.Errorf("Bad subnet: %s", c.SubnetNames[0]) 272 } 273 274 if c.NetworkSecurityGroup != "terraform-security-group2" { 275 return fmt.Errorf("Bad security group: %s", c.NetworkSecurityGroup) 276 } 277 } 278 } 279 280 return nil 281 } 282 } 283 284 func testAccCheckAzureInstanceDestroy(s *terraform.State) error { 285 hostedServiceClient := testAccProvider.Meta().(*Client).hostedServiceClient 286 287 for _, rs := range s.RootModule().Resources { 288 if rs.Type != "azure_instance" { 289 continue 290 } 291 292 if rs.Primary.ID == "" { 293 return fmt.Errorf("No instance ID is set") 294 } 295 296 _, err := hostedServiceClient.GetHostedService(rs.Primary.ID) 297 if err == nil { 298 return fmt.Errorf("Instance %s still exists", rs.Primary.ID) 299 } 300 301 if !management.IsResourceNotFoundError(err) { 302 return err 303 } 304 } 305 306 return nil 307 } 308 309 var testAccAzureInstance_basic = fmt.Sprintf(` 310 resource "azure_instance" "foo" { 311 name = "terraform-test" 312 image = "Ubuntu Server 14.04 LTS" 313 size = "Basic_A1" 314 storage_service_name = "%s" 315 location = "West US" 316 username = "terraform" 317 password = "Pass!admin123" 318 319 endpoint { 320 name = "SSH" 321 protocol = "tcp" 322 public_port = 22 323 private_port = 22 324 } 325 }`, testAccStorageServiceName) 326 327 var testAccAzureInstance_advanced = fmt.Sprintf(` 328 resource "azure_virtual_network" "foo" { 329 name = "terraform-vnet" 330 address_space = ["10.1.2.0/24"] 331 location = "West US" 332 333 subnet { 334 name = "subnet1" 335 address_prefix = "10.1.2.0/25" 336 } 337 338 subnet { 339 name = "subnet2" 340 address_prefix = "10.1.2.128/25" 341 } 342 } 343 344 resource "azure_security_group" "foo" { 345 name = "terraform-security-group1" 346 location = "West US" 347 } 348 349 resource "azure_security_group_rule" "foo" { 350 name = "rdp" 351 security_group_name = "${azure_security_group.foo.name}" 352 priority = 101 353 source_address_prefix = "*" 354 source_port_range = "*" 355 destination_address_prefix = "*" 356 destination_port_range = "3389" 357 action = "Deny" 358 type = "Inbound" 359 protocol = "TCP" 360 } 361 362 resource "azure_instance" "foo" { 363 name = "terraform-test1" 364 image = "Windows Server 2012 R2 Datacenter, April 2015" 365 size = "Basic_A1" 366 storage_service_name = "%s" 367 location = "West US" 368 time_zone = "America/Los_Angeles" 369 subnet = "subnet1" 370 virtual_network = "${azure_virtual_network.foo.name}" 371 security_group = "${azure_security_group.foo.name}" 372 username = "terraform" 373 password = "Pass!admin123" 374 375 endpoint { 376 name = "RDP" 377 protocol = "tcp" 378 public_port = 3389 379 private_port = 3389 380 } 381 }`, testAccStorageServiceName) 382 383 var testAccAzureInstance_update = fmt.Sprintf(` 384 resource "azure_virtual_network" "foo" { 385 name = "terraform-vnet" 386 address_space = ["10.1.2.0/24"] 387 location = "West US" 388 389 subnet { 390 name = "subnet1" 391 address_prefix = "10.1.2.0/25" 392 } 393 394 subnet { 395 name = "subnet2" 396 address_prefix = "10.1.2.128/25" 397 } 398 } 399 400 resource "azure_security_group" "foo" { 401 name = "terraform-security-group1" 402 location = "West US" 403 } 404 405 resource "azure_security_group_rule" "foo" { 406 name = "rdp" 407 security_group_name = "${azure_security_group.foo.name}" 408 priority = 101 409 source_address_prefix = "*" 410 source_port_range = "*" 411 destination_address_prefix = "*" 412 destination_port_range = "3389" 413 type = "Inbound" 414 action = "Deny" 415 protocol = "TCP" 416 } 417 418 resource "azure_security_group" "bar" { 419 name = "terraform-security-group2" 420 location = "West US" 421 } 422 423 resource "azure_security_group_rule" "bar" { 424 name = "rdp" 425 security_group_name = "${azure_security_group.bar.name}" 426 priority = 101 427 source_address_prefix = "192.168.0.0/24" 428 source_port_range = "*" 429 destination_address_prefix = "*" 430 destination_port_range = "3389" 431 type = "Inbound" 432 action = "Deny" 433 protocol = "TCP" 434 } 435 436 resource "azure_instance" "foo" { 437 name = "terraform-test1" 438 image = "Windows Server 2012 R2 Datacenter, April 2015" 439 size = "Basic_A2" 440 storage_service_name = "%s" 441 location = "West US" 442 time_zone = "America/Los_Angeles" 443 subnet = "subnet1" 444 virtual_network = "${azure_virtual_network.foo.name}" 445 security_group = "${azure_security_group.bar.name}" 446 username = "terraform" 447 password = "Pass!admin123" 448 449 endpoint { 450 name = "RDP" 451 protocol = "tcp" 452 public_port = 3389 453 private_port = 3389 454 } 455 456 endpoint { 457 name = "WINRM" 458 protocol = "tcp" 459 public_port = 5985 460 private_port = 5985 461 } 462 }`, testAccStorageServiceName)