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