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