github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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_withTags(t *testing.T) {
    38  
    39  	ri := acctest.RandInt()
    40  	preConfig := fmt.Sprintf(testAccAzureRMVAvailabilitySet_withTags, ri, ri)
    41  	postConfig := fmt.Sprintf(testAccAzureRMVAvailabilitySet_withUpdatedTags, ri, ri)
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testCheckAzureRMAvailabilitySetDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: preConfig,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testCheckAzureRMAvailabilitySetExists("azurerm_availability_set.test"),
    52  					resource.TestCheckResourceAttr(
    53  						"azurerm_availability_set.test", "tags.%", "2"),
    54  					resource.TestCheckResourceAttr(
    55  						"azurerm_availability_set.test", "tags.environment", "Production"),
    56  					resource.TestCheckResourceAttr(
    57  						"azurerm_availability_set.test", "tags.cost_center", "MSFT"),
    58  				),
    59  			},
    60  
    61  			resource.TestStep{
    62  				Config: postConfig,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testCheckAzureRMAvailabilitySetExists("azurerm_availability_set.test"),
    65  					resource.TestCheckResourceAttr(
    66  						"azurerm_availability_set.test", "tags.%", "1"),
    67  					resource.TestCheckResourceAttr(
    68  						"azurerm_availability_set.test", "tags.environment", "staging"),
    69  				),
    70  			},
    71  		},
    72  	})
    73  }
    74  
    75  func TestAccAzureRMAvailabilitySet_withDomainCounts(t *testing.T) {
    76  
    77  	ri := acctest.RandInt()
    78  	config := fmt.Sprintf(testAccAzureRMVAvailabilitySet_withDomainCounts, ri, ri)
    79  
    80  	resource.Test(t, resource.TestCase{
    81  		PreCheck:     func() { testAccPreCheck(t) },
    82  		Providers:    testAccProviders,
    83  		CheckDestroy: testCheckAzureRMAvailabilitySetDestroy,
    84  		Steps: []resource.TestStep{
    85  			resource.TestStep{
    86  				Config: config,
    87  				Check: resource.ComposeTestCheckFunc(
    88  					testCheckAzureRMAvailabilitySetExists("azurerm_availability_set.test"),
    89  					resource.TestCheckResourceAttr(
    90  						"azurerm_availability_set.test", "platform_update_domain_count", "10"),
    91  					resource.TestCheckResourceAttr(
    92  						"azurerm_availability_set.test", "platform_fault_domain_count", "1"),
    93  				),
    94  			},
    95  		},
    96  	})
    97  }
    98  
    99  func testCheckAzureRMAvailabilitySetExists(name string) resource.TestCheckFunc {
   100  	return func(s *terraform.State) error {
   101  		// Ensure we have enough information in state to look up in API
   102  		rs, ok := s.RootModule().Resources[name]
   103  		if !ok {
   104  			return fmt.Errorf("Not found: %s", name)
   105  		}
   106  
   107  		availSetName := rs.Primary.Attributes["name"]
   108  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   109  		if !hasResourceGroup {
   110  			return fmt.Errorf("Bad: no resource group found in state for availability set: %s", availSetName)
   111  		}
   112  
   113  		conn := testAccProvider.Meta().(*ArmClient).availSetClient
   114  
   115  		resp, err := conn.Get(resourceGroup, availSetName)
   116  		if err != nil {
   117  			return fmt.Errorf("Bad: Get on availSetClient: %s", err)
   118  		}
   119  
   120  		if resp.StatusCode == http.StatusNotFound {
   121  			return fmt.Errorf("Bad: Availability Set %q (resource group: %q) does not exist", name, resourceGroup)
   122  		}
   123  
   124  		return nil
   125  	}
   126  }
   127  
   128  func testCheckAzureRMAvailabilitySetDestroy(s *terraform.State) error {
   129  	conn := testAccProvider.Meta().(*ArmClient).availSetClient
   130  
   131  	for _, rs := range s.RootModule().Resources {
   132  		if rs.Type != "azurerm_availability_set" {
   133  			continue
   134  		}
   135  
   136  		name := rs.Primary.Attributes["name"]
   137  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   138  
   139  		resp, err := conn.Get(resourceGroup, name)
   140  
   141  		if err != nil {
   142  			return nil
   143  		}
   144  
   145  		if resp.StatusCode != http.StatusNotFound {
   146  			return fmt.Errorf("Availability Set still exists:\n%#v", resp.Properties)
   147  		}
   148  	}
   149  
   150  	return nil
   151  }
   152  
   153  var testAccAzureRMVAvailabilitySet_basic = `
   154  resource "azurerm_resource_group" "test" {
   155      name = "acctestrg-%d"
   156      location = "West US"
   157  }
   158  resource "azurerm_availability_set" "test" {
   159      name = "acctestavset-%d"
   160      location = "West US"
   161      resource_group_name = "${azurerm_resource_group.test.name}"
   162  }
   163  `
   164  
   165  var testAccAzureRMVAvailabilitySet_withTags = `
   166  resource "azurerm_resource_group" "test" {
   167      name = "acctestrg-%d"
   168      location = "West US"
   169  }
   170  resource "azurerm_availability_set" "test" {
   171      name = "acctestavset-%d"
   172      location = "West US"
   173      resource_group_name = "${azurerm_resource_group.test.name}"
   174  
   175      tags {
   176  	environment = "Production"
   177  	cost_center = "MSFT"
   178      }
   179  }
   180  `
   181  
   182  var testAccAzureRMVAvailabilitySet_withUpdatedTags = `
   183  resource "azurerm_resource_group" "test" {
   184      name = "acctestrg-%d"
   185      location = "West US"
   186  }
   187  resource "azurerm_availability_set" "test" {
   188      name = "acctestavset-%d"
   189      location = "West US"
   190      resource_group_name = "${azurerm_resource_group.test.name}"
   191  
   192      tags {
   193  	environment = "staging"
   194      }
   195  }
   196  `
   197  
   198  var testAccAzureRMVAvailabilitySet_withDomainCounts = `
   199  resource "azurerm_resource_group" "test" {
   200      name = "acctestrg-%d"
   201      location = "West US"
   202  }
   203  resource "azurerm_availability_set" "test" {
   204      name = "acctestavset-%d"
   205      location = "West US"
   206      resource_group_name = "${azurerm_resource_group.test.name}"
   207      platform_update_domain_count = 10
   208      platform_fault_domain_count = 1
   209  }
   210  `