github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestResourceAzureRMCdnProfileSKU_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: "Standard_Verizon", 24 ErrCount: 0, 25 }, 26 { 27 Value: "Premium_Verizon", 28 ErrCount: 0, 29 }, 30 { 31 Value: "Standard_Akamai", 32 ErrCount: 0, 33 }, 34 { 35 Value: "STANDARD_AKAMAI", 36 ErrCount: 0, 37 }, 38 { 39 Value: "standard_akamai", 40 ErrCount: 0, 41 }, 42 } 43 44 for _, tc := range cases { 45 _, errors := validateCdnProfileSku(tc.Value, "azurerm_cdn_profile") 46 47 if len(errors) != tc.ErrCount { 48 t.Fatalf("Expected the Azure RM CDN Profile SKU to trigger a validation error") 49 } 50 } 51 } 52 53 func TestAccAzureRMCdnProfile_basic(t *testing.T) { 54 55 ri := acctest.RandInt() 56 config := fmt.Sprintf(testAccAzureRMCdnProfile_basic, ri, ri) 57 58 resource.Test(t, resource.TestCase{ 59 PreCheck: func() { testAccPreCheck(t) }, 60 Providers: testAccProviders, 61 CheckDestroy: testCheckAzureRMCdnProfileDestroy, 62 Steps: []resource.TestStep{ 63 { 64 Config: config, 65 Check: resource.ComposeTestCheckFunc( 66 testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"), 67 ), 68 }, 69 }, 70 }) 71 } 72 73 func TestAccAzureRMCdnProfile_withTags(t *testing.T) { 74 75 ri := acctest.RandInt() 76 preConfig := fmt.Sprintf(testAccAzureRMCdnProfile_withTags, ri, ri) 77 postConfig := fmt.Sprintf(testAccAzureRMCdnProfile_withTagsUpdate, ri, ri) 78 79 resource.Test(t, resource.TestCase{ 80 PreCheck: func() { testAccPreCheck(t) }, 81 Providers: testAccProviders, 82 CheckDestroy: testCheckAzureRMCdnProfileDestroy, 83 Steps: []resource.TestStep{ 84 { 85 Config: preConfig, 86 Check: resource.ComposeTestCheckFunc( 87 testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"), 88 resource.TestCheckResourceAttr( 89 "azurerm_cdn_profile.test", "tags.%", "2"), 90 resource.TestCheckResourceAttr( 91 "azurerm_cdn_profile.test", "tags.environment", "Production"), 92 resource.TestCheckResourceAttr( 93 "azurerm_cdn_profile.test", "tags.cost_center", "MSFT"), 94 ), 95 }, 96 97 { 98 Config: postConfig, 99 Check: resource.ComposeTestCheckFunc( 100 testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"), 101 resource.TestCheckResourceAttr( 102 "azurerm_cdn_profile.test", "tags.%", "1"), 103 resource.TestCheckResourceAttr( 104 "azurerm_cdn_profile.test", "tags.environment", "staging"), 105 ), 106 }, 107 }, 108 }) 109 } 110 111 func TestAccAzureRMCdnProfile_NonStandardCasing(t *testing.T) { 112 113 ri := acctest.RandInt() 114 config := testAccAzureRMCdnProfileNonStandardCasing(ri) 115 116 resource.Test(t, resource.TestCase{ 117 PreCheck: func() { testAccPreCheck(t) }, 118 Providers: testAccProviders, 119 CheckDestroy: testCheckAzureRMCdnProfileDestroy, 120 Steps: []resource.TestStep{ 121 resource.TestStep{ 122 Config: config, 123 Check: resource.ComposeTestCheckFunc( 124 testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"), 125 ), 126 }, 127 128 resource.TestStep{ 129 Config: config, 130 PlanOnly: true, 131 ExpectNonEmptyPlan: false, 132 }, 133 }, 134 }) 135 } 136 137 func testCheckAzureRMCdnProfileExists(name string) resource.TestCheckFunc { 138 return func(s *terraform.State) error { 139 // Ensure we have enough information in state to look up in API 140 rs, ok := s.RootModule().Resources[name] 141 if !ok { 142 return fmt.Errorf("Not found: %s", name) 143 } 144 145 name := rs.Primary.Attributes["name"] 146 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 147 if !hasResourceGroup { 148 return fmt.Errorf("Bad: no resource group found in state for cdn profile: %s", name) 149 } 150 151 conn := testAccProvider.Meta().(*ArmClient).cdnProfilesClient 152 153 resp, err := conn.Get(resourceGroup, name) 154 if err != nil { 155 return fmt.Errorf("Bad: Get on cdnProfilesClient: %s", err) 156 } 157 158 if resp.StatusCode == http.StatusNotFound { 159 return fmt.Errorf("Bad: CDN Profile %q (resource group: %q) does not exist", name, resourceGroup) 160 } 161 162 return nil 163 } 164 } 165 166 func testCheckAzureRMCdnProfileDestroy(s *terraform.State) error { 167 conn := testAccProvider.Meta().(*ArmClient).cdnProfilesClient 168 169 for _, rs := range s.RootModule().Resources { 170 if rs.Type != "azurerm_cdn_profile" { 171 continue 172 } 173 174 name := rs.Primary.Attributes["name"] 175 resourceGroup := rs.Primary.Attributes["resource_group_name"] 176 177 resp, err := conn.Get(resourceGroup, name) 178 179 if err != nil { 180 return nil 181 } 182 183 if resp.StatusCode != http.StatusNotFound { 184 return fmt.Errorf("CDN Profile still exists:\n%#v", resp.ProfileProperties) 185 } 186 } 187 188 return nil 189 } 190 191 var testAccAzureRMCdnProfile_basic = ` 192 resource "azurerm_resource_group" "test" { 193 name = "acctestRG-%d" 194 location = "West US" 195 } 196 resource "azurerm_cdn_profile" "test" { 197 name = "acctestcdnprof%d" 198 location = "West US" 199 resource_group_name = "${azurerm_resource_group.test.name}" 200 sku = "Standard_Verizon" 201 } 202 ` 203 204 var testAccAzureRMCdnProfile_withTags = ` 205 resource "azurerm_resource_group" "test" { 206 name = "acctestRG-%d" 207 location = "West US" 208 } 209 resource "azurerm_cdn_profile" "test" { 210 name = "acctestcdnprof%d" 211 location = "West US" 212 resource_group_name = "${azurerm_resource_group.test.name}" 213 sku = "Standard_Verizon" 214 215 tags { 216 environment = "Production" 217 cost_center = "MSFT" 218 } 219 } 220 ` 221 222 var testAccAzureRMCdnProfile_withTagsUpdate = ` 223 resource "azurerm_resource_group" "test" { 224 name = "acctestRG-%d" 225 location = "West US" 226 } 227 resource "azurerm_cdn_profile" "test" { 228 name = "acctestcdnprof%d" 229 location = "West US" 230 resource_group_name = "${azurerm_resource_group.test.name}" 231 sku = "Standard_Verizon" 232 233 tags { 234 environment = "staging" 235 } 236 } 237 ` 238 239 func testAccAzureRMCdnProfileNonStandardCasing(ri int) string { 240 return fmt.Sprintf(` 241 resource "azurerm_resource_group" "test" { 242 name = "acctestRG-%d" 243 location = "West US" 244 } 245 resource "azurerm_cdn_profile" "test" { 246 name = "acctestcdnprof%d" 247 location = "West US" 248 resource_group_name = "${azurerm_resource_group.test.name}" 249 sku = "standard_verizon" 250 } 251 `, ri, ri) 252 }