blob: 6dcfcdcc5cdf9d151cc23606874ea3b070774242 [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001// +build acceptance networking
2
3package v2
4
Jamie Hannafordf84171d2014-09-18 14:00:01 +02005import (
6 "testing"
7
8 "github.com/rackspace/gophercloud/openstack/networking/v2/networks"
9 "github.com/rackspace/gophercloud/openstack/networking/v2/subnets"
10 "github.com/rackspace/gophercloud/pagination"
11 th "github.com/rackspace/gophercloud/testhelper"
12)
Jamie Hannaford89f9af22014-09-17 12:21:48 +020013
14func TestList(t *testing.T) {
Jamie Hannafordf84171d2014-09-18 14:00:01 +020015 Setup(t)
16 defer Teardown()
Jamie Hannaford89f9af22014-09-17 12:21:48 +020017
Jamie Hannafordf84171d2014-09-18 14:00:01 +020018 pager := subnets.List(Client, subnets.ListOpts{Limit: 2})
19 err := pager.EachPage(func(page pagination.Page) (bool, error) {
20 t.Logf("--- Page ---")
21
Jamie Hannaford2a0492a2014-09-22 12:02:11 +020022 subnetList, err := subnets.ExtractSubnets(page)
Jamie Hannafordf84171d2014-09-18 14:00:01 +020023 th.AssertNoErr(t, err)
24
Jamie Hannaford2a0492a2014-09-22 12:02:11 +020025 for _, s := range subnetList {
Jamie Hannafordf84171d2014-09-18 14:00:01 +020026 t.Logf("Subnet: ID [%s] Name [%s] IP Version [%d] CIDR [%s] GatewayIP [%s]",
27 s.ID, s.Name, s.IPVersion, s.CIDR, s.GatewayIP)
28 }
29
30 return true, nil
31 })
32 th.CheckNoErr(t, err)
Jamie Hannaford89f9af22014-09-17 12:21:48 +020033}
34
35func TestCRUD(t *testing.T) {
Jamie Hannafordf84171d2014-09-18 14:00:01 +020036 Setup(t)
37 defer Teardown()
38
39 // Setup network
40 t.Log("Setting up network")
Jamie Hannaford5e45ccb2014-09-22 15:41:10 +020041 res, err := networks.Create(Client, networks.CreateOpts{Name: "tmp_network", AdminStateUp: true})
Jamie Hannafordf84171d2014-09-18 14:00:01 +020042 th.AssertNoErr(t, err)
43 networkID := res.ID
44 defer networks.Delete(Client, networkID)
Jamie Hannaford89f9af22014-09-17 12:21:48 +020045
46 // Create subnet
Jamie Hannafordf84171d2014-09-18 14:00:01 +020047 t.Log("Create subnet")
48 enable := false
Jamie Hannaford5e45ccb2014-09-22 15:41:10 +020049 opts := subnets.CreateOpts{
Jamie Hannafordf84171d2014-09-18 14:00:01 +020050 NetworkID: networkID,
51 CIDR: "192.168.199.0/24",
52 IPVersion: subnets.IPv4,
53 Name: "my_subnet",
54 EnableDHCP: &enable,
55 }
56 s, err := subnets.Create(Client, opts)
57 th.AssertNoErr(t, err)
58
59 th.AssertEquals(t, s.NetworkID, networkID)
60 th.AssertEquals(t, s.CIDR, "192.168.199.0/24")
61 th.AssertEquals(t, s.IPVersion, 4)
62 th.AssertEquals(t, s.Name, "my_subnet")
63 th.AssertEquals(t, s.EnableDHCP, false)
64 subnetID := s.ID
Jamie Hannaford89f9af22014-09-17 12:21:48 +020065
66 // Get subnet
Jamie Hannafordf84171d2014-09-18 14:00:01 +020067 t.Log("Getting subnet")
68 s, err = subnets.Get(Client, subnetID)
69 th.AssertNoErr(t, err)
70 th.AssertEquals(t, s.ID, subnetID)
Jamie Hannaford89f9af22014-09-17 12:21:48 +020071
72 // Update subnet
Jamie Hannafordf84171d2014-09-18 14:00:01 +020073 t.Log("Update subnet")
Jamie Hannaford5e45ccb2014-09-22 15:41:10 +020074 s, err = subnets.Update(Client, subnetID, subnets.UpdateOpts{Name: "new_subnet_name"})
Jamie Hannafordf84171d2014-09-18 14:00:01 +020075 th.AssertNoErr(t, err)
76 th.AssertEquals(t, s.Name, "new_subnet_name")
Jamie Hannaford89f9af22014-09-17 12:21:48 +020077
78 // Delete subnet
Jamie Hannafordf84171d2014-09-18 14:00:01 +020079 t.Log("Delete subnet")
80 err = subnets.Delete(Client, subnetID)
81 th.AssertNoErr(t, err)
Jamie Hannaford89f9af22014-09-17 12:21:48 +020082}
83
84func TestBatchCreate(t *testing.T) {
Jamie Hannafordf84171d2014-09-18 14:00:01 +020085 // todo
Jamie Hannaford89f9af22014-09-17 12:21:48 +020086}