github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/azurerm/resource_arm_public_ip_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 TestResourceAzureRMPublicIpAllocation_validation(t *testing.T) { 14 cases := []struct { 15 Value string 16 ErrCount int 17 }{ 18 { 19 Value: "Random", 20 ErrCount: 1, 21 }, 22 { 23 Value: "Static", 24 ErrCount: 0, 25 }, 26 { 27 Value: "Dynamic", 28 ErrCount: 0, 29 }, 30 { 31 Value: "STATIC", 32 ErrCount: 0, 33 }, 34 { 35 Value: "static", 36 ErrCount: 0, 37 }, 38 } 39 40 for _, tc := range cases { 41 _, errors := validatePublicIpAllocation(tc.Value, "azurerm_public_ip") 42 43 if len(errors) != tc.ErrCount { 44 t.Fatalf("Expected the Azure RM Public IP allocation to trigger a validation error") 45 } 46 } 47 } 48 49 func TestResourceAzureRMPublicIpDomainNameLabel_validation(t *testing.T) { 50 cases := []struct { 51 Value string 52 ErrCount int 53 }{ 54 { 55 Value: "tEsting123", 56 ErrCount: 1, 57 }, 58 { 59 Value: "testing123!", 60 ErrCount: 1, 61 }, 62 { 63 Value: "testing123-", 64 ErrCount: 1, 65 }, 66 { 67 Value: acctest.RandString(80), 68 ErrCount: 1, 69 }, 70 } 71 72 for _, tc := range cases { 73 _, errors := validatePublicIpDomainNameLabel(tc.Value, "azurerm_public_ip") 74 75 if len(errors) != tc.ErrCount { 76 t.Fatalf("Expected the Azure RM Public IP Domain Name Label to trigger a validation error") 77 } 78 } 79 } 80 81 func TestAccAzureRMPublicIpStatic_basic(t *testing.T) { 82 83 ri := acctest.RandInt() 84 config := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri) 85 86 resource.Test(t, resource.TestCase{ 87 PreCheck: func() { testAccPreCheck(t) }, 88 Providers: testAccProviders, 89 CheckDestroy: testCheckAzureRMPublicIpDestroy, 90 Steps: []resource.TestStep{ 91 resource.TestStep{ 92 Config: config, 93 Check: resource.ComposeTestCheckFunc( 94 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 95 ), 96 }, 97 }, 98 }) 99 } 100 101 func TestAccAzureRMPublicIpStatic_withTags(t *testing.T) { 102 103 ri := acctest.RandInt() 104 preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTags, ri, ri) 105 postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTagsUpdate, ri, ri) 106 107 resource.Test(t, resource.TestCase{ 108 PreCheck: func() { testAccPreCheck(t) }, 109 Providers: testAccProviders, 110 CheckDestroy: testCheckAzureRMPublicIpDestroy, 111 Steps: []resource.TestStep{ 112 resource.TestStep{ 113 Config: preConfig, 114 Check: resource.ComposeTestCheckFunc( 115 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 116 resource.TestCheckResourceAttr( 117 "azurerm_public_ip.test", "tags.#", "2"), 118 resource.TestCheckResourceAttr( 119 "azurerm_public_ip.test", "tags.environment", "Production"), 120 resource.TestCheckResourceAttr( 121 "azurerm_public_ip.test", "tags.cost_center", "MSFT"), 122 ), 123 }, 124 125 resource.TestStep{ 126 Config: postConfig, 127 Check: resource.ComposeTestCheckFunc( 128 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 129 resource.TestCheckResourceAttr( 130 "azurerm_public_ip.test", "tags.#", "1"), 131 resource.TestCheckResourceAttr( 132 "azurerm_public_ip.test", "tags.environment", "staging"), 133 ), 134 }, 135 }, 136 }) 137 } 138 139 func TestAccAzureRMPublicIpStatic_update(t *testing.T) { 140 141 ri := acctest.RandInt() 142 preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri) 143 postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_update, ri, ri) 144 145 resource.Test(t, resource.TestCase{ 146 PreCheck: func() { testAccPreCheck(t) }, 147 Providers: testAccProviders, 148 CheckDestroy: testCheckAzureRMPublicIpDestroy, 149 Steps: []resource.TestStep{ 150 resource.TestStep{ 151 Config: preConfig, 152 Check: resource.ComposeTestCheckFunc( 153 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 154 ), 155 }, 156 157 resource.TestStep{ 158 Config: postConfig, 159 Check: resource.ComposeTestCheckFunc( 160 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 161 resource.TestCheckResourceAttr( 162 "azurerm_public_ip.test", "domain_name_label", "mylabel01"), 163 ), 164 }, 165 }, 166 }) 167 } 168 169 func TestAccAzureRMPublicIpDynamic_basic(t *testing.T) { 170 171 ri := acctest.RandInt() 172 config := fmt.Sprintf(testAccAzureRMVPublicIpDynamic_basic, ri, ri) 173 174 resource.Test(t, resource.TestCase{ 175 PreCheck: func() { testAccPreCheck(t) }, 176 Providers: testAccProviders, 177 CheckDestroy: testCheckAzureRMPublicIpDestroy, 178 Steps: []resource.TestStep{ 179 resource.TestStep{ 180 Config: config, 181 Check: resource.ComposeTestCheckFunc( 182 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 183 ), 184 }, 185 }, 186 }) 187 } 188 189 func testCheckAzureRMPublicIpExists(name string) resource.TestCheckFunc { 190 return func(s *terraform.State) error { 191 // Ensure we have enough information in state to look up in API 192 rs, ok := s.RootModule().Resources[name] 193 if !ok { 194 return fmt.Errorf("Not found: %s", name) 195 } 196 197 availSetName := rs.Primary.Attributes["name"] 198 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 199 if !hasResourceGroup { 200 return fmt.Errorf("Bad: no resource group found in state for public ip: %s", availSetName) 201 } 202 203 conn := testAccProvider.Meta().(*ArmClient).publicIPClient 204 205 resp, err := conn.Get(resourceGroup, availSetName, "") 206 if err != nil { 207 return fmt.Errorf("Bad: Get on publicIPClient: %s", err) 208 } 209 210 if resp.StatusCode == http.StatusNotFound { 211 return fmt.Errorf("Bad: Public IP %q (resource group: %q) does not exist", name, resourceGroup) 212 } 213 214 return nil 215 } 216 } 217 218 func testCheckAzureRMPublicIpDestroy(s *terraform.State) error { 219 conn := testAccProvider.Meta().(*ArmClient).publicIPClient 220 221 for _, rs := range s.RootModule().Resources { 222 if rs.Type != "azurerm_public_ip" { 223 continue 224 } 225 226 name := rs.Primary.Attributes["name"] 227 resourceGroup := rs.Primary.Attributes["resource_group_name"] 228 229 resp, err := conn.Get(resourceGroup, name, "") 230 231 if err != nil { 232 return nil 233 } 234 235 if resp.StatusCode != http.StatusNotFound { 236 return fmt.Errorf("Public IP still exists:\n%#v", resp.Properties) 237 } 238 } 239 240 return nil 241 } 242 243 var testAccAzureRMVPublicIpStatic_basic = ` 244 resource "azurerm_resource_group" "test" { 245 name = "acctestrg-%d" 246 location = "West US" 247 } 248 resource "azurerm_public_ip" "test" { 249 name = "acctestpublicip-%d" 250 location = "West US" 251 resource_group_name = "${azurerm_resource_group.test.name}" 252 public_ip_address_allocation = "static" 253 } 254 ` 255 256 var testAccAzureRMVPublicIpStatic_update = ` 257 resource "azurerm_resource_group" "test" { 258 name = "acctestrg-%d" 259 location = "West US" 260 } 261 resource "azurerm_public_ip" "test" { 262 name = "acctestpublicip-%d" 263 location = "West US" 264 resource_group_name = "${azurerm_resource_group.test.name}" 265 public_ip_address_allocation = "static" 266 domain_name_label = "mylabel01" 267 } 268 ` 269 270 var testAccAzureRMVPublicIpDynamic_basic = ` 271 resource "azurerm_resource_group" "test" { 272 name = "acctestrg-%d" 273 location = "West US" 274 } 275 resource "azurerm_public_ip" "test" { 276 name = "acctestpublicip-%d" 277 location = "West US" 278 resource_group_name = "${azurerm_resource_group.test.name}" 279 public_ip_address_allocation = "dynamic" 280 } 281 ` 282 283 var testAccAzureRMVPublicIpStatic_withTags = ` 284 resource "azurerm_resource_group" "test" { 285 name = "acctestrg-%d" 286 location = "West US" 287 } 288 resource "azurerm_public_ip" "test" { 289 name = "acctestpublicip-%d" 290 location = "West US" 291 resource_group_name = "${azurerm_resource_group.test.name}" 292 public_ip_address_allocation = "static" 293 294 tags { 295 environment = "Production" 296 cost_center = "MSFT" 297 } 298 } 299 ` 300 301 var testAccAzureRMVPublicIpStatic_withTagsUpdate = ` 302 resource "azurerm_resource_group" "test" { 303 name = "acctestrg-%d" 304 location = "West US" 305 } 306 resource "azurerm_public_ip" "test" { 307 name = "acctestpublicip-%d" 308 location = "West US" 309 resource_group_name = "${azurerm_resource_group.test.name}" 310 public_ip_address_allocation = "static" 311 312 tags { 313 environment = "staging" 314 } 315 } 316 `