github.com/jonasi/terraform@v0.6.10-0.20160125170522-e865c342cc1f/builtin/providers/azurerm/resource_arm_cdn_profile_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestResourceAzureRMCdnProfileSKU_validation(t *testing.T) { 13 cases := []struct { 14 Value string 15 ErrCount int 16 }{ 17 { 18 Value: "Random", 19 ErrCount: 1, 20 }, 21 { 22 Value: "Standard", 23 ErrCount: 0, 24 }, 25 { 26 Value: "Premium", 27 ErrCount: 0, 28 }, 29 { 30 Value: "STANDARD", 31 ErrCount: 0, 32 }, 33 { 34 Value: "PREMIUM", 35 ErrCount: 0, 36 }, 37 } 38 39 for _, tc := range cases { 40 _, errors := validateCdnProfileSku(tc.Value, "azurerm_cdn_profile") 41 42 if len(errors) != tc.ErrCount { 43 t.Fatalf("Expected the Azure RM CDN Profile SKU to trigger a validation error") 44 } 45 } 46 } 47 48 func TestAccAzureRMCdnProfile_basic(t *testing.T) { 49 50 resource.Test(t, resource.TestCase{ 51 PreCheck: func() { testAccPreCheck(t) }, 52 Providers: testAccProviders, 53 CheckDestroy: testCheckAzureRMCdnProfileDestroy, 54 Steps: []resource.TestStep{ 55 resource.TestStep{ 56 Config: testAccAzureRMCdnProfile_basic, 57 Check: resource.ComposeTestCheckFunc( 58 testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"), 59 ), 60 }, 61 }, 62 }) 63 } 64 65 func TestAccAzureRMCdnProfile_withTags(t *testing.T) { 66 67 resource.Test(t, resource.TestCase{ 68 PreCheck: func() { testAccPreCheck(t) }, 69 Providers: testAccProviders, 70 CheckDestroy: testCheckAzureRMCdnProfileDestroy, 71 Steps: []resource.TestStep{ 72 resource.TestStep{ 73 Config: testAccAzureRMCdnProfile_withTags, 74 Check: resource.ComposeTestCheckFunc( 75 testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"), 76 resource.TestCheckResourceAttr( 77 "azurerm_cdn_profile.test", "tags.#", "2"), 78 resource.TestCheckResourceAttr( 79 "azurerm_cdn_profile.test", "tags.environment", "Production"), 80 resource.TestCheckResourceAttr( 81 "azurerm_cdn_profile.test", "tags.cost_center", "MSFT"), 82 ), 83 }, 84 85 resource.TestStep{ 86 Config: testAccAzureRMCdnProfile_withTagsUpdate, 87 Check: resource.ComposeTestCheckFunc( 88 testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"), 89 resource.TestCheckResourceAttr( 90 "azurerm_cdn_profile.test", "tags.#", "1"), 91 resource.TestCheckResourceAttr( 92 "azurerm_cdn_profile.test", "tags.environment", "staging"), 93 ), 94 }, 95 }, 96 }) 97 } 98 99 func testCheckAzureRMCdnProfileExists(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 name := 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 cdn profile: %s", name) 111 } 112 113 conn := testAccProvider.Meta().(*ArmClient).cdnProfilesClient 114 115 resp, err := conn.Get(name, resourceGroup) 116 if err != nil { 117 return fmt.Errorf("Bad: Get on cdnProfilesClient: %s", err) 118 } 119 120 if resp.StatusCode == http.StatusNotFound { 121 return fmt.Errorf("Bad: CDN Profile %q (resource group: %q) does not exist", name, resourceGroup) 122 } 123 124 return nil 125 } 126 } 127 128 func testCheckAzureRMCdnProfileDestroy(s *terraform.State) error { 129 conn := testAccProvider.Meta().(*ArmClient).cdnProfilesClient 130 131 for _, rs := range s.RootModule().Resources { 132 if rs.Type != "azurerm_cdn_profile" { 133 continue 134 } 135 136 name := rs.Primary.Attributes["name"] 137 resourceGroup := rs.Primary.Attributes["resource_group_name"] 138 139 resp, err := conn.Get(name, resourceGroup) 140 141 if err != nil { 142 return nil 143 } 144 145 if resp.StatusCode != http.StatusNotFound { 146 return fmt.Errorf("CDN Profile still exists:\n%#v", resp.Properties) 147 } 148 } 149 150 return nil 151 } 152 153 var testAccAzureRMCdnProfile_basic = ` 154 resource "azurerm_resource_group" "test" { 155 name = "acceptanceTestResourceGroup1" 156 location = "West US" 157 } 158 resource "azurerm_cdn_profile" "test" { 159 name = "acceptanceTestCdnProfile1" 160 location = "West US" 161 resource_group_name = "${azurerm_resource_group.test.name}" 162 sku = "Standard" 163 } 164 ` 165 166 var testAccAzureRMCdnProfile_withTags = ` 167 resource "azurerm_resource_group" "test" { 168 name = "acceptanceTestResourceGroup1" 169 location = "West US" 170 } 171 resource "azurerm_cdn_profile" "test" { 172 name = "acceptanceTestCdnProfile1" 173 location = "West US" 174 resource_group_name = "${azurerm_resource_group.test.name}" 175 sku = "Standard" 176 177 tags { 178 environment = "Production" 179 cost_center = "MSFT" 180 } 181 } 182 ` 183 184 var testAccAzureRMCdnProfile_withTagsUpdate = ` 185 resource "azurerm_resource_group" "test" { 186 name = "acceptanceTestResourceGroup1" 187 location = "West US" 188 } 189 resource "azurerm_cdn_profile" "test" { 190 name = "acceptanceTestCdnProfile1" 191 location = "West US" 192 resource_group_name = "${azurerm_resource_group.test.name}" 193 sku = "Standard" 194 195 tags { 196 environment = "staging" 197 } 198 } 199 `