github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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"),
    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"),
   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-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" {
   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 testAccCheckAzureInstanceUpdatedAttributes(
   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" {
   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_A2" {
   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) != 2 {
   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[1].Name != "WINRM" {
   306  					return fmt.Errorf("Bad endpoint name: %s", c.InputEndpoints[1].Name)
   307  				}
   308  
   309  				if c.InputEndpoints[1].Port != 5985 {
   310  					return fmt.Errorf("Bad endpoint port: %d", c.InputEndpoints[1].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-group2" {
   324  					return fmt.Errorf("Bad security group: %s", c.NetworkSecurityGroup)
   325  				}
   326  			}
   327  		}
   328  
   329  		return nil
   330  	}
   331  }
   332  
   333  func testAccCheckAzureInstanceDestroyed(hostedServiceName string) resource.TestCheckFunc {
   334  	return func(s *terraform.State) error {
   335  		hostedServiceClient := testAccProvider.Meta().(*Client).hostedServiceClient
   336  
   337  		for _, rs := range s.RootModule().Resources {
   338  			if rs.Type != "azure_instance" {
   339  				continue
   340  			}
   341  
   342  			if rs.Primary.ID == "" {
   343  				return fmt.Errorf("No instance ID is set")
   344  			}
   345  
   346  			// if not hosted service was provided; it means that we expect it
   347  			// to be identical with the name of the instance; which is in the ID.
   348  			var serviceName string
   349  			if hostedServiceName == "" {
   350  				serviceName = rs.Primary.ID
   351  			} else {
   352  				serviceName = hostedServiceName
   353  			}
   354  
   355  			_, err := hostedServiceClient.GetHostedService(serviceName)
   356  			if err == nil {
   357  				return fmt.Errorf("Instance %s still exists", rs.Primary.ID)
   358  			}
   359  
   360  			if !management.IsResourceNotFoundError(err) {
   361  				return err
   362  			}
   363  		}
   364  
   365  		return nil
   366  	}
   367  }
   368  
   369  var testAccAzureInstance_basic = fmt.Sprintf(`
   370  resource "azure_instance" "foo" {
   371      name = "%s"
   372      image = "Ubuntu Server 14.04 LTS"
   373      size = "Basic_A1"
   374      storage_service_name = "%s"
   375      location = "West US"
   376      username = "terraform"
   377      password = "Pass!admin123"
   378  
   379      endpoint {
   380          name = "SSH"
   381          protocol = "tcp"
   382          public_port = 22
   383          private_port = 22
   384      }
   385  }`, instanceName, testAccStorageServiceName)
   386  
   387  var testAccAzureInstance_separateHostedService = fmt.Sprintf(`
   388  resource "azure_hosted_service" "foo" {
   389  	name = "%s"
   390  	location = "West US"
   391  	ephemeral_contents = true
   392  }
   393  
   394  resource "azure_instance" "foo" {
   395      name = "%s"
   396      hosted_service_name = "${azure_hosted_service.foo.name}"
   397      image = "Ubuntu Server 14.04 LTS"
   398      size = "Basic_A1"
   399      storage_service_name = "%s"
   400      location = "West US"
   401      username = "terraform"
   402      password = "Pass!admin123"
   403  
   404      endpoint {
   405          name = "SSH"
   406          protocol = "tcp"
   407          public_port = 22
   408          private_port = 22
   409      }
   410  }`, testAccHostedServiceName, instanceName, testAccStorageServiceName)
   411  
   412  var testAccAzureInstance_advanced = fmt.Sprintf(`
   413  resource "azure_virtual_network" "foo" {
   414      name = "terraform-vnet"
   415      address_space = ["10.1.2.0/24"]
   416  		location = "West US"
   417  
   418  		subnet {
   419          name = "subnet1"
   420  				address_prefix = "10.1.2.0/25"
   421  		}
   422  
   423  		subnet {
   424          name = "subnet2"
   425  				address_prefix = "10.1.2.128/25"
   426      }
   427  }
   428  
   429  resource "azure_security_group" "foo" {
   430      name = "terraform-security-group1"
   431      location = "West US"
   432  }
   433  
   434  resource "azure_security_group_rule" "foo" {
   435      name = "rdp"
   436      security_group_names = ["${azure_security_group.foo.name}"]
   437      priority = 101
   438      source_address_prefix = "*"
   439      source_port_range = "*"
   440      destination_address_prefix = "*"
   441      destination_port_range = "3389"
   442  	action = "Deny"
   443  	type = "Inbound"
   444      protocol = "TCP"
   445  }
   446  
   447  resource "azure_instance" "foo" {
   448      name = "terraform-test1"
   449      image = "Windows Server 2012 R2 Datacenter, September 2015"
   450      size = "Basic_A1"
   451      storage_service_name = "%s"
   452      location = "West US"
   453      time_zone = "America/Los_Angeles"
   454      subnet = "subnet1"
   455      virtual_network = "${azure_virtual_network.foo.name}"
   456      security_group = "${azure_security_group.foo.name}"
   457      username = "terraform"
   458      password = "Pass!admin123"
   459  
   460      endpoint {
   461          name = "RDP"
   462          protocol = "tcp"
   463          public_port = 3389
   464          private_port = 3389
   465      }
   466  }`, testAccStorageServiceName)
   467  
   468  var testAccAzureInstance_update = fmt.Sprintf(`
   469  resource "azure_virtual_network" "foo" {
   470      name = "terraform-vnet"
   471      address_space = ["10.1.2.0/24"]
   472  		location = "West US"
   473  
   474      subnet {
   475          name = "subnet1"
   476  		address_prefix = "10.1.2.0/25"
   477  	}
   478  
   479      subnet {
   480          name = "subnet2"
   481  		address_prefix = "10.1.2.128/25"
   482      }
   483  }
   484  
   485  resource "azure_security_group" "foo" {
   486      name = "terraform-security-group1"
   487      location = "West US"
   488  }
   489  
   490  resource "azure_security_group_rule" "foo" {
   491      name = "rdp"
   492      security_group_names = ["${azure_security_group.foo.name}"]
   493      priority = 101
   494      source_address_prefix = "*"
   495      source_port_range = "*"
   496      destination_address_prefix = "*"
   497      destination_port_range = "3389"
   498  	type = "Inbound"
   499  	action = "Deny"
   500      protocol = "TCP"
   501  }
   502  
   503  resource "azure_security_group" "bar" {
   504      name = "terraform-security-group2"
   505      location = "West US"
   506  }
   507  
   508  resource "azure_security_group_rule" "bar" {
   509      name = "rdp"
   510      security_group_names = ["${azure_security_group.bar.name}"]
   511      priority = 101
   512      source_address_prefix = "192.168.0.0/24"
   513      source_port_range = "*"
   514      destination_address_prefix = "*"
   515      destination_port_range = "3389"
   516  	type = "Inbound"
   517  	action = "Deny"
   518      protocol = "TCP"
   519  }
   520  
   521  resource "azure_instance" "foo" {
   522      name = "terraform-test1"
   523      image = "Windows Server 2012 R2 Datacenter, September 2015"
   524      size = "Basic_A2"
   525      storage_service_name = "%s"
   526      location = "West US"
   527      time_zone = "America/Los_Angeles"
   528      subnet = "subnet1"
   529      virtual_network = "${azure_virtual_network.foo.name}"
   530      security_group = "${azure_security_group.bar.name}"
   531      username = "terraform"
   532      password = "Pass!admin123"
   533  
   534      endpoint {
   535          name = "RDP"
   536          protocol = "tcp"
   537          public_port = 3389
   538          private_port = 3389
   539      }
   540  
   541      endpoint {
   542          name = "WINRM"
   543          protocol = "tcp"
   544          public_port = 5985
   545          private_port = 5985
   546      }
   547  }`, testAccStorageServiceName)