152 lines
3.7 KiB
Go
152 lines
3.7 KiB
Go
|
package azurerm
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/hashicorp/terraform/helper/resource"
|
||
|
"github.com/hashicorp/terraform/terraform"
|
||
|
)
|
||
|
|
||
|
func TestAccAzureRMRoute_basic(t *testing.T) {
|
||
|
|
||
|
resource.Test(t, resource.TestCase{
|
||
|
PreCheck: func() { testAccPreCheck(t) },
|
||
|
Providers: testAccProviders,
|
||
|
CheckDestroy: testCheckAzureRMRouteDestroy,
|
||
|
Steps: []resource.TestStep{
|
||
|
resource.TestStep{
|
||
|
Config: testAccAzureRMRoute_basic,
|
||
|
Check: resource.ComposeTestCheckFunc(
|
||
|
testCheckAzureRMRouteExists("azurerm_route.test"),
|
||
|
),
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestAccAzureRMRoute_multipleRoutes(t *testing.T) {
|
||
|
|
||
|
resource.Test(t, resource.TestCase{
|
||
|
PreCheck: func() { testAccPreCheck(t) },
|
||
|
Providers: testAccProviders,
|
||
|
CheckDestroy: testCheckAzureRMRouteDestroy,
|
||
|
Steps: []resource.TestStep{
|
||
|
resource.TestStep{
|
||
|
Config: testAccAzureRMRoute_basic,
|
||
|
Check: resource.ComposeTestCheckFunc(
|
||
|
testCheckAzureRMRouteExists("azurerm_route.test"),
|
||
|
),
|
||
|
},
|
||
|
|
||
|
resource.TestStep{
|
||
|
Config: testAccAzureRMRoute_multipleRoutes,
|
||
|
Check: resource.ComposeTestCheckFunc(
|
||
|
testCheckAzureRMRouteExists("azurerm_route.test1"),
|
||
|
),
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func testCheckAzureRMRouteExists(name string) resource.TestCheckFunc {
|
||
|
return func(s *terraform.State) error {
|
||
|
|
||
|
rs, ok := s.RootModule().Resources[name]
|
||
|
if !ok {
|
||
|
return fmt.Errorf("Not found: %s", name)
|
||
|
}
|
||
|
|
||
|
name := rs.Primary.Attributes["name"]
|
||
|
rtName := rs.Primary.Attributes["route_table_name"]
|
||
|
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
|
||
|
if !hasResourceGroup {
|
||
|
return fmt.Errorf("Bad: no resource group found in state for route: %s", name)
|
||
|
}
|
||
|
|
||
|
conn := testAccProvider.Meta().(*ArmClient).routesClient
|
||
|
|
||
|
resp, err := conn.Get(resourceGroup, rtName, name)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Bad: Get on routesClient: %s", err)
|
||
|
}
|
||
|
|
||
|
if resp.StatusCode == http.StatusNotFound {
|
||
|
return fmt.Errorf("Bad: Route %q (resource group: %q) does not exist", name, resourceGroup)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func testCheckAzureRMRouteDestroy(s *terraform.State) error {
|
||
|
conn := testAccProvider.Meta().(*ArmClient).routesClient
|
||
|
|
||
|
for _, rs := range s.RootModule().Resources {
|
||
|
if rs.Type != "azurerm_route" {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
name := rs.Primary.Attributes["name"]
|
||
|
rtName := rs.Primary.Attributes["route_table_name"]
|
||
|
resourceGroup := rs.Primary.Attributes["resource_group_name"]
|
||
|
|
||
|
resp, err := conn.Get(resourceGroup, rtName, name)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
if resp.StatusCode != http.StatusNotFound {
|
||
|
return fmt.Errorf("Route still exists:\n%#v", resp.Properties)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
var testAccAzureRMRoute_basic = `
|
||
|
resource "azurerm_resource_group" "test" {
|
||
|
name = "acceptanceTestResourceGroup1"
|
||
|
location = "West US"
|
||
|
}
|
||
|
|
||
|
resource "azurerm_route_table" "test" {
|
||
|
name = "acceptanceTestRouteTable1"
|
||
|
location = "West US"
|
||
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
||
|
}
|
||
|
|
||
|
resource "azurerm_route" "test" {
|
||
|
name = "acceptanceTestRoute1"
|
||
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
||
|
route_table_name = "${azurerm_route_table.test.name}"
|
||
|
|
||
|
address_prefix = "10.1.0.0/16"
|
||
|
next_hop_type = "vnetlocal"
|
||
|
}
|
||
|
`
|
||
|
|
||
|
var testAccAzureRMRoute_multipleRoutes = `
|
||
|
resource "azurerm_resource_group" "test" {
|
||
|
name = "acceptanceTestResourceGroup1"
|
||
|
location = "West US"
|
||
|
}
|
||
|
|
||
|
resource "azurerm_route_table" "test" {
|
||
|
name = "acceptanceTestRouteTable1"
|
||
|
location = "West US"
|
||
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
||
|
}
|
||
|
|
||
|
resource "azurerm_route" "test1" {
|
||
|
name = "acceptanceTestRoute2"
|
||
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
||
|
route_table_name = "${azurerm_route_table.test.name}"
|
||
|
|
||
|
address_prefix = "10.2.0.0/16"
|
||
|
next_hop_type = "none"
|
||
|
}
|
||
|
`
|