github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_route_table_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 TestResourceAzureRMRouteTableNextHopType_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: "VirtualNetworkGateway", 24 ErrCount: 0, 25 }, 26 { 27 Value: "VNETLocal", 28 ErrCount: 0, 29 }, 30 { 31 Value: "Internet", 32 ErrCount: 0, 33 }, 34 { 35 Value: "VirtualAppliance", 36 ErrCount: 0, 37 }, 38 { 39 Value: "None", 40 ErrCount: 0, 41 }, 42 { 43 Value: "VIRTUALNETWORKGATEWAY", 44 ErrCount: 0, 45 }, 46 { 47 Value: "virtualnetworkgateway", 48 ErrCount: 0, 49 }, 50 } 51 52 for _, tc := range cases { 53 _, errors := validateRouteTableNextHopType(tc.Value, "azurerm_route_table") 54 55 if len(errors) != tc.ErrCount { 56 t.Fatalf("Expected the Azure RM Route Table nextHopType to trigger a validation error") 57 } 58 } 59 } 60 61 func TestAccAzureRMRouteTable_basic(t *testing.T) { 62 63 ri := acctest.RandInt() 64 config := fmt.Sprintf(testAccAzureRMRouteTable_basic, ri, ri) 65 66 resource.Test(t, resource.TestCase{ 67 PreCheck: func() { testAccPreCheck(t) }, 68 Providers: testAccProviders, 69 CheckDestroy: testCheckAzureRMRouteTableDestroy, 70 Steps: []resource.TestStep{ 71 resource.TestStep{ 72 Config: config, 73 Check: resource.ComposeTestCheckFunc( 74 testCheckAzureRMRouteTableExists("azurerm_route_table.test"), 75 ), 76 }, 77 }, 78 }) 79 } 80 81 func TestAccAzureRMRouteTable_disappears(t *testing.T) { 82 83 ri := acctest.RandInt() 84 config := fmt.Sprintf(testAccAzureRMRouteTable_basic, ri, ri) 85 86 resource.Test(t, resource.TestCase{ 87 PreCheck: func() { testAccPreCheck(t) }, 88 Providers: testAccProviders, 89 CheckDestroy: testCheckAzureRMRouteTableDestroy, 90 Steps: []resource.TestStep{ 91 resource.TestStep{ 92 Config: config, 93 Check: resource.ComposeTestCheckFunc( 94 testCheckAzureRMRouteTableExists("azurerm_route_table.test"), 95 testCheckAzureRMRouteTableDisappears("azurerm_route_table.test"), 96 ), 97 ExpectNonEmptyPlan: true, 98 }, 99 }, 100 }) 101 } 102 103 func TestAccAzureRMRouteTable_withTags(t *testing.T) { 104 105 ri := acctest.RandInt() 106 preConfig := fmt.Sprintf(testAccAzureRMRouteTable_withTags, ri, ri) 107 postConfig := fmt.Sprintf(testAccAzureRMRouteTable_withTagsUpdate, ri, ri) 108 109 resource.Test(t, resource.TestCase{ 110 PreCheck: func() { testAccPreCheck(t) }, 111 Providers: testAccProviders, 112 CheckDestroy: testCheckAzureRMRouteTableDestroy, 113 Steps: []resource.TestStep{ 114 resource.TestStep{ 115 Config: preConfig, 116 Check: resource.ComposeTestCheckFunc( 117 testCheckAzureRMRouteTableExists("azurerm_route_table.test"), 118 resource.TestCheckResourceAttr( 119 "azurerm_route_table.test", "tags.%", "2"), 120 resource.TestCheckResourceAttr( 121 "azurerm_route_table.test", "tags.environment", "Production"), 122 resource.TestCheckResourceAttr( 123 "azurerm_route_table.test", "tags.cost_center", "MSFT"), 124 ), 125 }, 126 127 resource.TestStep{ 128 Config: postConfig, 129 Check: resource.ComposeTestCheckFunc( 130 testCheckAzureRMRouteTableExists("azurerm_route_table.test"), 131 resource.TestCheckResourceAttr( 132 "azurerm_route_table.test", "tags.%", "1"), 133 resource.TestCheckResourceAttr( 134 "azurerm_route_table.test", "tags.environment", "staging"), 135 ), 136 }, 137 }, 138 }) 139 } 140 141 func TestAccAzureRMRouteTable_multipleRoutes(t *testing.T) { 142 143 ri := acctest.RandInt() 144 preConfig := fmt.Sprintf(testAccAzureRMRouteTable_basic, ri, ri) 145 postConfig := fmt.Sprintf(testAccAzureRMRouteTable_multipleRoutes, ri, ri) 146 147 resource.Test(t, resource.TestCase{ 148 PreCheck: func() { testAccPreCheck(t) }, 149 Providers: testAccProviders, 150 CheckDestroy: testCheckAzureRMRouteTableDestroy, 151 Steps: []resource.TestStep{ 152 resource.TestStep{ 153 Config: preConfig, 154 Check: resource.ComposeTestCheckFunc( 155 testCheckAzureRMRouteTableExists("azurerm_route_table.test"), 156 resource.TestCheckResourceAttr( 157 "azurerm_route_table.test", "route.#", "1"), 158 ), 159 }, 160 161 resource.TestStep{ 162 Config: postConfig, 163 Check: resource.ComposeTestCheckFunc( 164 testCheckAzureRMRouteTableExists("azurerm_route_table.test"), 165 resource.TestCheckResourceAttr( 166 "azurerm_route_table.test", "route.#", "2"), 167 ), 168 }, 169 }, 170 }) 171 } 172 173 func testCheckAzureRMRouteTableExists(name string) resource.TestCheckFunc { 174 return func(s *terraform.State) error { 175 176 rs, ok := s.RootModule().Resources[name] 177 if !ok { 178 return fmt.Errorf("Not found: %s", name) 179 } 180 181 name := rs.Primary.Attributes["name"] 182 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 183 if !hasResourceGroup { 184 return fmt.Errorf("Bad: no resource group found in state for route table: %s", name) 185 } 186 187 conn := testAccProvider.Meta().(*ArmClient).routeTablesClient 188 189 resp, err := conn.Get(resourceGroup, name, "") 190 if err != nil { 191 return fmt.Errorf("Bad: Get on routeTablesClient: %s", err) 192 } 193 194 if resp.StatusCode == http.StatusNotFound { 195 return fmt.Errorf("Bad: Route Table %q (resource group: %q) does not exist", name, resourceGroup) 196 } 197 198 return nil 199 } 200 } 201 202 func testCheckAzureRMRouteTableDisappears(name string) resource.TestCheckFunc { 203 return func(s *terraform.State) error { 204 205 rs, ok := s.RootModule().Resources[name] 206 if !ok { 207 return fmt.Errorf("Not found: %s", name) 208 } 209 210 name := rs.Primary.Attributes["name"] 211 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 212 if !hasResourceGroup { 213 return fmt.Errorf("Bad: no resource group found in state for route table: %s", name) 214 } 215 216 conn := testAccProvider.Meta().(*ArmClient).routeTablesClient 217 218 _, err := conn.Delete(resourceGroup, name, make(chan struct{})) 219 if err != nil { 220 return fmt.Errorf("Bad: Delete on routeTablesClient: %s", err) 221 } 222 223 return nil 224 } 225 } 226 227 func testCheckAzureRMRouteTableDestroy(s *terraform.State) error { 228 conn := testAccProvider.Meta().(*ArmClient).routeTablesClient 229 230 for _, rs := range s.RootModule().Resources { 231 if rs.Type != "azurerm_route_table" { 232 continue 233 } 234 235 name := rs.Primary.Attributes["name"] 236 resourceGroup := rs.Primary.Attributes["resource_group_name"] 237 238 resp, err := conn.Get(resourceGroup, name, "") 239 240 if err != nil { 241 return nil 242 } 243 244 if resp.StatusCode != http.StatusNotFound { 245 return fmt.Errorf("Route Table still exists:\n%#v", resp.RouteTablePropertiesFormat) 246 } 247 } 248 249 return nil 250 } 251 252 var testAccAzureRMRouteTable_basic = ` 253 resource "azurerm_resource_group" "test" { 254 name = "acctestRG-%d" 255 location = "West US" 256 } 257 258 resource "azurerm_route_table" "test" { 259 name = "acctestrt%d" 260 location = "West US" 261 resource_group_name = "${azurerm_resource_group.test.name}" 262 263 route { 264 name = "route1" 265 address_prefix = "10.1.0.0/16" 266 next_hop_type = "vnetlocal" 267 } 268 } 269 ` 270 271 var testAccAzureRMRouteTable_multipleRoutes = ` 272 resource "azurerm_resource_group" "test" { 273 name = "acctestRG-%d" 274 location = "West US" 275 } 276 277 resource "azurerm_route_table" "test" { 278 name = "acctestrt%d" 279 location = "West US" 280 resource_group_name = "${azurerm_resource_group.test.name}" 281 282 route { 283 name = "route1" 284 address_prefix = "10.1.0.0/16" 285 next_hop_type = "vnetlocal" 286 } 287 288 route { 289 name = "route2" 290 address_prefix = "10.2.0.0/16" 291 next_hop_type = "vnetlocal" 292 } 293 } 294 ` 295 296 var testAccAzureRMRouteTable_withTags = ` 297 resource "azurerm_resource_group" "test" { 298 name = "acctestRG-%d" 299 location = "West US" 300 } 301 302 resource "azurerm_route_table" "test" { 303 name = "acctestrt%d" 304 location = "West US" 305 resource_group_name = "${azurerm_resource_group.test.name}" 306 307 route { 308 name = "route1" 309 address_prefix = "10.1.0.0/16" 310 next_hop_type = "vnetlocal" 311 } 312 313 tags { 314 environment = "Production" 315 cost_center = "MSFT" 316 } 317 } 318 ` 319 320 var testAccAzureRMRouteTable_withTagsUpdate = ` 321 resource "azurerm_resource_group" "test" { 322 name = "acctestRG-%d" 323 location = "West US" 324 } 325 326 resource "azurerm_route_table" "test" { 327 name = "acctestrt%d" 328 location = "West US" 329 resource_group_name = "${azurerm_resource_group.test.name}" 330 331 route { 332 name = "route1" 333 address_prefix = "10.1.0.0/16" 334 next_hop_type = "vnetlocal" 335 } 336 337 tags { 338 environment = "staging" 339 } 340 } 341 `