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