github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_resource_group_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/Azure/azure-sdk-for-go/core/http" 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAzureRMResourceGroup_basic(t *testing.T) { 14 ri := acctest.RandInt() 15 config := fmt.Sprintf(testAccAzureRMResourceGroup_basic, ri) 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testCheckAzureRMResourceGroupDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: config, 24 Check: resource.ComposeTestCheckFunc( 25 testCheckAzureRMResourceGroupExists("azurerm_resource_group.test"), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccAzureRMResourceGroup_withTags(t *testing.T) { 33 ri := acctest.RandInt() 34 preConfig := fmt.Sprintf(testAccAzureRMResourceGroup_withTags, ri) 35 postConfig := fmt.Sprintf(testAccAzureRMResourceGroup_withTagsUpdated, ri) 36 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: testCheckAzureRMResourceGroupDestroy, 41 Steps: []resource.TestStep{ 42 resource.TestStep{ 43 Config: preConfig, 44 Check: resource.ComposeTestCheckFunc( 45 testCheckAzureRMResourceGroupExists("azurerm_resource_group.test"), 46 resource.TestCheckResourceAttr( 47 "azurerm_resource_group.test", "tags.%", "2"), 48 resource.TestCheckResourceAttr( 49 "azurerm_resource_group.test", "tags.environment", "Production"), 50 resource.TestCheckResourceAttr( 51 "azurerm_resource_group.test", "tags.cost_center", "MSFT"), 52 ), 53 }, 54 55 resource.TestStep{ 56 Config: postConfig, 57 Check: resource.ComposeTestCheckFunc( 58 testCheckAzureRMResourceGroupExists("azurerm_resource_group.test"), 59 resource.TestCheckResourceAttr( 60 "azurerm_resource_group.test", "tags.%", "1"), 61 resource.TestCheckResourceAttr( 62 "azurerm_resource_group.test", "tags.environment", "staging"), 63 ), 64 }, 65 }, 66 }) 67 } 68 69 func testCheckAzureRMResourceGroupExists(name string) resource.TestCheckFunc { 70 return func(s *terraform.State) error { 71 // Ensure we have enough information in state to look up in API 72 rs, ok := s.RootModule().Resources[name] 73 if !ok { 74 return fmt.Errorf("Not found: %s", name) 75 } 76 77 resourceGroup := rs.Primary.Attributes["name"] 78 79 // Ensure resource group exists in API 80 conn := testAccProvider.Meta().(*ArmClient).resourceGroupClient 81 82 resp, err := conn.Get(resourceGroup) 83 if err != nil { 84 return fmt.Errorf("Bad: Get on resourceGroupClient: %s", err) 85 } 86 87 if resp.StatusCode == http.StatusNotFound { 88 return fmt.Errorf("Bad: Virtual Network %q (resource group: %q) does not exist", name, resourceGroup) 89 } 90 91 return nil 92 } 93 } 94 95 func testCheckAzureRMResourceGroupDestroy(s *terraform.State) error { 96 conn := testAccProvider.Meta().(*ArmClient).resourceGroupClient 97 98 for _, rs := range s.RootModule().Resources { 99 if rs.Type != "azurerm_resource_group" { 100 continue 101 } 102 103 resourceGroup := rs.Primary.ID 104 105 resp, err := conn.Get(resourceGroup) 106 if err != nil { 107 return nil 108 } 109 110 if resp.StatusCode != http.StatusNotFound { 111 return fmt.Errorf("Resource Group still exists:\n%#v", resp.Properties) 112 } 113 } 114 115 return nil 116 } 117 118 var testAccAzureRMResourceGroup_basic = ` 119 resource "azurerm_resource_group" "test" { 120 name = "acctestrg-%d" 121 location = "West US" 122 } 123 ` 124 125 var testAccAzureRMResourceGroup_withTags = ` 126 resource "azurerm_resource_group" "test" { 127 name = "acctestrg-%d" 128 location = "West US" 129 130 tags { 131 environment = "Production" 132 cost_center = "MSFT" 133 } 134 } 135 ` 136 137 var testAccAzureRMResourceGroup_withTagsUpdated = ` 138 resource "azurerm_resource_group" "test" { 139 name = "acctestrg-%d" 140 location = "West US" 141 142 tags { 143 environment = "staging" 144 } 145 } 146 `