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