github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 TestAccAzureRMAvailabilitySet_managed(t *testing.T) {
   126  	ri := acctest.RandInt()
   127  	config := fmt.Sprintf(testAccAzureRMVAvailabilitySet_managed, ri, ri)
   128  
   129  	resource.Test(t, resource.TestCase{
   130  		PreCheck:     func() { testAccPreCheck(t) },
   131  		Providers:    testAccProviders,
   132  		CheckDestroy: testCheckAzureRMAvailabilitySetDestroy,
   133  		Steps: []resource.TestStep{
   134  			resource.TestStep{
   135  				Config: config,
   136  				Check: resource.ComposeTestCheckFunc(
   137  					testCheckAzureRMAvailabilitySetExists("azurerm_availability_set.test"),
   138  					resource.TestCheckResourceAttr(
   139  						"azurerm_availability_set.test", "managed", "true"),
   140  				),
   141  			},
   142  		},
   143  	})
   144  }
   145  
   146  func testCheckAzureRMAvailabilitySetExists(name string) resource.TestCheckFunc {
   147  	return func(s *terraform.State) error {
   148  		// Ensure we have enough information in state to look up in API
   149  		rs, ok := s.RootModule().Resources[name]
   150  		if !ok {
   151  			return fmt.Errorf("Not found: %s", name)
   152  		}
   153  
   154  		availSetName := rs.Primary.Attributes["name"]
   155  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   156  		if !hasResourceGroup {
   157  			return fmt.Errorf("Bad: no resource group found in state for availability set: %s", availSetName)
   158  		}
   159  
   160  		conn := testAccProvider.Meta().(*ArmClient).availSetClient
   161  
   162  		resp, err := conn.Get(resourceGroup, availSetName)
   163  		if err != nil {
   164  			return fmt.Errorf("Bad: Get on availSetClient: %s", err)
   165  		}
   166  
   167  		if resp.StatusCode == http.StatusNotFound {
   168  			return fmt.Errorf("Bad: Availability Set %q (resource group: %q) does not exist", name, resourceGroup)
   169  		}
   170  
   171  		return nil
   172  	}
   173  }
   174  
   175  func testCheckAzureRMAvailabilitySetDisappears(name string) resource.TestCheckFunc {
   176  	return func(s *terraform.State) error {
   177  		// Ensure we have enough information in state to look up in API
   178  		rs, ok := s.RootModule().Resources[name]
   179  		if !ok {
   180  			return fmt.Errorf("Not found: %s", name)
   181  		}
   182  
   183  		availSetName := rs.Primary.Attributes["name"]
   184  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   185  		if !hasResourceGroup {
   186  			return fmt.Errorf("Bad: no resource group found in state for availability set: %s", availSetName)
   187  		}
   188  
   189  		conn := testAccProvider.Meta().(*ArmClient).availSetClient
   190  
   191  		_, err := conn.Delete(resourceGroup, availSetName)
   192  		if err != nil {
   193  			return fmt.Errorf("Bad: Delete on availSetClient: %s", err)
   194  		}
   195  
   196  		return nil
   197  	}
   198  }
   199  
   200  func testCheckAzureRMAvailabilitySetDestroy(s *terraform.State) error {
   201  	conn := testAccProvider.Meta().(*ArmClient).availSetClient
   202  
   203  	for _, rs := range s.RootModule().Resources {
   204  		if rs.Type != "azurerm_availability_set" {
   205  			continue
   206  		}
   207  
   208  		name := rs.Primary.Attributes["name"]
   209  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   210  
   211  		resp, err := conn.Get(resourceGroup, name)
   212  
   213  		if err != nil {
   214  			return nil
   215  		}
   216  
   217  		if resp.StatusCode != http.StatusNotFound {
   218  			return fmt.Errorf("Availability Set still exists:\n%#v", resp.AvailabilitySetProperties)
   219  		}
   220  	}
   221  
   222  	return nil
   223  }
   224  
   225  var testAccAzureRMVAvailabilitySet_basic = `
   226  resource "azurerm_resource_group" "test" {
   227      name = "acctestRG-%d"
   228      location = "West US"
   229  }
   230  resource "azurerm_availability_set" "test" {
   231      name = "acctestavset-%d"
   232      location = "West US"
   233      resource_group_name = "${azurerm_resource_group.test.name}"
   234  }
   235  `
   236  
   237  var testAccAzureRMVAvailabilitySet_withTags = `
   238  resource "azurerm_resource_group" "test" {
   239      name = "acctestRG-%d"
   240      location = "West US"
   241  }
   242  resource "azurerm_availability_set" "test" {
   243      name = "acctestavset-%d"
   244      location = "West US"
   245      resource_group_name = "${azurerm_resource_group.test.name}"
   246  
   247      tags {
   248         environment = "Production"
   249         cost_center = "MSFT"
   250      }
   251  }
   252  `
   253  
   254  var testAccAzureRMVAvailabilitySet_withUpdatedTags = `
   255  resource "azurerm_resource_group" "test" {
   256      name = "acctestRG-%d"
   257      location = "West US"
   258  }
   259  resource "azurerm_availability_set" "test" {
   260      name = "acctestavset-%d"
   261      location = "West US"
   262      resource_group_name = "${azurerm_resource_group.test.name}"
   263  
   264      tags {
   265         environment = "staging"
   266      }
   267  }
   268  `
   269  
   270  var testAccAzureRMVAvailabilitySet_withDomainCounts = `
   271  resource "azurerm_resource_group" "test" {
   272      name = "acctestRG-%d"
   273      location = "West US"
   274  }
   275  resource "azurerm_availability_set" "test" {
   276      name = "acctestavset-%d"
   277      location = "West US"
   278      resource_group_name = "${azurerm_resource_group.test.name}"
   279      platform_update_domain_count = 10
   280      platform_fault_domain_count = 1
   281  }
   282  `
   283  
   284  var testAccAzureRMVAvailabilitySet_managed = `
   285  resource "azurerm_resource_group" "test" {
   286      name = "acctestRG-%d"
   287      location = "West US"
   288  }
   289  resource "azurerm_availability_set" "test" {
   290      name = "acctestavset-%d"
   291      location = "West US"
   292      resource_group_name = "${azurerm_resource_group.test.name}"
   293      platform_update_domain_count = 10
   294      platform_fault_domain_count = 1
   295      managed = true
   296  }
   297  `