Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 1 | package subnets |
| 2 | |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 8 | fake "github.com/rackspace/gophercloud/openstack/networking/v2/common" |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 13 | func TestList(t *testing.T) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 16 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 17 | th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 18 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 19 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 20 | |
| 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 Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 70 | List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 71 | 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 Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 84 | DNSNameservers: []string{}, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 85 | AllocationPools: []AllocationPool{ |
| 86 | AllocationPool{ |
| 87 | Start: "10.0.0.2", |
| 88 | End: "10.0.0.254", |
| 89 | }, |
| 90 | }, |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 91 | HostRoutes: []HostRoute{}, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 92 | 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 Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 102 | DNSNameservers: []string{}, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 103 | AllocationPools: []AllocationPool{ |
| 104 | AllocationPool{ |
| 105 | Start: "192.0.0.2", |
| 106 | End: "192.255.255.254", |
| 107 | }, |
| 108 | }, |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 109 | HostRoutes: []HostRoute{}, |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 110 | 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 | |
| 127 | func TestGet(t *testing.T) { |
| 128 | th.SetupHTTP() |
| 129 | defer th.TeardownHTTP() |
| 130 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 131 | th.Mux.HandleFunc("/v2.0/subnets/54d6f61d-db07-451c-9ab3-b9609b6b6f0b", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 132 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 133 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 134 | |
| 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 Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 162 | s, err := Get(fake.ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract() |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 163 | 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 Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 169 | th.AssertDeepEquals(t, s.DNSNameservers, []string{}) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 170 | th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{ |
| 171 | AllocationPool{ |
| 172 | Start: "192.0.0.2", |
| 173 | End: "192.255.255.254", |
| 174 | }, |
| 175 | }) |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 176 | th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{}) |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame] | 177 | 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 Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 181 | } |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 182 | |
| 183 | func TestCreate(t *testing.T) { |
| 184 | th.SetupHTTP() |
| 185 | defer th.TeardownHTTP() |
| 186 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 187 | th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 188 | th.TestMethod(t, r, "POST") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 189 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 190 | 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, |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 197 | "cidr": "192.168.199.0/24", |
| 198 | "dns_nameservers": ["foo"], |
| 199 | "allocation_pools": [ |
| 200 | { |
| 201 | "start": "192.168.199.2", |
| 202 | "end": "192.168.199.254" |
| 203 | } |
| 204 | ], |
| 205 | "host_routes": [{"destination":"","nexthop": "bar"}] |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | `) |
| 209 | |
| 210 | w.Header().Add("Content-Type", "application/json") |
| 211 | w.WriteHeader(http.StatusCreated) |
| 212 | |
| 213 | fmt.Fprintf(w, ` |
| 214 | { |
| 215 | "subnet": { |
| 216 | "name": "", |
| 217 | "enable_dhcp": true, |
| 218 | "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", |
| 219 | "tenant_id": "4fd44f30292945e481c7b8a0c8908869", |
| 220 | "dns_nameservers": [], |
| 221 | "allocation_pools": [ |
| 222 | { |
| 223 | "start": "192.168.199.2", |
| 224 | "end": "192.168.199.254" |
| 225 | } |
| 226 | ], |
| 227 | "host_routes": [], |
| 228 | "ip_version": 4, |
| 229 | "gateway_ip": "192.168.199.1", |
| 230 | "cidr": "192.168.199.0/24", |
| 231 | "id": "3b80198d-4f7b-4f77-9ef5-774d54e17126" |
| 232 | } |
| 233 | } |
| 234 | `) |
| 235 | }) |
| 236 | |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 237 | opts := CreateOpts{ |
| 238 | NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", |
| 239 | IPVersion: 4, |
| 240 | CIDR: "192.168.199.0/24", |
| 241 | AllocationPools: []AllocationPool{ |
| 242 | AllocationPool{ |
| 243 | Start: "192.168.199.2", |
| 244 | End: "192.168.199.254", |
| 245 | }, |
| 246 | }, |
| 247 | DNSNameservers: []string{"foo"}, |
| 248 | HostRoutes: []HostRoute{ |
| 249 | HostRoute{NextHop: "bar"}, |
| 250 | }, |
| 251 | } |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 252 | s, err := Create(fake.ServiceClient(), opts).Extract() |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 253 | th.AssertNoErr(t, err) |
| 254 | |
| 255 | th.AssertEquals(t, s.Name, "") |
| 256 | th.AssertEquals(t, s.EnableDHCP, true) |
| 257 | th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 258 | th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869") |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 259 | th.AssertDeepEquals(t, s.DNSNameservers, []string{}) |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 260 | th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{ |
| 261 | AllocationPool{ |
| 262 | Start: "192.168.199.2", |
| 263 | End: "192.168.199.254", |
| 264 | }, |
| 265 | }) |
Jamie Hannaford | f283540 | 2014-09-23 11:01:21 +0200 | [diff] [blame] | 266 | th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{}) |
Jamie Hannaford | 6363143 | 2014-09-18 11:40:09 +0200 | [diff] [blame] | 267 | th.AssertEquals(t, s.IPVersion, 4) |
| 268 | th.AssertEquals(t, s.GatewayIP, "192.168.199.1") |
| 269 | th.AssertEquals(t, s.CIDR, "192.168.199.0/24") |
| 270 | th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126") |
| 271 | } |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 272 | |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 273 | func TestRequiredCreateOpts(t *testing.T) { |
| 274 | res := Create(fake.ServiceClient(), CreateOpts{}) |
| 275 | if res.Err == nil { |
| 276 | t.Fatalf("Expected error, got none") |
| 277 | } |
| 278 | |
| 279 | res = Create(fake.ServiceClient(), CreateOpts{NetworkID: "foo"}) |
| 280 | if res.Err == nil { |
| 281 | t.Fatalf("Expected error, got none") |
| 282 | } |
| 283 | |
| 284 | res = Create(fake.ServiceClient(), CreateOpts{NetworkID: "foo", CIDR: "bar", IPVersion: 40}) |
| 285 | if res.Err == nil { |
| 286 | t.Fatalf("Expected error, got none") |
| 287 | } |
| 288 | } |
| 289 | |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 290 | func TestUpdate(t *testing.T) { |
| 291 | th.SetupHTTP() |
| 292 | defer th.TeardownHTTP() |
| 293 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 294 | th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 295 | th.TestMethod(t, r, "PUT") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 296 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 297 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 298 | th.TestHeader(t, r, "Accept", "application/json") |
| 299 | th.TestJSONRequest(t, r, ` |
| 300 | { |
| 301 | "subnet": { |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 302 | "name": "my_new_subnet", |
| 303 | "dns_nameservers": ["foo"], |
| 304 | "host_routes": [{"destination":"","nexthop": "bar"}] |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | `) |
| 308 | |
| 309 | w.Header().Add("Content-Type", "application/json") |
| 310 | w.WriteHeader(http.StatusCreated) |
| 311 | |
| 312 | fmt.Fprintf(w, ` |
| 313 | { |
| 314 | "subnet": { |
| 315 | "name": "my_new_subnet", |
| 316 | "enable_dhcp": true, |
| 317 | "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324", |
| 318 | "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e", |
| 319 | "dns_nameservers": [], |
| 320 | "allocation_pools": [ |
| 321 | { |
| 322 | "start": "10.0.0.2", |
| 323 | "end": "10.0.0.254" |
| 324 | } |
| 325 | ], |
| 326 | "host_routes": [], |
| 327 | "ip_version": 4, |
| 328 | "gateway_ip": "10.0.0.1", |
| 329 | "cidr": "10.0.0.0/24", |
| 330 | "id": "08eae331-0402-425a-923c-34f7cfe39c1b" |
| 331 | } |
| 332 | } |
| 333 | `) |
| 334 | }) |
| 335 | |
Jamie Hannaford | 5d93f56 | 2014-10-09 10:53:32 +0200 | [diff] [blame] | 336 | opts := UpdateOpts{ |
| 337 | Name: "my_new_subnet", |
| 338 | DNSNameservers: []string{"foo"}, |
| 339 | HostRoutes: []HostRoute{ |
| 340 | HostRoute{NextHop: "bar"}, |
| 341 | }, |
| 342 | } |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 343 | s, err := Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract() |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 344 | th.AssertNoErr(t, err) |
| 345 | |
| 346 | th.AssertEquals(t, s.Name, "my_new_subnet") |
| 347 | th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b") |
| 348 | } |
| 349 | |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 350 | func TestDelete(t *testing.T) { |
| 351 | th.SetupHTTP() |
| 352 | defer th.TeardownHTTP() |
| 353 | |
Jamie Hannaford | a2976ab | 2014-10-09 10:32:58 +0200 | [diff] [blame] | 354 | th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 355 | th.TestMethod(t, r, "DELETE") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 356 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 357 | w.WriteHeader(http.StatusNoContent) |
| 358 | }) |
| 359 | |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 360 | res := Delete(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b") |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 361 | th.AssertNoErr(t, res.Err) |
Jamie Hannaford | d11e20c | 2014-09-18 12:03:01 +0200 | [diff] [blame] | 362 | } |