blob: e6a789e2456d19cfd7f53a206c38e9bf5fdcf7bd [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
Joe Topjianf92ae6c2016-04-06 21:24:43 -060014func TestSubnetList(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
Joe Topjianf92ae6c2016-04-06 21:24:43 -060035func TestSubnetCRUD(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 Hannaford9b18ae22014-10-09 10:42:21 +020041 n, err := networks.Create(Client, networks.CreateOpts{Name: "tmp_network", AdminStateUp: networks.Up}).Extract()
Jamie Hannafordf84171d2014-09-18 14:00:01 +020042 th.AssertNoErr(t, err)
Jamie Hannafordd9036422014-09-23 17:50:24 +020043 networkID := n.ID
Jamie Hannafordf84171d2014-09-18 14:00:01 +020044 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 }
Jamie Hannafordd9036422014-09-23 17:50:24 +020056 s, err := subnets.Create(Client, opts).Extract()
Jamie Hannafordf84171d2014-09-18 14:00:01 +020057 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)
Joe Topjianf92ae6c2016-04-06 21:24:43 -060064 th.AssertEquals(t, s.GatewayIP, "192.168.199.1")
Jamie Hannafordf84171d2014-09-18 14:00:01 +020065 subnetID := s.ID
Jamie Hannaford89f9af22014-09-17 12:21:48 +020066
67 // Get subnet
Jamie Hannafordf84171d2014-09-18 14:00:01 +020068 t.Log("Getting subnet")
Jamie Hannafordd9036422014-09-23 17:50:24 +020069 s, err = subnets.Get(Client, subnetID).Extract()
Jamie Hannafordf84171d2014-09-18 14:00:01 +020070 th.AssertNoErr(t, err)
71 th.AssertEquals(t, s.ID, subnetID)
Jamie Hannaford89f9af22014-09-17 12:21:48 +020072
73 // Update subnet
Jamie Hannafordf84171d2014-09-18 14:00:01 +020074 t.Log("Update subnet")
Jamie Hannafordd9036422014-09-23 17:50:24 +020075 s, err = subnets.Update(Client, subnetID, subnets.UpdateOpts{Name: "new_subnet_name"}).Extract()
Jamie Hannafordf84171d2014-09-18 14:00:01 +020076 th.AssertNoErr(t, err)
77 th.AssertEquals(t, s.Name, "new_subnet_name")
Jamie Hannaford89f9af22014-09-17 12:21:48 +020078
79 // Delete subnet
Jamie Hannafordf84171d2014-09-18 14:00:01 +020080 t.Log("Delete subnet")
Jamie Hannafordd9036422014-09-23 17:50:24 +020081 res := subnets.Delete(Client, subnetID)
82 th.AssertNoErr(t, res.Err)
Joe Topjianf92ae6c2016-04-06 21:24:43 -060083
84 // Create subnet with no gateway
85 t.Log("Create subnet with no gateway")
86 opts = subnets.CreateOpts{
87 NetworkID: networkID,
88 CIDR: "192.168.199.0/24",
89 IPVersion: subnets.IPv4,
90 Name: "my_subnet",
91 EnableDHCP: &enable,
92 NoGateway: true,
93 }
94 s, err = subnets.Create(Client, opts).Extract()
95 th.AssertNoErr(t, err)
96
97 th.AssertEquals(t, s.NetworkID, networkID)
98 th.AssertEquals(t, s.CIDR, "192.168.199.0/24")
99 th.AssertEquals(t, s.IPVersion, 4)
100 th.AssertEquals(t, s.Name, "my_subnet")
101 th.AssertEquals(t, s.EnableDHCP, false)
102 th.AssertEquals(t, s.GatewayIP, "")
103 subnetID = s.ID
104
105 // Get subnet
106 t.Log("Getting subnet with no gateway")
107 s, err = subnets.Get(Client, subnetID).Extract()
108 th.AssertNoErr(t, err)
109 th.AssertEquals(t, s.ID, subnetID)
110
111 // Update subnet
112 t.Log("Update subnet with no gateway")
113 s, err = subnets.Update(Client, subnetID, subnets.UpdateOpts{Name: "new_subnet_name"}).Extract()
114 th.AssertNoErr(t, err)
115 th.AssertEquals(t, s.Name, "new_subnet_name")
116
117 // Delete subnet
118 t.Log("Delete subnet with no gateway")
119 res = subnets.Delete(Client, subnetID)
120 th.AssertNoErr(t, res.Err)
Joe Topjian2524d112016-04-07 15:41:39 +0000121
122 // Create subnet with invalid gateway configuration
123 t.Log("Create subnet with invalid gateway configuration")
124 opts = subnets.CreateOpts{
125 NetworkID: networkID,
126 CIDR: "192.168.199.0/24",
127 IPVersion: subnets.IPv4,
128 Name: "my_subnet",
129 EnableDHCP: &enable,
130 NoGateway: true,
131 GatewayIP: "192.168.199.1",
132 }
133 _, err = subnets.Create(Client, opts).Extract()
134 if err == nil {
135 t.Fatalf("Expected an error, got none")
136 }
Jamie Hannaford89f9af22014-09-17 12:21:48 +0200137}
138
139func TestBatchCreate(t *testing.T) {
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200140 // todo
Jamie Hannaford89f9af22014-09-17 12:21:48 +0200141}