Skip to content

Commit

Permalink
WIP: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drebes committed Apr 17, 2016
1 parent 660271a commit 0d0ba0a
Showing 1 changed file with 192 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package openstack

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"

"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/routers"
"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
"github.com/rackspace/gophercloud/openstack/networking/v2/subnets"
)

func TestAccNetworkingV2RouterTable_basic(t *testing.T) {
var router routers.Router
var network [2]networks.Network
var subnet [2]subnets.Subnet

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNetworkingV2RouterTableDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccNetworkingV2RouterTable_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2RouterExists(t, "openstack_networking_router_v2.router_1", &router),
testAccCheckNetworkingV2NetworkExists(t, "openstack_networking_network_v2.network_1", &network[0]),
testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet[0]),
testAccCheckNetworkingV2NetworkExists(t, "openstack_networking_network_v2.network_1", &network[1]),
testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet[1]),
testAccCheckNetworkingV2RouterInterfaceExists(t, "openstack_networking_router_interface_v2.int_1"),
testAccCheckNetworkingV2RouterInterfaceExists(t, "openstack_networking_router_interface_v2.int_2"),
testAccCheckNetworkingV2RouterTableExists(t, "openstack_networking_router_table_v2.router_table_1"),
),
},
},
})
}

func testAccCheckNetworkingV2RouterTableDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("(testAccCheckNetworkingV2RouterTableDestroy) Error creating OpenStack networking client: %s", err)
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "openstack_networking_router_table_v2" {
continue
}

router, err := routers.Get(networkingClient, rs.Primary.Attributes["router_id"]).Extract()
if err != nil {
return err
}

if router.ID != rs.Primary.Attributes["router_id"] {
return fmt.Errorf("Router for table not found")
}

if len(router.Routes) != 1 {
return fmt.Errorf("Invalid number of route entries: %d", len(router.Routes))
}
}

return nil
}

func testAccCheckNetworkingV2RouterTableExists(t *testing.T, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

config := testAccProvider.Meta().(*Config)
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("(testAccCheckNetworkingV2RouterTableExists) Error creating OpenStack networking client: %s", err)
}

router, err := routers.Get(networkingClient, rs.Primary.Attributes["router_id"]).Extract()
if err != nil {
return err
}

if router.ID != rs.Primary.Attributes["router_id"] {
return fmt.Errorf("Router for table not found")
}

if len(router.Routes) != 2 {
return fmt.Errorf("Invalid number of route entries: %d", len(router.Routes))
}

if router.Routes[0].DestinationCIDR != "10.0.1.0/24" {
return fmt.Errorf("Invalid destination CIDR: %s", router.Routes[0].DestinationCIDR)
}

if router.Routes[0].NextHop != "192.168.199.254" {
return fmt.Errorf("Invalid next hop: %s", router.Routes[0].NextHop)
}

if router.Routes[1].DestinationCIDR != "10.0.2.0/24" {
return fmt.Errorf("Invalid destination CIDR: %s", router.Routes[0].DestinationCIDR)
}

if router.Routes[1].NextHop != "192.168.200.254" {
return fmt.Errorf("Invalid next hop: %s", router.Routes[0].NextHop)
}

return nil
}
}

var testAccNetworkingV2RouterTable_basic = fmt.Sprintf(`
resource "openstack_networking_router_v2" "router_1" {
name = "router_1"
admin_state_up = "true"
}
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "subnet_1" {
network_id = "${openstack_networking_network_v2.network_1.id}"
cidr = "192.168.199.0/24"
ip_version = 4
}
resource "openstack_networking_port_v2" "port_1" {
name = "port_1"
network_id = "${openstack_networking_network_v2.network_1.id}"
admin_state_up = "true"
fixed_ip {
subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
ip_address = "192.168.199.1"
}
}
resource "openstack_networking_router_interface_v2" "int_1" {
router_id = "${openstack_networking_router_v2.router_1.id}"
port_id = "${openstack_networking_port_v2.port_1.id}"
}
resource "openstack_networking_network_v2" "network_2" {
name = "network_2"
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "subnet_2" {
network_id = "${openstack_networking_network_v2.network_2.id}"
cidr = "192.168.200.0/24"
ip_version = 4
}
resource "openstack_networking_port_v2" "port_2" {
name = "port_2"
network_id = "${openstack_networking_network_v2.network_2.id}"
admin_state_up = "true"
fixed_ip {
subnet_id = "${openstack_networking_subnet_v2.subnet_2.id}"
ip_address = "192.168.200.1"
}
}
resource "openstack_networking_router_interface_v2" "int_2" {
router_id = "${openstack_networking_router_v2.router_1.id}"
port_id = "${openstack_networking_port_v2.port_2.id}"
}
resource "openstack_networking_router_table_v2" "router_table_1" {
depends_on = ["openstack_networking_router_interface_v2.int_1", "openstack_networking_router_interface_v2.int_2"]
router_id = "${openstack_networking_router_v2.router_1.id}"
route {
destination_cidr = "10.0.1.0/24"
next_hop = "192.168.199.254"
}
route {
destination_cidr = "10.0.2.0/24"
next_hop = "192.168.200.254"
}
}`)

0 comments on commit 0d0ba0a

Please sign in to comment.