github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/resource_arm_container_registry_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 TestAccAzureRMContainerRegistryName_validation(t *testing.T) { 14 cases := []struct { 15 Value string 16 ErrCount int 17 }{ 18 { 19 Value: "four", 20 ErrCount: 1, 21 }, 22 { 23 Value: "5five", 24 ErrCount: 0, 25 }, 26 { 27 Value: "hello-world", 28 ErrCount: 1, 29 }, 30 { 31 Value: "hello_world", 32 ErrCount: 1, 33 }, 34 { 35 Value: "helloWorld", 36 ErrCount: 0, 37 }, 38 { 39 Value: "helloworld12", 40 ErrCount: 0, 41 }, 42 { 43 Value: "hello@world", 44 ErrCount: 1, 45 }, 46 { 47 Value: "qfvbdsbvipqdbwsbddbdcwqffewsqwcdw21ddwqwd3324120", 48 ErrCount: 0, 49 }, 50 { 51 Value: "qfvbdsbvipqdbwsbddbdcwqffewsqwcdw21ddwqwd33241202", 52 ErrCount: 0, 53 }, 54 { 55 Value: "qfvbdsbvipqdbwsbddbdcwqfjjfewsqwcdw21ddwqwd3324120", 56 ErrCount: 1, 57 }, 58 } 59 60 for _, tc := range cases { 61 _, errors := validateAzureRMContainerRegistryName(tc.Value, "azurerm_container_registry") 62 63 if len(errors) != tc.ErrCount { 64 t.Fatalf("Expected the Azure RM Container Registry Name to trigger a validation error: %v", errors) 65 } 66 } 67 } 68 69 func TestAccAzureRMContainerRegistry_basic(t *testing.T) { 70 ri := acctest.RandInt() 71 rs := acctest.RandString(4) 72 config := testAccAzureRMContainerRegistry_basic(ri, rs) 73 74 resource.Test(t, resource.TestCase{ 75 PreCheck: func() { testAccPreCheck(t) }, 76 Providers: testAccProviders, 77 CheckDestroy: testCheckAzureRMContainerRegistryDestroy, 78 Steps: []resource.TestStep{ 79 { 80 Config: config, 81 Check: resource.ComposeTestCheckFunc( 82 testCheckAzureRMContainerRegistryExists("azurerm_container_registry.test"), 83 ), 84 }, 85 }, 86 }) 87 } 88 89 func TestAccAzureRMContainerRegistry_complete(t *testing.T) { 90 ri := acctest.RandInt() 91 rs := acctest.RandString(4) 92 config := testAccAzureRMContainerRegistry_complete(ri, rs) 93 94 resource.Test(t, resource.TestCase{ 95 PreCheck: func() { testAccPreCheck(t) }, 96 Providers: testAccProviders, 97 CheckDestroy: testCheckAzureRMContainerRegistryDestroy, 98 Steps: []resource.TestStep{ 99 { 100 Config: config, 101 Check: resource.ComposeTestCheckFunc( 102 testCheckAzureRMContainerRegistryExists("azurerm_container_registry.test"), 103 ), 104 }, 105 }, 106 }) 107 } 108 109 func TestAccAzureRMContainerRegistry_update(t *testing.T) { 110 ri := acctest.RandInt() 111 rs := acctest.RandString(4) 112 config := testAccAzureRMContainerRegistry_complete(ri, rs) 113 updatedConfig := testAccAzureRMContainerRegistry_completeUpdated(ri, rs) 114 115 resource.Test(t, resource.TestCase{ 116 PreCheck: func() { testAccPreCheck(t) }, 117 Providers: testAccProviders, 118 CheckDestroy: testCheckAzureRMContainerRegistryDestroy, 119 Steps: []resource.TestStep{ 120 { 121 Config: config, 122 Check: resource.ComposeTestCheckFunc( 123 testCheckAzureRMContainerRegistryExists("azurerm_container_registry.test"), 124 ), 125 }, 126 { 127 Config: updatedConfig, 128 Check: resource.ComposeTestCheckFunc( 129 testCheckAzureRMContainerRegistryExists("azurerm_container_registry.test"), 130 ), 131 }, 132 }, 133 }) 134 } 135 136 func testCheckAzureRMContainerRegistryDestroy(s *terraform.State) error { 137 conn := testAccProvider.Meta().(*ArmClient).containerRegistryClient 138 139 for _, rs := range s.RootModule().Resources { 140 if rs.Type != "azurerm_container_registry" { 141 continue 142 } 143 144 name := rs.Primary.Attributes["name"] 145 resourceGroup := rs.Primary.Attributes["resource_group_name"] 146 147 resp, err := conn.Get(resourceGroup, name) 148 149 if err != nil { 150 return nil 151 } 152 153 if resp.StatusCode != http.StatusNotFound { 154 return fmt.Errorf("Container Registry still exists:\n%#v", resp) 155 } 156 } 157 158 return nil 159 } 160 161 func testCheckAzureRMContainerRegistryExists(name string) resource.TestCheckFunc { 162 return func(s *terraform.State) error { 163 // Ensure we have enough information in state to look up in API 164 rs, ok := s.RootModule().Resources[name] 165 if !ok { 166 return fmt.Errorf("Not found: %s", name) 167 } 168 169 name := rs.Primary.Attributes["name"] 170 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 171 if !hasResourceGroup { 172 return fmt.Errorf("Bad: no resource group found in state for Container Registry: %s", name) 173 } 174 175 conn := testAccProvider.Meta().(*ArmClient).containerRegistryClient 176 177 resp, err := conn.Get(resourceGroup, name) 178 if err != nil { 179 return fmt.Errorf("Bad: Get on containerRegistryClient: %s", err) 180 } 181 182 if resp.StatusCode == http.StatusNotFound { 183 return fmt.Errorf("Bad: Container Registry %q (resource group: %q) does not exist", name, resourceGroup) 184 } 185 186 return nil 187 } 188 } 189 190 func testAccAzureRMContainerRegistry_basic(rInt int, rStr string) string { 191 return fmt.Sprintf(` 192 resource "azurerm_resource_group" "test" { 193 name = "testAccRg-%d" 194 location = "West US" 195 } 196 197 resource "azurerm_storage_account" "test" { 198 name = "testaccsa%s" 199 resource_group_name = "${azurerm_resource_group.test.name}" 200 location = "${azurerm_resource_group.test.location}" 201 account_type = "Standard_LRS" 202 } 203 204 resource "azurerm_container_registry" "test" { 205 name = "testacccr%d" 206 resource_group_name = "${azurerm_resource_group.test.name}" 207 location = "${azurerm_resource_group.test.location}" 208 sku = "Basic" 209 210 storage_account { 211 name = "${azurerm_storage_account.test.name}" 212 access_key = "${azurerm_storage_account.test.primary_access_key}" 213 } 214 } 215 `, rInt, rStr, rInt) 216 } 217 218 func testAccAzureRMContainerRegistry_complete(rInt int, rStr string) string { 219 return fmt.Sprintf(` 220 resource "azurerm_resource_group" "test" { 221 name = "testAccRg-%d" 222 location = "West US" 223 } 224 225 resource "azurerm_storage_account" "test" { 226 name = "testaccsa%s" 227 resource_group_name = "${azurerm_resource_group.test.name}" 228 location = "${azurerm_resource_group.test.location}" 229 account_type = "Standard_LRS" 230 } 231 232 resource "azurerm_container_registry" "test" { 233 name = "testacccr%d" 234 resource_group_name = "${azurerm_resource_group.test.name}" 235 location = "${azurerm_resource_group.test.location}" 236 admin_enabled = false 237 sku = "Basic" 238 239 storage_account { 240 name = "${azurerm_storage_account.test.name}" 241 access_key = "${azurerm_storage_account.test.primary_access_key}" 242 } 243 244 tags { 245 environment = "production" 246 } 247 } 248 `, rInt, rStr, rInt) 249 } 250 251 func testAccAzureRMContainerRegistry_completeUpdated(rInt int, rStr string) string { 252 return fmt.Sprintf(` 253 resource "azurerm_resource_group" "test" { 254 name = "testAccRg-%d" 255 location = "West US" 256 } 257 258 resource "azurerm_storage_account" "test" { 259 name = "testaccsa%s" 260 resource_group_name = "${azurerm_resource_group.test.name}" 261 location = "${azurerm_resource_group.test.location}" 262 account_type = "Standard_LRS" 263 } 264 265 resource "azurerm_container_registry" "test" { 266 name = "testacccr%d" 267 resource_group_name = "${azurerm_resource_group.test.name}" 268 location = "${azurerm_resource_group.test.location}" 269 admin_enabled = true 270 sku = "Basic" 271 272 storage_account { 273 name = "${azurerm_storage_account.test.name}" 274 access_key = "${azurerm_storage_account.test.primary_access_key}" 275 } 276 277 tags { 278 environment = "production" 279 } 280 } 281 `, rInt, rStr, rInt) 282 }