terraform/builtin/providers/azurerm/resource_arm_cdn_profile_te...

200 lines
4.8 KiB
Go

package azurerm
import (
"fmt"
"net/http"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestResourceAzureRMCdnProfileSKU_validation(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "Random",
ErrCount: 1,
},
{
Value: "Standard",
ErrCount: 0,
},
{
Value: "Premium",
ErrCount: 0,
},
{
Value: "STANDARD",
ErrCount: 0,
},
{
Value: "PREMIUM",
ErrCount: 0,
},
}
for _, tc := range cases {
_, errors := validateCdnProfileSku(tc.Value, "azurerm_cdn_profile")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected the Azure RM CDN Profile SKU to trigger a validation error")
}
}
}
func TestAccAzureRMCdnProfile_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMCdnProfileDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMCdnProfile_basic,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"),
),
},
},
})
}
func TestAccAzureRMCdnProfile_withTags(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMCdnProfileDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMCdnProfile_withTags,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"),
resource.TestCheckResourceAttr(
"azurerm_cdn_profile.test", "tags.#", "2"),
resource.TestCheckResourceAttr(
"azurerm_cdn_profile.test", "tags.environment", "Production"),
resource.TestCheckResourceAttr(
"azurerm_cdn_profile.test", "tags.cost_center", "MSFT"),
),
},
resource.TestStep{
Config: testAccAzureRMCdnProfile_withTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"),
resource.TestCheckResourceAttr(
"azurerm_cdn_profile.test", "tags.#", "1"),
resource.TestCheckResourceAttr(
"azurerm_cdn_profile.test", "tags.environment", "staging"),
),
},
},
})
}
func testCheckAzureRMCdnProfileExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
name := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for availability set: %s", name)
}
conn := testAccProvider.Meta().(*ArmClient).cdnProfilesClient
resp, err := conn.Get(name, resourceGroup)
if err != nil {
return fmt.Errorf("Bad: Get on cdnProfilesClient: %s", err)
}
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Bad: CDN Profile %q (resource group: %q) does not exist", name, resourceGroup)
}
return nil
}
}
func testCheckAzureRMCdnProfileDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).cdnProfilesClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_cdn_profile" {
continue
}
name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
resp, err := conn.Get(name, resourceGroup)
if err != nil {
return nil
}
if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("CDN Profile still exists:\n%#v", resp.Properties)
}
}
return nil
}
var testAccAzureRMCdnProfile_basic = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_cdn_profile" "test" {
name = "acceptanceTestCdnProfile1"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Standard"
}
`
var testAccAzureRMCdnProfile_withTags = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_cdn_profile" "test" {
name = "acceptanceTestCdnProfile1"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Standard"
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMCdnProfile_withTagsUpdate = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_cdn_profile" "test" {
name = "acceptanceTestCdnProfile1"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Standard"
tags {
environment = "staging"
}
}
`