blob: c30bd551047218aa5c4a8d5926c521884250a06c [file] [log] [blame]
Ash Wilsonbdfc3302014-09-04 10:16:28 -04001package endpoints
2
3import (
4 "fmt"
5 "net/http"
6 "reflect"
7 "testing"
8
9 "github.com/rackspace/gophercloud"
Ash Wilson3c8cc772014-09-16 11:40:49 -040010 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonbdfc3302014-09-04 10:16:28 -040011 "github.com/rackspace/gophercloud/testhelper"
12)
13
14const tokenID = "abcabcabcabc"
15
16func serviceClient() *gophercloud.ServiceClient {
17 return &gophercloud.ServiceClient{
18 Provider: &gophercloud.ProviderClient{TokenID: tokenID},
19 Endpoint: testhelper.Endpoint(),
20 }
21}
22
23func TestCreateSuccessful(t *testing.T) {
24 testhelper.SetupHTTP()
25 defer testhelper.TeardownHTTP()
26
27 testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
28 testhelper.TestMethod(t, r, "POST")
29 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
30 testhelper.TestJSONRequest(t, r, `
31 {
32 "endpoint": {
33 "interface": "public",
34 "name": "the-endiest-of-points",
35 "region": "underground",
36 "url": "https://1.2.3.4:9000/",
37 "service_id": "asdfasdfasdfasdf"
38 }
39 }
40 `)
41
Ash Wilson989ce542014-09-04 10:52:49 -040042 w.WriteHeader(http.StatusCreated)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040043 fmt.Fprintf(w, `
44 {
45 "endpoint": {
46 "id": "12",
47 "interface": "public",
48 "links": {
49 "self": "https://localhost:5000/v3/endpoints/12"
50 },
51 "name": "the-endiest-of-points",
52 "region": "underground",
53 "service_id": "asdfasdfasdfasdf",
54 "url": "https://1.2.3.4:9000/"
55 }
56 }
57 `)
58 })
59
60 client := serviceClient()
61
Ash Wilson3f59ade2014-10-02 09:22:23 -040062 actual, err := Create(client, EndpointOpts{
Ash Wilsonefac18b2014-09-10 14:44:42 -040063 Availability: gophercloud.AvailabilityPublic,
64 Name: "the-endiest-of-points",
65 Region: "underground",
66 URL: "https://1.2.3.4:9000/",
67 ServiceID: "asdfasdfasdfasdf",
Ash Wilson3f59ade2014-10-02 09:22:23 -040068 }).Extract()
Ash Wilsonbdfc3302014-09-04 10:16:28 -040069 if err != nil {
70 t.Fatalf("Unable to create an endpoint: %v", err)
71 }
72
Ash Wilson989ce542014-09-04 10:52:49 -040073 expected := &Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -040074 ID: "12",
75 Availability: gophercloud.AvailabilityPublic,
76 Name: "the-endiest-of-points",
77 Region: "underground",
78 ServiceID: "asdfasdfasdfasdf",
79 URL: "https://1.2.3.4:9000/",
Ash Wilsonbdfc3302014-09-04 10:16:28 -040080 }
81
Ash Wilson3f59ade2014-10-02 09:22:23 -040082 if !reflect.DeepEqual(actual, expected) {
83 t.Errorf("Expected %#v, was %#v", expected, actual)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040084 }
85}
86
87func TestListEndpoints(t *testing.T) {
88 testhelper.SetupHTTP()
89 defer testhelper.TeardownHTTP()
90
91 testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
92 testhelper.TestMethod(t, r, "GET")
93 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
94
Ash Wilsonab6be612014-09-15 15:51:22 -040095 w.Header().Add("Content-Type", "application/json")
Ash Wilsonbdfc3302014-09-04 10:16:28 -040096 fmt.Fprintf(w, `
Ash Wilson8df23c82014-09-05 14:18:20 -040097 {
98 "endpoints": [
99 {
100 "id": "12",
101 "interface": "public",
102 "links": {
103 "self": "https://localhost:5000/v3/endpoints/12"
104 },
105 "name": "the-endiest-of-points",
106 "region": "underground",
107 "service_id": "asdfasdfasdfasdf",
108 "url": "https://1.2.3.4:9000/"
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400109 },
Ash Wilson8df23c82014-09-05 14:18:20 -0400110 {
111 "id": "13",
112 "interface": "internal",
113 "links": {
114 "self": "https://localhost:5000/v3/endpoints/13"
115 },
116 "name": "shhhh",
117 "region": "underground",
118 "service_id": "asdfasdfasdfasdf",
119 "url": "https://1.2.3.4:9001/"
120 }
121 ],
122 "links": {
123 "next": null,
124 "previous": null
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400125 }
Ash Wilson8df23c82014-09-05 14:18:20 -0400126 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400127 `)
128 })
129
130 client := serviceClient()
131
Ash Wilson6269f252014-09-12 14:33:56 -0400132 count := 0
Ash Wilson3c8cc772014-09-16 11:40:49 -0400133 List(client, ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson6269f252014-09-12 14:33:56 -0400134 count++
135 actual, err := ExtractEndpoints(page)
136 if err != nil {
137 t.Errorf("Failed to extract endpoints: %v", err)
Ash Wilson6b35e502014-09-12 15:15:23 -0400138 return false, err
Ash Wilson6269f252014-09-12 14:33:56 -0400139 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400140
Ash Wilson6269f252014-09-12 14:33:56 -0400141 expected := []Endpoint{
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400142 Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400143 ID: "12",
144 Availability: gophercloud.AvailabilityPublic,
145 Name: "the-endiest-of-points",
146 Region: "underground",
147 ServiceID: "asdfasdfasdfasdf",
148 URL: "https://1.2.3.4:9000/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400149 },
150 Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400151 ID: "13",
152 Availability: gophercloud.AvailabilityInternal,
153 Name: "shhhh",
154 Region: "underground",
155 ServiceID: "asdfasdfasdfasdf",
156 URL: "https://1.2.3.4:9001/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400157 },
Ash Wilson6269f252014-09-12 14:33:56 -0400158 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400159
Ash Wilson6269f252014-09-12 14:33:56 -0400160 if !reflect.DeepEqual(expected, actual) {
161 t.Errorf("Expected %#v, got %#v", expected, actual)
162 }
163
Ash Wilson6b35e502014-09-12 15:15:23 -0400164 return true, nil
Ash Wilson6269f252014-09-12 14:33:56 -0400165 })
166 if count != 1 {
167 t.Errorf("Expected 1 page, got %d", count)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400168 }
169}
170
171func TestUpdateEndpoint(t *testing.T) {
172 testhelper.SetupHTTP()
173 defer testhelper.TeardownHTTP()
174
175 testhelper.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) {
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400176 testhelper.TestMethod(t, r, "PATCH")
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400177 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
178 testhelper.TestJSONRequest(t, r, `
179 {
180 "endpoint": {
181 "name": "renamed",
182 "region": "somewhere-else"
183 }
184 }
185 `)
186
187 fmt.Fprintf(w, `
188 {
189 "endpoint": {
190 "id": "12",
191 "interface": "public",
192 "links": {
193 "self": "https://localhost:5000/v3/endpoints/12"
194 },
195 "name": "renamed",
196 "region": "somewhere-else",
197 "service_id": "asdfasdfasdfasdf",
198 "url": "https://1.2.3.4:9000/"
199 }
200 }
201 `)
202 })
203
204 client := serviceClient()
205 actual, err := Update(client, "12", EndpointOpts{
206 Name: "renamed",
207 Region: "somewhere-else",
Ash Wilson3f59ade2014-10-02 09:22:23 -0400208 }).Extract()
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400209 if err != nil {
210 t.Fatalf("Unexpected error from Update: %v", err)
211 }
212
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400213 expected := &Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400214 ID: "12",
215 Availability: gophercloud.AvailabilityPublic,
216 Name: "renamed",
217 Region: "somewhere-else",
218 ServiceID: "asdfasdfasdfasdf",
219 URL: "https://1.2.3.4:9000/",
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400220 }
221 if !reflect.DeepEqual(expected, actual) {
222 t.Errorf("Expected %#v, was %#v", expected, actual)
223 }
224}
225
226func TestDeleteEndpoint(t *testing.T) {
227 testhelper.SetupHTTP()
228 defer testhelper.TeardownHTTP()
229
230 testhelper.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) {
231 testhelper.TestMethod(t, r, "DELETE")
232 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
233
234 w.WriteHeader(http.StatusNoContent)
235 })
236
237 client := serviceClient()
238
239 err := Delete(client, "34")
240 if err != nil {
241 t.Fatalf("Unexpected error from Delete: %v", err)
242 }
243}