github.com/ggiamarchi/terraform@v0.3.7-0.20150607194748-ed2a66a46a71/builtin/providers/azure/resource_azure_instance_test.go (about)

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