github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_resource_group_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 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_disappears(t *testing.T) { 33 ri := acctest.RandInt() 34 config := fmt.Sprintf(testAccAzureRMResourceGroup_basic, ri) 35 36 resource.Test(t, resource.TestCase{ 37 PreCheck: func() { testAccPreCheck(t) }, 38 Providers: testAccProviders, 39 CheckDestroy: testCheckAzureRMResourceGroupDestroy, 40 Steps: []resource.TestStep{ 41 resource.TestStep{ 42 Config: config, 43 Check: resource.ComposeTestCheckFunc( 44 testCheckAzureRMResourceGroupExists("azurerm_resource_group.test"), 45 testCheckAzureRMResourceGroupDisappears("azurerm_resource_group.test"), 46 ), 47 ExpectNonEmptyPlan: true, 48 }, 49 }, 50 }) 51 } 52 53 func TestAccAzureRMResourceGroup_withTags(t *testing.T) { 54 ri := acctest.RandInt() 55 preConfig := fmt.Sprintf(testAccAzureRMResourceGroup_withTags, ri) 56 postConfig := fmt.Sprintf(testAccAzureRMResourceGroup_withTagsUpdated, ri) 57 58 resource.Test(t, resource.TestCase{ 59 PreCheck: func() { testAccPreCheck(t) }, 60 Providers: testAccProviders, 61 CheckDestroy: testCheckAzureRMResourceGroupDestroy, 62 Steps: []resource.TestStep{ 63 resource.TestStep{ 64 Config: preConfig, 65 Check: resource.ComposeTestCheckFunc( 66 testCheckAzureRMResourceGroupExists("azurerm_resource_group.test"), 67 resource.TestCheckResourceAttr( 68 "azurerm_resource_group.test", "tags.%", "2"), 69 resource.TestCheckResourceAttr( 70 "azurerm_resource_group.test", "tags.environment", "Production"), 71 resource.TestCheckResourceAttr( 72 "azurerm_resource_group.test", "tags.cost_center", "MSFT"), 73 ), 74 }, 75 76 resource.TestStep{ 77 Config: postConfig, 78 Check: resource.ComposeTestCheckFunc( 79 testCheckAzureRMResourceGroupExists("azurerm_resource_group.test"), 80 resource.TestCheckResourceAttr( 81 "azurerm_resource_group.test", "tags.%", "1"), 82 resource.TestCheckResourceAttr( 83 "azurerm_resource_group.test", "tags.environment", "staging"), 84 ), 85 }, 86 }, 87 }) 88 } 89 90 func testCheckAzureRMResourceGroupExists(name string) resource.TestCheckFunc { 91 return func(s *terraform.State) error { 92 // Ensure we have enough information in state to look up in API 93 rs, ok := s.RootModule().Resources[name] 94 if !ok { 95 return fmt.Errorf("Not found: %s", name) 96 } 97 98 resourceGroup := rs.Primary.Attributes["name"] 99 100 // Ensure resource group exists in API 101 conn := testAccProvider.Meta().(*ArmClient).resourceGroupClient 102 103 resp, err := conn.Get(resourceGroup) 104 if err != nil { 105 return fmt.Errorf("Bad: Get on resourceGroupClient: %s", err) 106 } 107 108 if resp.StatusCode == http.StatusNotFound { 109 return fmt.Errorf("Bad: Virtual Network %q (resource group: %q) does not exist", name, resourceGroup) 110 } 111 112 return nil 113 } 114 } 115 116 func testCheckAzureRMResourceGroupDisappears(name string) resource.TestCheckFunc { 117 return func(s *terraform.State) error { 118 // Ensure we have enough information in state to look up in API 119 rs, ok := s.RootModule().Resources[name] 120 if !ok { 121 return fmt.Errorf("Not found: %s", name) 122 } 123 124 resourceGroup := rs.Primary.Attributes["name"] 125 126 // Ensure resource group exists in API 127 conn := testAccProvider.Meta().(*ArmClient).resourceGroupClient 128 129 _, err := conn.Delete(resourceGroup, make(chan struct{})) 130 if err != nil { 131 return fmt.Errorf("Bad: Delete on resourceGroupClient: %s", err) 132 } 133 134 return nil 135 } 136 } 137 138 func testCheckAzureRMResourceGroupDestroy(s *terraform.State) error { 139 conn := testAccProvider.Meta().(*ArmClient).resourceGroupClient 140 141 for _, rs := range s.RootModule().Resources { 142 if rs.Type != "azurerm_resource_group" { 143 continue 144 } 145 146 resourceGroup := rs.Primary.ID 147 148 resp, err := conn.Get(resourceGroup) 149 if err != nil { 150 return nil 151 } 152 153 if resp.StatusCode != http.StatusNotFound { 154 return fmt.Errorf("Resource Group still exists:\n%#v", resp.Properties) 155 } 156 } 157 158 return nil 159 } 160 161 var testAccAzureRMResourceGroup_basic = ` 162 resource "azurerm_resource_group" "test" { 163 name = "acctestRG-%d" 164 location = "West US" 165 } 166 ` 167 168 var testAccAzureRMResourceGroup_withTags = ` 169 resource "azurerm_resource_group" "test" { 170 name = "acctestRG-%d" 171 location = "West US" 172 173 tags { 174 environment = "Production" 175 cost_center = "MSFT" 176 } 177 } 178 ` 179 180 var testAccAzureRMResourceGroup_withTagsUpdated = ` 181 resource "azurerm_resource_group" "test" { 182 name = "acctestRG-%d" 183 location = "West US" 184 185 tags { 186 environment = "staging" 187 } 188 } 189 `