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