blob: 1269b22cd8df8f0dcdb4bffc79072458497e1a8f [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilsonbdfc3302014-09-04 10:16:28 -04002
3import (
4 "fmt"
5 "net/http"
Ash Wilsonbdfc3302014-09-04 10:16:28 -04006 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02009 "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 Wilsonbdfc3302014-09-04 10:16:28 -040013)
14
Ash Wilsonbdfc3302014-09-04 10:16:28 -040015func TestCreateSuccessful(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060016 th.SetupHTTP()
17 defer th.TeardownHTTP()
Ash Wilsonbdfc3302014-09-04 10:16:28 -040018
Jon Perrittdb0ae142016-03-13 00:33:41 -060019 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 Wilsonbdfc3302014-09-04 10:16:28 -040023 {
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 Wilson989ce542014-09-04 10:52:49 -040034 w.WriteHeader(http.StatusCreated)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040035 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
jrperritt3d966162016-06-06 14:08:54 -050052 actual, err := endpoints.Create(client.ServiceClient(), endpoints.CreateOpts{
Ash Wilsonefac18b2014-09-10 14:44:42 -040053 Availability: gophercloud.AvailabilityPublic,
54 Name: "the-endiest-of-points",
55 Region: "underground",
56 URL: "https://1.2.3.4:9000/",
57 ServiceID: "asdfasdfasdfasdf",
Ash Wilson3f59ade2014-10-02 09:22:23 -040058 }).Extract()
Jon Perrittdb0ae142016-03-13 00:33:41 -060059 th.AssertNoErr(t, err)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040060
jrperritt3d966162016-06-06 14:08:54 -050061 expected := &endpoints.Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -040062 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 Wilsonbdfc3302014-09-04 10:16:28 -040068 }
69
Jon Perrittdb0ae142016-03-13 00:33:41 -060070 th.AssertDeepEquals(t, expected, actual)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040071}
72
73func TestListEndpoints(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060074 th.SetupHTTP()
75 defer th.TeardownHTTP()
Ash Wilsonbdfc3302014-09-04 10:16:28 -040076
Jon Perrittdb0ae142016-03-13 00:33:41 -060077 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 Wilsonbdfc3302014-09-04 10:16:28 -040080
Ash Wilsonab6be612014-09-15 15:51:22 -040081 w.Header().Add("Content-Type", "application/json")
Ash Wilsonbdfc3302014-09-04 10:16:28 -040082 fmt.Fprintf(w, `
Ash Wilson8df23c82014-09-05 14:18:20 -040083 {
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 Wilsonbdfc3302014-09-04 10:16:28 -040095 },
Ash Wilson8df23c82014-09-05 14:18:20 -040096 {
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 Wilsonbdfc3302014-09-04 10:16:28 -0400111 }
Ash Wilson8df23c82014-09-05 14:18:20 -0400112 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400113 `)
114 })
115
Ash Wilson6269f252014-09-12 14:33:56 -0400116 count := 0
jrperritt3d966162016-06-06 14:08:54 -0500117 endpoints.List(client.ServiceClient(), endpoints.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson6269f252014-09-12 14:33:56 -0400118 count++
jrperritt3d966162016-06-06 14:08:54 -0500119 actual, err := endpoints.ExtractEndpoints(page)
Ash Wilson6269f252014-09-12 14:33:56 -0400120 if err != nil {
121 t.Errorf("Failed to extract endpoints: %v", err)
Ash Wilson6b35e502014-09-12 15:15:23 -0400122 return false, err
Ash Wilson6269f252014-09-12 14:33:56 -0400123 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400124
jrperritt3d966162016-06-06 14:08:54 -0500125 expected := []endpoints.Endpoint{
126 {
Ash Wilsonefac18b2014-09-10 14:44:42 -0400127 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 Wilson32c0e8d2014-09-04 10:53:08 -0400133 },
jrperritt3d966162016-06-06 14:08:54 -0500134 {
Ash Wilsonefac18b2014-09-10 14:44:42 -0400135 ID: "13",
136 Availability: gophercloud.AvailabilityInternal,
137 Name: "shhhh",
138 Region: "underground",
139 ServiceID: "asdfasdfasdfasdf",
140 URL: "https://1.2.3.4:9001/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400141 },
Ash Wilson6269f252014-09-12 14:33:56 -0400142 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600143 th.AssertDeepEquals(t, expected, actual)
Ash Wilson6b35e502014-09-12 15:15:23 -0400144 return true, nil
Ash Wilson6269f252014-09-12 14:33:56 -0400145 })
Jon Perrittdb0ae142016-03-13 00:33:41 -0600146 th.AssertEquals(t, 1, count)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400147}
148
149func TestUpdateEndpoint(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600150 th.SetupHTTP()
151 defer th.TeardownHTTP()
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400152
Jon Perrittdb0ae142016-03-13 00:33:41 -0600153 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 Wilsonbdfc3302014-09-04 10:16:28 -0400157 {
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
jrperritt3d966162016-06-06 14:08:54 -0500182 actual, err := endpoints.Update(client.ServiceClient(), "12", endpoints.UpdateOpts{
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400183 Name: "renamed",
184 Region: "somewhere-else",
Ash Wilson3f59ade2014-10-02 09:22:23 -0400185 }).Extract()
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400186 if err != nil {
187 t.Fatalf("Unexpected error from Update: %v", err)
188 }
189
jrperritt3d966162016-06-06 14:08:54 -0500190 expected := &endpoints.Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400191 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 Wilsonbdfc3302014-09-04 10:16:28 -0400197 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600198 th.AssertDeepEquals(t, expected, actual)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400199}
200
201func TestDeleteEndpoint(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600202 th.SetupHTTP()
203 defer th.TeardownHTTP()
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400204
Jon Perrittdb0ae142016-03-13 00:33:41 -0600205 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 Wilsonbdfc3302014-09-04 10:16:28 -0400208
209 w.WriteHeader(http.StatusNoContent)
210 })
211
jrperritt3d966162016-06-06 14:08:54 -0500212 res := endpoints.Delete(client.ServiceClient(), "34")
Jon Perrittdb0ae142016-03-13 00:33:41 -0600213 th.AssertNoErr(t, res.Err)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400214}