github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_availability_set_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMAvailabilitySet_basic(t *testing.T) {
    14  
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccAzureRMVAvailabilitySet_basic, ri, ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testCheckAzureRMAvailabilitySetDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: config,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testCheckAzureRMAvailabilitySetExists("azurerm_availability_set.test"),
    27  					resource.TestCheckResourceAttr(
    28  						"azurerm_availability_set.test", "platform_update_domain_count", "5"),
    29  					resource.TestCheckResourceAttr(
    30  						"azurerm_availability_set.test", "platform_fault_domain_count", "3"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccAzureRMAvailabilitySet_disappears(t *testing.T) {
    38  
    39  	ri := acctest.RandInt()
    40  	config := fmt.Sprintf(testAccAzureRMVAvailabilitySet_basic, ri, ri)
    41  
    42  	resource.Test(t, resource.TestCase{
    43  		PreCheck:     func() { testAccPreCheck(t) },
    44  		Providers:    testAccProviders,
    45  		CheckDestroy: testCheckAzureRMAvailabilitySetDestroy,
    46  		Steps: []resource.TestStep{
    47  			resource.TestStep{
    48  				Config: config,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testCheckAzureRMAvailabilitySetExists("azurerm_availability_set.test"),
    51  					resource.TestCheckResourceAttr(
    52  						"azurerm_availability_set.test", "platform_update_domain_count", "5"),
    53  					resource.TestCheckResourceAttr(
    54  						"azurerm_availability_set.test", "platform_fault_domain_count", "3"),
    55  					testCheckAzureRMAvailabilitySetDisappears("azurerm_availability_set.test"),
    56  				),
    57  				ExpectNonEmptyPlan: true,
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccAzureRMAvailabilitySet_withTags(t *testing.T) {
    64  
    65  	ri := acctest.RandInt()
    66  	preConfig := fmt.Sprintf(testAccAzureRMVAvailabilitySet_withTags, ri, ri)
    67  	postConfig := fmt.Sprintf(testAccAzureRMVAvailabilitySet_withUpdatedTags, ri, ri)
    68  
    69  	resource.Test(t, resource.TestCase{
    70  		PreCheck:     func() { testAccPreCheck(t) },
    71  		Providers:    testAccProviders,
    72  		CheckDestroy: testCheckAzureRMAvailabilitySetDestroy,
    73  		Steps: []resource.TestStep{
    74  			resource.TestStep{
    75  				Config: preConfig,
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testCheckAzureRMAvailabilitySetExists("azurerm_availability_set.test"),
    78  					resource.TestCheckResourceAttr(
    79  						"azurerm_availability_set.test", "tags.%", "2"),
    80  					resource.TestCheckResourceAttr(
    81  						"azurerm_availability_set.test", "tags.environment", "Production"),
    82  					resource.TestCheckResourceAttr(
    83  						"azurerm_availability_set.test", "tags.cost_center", "MSFT"),
    84  				),
    85  			},
    86  
    87  			resource.TestStep{
    88  				Config: postConfig,
    89  				Check: resource.ComposeTestCheckFunc(
    90  					testCheckAzureRMAvailabilitySetExists("azurerm_availability_set.test"),
    91  					resource.TestCheckResourceAttr(
    92  						"azurerm_availability_set.test", "tags.%", "1"),
    93  					resource.TestCheckResourceAttr(
    94  						"azurerm_availability_set.test", "tags.environment", "staging"),
    95  				),
    96  			},
    97  		},
    98  	})
    99  }
   100  
   101  func TestAccAzureRMAvailabilitySet_withDomainCounts(t *testing.T) {
   102  
   103  	ri := acctest.RandInt()
   104  	config := fmt.Sprintf(testAccAzureRMVAvailabilitySet_withDomainCounts, ri, ri)
   105  
   106  	resource.Test(t, resource.TestCase{
   107  		PreCheck:     func() { testAccPreCheck(t) },
   108  		Providers:    testAccProviders,
   109  		CheckDestroy: testCheckAzureRMAvailabilitySetDestroy,
   110  		Steps: []resource.TestStep{
   111  			resource.TestStep{
   112  				Config: config,
   113  				Check: resource.ComposeTestCheckFunc(
   114  					testCheckAzureRMAvailabilitySetExists("azurerm_availability_set.test"),
   115  					resource.TestCheckResourceAttr(
   116  						"azurerm_availability_set.test", "platform_update_domain_count", "10"),
   117  					resource.TestCheckResourceAttr(
   118  						"azurerm_availability_set.test", "platform_fault_domain_count", "1"),
   119  				),
   120  			},
   121  		},
   122  	})
   123  }
   124  
   125  func testCheckAzureRMAvailabilitySetExists(name string) resource.TestCheckFunc {
   126  	return func(s *terraform.State) error {
   127  		// Ensure we have enough information in state to look up in API
   128  		rs, ok := s.RootModule().Resources[name]
   129  		if !ok {
   130  			return fmt.Errorf("Not found: %s", name)
   131  		}
   132  
   133  		availSetName := rs.Primary.Attributes["name"]
   134  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   135  		if !hasResourceGroup {
   136  			return fmt.Errorf("Bad: no resource group found in state for availability set: %s", availSetName)
   137  		}
   138  
   139  		conn := testAccProvider.Meta().(*ArmClient).availSetClient
   140  
   141  		resp, err := conn.Get(resourceGroup, availSetName)
   142  		if err != nil {
   143  			return fmt.Errorf("Bad: Get on availSetClient: %s", err)
   144  		}
   145  
   146  		if resp.StatusCode == http.StatusNotFound {
   147  			return fmt.Errorf("Bad: Availability Set %q (resource group: %q) does not exist", name, resourceGroup)
   148  		}
   149  
   150  		return nil
   151  	}
   152  }
   153  
   154  func testCheckAzureRMAvailabilitySetDisappears(name string) resource.TestCheckFunc {
   155  	return func(s *terraform.State) error {
   156  		// Ensure we have enough information in state to look up in API
   157  		rs, ok := s.RootModule().Resources[name]
   158  		if !ok {
   159  			return fmt.Errorf("Not found: %s", name)
   160  		}
   161  
   162  		availSetName := rs.Primary.Attributes["name"]
   163  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   164  		if !hasResourceGroup {
   165  			return fmt.Errorf("Bad: no resource group found in state for availability set: %s", availSetName)
   166  		}
   167  
   168  		conn := testAccProvider.Meta().(*ArmClient).availSetClient
   169  
   170  		_, err := conn.Delete(resourceGroup, availSetName)
   171  		if err != nil {
   172  			return fmt.Errorf("Bad: Delete on availSetClient: %s", err)
   173  		}
   174  
   175  		return nil
   176  	}
   177  }
   178  
   179  func testCheckAzureRMAvailabilitySetDestroy(s *terraform.State) error {
   180  	conn := testAccProvider.Meta().(*ArmClient).availSetClient
   181  
   182  	for _, rs := range s.RootModule().Resources {
   183  		if rs.Type != "azurerm_availability_set" {
   184  			continue
   185  		}
   186  
   187  		name := rs.Primary.Attributes["name"]
   188  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   189  
   190  		resp, err := conn.Get(resourceGroup, name)
   191  
   192  		if err != nil {
   193  			return nil
   194  		}
   195  
   196  		if resp.StatusCode != http.StatusNotFound {
   197  			return fmt.Errorf("Availability Set still exists:\n%#v", resp.Properties)
   198  		}
   199  	}
   200  
   201  	return nil
   202  }
   203  
   204  var testAccAzureRMVAvailabilitySet_basic = `
   205  resource "azurerm_resource_group" "test" {
   206      name = "acctestRG-%d"
   207      location = "West US"
   208  }
   209  resource "azurerm_availability_set" "test" {
   210      name = "acctestavset-%d"
   211      location = "West US"
   212      resource_group_name = "${azurerm_resource_group.test.name}"
   213  }
   214  `
   215  
   216  var testAccAzureRMVAvailabilitySet_withTags = `
   217  resource "azurerm_resource_group" "test" {
   218      name = "acctestRG-%d"
   219      location = "West US"
   220  }
   221  resource "azurerm_availability_set" "test" {
   222      name = "acctestavset-%d"
   223      location = "West US"
   224      resource_group_name = "${azurerm_resource_group.test.name}"
   225  
   226      tags {
   227  	environment = "Production"
   228  	cost_center = "MSFT"
   229      }
   230  }
   231  `
   232  
   233  var testAccAzureRMVAvailabilitySet_withUpdatedTags = `
   234  resource "azurerm_resource_group" "test" {
   235      name = "acctestRG-%d"
   236      location = "West US"
   237  }
   238  resource "azurerm_availability_set" "test" {
   239      name = "acctestavset-%d"
   240      location = "West US"
   241      resource_group_name = "${azurerm_resource_group.test.name}"
   242  
   243      tags {
   244  	environment = "staging"
   245      }
   246  }
   247  `
   248  
   249  var testAccAzureRMVAvailabilitySet_withDomainCounts = `
   250  resource "azurerm_resource_group" "test" {
   251      name = "acctestRG-%d"
   252      location = "West US"
   253  }
   254  resource "azurerm_availability_set" "test" {
   255      name = "acctestavset-%d"
   256      location = "West US"
   257      resource_group_name = "${azurerm_resource_group.test.name}"
   258      platform_update_domain_count = 10
   259      platform_fault_domain_count = 1
   260  }
   261  `