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 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
| 13 | const TokenID = "123" |
| 14 | |
| 15 | func ServiceClient() *gophercloud.ServiceClient { |
| 16 | return &gophercloud.ServiceClient{ |
| 17 | Provider: &gophercloud.ProviderClient{ |
| 18 | TokenID: TokenID, |
| 19 | }, |
| 20 | Endpoint: th.Endpoint(), |
| 21 | } |
| 22 | } |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 23 | |
| 24 | func TestList(t *testing.T) { |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame^] | 25 | th.SetupHTTP() |
| 26 | defer th.TeardownHTTP() |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 27 | |
Jamie Hannaford | 0708c00 | 2014-09-17 16:08:49 +0200 | [diff] [blame^] | 28 | th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { |
| 29 | th.TestMethod(t, r, "GET") |
| 30 | th.TestHeader(t, r, "X-Auth-Token", TokenID) |
| 31 | |
| 32 | w.Header().Add("Content-Type", "application/json") |
| 33 | w.WriteHeader(http.StatusOK) |
| 34 | |
| 35 | fmt.Fprintf(w, ` |
| 36 | { |
| 37 | "subnets": [ |
| 38 | { |
| 39 | "name": "private-subnet", |
| 40 | "enable_dhcp": true, |
| 41 | "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324", |
| 42 | "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e", |
| 43 | "dns_nameservers": [], |
| 44 | "allocation_pools": [ |
| 45 | { |
| 46 | "start": "10.0.0.2", |
| 47 | "end": "10.0.0.254" |
| 48 | } |
| 49 | ], |
| 50 | "host_routes": [], |
| 51 | "ip_version": 4, |
| 52 | "gateway_ip": "10.0.0.1", |
| 53 | "cidr": "10.0.0.0/24", |
| 54 | "id": "08eae331-0402-425a-923c-34f7cfe39c1b" |
| 55 | }, |
| 56 | { |
| 57 | "name": "my_subnet", |
| 58 | "enable_dhcp": true, |
| 59 | "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", |
| 60 | "tenant_id": "4fd44f30292945e481c7b8a0c8908869", |
| 61 | "dns_nameservers": [], |
| 62 | "allocation_pools": [ |
| 63 | { |
| 64 | "start": "192.0.0.2", |
| 65 | "end": "192.255.255.254" |
| 66 | } |
| 67 | ], |
| 68 | "host_routes": [], |
| 69 | "ip_version": 4, |
| 70 | "gateway_ip": "192.0.0.1", |
| 71 | "cidr": "192.0.0.0/8", |
| 72 | "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b" |
| 73 | } |
| 74 | ] |
| 75 | } |
| 76 | `) |
| 77 | }) |
| 78 | |
| 79 | count := 0 |
| 80 | |
| 81 | List(ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 82 | count++ |
| 83 | actual, err := ExtractSubnets(page) |
| 84 | if err != nil { |
| 85 | t.Errorf("Failed to extract subnets: %v", err) |
| 86 | return false, nil |
| 87 | } |
| 88 | |
| 89 | expected := []Subnet{ |
| 90 | Subnet{ |
| 91 | Name: "private-subnet", |
| 92 | EnableDHCP: true, |
| 93 | NetworkID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324", |
| 94 | TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e", |
| 95 | DNSNameservers: []interface{}{}, |
| 96 | AllocationPools: []AllocationPool{ |
| 97 | AllocationPool{ |
| 98 | Start: "10.0.0.2", |
| 99 | End: "10.0.0.254", |
| 100 | }, |
| 101 | }, |
| 102 | HostRoutes: []interface{}{}, |
| 103 | IPVersion: 4, |
| 104 | GatewayIP: "10.0.0.1", |
| 105 | CIDR: "10.0.0.0/24", |
| 106 | ID: "08eae331-0402-425a-923c-34f7cfe39c1b", |
| 107 | }, |
| 108 | Subnet{ |
| 109 | Name: "my_subnet", |
| 110 | EnableDHCP: true, |
| 111 | NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", |
| 112 | TenantID: "4fd44f30292945e481c7b8a0c8908869", |
| 113 | DNSNameservers: []interface{}{}, |
| 114 | AllocationPools: []AllocationPool{ |
| 115 | AllocationPool{ |
| 116 | Start: "192.0.0.2", |
| 117 | End: "192.255.255.254", |
| 118 | }, |
| 119 | }, |
| 120 | HostRoutes: []interface{}{}, |
| 121 | IPVersion: 4, |
| 122 | GatewayIP: "192.0.0.1", |
| 123 | CIDR: "192.0.0.0/8", |
| 124 | ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b", |
| 125 | }, |
| 126 | } |
| 127 | |
| 128 | th.CheckDeepEquals(t, expected, actual) |
| 129 | |
| 130 | return true, nil |
| 131 | }) |
| 132 | |
| 133 | if count != 1 { |
| 134 | t.Errorf("Expected 1 page, got %d", count) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestGet(t *testing.T) { |
| 139 | th.SetupHTTP() |
| 140 | defer th.TeardownHTTP() |
| 141 | |
| 142 | th.Mux.HandleFunc("/v2.0/subnets/54d6f61d-db07-451c-9ab3-b9609b6b6f0b", func(w http.ResponseWriter, r *http.Request) { |
| 143 | th.TestMethod(t, r, "GET") |
| 144 | th.TestHeader(t, r, "X-Auth-Token", TokenID) |
| 145 | |
| 146 | w.Header().Add("Content-Type", "application/json") |
| 147 | w.WriteHeader(http.StatusOK) |
| 148 | |
| 149 | fmt.Fprintf(w, ` |
| 150 | { |
| 151 | "subnet": { |
| 152 | "name": "my_subnet", |
| 153 | "enable_dhcp": true, |
| 154 | "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", |
| 155 | "tenant_id": "4fd44f30292945e481c7b8a0c8908869", |
| 156 | "dns_nameservers": [], |
| 157 | "allocation_pools": [ |
| 158 | { |
| 159 | "start": "192.0.0.2", |
| 160 | "end": "192.255.255.254" |
| 161 | } |
| 162 | ], |
| 163 | "host_routes": [], |
| 164 | "ip_version": 4, |
| 165 | "gateway_ip": "192.0.0.1", |
| 166 | "cidr": "192.0.0.0/8", |
| 167 | "id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b" |
| 168 | } |
| 169 | } |
| 170 | `) |
| 171 | }) |
| 172 | |
| 173 | s, err := Get(ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b") |
| 174 | th.AssertNoErr(t, err) |
| 175 | |
| 176 | th.AssertEquals(t, s.Name, "my_subnet") |
| 177 | th.AssertEquals(t, s.EnableDHCP, true) |
| 178 | th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 179 | th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869") |
| 180 | th.AssertDeepEquals(t, s.DNSNameservers, []interface{}{}) |
| 181 | th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{ |
| 182 | AllocationPool{ |
| 183 | Start: "192.0.0.2", |
| 184 | End: "192.255.255.254", |
| 185 | }, |
| 186 | }) |
| 187 | th.AssertDeepEquals(t, s.HostRoutes, []interface{}{}) |
| 188 | th.AssertEquals(t, s.IPVersion, 4) |
| 189 | th.AssertEquals(t, s.GatewayIP, "192.0.0.1") |
| 190 | th.AssertEquals(t, s.CIDR, "192.0.0.0/8") |
| 191 | th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0b") |
Jamie Hannaford | 89f9af2 | 2014-09-17 12:21:48 +0200 | [diff] [blame] | 192 | } |