blob: f988770d35c4ca2eb1a5c8c254a75aef9eef9300 [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"
10 "github.com/rackspace/gophercloud/testhelper"
11)
12
13const tokenID = "abcabcabcabc"
14
15func serviceClient() *gophercloud.ServiceClient {
16 return &gophercloud.ServiceClient{
17 Provider: &gophercloud.ProviderClient{TokenID: tokenID},
18 Endpoint: testhelper.Endpoint(),
19 }
20}
21
22func TestCreateSuccessful(t *testing.T) {
23 testhelper.SetupHTTP()
24 defer testhelper.TeardownHTTP()
25
26 testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
27 testhelper.TestMethod(t, r, "POST")
28 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
29 testhelper.TestJSONRequest(t, r, `
30 {
31 "endpoint": {
32 "interface": "public",
33 "name": "the-endiest-of-points",
34 "region": "underground",
35 "url": "https://1.2.3.4:9000/",
36 "service_id": "asdfasdfasdfasdf"
37 }
38 }
39 `)
40
Ash Wilson989ce542014-09-04 10:52:49 -040041 w.WriteHeader(http.StatusCreated)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040042 fmt.Fprintf(w, `
43 {
44 "endpoint": {
45 "id": "12",
46 "interface": "public",
47 "links": {
48 "self": "https://localhost:5000/v3/endpoints/12"
49 },
50 "name": "the-endiest-of-points",
51 "region": "underground",
52 "service_id": "asdfasdfasdfasdf",
53 "url": "https://1.2.3.4:9000/"
54 }
55 }
56 `)
57 })
58
59 client := serviceClient()
60
61 result, err := Create(client, EndpointOpts{
Ash Wilsonefac18b2014-09-10 14:44:42 -040062 Availability: gophercloud.AvailabilityPublic,
63 Name: "the-endiest-of-points",
64 Region: "underground",
65 URL: "https://1.2.3.4:9000/",
66 ServiceID: "asdfasdfasdfasdf",
Ash Wilsonbdfc3302014-09-04 10:16:28 -040067 })
68 if err != nil {
69 t.Fatalf("Unable to create an endpoint: %v", err)
70 }
71
Ash Wilson989ce542014-09-04 10:52:49 -040072 expected := &Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -040073 ID: "12",
74 Availability: gophercloud.AvailabilityPublic,
75 Name: "the-endiest-of-points",
76 Region: "underground",
77 ServiceID: "asdfasdfasdfasdf",
78 URL: "https://1.2.3.4:9000/",
Ash Wilsonbdfc3302014-09-04 10:16:28 -040079 }
80
81 if !reflect.DeepEqual(result, expected) {
82 t.Errorf("Expected %#v, was %#v", expected, result)
83 }
84}
85
86func TestListEndpoints(t *testing.T) {
87 testhelper.SetupHTTP()
88 defer testhelper.TeardownHTTP()
89
90 testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
91 testhelper.TestMethod(t, r, "GET")
92 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
93
94 fmt.Fprintf(w, `
Ash Wilson8df23c82014-09-05 14:18:20 -040095 {
96 "endpoints": [
97 {
98 "id": "12",
99 "interface": "public",
100 "links": {
101 "self": "https://localhost:5000/v3/endpoints/12"
102 },
103 "name": "the-endiest-of-points",
104 "region": "underground",
105 "service_id": "asdfasdfasdfasdf",
106 "url": "https://1.2.3.4:9000/"
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400107 },
Ash Wilson8df23c82014-09-05 14:18:20 -0400108 {
109 "id": "13",
110 "interface": "internal",
111 "links": {
112 "self": "https://localhost:5000/v3/endpoints/13"
113 },
114 "name": "shhhh",
115 "region": "underground",
116 "service_id": "asdfasdfasdfasdf",
117 "url": "https://1.2.3.4:9001/"
118 }
119 ],
120 "links": {
121 "next": null,
122 "previous": null
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400123 }
Ash Wilson8df23c82014-09-05 14:18:20 -0400124 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400125 `)
126 })
127
128 client := serviceClient()
129
Ash Wilson6269f252014-09-12 14:33:56 -0400130 count := 0
Ash Wilson6b35e502014-09-12 15:15:23 -0400131 List(client, ListOpts{}).EachPage(func(page gophercloud.Page) (bool, error) {
Ash Wilson6269f252014-09-12 14:33:56 -0400132 count++
133 actual, err := ExtractEndpoints(page)
134 if err != nil {
135 t.Errorf("Failed to extract endpoints: %v", err)
Ash Wilson6b35e502014-09-12 15:15:23 -0400136 return false, err
Ash Wilson6269f252014-09-12 14:33:56 -0400137 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400138
Ash Wilson6269f252014-09-12 14:33:56 -0400139 expected := []Endpoint{
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400140 Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400141 ID: "12",
142 Availability: gophercloud.AvailabilityPublic,
143 Name: "the-endiest-of-points",
144 Region: "underground",
145 ServiceID: "asdfasdfasdfasdf",
146 URL: "https://1.2.3.4:9000/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400147 },
148 Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400149 ID: "13",
150 Availability: gophercloud.AvailabilityInternal,
151 Name: "shhhh",
152 Region: "underground",
153 ServiceID: "asdfasdfasdfasdf",
154 URL: "https://1.2.3.4:9001/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400155 },
Ash Wilson6269f252014-09-12 14:33:56 -0400156 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400157
Ash Wilson6269f252014-09-12 14:33:56 -0400158 if !reflect.DeepEqual(expected, actual) {
159 t.Errorf("Expected %#v, got %#v", expected, actual)
160 }
161
Ash Wilson6b35e502014-09-12 15:15:23 -0400162 return true, nil
Ash Wilson6269f252014-09-12 14:33:56 -0400163 })
164 if count != 1 {
165 t.Errorf("Expected 1 page, got %d", count)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400166 }
167}
168
169func TestUpdateEndpoint(t *testing.T) {
170 testhelper.SetupHTTP()
171 defer testhelper.TeardownHTTP()
172
173 testhelper.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) {
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400174 testhelper.TestMethod(t, r, "PATCH")
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400175 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
176 testhelper.TestJSONRequest(t, r, `
177 {
178 "endpoint": {
179 "name": "renamed",
180 "region": "somewhere-else"
181 }
182 }
183 `)
184
185 fmt.Fprintf(w, `
186 {
187 "endpoint": {
188 "id": "12",
189 "interface": "public",
190 "links": {
191 "self": "https://localhost:5000/v3/endpoints/12"
192 },
193 "name": "renamed",
194 "region": "somewhere-else",
195 "service_id": "asdfasdfasdfasdf",
196 "url": "https://1.2.3.4:9000/"
197 }
198 }
199 `)
200 })
201
202 client := serviceClient()
203 actual, err := Update(client, "12", EndpointOpts{
204 Name: "renamed",
205 Region: "somewhere-else",
206 })
207 if err != nil {
208 t.Fatalf("Unexpected error from Update: %v", err)
209 }
210
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400211 expected := &Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400212 ID: "12",
213 Availability: gophercloud.AvailabilityPublic,
214 Name: "renamed",
215 Region: "somewhere-else",
216 ServiceID: "asdfasdfasdfasdf",
217 URL: "https://1.2.3.4:9000/",
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400218 }
219 if !reflect.DeepEqual(expected, actual) {
220 t.Errorf("Expected %#v, was %#v", expected, actual)
221 }
222}
223
224func TestDeleteEndpoint(t *testing.T) {
225 testhelper.SetupHTTP()
226 defer testhelper.TeardownHTTP()
227
228 testhelper.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) {
229 testhelper.TestMethod(t, r, "DELETE")
230 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
231
232 w.WriteHeader(http.StatusNoContent)
233 })
234
235 client := serviceClient()
236
237 err := Delete(client, "34")
238 if err != nil {
239 t.Fatalf("Unexpected error from Delete: %v", err)
240 }
241}