blob: ed12c3472067c4d1c52317f5459ac58208ea8f07 [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{
62 Interface: InterfacePublic,
63 Name: "the-endiest-of-points",
64 Region: "underground",
65 URL: "https://1.2.3.4:9000/",
66 ServiceID: "asdfasdfasdfasdf",
67 })
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 Wilsonbdfc3302014-09-04 10:16:28 -040073 ID: "12",
74 Interface: InterfacePublic,
75 Name: "the-endiest-of-points",
76 Region: "underground",
77 ServiceID: "asdfasdfasdfasdf",
78 URL: "https://1.2.3.4:9000/",
79 }
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, `
95 [
96 {
97 "id": "12",
98 "interface": "public",
99 "links": {
100 "self": "https://localhost:5000/v3/endpoints/12"
101 },
102 "name": "the-endiest-of-points",
103 "region": "underground",
104 "service_id": "asdfasdfasdfasdf",
105 "url": "https://1.2.3.4:9000/"
106 },
107 {
108 "id": "13",
109 "interface": "internal",
110 "links": {
111 "self": "https://localhost:5000/v3/endpoints/13"
112 },
113 "name": "shhhh",
114 "region": "underground",
115 "service_id": "asdfasdfasdfasdf",
116 "url": "https://1.2.3.4:9001/"
117 }
118 ]
119 `)
120 })
121
122 client := serviceClient()
123
124 actual, err := List(client, ListOpts{})
125 if err != nil {
126 t.Fatalf("Unexpected error listing endpoints: %v", err)
127 }
128
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400129 expected := &EndpointList{
130 Endpoints: []Endpoint{
131 Endpoint{
132 ID: "12",
133 Interface: InterfacePublic,
134 Name: "the-endiest-of-points",
135 Region: "underground",
136 ServiceID: "asdfasdfasdfasdf",
137 URL: "https://1.2.3.4:9000/",
138 },
139 Endpoint{
140 ID: "13",
141 Interface: InterfaceInternal,
142 Name: "shhhh",
143 Region: "underground",
144 ServiceID: "asdfasdfasdfasdf",
145 URL: "https://1.2.3.4:9001/",
146 },
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400147 },
148 }
149
150 if !reflect.DeepEqual(expected, actual) {
151 t.Errorf("Expected %#v, got %#v", expected, actual)
152 }
153}
154
155func TestUpdateEndpoint(t *testing.T) {
156 testhelper.SetupHTTP()
157 defer testhelper.TeardownHTTP()
158
159 testhelper.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) {
160 testhelper.TestMethod(t, r, "GET")
161 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
162 testhelper.TestJSONRequest(t, r, `
163 {
164 "endpoint": {
165 "name": "renamed",
166 "region": "somewhere-else"
167 }
168 }
169 `)
170
171 fmt.Fprintf(w, `
172 {
173 "endpoint": {
174 "id": "12",
175 "interface": "public",
176 "links": {
177 "self": "https://localhost:5000/v3/endpoints/12"
178 },
179 "name": "renamed",
180 "region": "somewhere-else",
181 "service_id": "asdfasdfasdfasdf",
182 "url": "https://1.2.3.4:9000/"
183 }
184 }
185 `)
186 })
187
188 client := serviceClient()
189 actual, err := Update(client, "12", EndpointOpts{
190 Name: "renamed",
191 Region: "somewhere-else",
192 })
193 if err != nil {
194 t.Fatalf("Unexpected error from Update: %v", err)
195 }
196
197 expected := Endpoint{
198 ID: "12",
199 Interface: InterfacePublic,
200 Name: "renamed",
201 Region: "somewhere-else",
202 ServiceID: "asdfasdfasdfasdf",
203 URL: "https://1.2.3.4:9000/",
204 }
205 if !reflect.DeepEqual(expected, actual) {
206 t.Errorf("Expected %#v, was %#v", expected, actual)
207 }
208}
209
210func TestDeleteEndpoint(t *testing.T) {
211 testhelper.SetupHTTP()
212 defer testhelper.TeardownHTTP()
213
214 testhelper.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) {
215 testhelper.TestMethod(t, r, "DELETE")
216 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
217
218 w.WriteHeader(http.StatusNoContent)
219 })
220
221 client := serviceClient()
222
223 err := Delete(client, "34")
224 if err != nil {
225 t.Fatalf("Unexpected error from Delete: %v", err)
226 }
227}