blob: 84cb2dade864baa32794c9d3026c45d83929913a [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
Ash Wilsonab6be612014-09-15 15:51:22 -040094 w.Header().Add("Content-Type", "application/json")
Ash Wilsonbdfc3302014-09-04 10:16:28 -040095 fmt.Fprintf(w, `
Ash Wilson8df23c82014-09-05 14:18:20 -040096 {
97 "endpoints": [
98 {
99 "id": "12",
100 "interface": "public",
101 "links": {
102 "self": "https://localhost:5000/v3/endpoints/12"
103 },
104 "name": "the-endiest-of-points",
105 "region": "underground",
106 "service_id": "asdfasdfasdfasdf",
107 "url": "https://1.2.3.4:9000/"
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400108 },
Ash Wilson8df23c82014-09-05 14:18:20 -0400109 {
110 "id": "13",
111 "interface": "internal",
112 "links": {
113 "self": "https://localhost:5000/v3/endpoints/13"
114 },
115 "name": "shhhh",
116 "region": "underground",
117 "service_id": "asdfasdfasdfasdf",
118 "url": "https://1.2.3.4:9001/"
119 }
120 ],
121 "links": {
122 "next": null,
123 "previous": null
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400124 }
Ash Wilson8df23c82014-09-05 14:18:20 -0400125 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400126 `)
127 })
128
129 client := serviceClient()
130
Ash Wilson6269f252014-09-12 14:33:56 -0400131 count := 0
Ash Wilson6b35e502014-09-12 15:15:23 -0400132 List(client, ListOpts{}).EachPage(func(page gophercloud.Page) (bool, error) {
Ash Wilson6269f252014-09-12 14:33:56 -0400133 count++
134 actual, err := ExtractEndpoints(page)
135 if err != nil {
136 t.Errorf("Failed to extract endpoints: %v", err)
Ash Wilson6b35e502014-09-12 15:15:23 -0400137 return false, err
Ash Wilson6269f252014-09-12 14:33:56 -0400138 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400139
Ash Wilson6269f252014-09-12 14:33:56 -0400140 expected := []Endpoint{
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400141 Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400142 ID: "12",
143 Availability: gophercloud.AvailabilityPublic,
144 Name: "the-endiest-of-points",
145 Region: "underground",
146 ServiceID: "asdfasdfasdfasdf",
147 URL: "https://1.2.3.4:9000/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400148 },
149 Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400150 ID: "13",
151 Availability: gophercloud.AvailabilityInternal,
152 Name: "shhhh",
153 Region: "underground",
154 ServiceID: "asdfasdfasdfasdf",
155 URL: "https://1.2.3.4:9001/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400156 },
Ash Wilson6269f252014-09-12 14:33:56 -0400157 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400158
Ash Wilson6269f252014-09-12 14:33:56 -0400159 if !reflect.DeepEqual(expected, actual) {
160 t.Errorf("Expected %#v, got %#v", expected, actual)
161 }
162
Ash Wilson6b35e502014-09-12 15:15:23 -0400163 return true, nil
Ash Wilson6269f252014-09-12 14:33:56 -0400164 })
165 if count != 1 {
166 t.Errorf("Expected 1 page, got %d", count)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400167 }
168}
169
170func TestUpdateEndpoint(t *testing.T) {
171 testhelper.SetupHTTP()
172 defer testhelper.TeardownHTTP()
173
174 testhelper.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) {
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400175 testhelper.TestMethod(t, r, "PATCH")
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400176 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
177 testhelper.TestJSONRequest(t, r, `
178 {
179 "endpoint": {
180 "name": "renamed",
181 "region": "somewhere-else"
182 }
183 }
184 `)
185
186 fmt.Fprintf(w, `
187 {
188 "endpoint": {
189 "id": "12",
190 "interface": "public",
191 "links": {
192 "self": "https://localhost:5000/v3/endpoints/12"
193 },
194 "name": "renamed",
195 "region": "somewhere-else",
196 "service_id": "asdfasdfasdfasdf",
197 "url": "https://1.2.3.4:9000/"
198 }
199 }
200 `)
201 })
202
203 client := serviceClient()
204 actual, err := Update(client, "12", EndpointOpts{
205 Name: "renamed",
206 Region: "somewhere-else",
207 })
208 if err != nil {
209 t.Fatalf("Unexpected error from Update: %v", err)
210 }
211
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400212 expected := &Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400213 ID: "12",
214 Availability: gophercloud.AvailabilityPublic,
215 Name: "renamed",
216 Region: "somewhere-else",
217 ServiceID: "asdfasdfasdfasdf",
218 URL: "https://1.2.3.4:9000/",
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400219 }
220 if !reflect.DeepEqual(expected, actual) {
221 t.Errorf("Expected %#v, was %#v", expected, actual)
222 }
223}
224
225func TestDeleteEndpoint(t *testing.T) {
226 testhelper.SetupHTTP()
227 defer testhelper.TeardownHTTP()
228
229 testhelper.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) {
230 testhelper.TestMethod(t, r, "DELETE")
231 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
232
233 w.WriteHeader(http.StatusNoContent)
234 })
235
236 client := serviceClient()
237
238 err := Delete(client, "34")
239 if err != nil {
240 t.Fatalf("Unexpected error from Delete: %v", err)
241 }
242}