blob: c7562f77654207f3daaae27e20cd352766c68766 [file] [log] [blame]
Jamie Hannaford89f9af22014-09-17 12:21:48 +02001package subnets
2
Jamie Hannaford0708c002014-09-17 16:08:49 +02003import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jamie Hannaford0708c002014-09-17 16:08:49 +02008 "github.com/rackspace/gophercloud/pagination"
9 th "github.com/rackspace/gophercloud/testhelper"
Jamie Hannaford58b008f2014-10-06 10:07:47 +020010 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannaford0708c002014-09-17 16:08:49 +020011)
12
Jamie Hannaford89f9af22014-09-17 12:21:48 +020013func TestList(t *testing.T) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020014 th.SetupHTTP()
15 defer th.TeardownHTTP()
Jamie Hannaford89f9af22014-09-17 12:21:48 +020016
Jamie Hannaford0708c002014-09-17 16:08:49 +020017 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
18 th.TestMethod(t, r, "GET")
Jamie Hannaford58b008f2014-10-06 10:07:47 +020019 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford0708c002014-09-17 16:08:49 +020020
21 w.Header().Add("Content-Type", "application/json")
22 w.WriteHeader(http.StatusOK)
23
24 fmt.Fprintf(w, `
25{
26 "subnets": [
27 {
28 "name": "private-subnet",
29 "enable_dhcp": true,
30 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
31 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
32 "dns_nameservers": [],
33 "allocation_pools": [
34 {
35 "start": "10.0.0.2",
36 "end": "10.0.0.254"
37 }
38 ],
39 "host_routes": [],
40 "ip_version": 4,
41 "gateway_ip": "10.0.0.1",
42 "cidr": "10.0.0.0/24",
43 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
44 },
45 {
46 "name": "my_subnet",
47 "enable_dhcp": true,
48 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
49 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
50 "dns_nameservers": [],
51 "allocation_pools": [
52 {
53 "start": "192.0.0.2",
54 "end": "192.255.255.254"
55 }
56 ],
57 "host_routes": [],
58 "ip_version": 4,
59 "gateway_ip": "192.0.0.1",
60 "cidr": "192.0.0.0/8",
61 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
62 }
63 ]
64}
65 `)
66 })
67
68 count := 0
69
Jamie Hannaford58b008f2014-10-06 10:07:47 +020070 List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford0708c002014-09-17 16:08:49 +020071 count++
72 actual, err := ExtractSubnets(page)
73 if err != nil {
74 t.Errorf("Failed to extract subnets: %v", err)
75 return false, nil
76 }
77
78 expected := []Subnet{
79 Subnet{
80 Name: "private-subnet",
81 EnableDHCP: true,
82 NetworkID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
83 TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
Jamie Hannaford965ae702014-09-22 14:58:19 +020084 DNSNameservers: []string{},
Jamie Hannaford0708c002014-09-17 16:08:49 +020085 AllocationPools: []AllocationPool{
86 AllocationPool{
87 Start: "10.0.0.2",
88 End: "10.0.0.254",
89 },
90 },
Jamie Hannafordf2835402014-09-23 11:01:21 +020091 HostRoutes: []HostRoute{},
Jamie Hannaford0708c002014-09-17 16:08:49 +020092 IPVersion: 4,
93 GatewayIP: "10.0.0.1",
94 CIDR: "10.0.0.0/24",
95 ID: "08eae331-0402-425a-923c-34f7cfe39c1b",
96 },
97 Subnet{
98 Name: "my_subnet",
99 EnableDHCP: true,
100 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
101 TenantID: "4fd44f30292945e481c7b8a0c8908869",
Jamie Hannaford965ae702014-09-22 14:58:19 +0200102 DNSNameservers: []string{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200103 AllocationPools: []AllocationPool{
104 AllocationPool{
105 Start: "192.0.0.2",
106 End: "192.255.255.254",
107 },
108 },
Jamie Hannafordf2835402014-09-23 11:01:21 +0200109 HostRoutes: []HostRoute{},
Jamie Hannaford0708c002014-09-17 16:08:49 +0200110 IPVersion: 4,
111 GatewayIP: "192.0.0.1",
112 CIDR: "192.0.0.0/8",
113 ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b",
114 },
115 }
116
117 th.CheckDeepEquals(t, expected, actual)
118
119 return true, nil
120 })
121
122 if count != 1 {
123 t.Errorf("Expected 1 page, got %d", count)
124 }
125}
126
127func TestGet(t *testing.T) {
128 th.SetupHTTP()
129 defer th.TeardownHTTP()
130
131 th.Mux.HandleFunc("/v2.0/subnets/54d6f61d-db07-451c-9ab3-b9609b6b6f0b", func(w http.ResponseWriter, r *http.Request) {
132 th.TestMethod(t, r, "GET")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200133 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford0708c002014-09-17 16:08:49 +0200134
135 w.Header().Add("Content-Type", "application/json")
136 w.WriteHeader(http.StatusOK)
137
138 fmt.Fprintf(w, `
139{
140 "subnet": {
141 "name": "my_subnet",
142 "enable_dhcp": true,
143 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
144 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
145 "dns_nameservers": [],
146 "allocation_pools": [
147 {
148 "start": "192.0.0.2",
149 "end": "192.255.255.254"
150 }
151 ],
152 "host_routes": [],
153 "ip_version": 4,
154 "gateway_ip": "192.0.0.1",
155 "cidr": "192.0.0.0/8",
156 "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
157 }
158}
159 `)
160 })
161
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200162 s, err := Get(fake.ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract()
Jamie Hannaford0708c002014-09-17 16:08:49 +0200163 th.AssertNoErr(t, err)
164
165 th.AssertEquals(t, s.Name, "my_subnet")
166 th.AssertEquals(t, s.EnableDHCP, true)
167 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
168 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
Jamie Hannaford965ae702014-09-22 14:58:19 +0200169 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
Jamie Hannaford0708c002014-09-17 16:08:49 +0200170 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
171 AllocationPool{
172 Start: "192.0.0.2",
173 End: "192.255.255.254",
174 },
175 })
Jamie Hannafordf2835402014-09-23 11:01:21 +0200176 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
Jamie Hannaford0708c002014-09-17 16:08:49 +0200177 th.AssertEquals(t, s.IPVersion, 4)
178 th.AssertEquals(t, s.GatewayIP, "192.0.0.1")
179 th.AssertEquals(t, s.CIDR, "192.0.0.0/8")
180 th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0b")
Jamie Hannaford89f9af22014-09-17 12:21:48 +0200181}
Jamie Hannaford63631432014-09-18 11:40:09 +0200182
183func TestCreate(t *testing.T) {
184 th.SetupHTTP()
185 defer th.TeardownHTTP()
186
187 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
188 th.TestMethod(t, r, "POST")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200189 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford63631432014-09-18 11:40:09 +0200190 th.TestHeader(t, r, "Content-Type", "application/json")
191 th.TestHeader(t, r, "Accept", "application/json")
192 th.TestJSONRequest(t, r, `
193{
194 "subnet": {
195 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
196 "ip_version": 4,
197 "cidr": "192.168.199.0/24"
198 }
199}
200 `)
201
202 w.Header().Add("Content-Type", "application/json")
203 w.WriteHeader(http.StatusCreated)
204
205 fmt.Fprintf(w, `
206{
207 "subnet": {
208 "name": "",
209 "enable_dhcp": true,
210 "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
211 "tenant_id": "4fd44f30292945e481c7b8a0c8908869",
212 "dns_nameservers": [],
213 "allocation_pools": [
214 {
215 "start": "192.168.199.2",
216 "end": "192.168.199.254"
217 }
218 ],
219 "host_routes": [],
220 "ip_version": 4,
221 "gateway_ip": "192.168.199.1",
222 "cidr": "192.168.199.0/24",
223 "id": "3b80198d-4f7b-4f77-9ef5-774d54e17126"
224 }
225}
226 `)
227 })
228
Jamie Hannaford965ae702014-09-22 14:58:19 +0200229 opts := CreateOpts{NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", IPVersion: 4, CIDR: "192.168.199.0/24"}
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200230 s, err := Create(fake.ServiceClient(), opts).Extract()
Jamie Hannaford63631432014-09-18 11:40:09 +0200231 th.AssertNoErr(t, err)
232
233 th.AssertEquals(t, s.Name, "")
234 th.AssertEquals(t, s.EnableDHCP, true)
235 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
236 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
Jamie Hannaford965ae702014-09-22 14:58:19 +0200237 th.AssertDeepEquals(t, s.DNSNameservers, []string{})
Jamie Hannaford63631432014-09-18 11:40:09 +0200238 th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
239 AllocationPool{
240 Start: "192.168.199.2",
241 End: "192.168.199.254",
242 },
243 })
Jamie Hannafordf2835402014-09-23 11:01:21 +0200244 th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
Jamie Hannaford63631432014-09-18 11:40:09 +0200245 th.AssertEquals(t, s.IPVersion, 4)
246 th.AssertEquals(t, s.GatewayIP, "192.168.199.1")
247 th.AssertEquals(t, s.CIDR, "192.168.199.0/24")
248 th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126")
249}
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200250
251func TestUpdate(t *testing.T) {
252 th.SetupHTTP()
253 defer th.TeardownHTTP()
254
255 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
256 th.TestMethod(t, r, "PUT")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200257 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200258 th.TestHeader(t, r, "Content-Type", "application/json")
259 th.TestHeader(t, r, "Accept", "application/json")
260 th.TestJSONRequest(t, r, `
261{
262 "subnet": {
263 "name": "my_new_subnet"
264 }
265}
266 `)
267
268 w.Header().Add("Content-Type", "application/json")
269 w.WriteHeader(http.StatusCreated)
270
271 fmt.Fprintf(w, `
272{
273 "subnet": {
274 "name": "my_new_subnet",
275 "enable_dhcp": true,
276 "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
277 "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
278 "dns_nameservers": [],
279 "allocation_pools": [
280 {
281 "start": "10.0.0.2",
282 "end": "10.0.0.254"
283 }
284 ],
285 "host_routes": [],
286 "ip_version": 4,
287 "gateway_ip": "10.0.0.1",
288 "cidr": "10.0.0.0/24",
289 "id": "08eae331-0402-425a-923c-34f7cfe39c1b"
290 }
291}
292 `)
293 })
294
Jamie Hannaford965ae702014-09-22 14:58:19 +0200295 opts := UpdateOpts{Name: "my_new_subnet"}
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200296 s, err := Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract()
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200297 th.AssertNoErr(t, err)
298
299 th.AssertEquals(t, s.Name, "my_new_subnet")
300 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b")
301}
302
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200303func TestDelete(t *testing.T) {
304 th.SetupHTTP()
305 defer th.TeardownHTTP()
306
307 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
308 th.TestMethod(t, r, "DELETE")
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200309 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200310 w.WriteHeader(http.StatusNoContent)
311 })
312
Jamie Hannaford58b008f2014-10-06 10:07:47 +0200313 res := Delete(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b")
Jamie Hannafordd9036422014-09-23 17:50:24 +0200314 th.AssertNoErr(t, res.Err)
Jamie Hannafordd11e20c2014-09-18 12:03:01 +0200315}