blob: c4014320a4c304312cc16661aefa5ec29504d51a [file] [log] [blame]
Jon Perritt53c8a3a2014-11-24 07:46:35 -07001// +build acceptance networking
2
3package v2
4
5import (
6 "testing"
7
8 osNetworks "github.com/rackspace/gophercloud/openstack/networking/v2/networks"
9 osSubnets "github.com/rackspace/gophercloud/openstack/networking/v2/subnets"
10 "github.com/rackspace/gophercloud/pagination"
11 "github.com/rackspace/gophercloud/rackspace/networking/v2/networks"
12 "github.com/rackspace/gophercloud/rackspace/networking/v2/subnets"
13 th "github.com/rackspace/gophercloud/testhelper"
14)
15
16func TestListSubnets(t *testing.T) {
17 Setup(t)
18 defer Teardown()
19
20 pager := subnets.List(Client, osSubnets.ListOpts{Limit: 2})
21 err := pager.EachPage(func(page pagination.Page) (bool, error) {
22 t.Logf("--- Page ---")
23
24 subnetList, err := osSubnets.ExtractSubnets(page)
25 th.AssertNoErr(t, err)
26
27 for _, s := range subnetList {
28 t.Logf("Subnet: ID [%s] Name [%s] IP Version [%d] CIDR [%s] GatewayIP [%s]",
29 s.ID, s.Name, s.IPVersion, s.CIDR, s.GatewayIP)
30 }
31
32 return true, nil
33 })
34 th.CheckNoErr(t, err)
35}
36
37func TestSubnetCRUD(t *testing.T) {
38 Setup(t)
39 defer Teardown()
40
41 // Setup network
42 t.Log("Setting up network")
43 n, err := networks.Create(Client, osNetworks.CreateOpts{Name: "tmp_network", AdminStateUp: osNetworks.Up}).Extract()
44 th.AssertNoErr(t, err)
45 networkID := n.ID
46 defer networks.Delete(Client, networkID)
47
48 // Create subnet
49 t.Log("Create subnet")
50 enable := false
51 opts := osSubnets.CreateOpts{
52 NetworkID: networkID,
53 CIDR: "192.168.199.0/24",
54 IPVersion: osSubnets.IPv4,
55 Name: "my_subnet",
56 EnableDHCP: &enable,
57 }
58 s, err := subnets.Create(Client, opts).Extract()
59 th.AssertNoErr(t, err)
60
61 th.AssertEquals(t, networkID, s.NetworkID)
62 th.AssertEquals(t, "192.168.199.0/24", s.CIDR)
63 th.AssertEquals(t, 4, s.IPVersion)
64 th.AssertEquals(t, "my_subnet", s.Name)
65 th.AssertEquals(t, false, s.EnableDHCP)
66 subnetID := s.ID
67
68 // Get subnet
69 t.Log("Getting subnet")
70 s, err = subnets.Get(Client, subnetID).Extract()
71 th.AssertNoErr(t, err)
72 th.AssertEquals(t, subnetID, s.ID)
73
74 // Update subnet
75 t.Log("Update subnet")
76 s, err = subnets.Update(Client, subnetID, osSubnets.UpdateOpts{Name: "new_subnet_name"}).Extract()
77 th.AssertNoErr(t, err)
78 th.AssertEquals(t, "new_subnet_name", s.Name)
79
80 // Delete subnet
81 t.Log("Delete subnet")
82 res := subnets.Delete(Client, subnetID)
83 th.AssertNoErr(t, res.Err)
84}