Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 1 | package endpoints |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
| 10 | "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
| 13 | const tokenID = "abcabcabcabc" |
| 14 | |
| 15 | func serviceClient() *gophercloud.ServiceClient { |
| 16 | return &gophercloud.ServiceClient{ |
| 17 | Provider: &gophercloud.ProviderClient{TokenID: tokenID}, |
| 18 | Endpoint: testhelper.Endpoint(), |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func TestCreateSuccessful(t *testing.T) { |
| 23 | testhelper.SetupHTTP() |
| 24 | defer testhelper.TeardownHTTP() |
| 25 | |
| 26 | testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) { |
| 27 | testhelper.TestMethod(t, r, "POST") |
| 28 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 29 | testhelper.TestJSONRequest(t, r, ` |
| 30 | { |
| 31 | "endpoint": { |
| 32 | "interface": "public", |
| 33 | "name": "the-endiest-of-points", |
| 34 | "region": "underground", |
| 35 | "url": "https://1.2.3.4:9000/", |
| 36 | "service_id": "asdfasdfasdfasdf" |
| 37 | } |
| 38 | } |
| 39 | `) |
| 40 | |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 41 | w.WriteHeader(http.StatusCreated) |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 42 | fmt.Fprintf(w, ` |
| 43 | { |
| 44 | "endpoint": { |
| 45 | "id": "12", |
| 46 | "interface": "public", |
| 47 | "links": { |
| 48 | "self": "https://localhost:5000/v3/endpoints/12" |
| 49 | }, |
| 50 | "name": "the-endiest-of-points", |
| 51 | "region": "underground", |
| 52 | "service_id": "asdfasdfasdfasdf", |
| 53 | "url": "https://1.2.3.4:9000/" |
| 54 | } |
| 55 | } |
| 56 | `) |
| 57 | }) |
| 58 | |
| 59 | client := serviceClient() |
| 60 | |
| 61 | result, err := Create(client, EndpointOpts{ |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 62 | Availability: gophercloud.AvailabilityPublic, |
| 63 | Name: "the-endiest-of-points", |
| 64 | Region: "underground", |
| 65 | URL: "https://1.2.3.4:9000/", |
| 66 | ServiceID: "asdfasdfasdfasdf", |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 67 | }) |
| 68 | if err != nil { |
| 69 | t.Fatalf("Unable to create an endpoint: %v", err) |
| 70 | } |
| 71 | |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 72 | expected := &Endpoint{ |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 73 | ID: "12", |
| 74 | Availability: gophercloud.AvailabilityPublic, |
| 75 | Name: "the-endiest-of-points", |
| 76 | Region: "underground", |
| 77 | ServiceID: "asdfasdfasdfasdf", |
| 78 | URL: "https://1.2.3.4:9000/", |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | if !reflect.DeepEqual(result, expected) { |
| 82 | t.Errorf("Expected %#v, was %#v", expected, result) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestListEndpoints(t *testing.T) { |
| 87 | testhelper.SetupHTTP() |
| 88 | defer testhelper.TeardownHTTP() |
| 89 | |
| 90 | testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) { |
| 91 | testhelper.TestMethod(t, r, "GET") |
| 92 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 93 | |
| 94 | fmt.Fprintf(w, ` |
Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 95 | { |
| 96 | "endpoints": [ |
| 97 | { |
| 98 | "id": "12", |
| 99 | "interface": "public", |
| 100 | "links": { |
| 101 | "self": "https://localhost:5000/v3/endpoints/12" |
| 102 | }, |
| 103 | "name": "the-endiest-of-points", |
| 104 | "region": "underground", |
| 105 | "service_id": "asdfasdfasdfasdf", |
| 106 | "url": "https://1.2.3.4:9000/" |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 107 | }, |
Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 108 | { |
| 109 | "id": "13", |
| 110 | "interface": "internal", |
| 111 | "links": { |
| 112 | "self": "https://localhost:5000/v3/endpoints/13" |
| 113 | }, |
| 114 | "name": "shhhh", |
| 115 | "region": "underground", |
| 116 | "service_id": "asdfasdfasdfasdf", |
| 117 | "url": "https://1.2.3.4:9001/" |
| 118 | } |
| 119 | ], |
| 120 | "links": { |
| 121 | "next": null, |
| 122 | "previous": null |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 123 | } |
Ash Wilson | 8df23c8 | 2014-09-05 14:18:20 -0400 | [diff] [blame] | 124 | } |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 125 | `) |
| 126 | }) |
| 127 | |
| 128 | client := serviceClient() |
| 129 | |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 130 | count := 0 |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame^] | 131 | List(client, ListOpts{}).EachPage(func(page gophercloud.Page) (bool, error) { |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 132 | count++ |
| 133 | actual, err := ExtractEndpoints(page) |
| 134 | if err != nil { |
| 135 | t.Errorf("Failed to extract endpoints: %v", err) |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame^] | 136 | return false, err |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 137 | } |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 138 | |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 139 | expected := []Endpoint{ |
Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 140 | Endpoint{ |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 141 | ID: "12", |
| 142 | Availability: gophercloud.AvailabilityPublic, |
| 143 | Name: "the-endiest-of-points", |
| 144 | Region: "underground", |
| 145 | ServiceID: "asdfasdfasdfasdf", |
| 146 | URL: "https://1.2.3.4:9000/", |
Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 147 | }, |
| 148 | Endpoint{ |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 149 | ID: "13", |
| 150 | Availability: gophercloud.AvailabilityInternal, |
| 151 | Name: "shhhh", |
| 152 | Region: "underground", |
| 153 | ServiceID: "asdfasdfasdfasdf", |
| 154 | URL: "https://1.2.3.4:9001/", |
Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 155 | }, |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 156 | } |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 157 | |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 158 | if !reflect.DeepEqual(expected, actual) { |
| 159 | t.Errorf("Expected %#v, got %#v", expected, actual) |
| 160 | } |
| 161 | |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame^] | 162 | return true, nil |
Ash Wilson | 6269f25 | 2014-09-12 14:33:56 -0400 | [diff] [blame] | 163 | }) |
| 164 | if count != 1 { |
| 165 | t.Errorf("Expected 1 page, got %d", count) |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | func TestUpdateEndpoint(t *testing.T) { |
| 170 | testhelper.SetupHTTP() |
| 171 | defer testhelper.TeardownHTTP() |
| 172 | |
| 173 | testhelper.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 174 | testhelper.TestMethod(t, r, "PATCH") |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 175 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 176 | testhelper.TestJSONRequest(t, r, ` |
| 177 | { |
| 178 | "endpoint": { |
| 179 | "name": "renamed", |
| 180 | "region": "somewhere-else" |
| 181 | } |
| 182 | } |
| 183 | `) |
| 184 | |
| 185 | fmt.Fprintf(w, ` |
| 186 | { |
| 187 | "endpoint": { |
| 188 | "id": "12", |
| 189 | "interface": "public", |
| 190 | "links": { |
| 191 | "self": "https://localhost:5000/v3/endpoints/12" |
| 192 | }, |
| 193 | "name": "renamed", |
| 194 | "region": "somewhere-else", |
| 195 | "service_id": "asdfasdfasdfasdf", |
| 196 | "url": "https://1.2.3.4:9000/" |
| 197 | } |
| 198 | } |
| 199 | `) |
| 200 | }) |
| 201 | |
| 202 | client := serviceClient() |
| 203 | actual, err := Update(client, "12", EndpointOpts{ |
| 204 | Name: "renamed", |
| 205 | Region: "somewhere-else", |
| 206 | }) |
| 207 | if err != nil { |
| 208 | t.Fatalf("Unexpected error from Update: %v", err) |
| 209 | } |
| 210 | |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 211 | expected := &Endpoint{ |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 212 | ID: "12", |
| 213 | Availability: gophercloud.AvailabilityPublic, |
| 214 | Name: "renamed", |
| 215 | Region: "somewhere-else", |
| 216 | ServiceID: "asdfasdfasdfasdf", |
| 217 | URL: "https://1.2.3.4:9000/", |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 218 | } |
| 219 | if !reflect.DeepEqual(expected, actual) { |
| 220 | t.Errorf("Expected %#v, was %#v", expected, actual) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | func TestDeleteEndpoint(t *testing.T) { |
| 225 | testhelper.SetupHTTP() |
| 226 | defer testhelper.TeardownHTTP() |
| 227 | |
| 228 | testhelper.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) { |
| 229 | testhelper.TestMethod(t, r, "DELETE") |
| 230 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 231 | |
| 232 | w.WriteHeader(http.StatusNoContent) |
| 233 | }) |
| 234 | |
| 235 | client := serviceClient() |
| 236 | |
| 237 | err := Delete(client, "34") |
| 238 | if err != nil { |
| 239 | t.Fatalf("Unexpected error from Delete: %v", err) |
| 240 | } |
| 241 | } |