blob: 77a2292bac737aa26bc372d3ca2488cdb1972b38 [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
41 fmt.Fprintf(w, `
42 {
43 "endpoint": {
44 "id": "12",
45 "interface": "public",
46 "links": {
47 "self": "https://localhost:5000/v3/endpoints/12"
48 },
49 "name": "the-endiest-of-points",
50 "region": "underground",
51 "service_id": "asdfasdfasdfasdf",
52 "url": "https://1.2.3.4:9000/"
53 }
54 }
55 `)
56 })
57
58 client := serviceClient()
59
60 result, err := Create(client, EndpointOpts{
61 Interface: InterfacePublic,
62 Name: "the-endiest-of-points",
63 Region: "underground",
64 URL: "https://1.2.3.4:9000/",
65 ServiceID: "asdfasdfasdfasdf",
66 })
67 if err != nil {
68 t.Fatalf("Unable to create an endpoint: %v", err)
69 }
70
71 expected := Endpoint{
72 ID: "12",
73 Interface: InterfacePublic,
74 Name: "the-endiest-of-points",
75 Region: "underground",
76 ServiceID: "asdfasdfasdfasdf",
77 URL: "https://1.2.3.4:9000/",
78 }
79
80 if !reflect.DeepEqual(result, expected) {
81 t.Errorf("Expected %#v, was %#v", expected, result)
82 }
83}
84
85func TestListEndpoints(t *testing.T) {
86 testhelper.SetupHTTP()
87 defer testhelper.TeardownHTTP()
88
89 testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
90 testhelper.TestMethod(t, r, "GET")
91 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
92
93 fmt.Fprintf(w, `
94 [
95 {
96 "id": "12",
97 "interface": "public",
98 "links": {
99 "self": "https://localhost:5000/v3/endpoints/12"
100 },
101 "name": "the-endiest-of-points",
102 "region": "underground",
103 "service_id": "asdfasdfasdfasdf",
104 "url": "https://1.2.3.4:9000/"
105 },
106 {
107 "id": "13",
108 "interface": "internal",
109 "links": {
110 "self": "https://localhost:5000/v3/endpoints/13"
111 },
112 "name": "shhhh",
113 "region": "underground",
114 "service_id": "asdfasdfasdfasdf",
115 "url": "https://1.2.3.4:9001/"
116 }
117 ]
118 `)
119 })
120
121 client := serviceClient()
122
123 actual, err := List(client, ListOpts{})
124 if err != nil {
125 t.Fatalf("Unexpected error listing endpoints: %v", err)
126 }
127
128 expected := []Endpoint{
129 Endpoint{
130 ID: "12",
131 Interface: InterfacePublic,
132 Name: "the-endiest-of-points",
133 Region: "underground",
134 ServiceID: "asdfasdfasdfasdf",
135 URL: "https://1.2.3.4:9000/",
136 },
137 Endpoint{
138 ID: "13",
139 Interface: InterfaceInternal,
140 Name: "shhhh",
141 Region: "underground",
142 ServiceID: "asdfasdfasdfasdf",
143 URL: "https://1.2.3.4:9001/",
144 },
145 }
146
147 if !reflect.DeepEqual(expected, actual) {
148 t.Errorf("Expected %#v, got %#v", expected, actual)
149 }
150}
151
152func TestUpdateEndpoint(t *testing.T) {
153 testhelper.SetupHTTP()
154 defer testhelper.TeardownHTTP()
155
156 testhelper.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) {
157 testhelper.TestMethod(t, r, "GET")
158 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
159 testhelper.TestJSONRequest(t, r, `
160 {
161 "endpoint": {
162 "name": "renamed",
163 "region": "somewhere-else"
164 }
165 }
166 `)
167
168 fmt.Fprintf(w, `
169 {
170 "endpoint": {
171 "id": "12",
172 "interface": "public",
173 "links": {
174 "self": "https://localhost:5000/v3/endpoints/12"
175 },
176 "name": "renamed",
177 "region": "somewhere-else",
178 "service_id": "asdfasdfasdfasdf",
179 "url": "https://1.2.3.4:9000/"
180 }
181 }
182 `)
183 })
184
185 client := serviceClient()
186 actual, err := Update(client, "12", EndpointOpts{
187 Name: "renamed",
188 Region: "somewhere-else",
189 })
190 if err != nil {
191 t.Fatalf("Unexpected error from Update: %v", err)
192 }
193
194 expected := Endpoint{
195 ID: "12",
196 Interface: InterfacePublic,
197 Name: "renamed",
198 Region: "somewhere-else",
199 ServiceID: "asdfasdfasdfasdf",
200 URL: "https://1.2.3.4:9000/",
201 }
202 if !reflect.DeepEqual(expected, actual) {
203 t.Errorf("Expected %#v, was %#v", expected, actual)
204 }
205}
206
207func TestDeleteEndpoint(t *testing.T) {
208 testhelper.SetupHTTP()
209 defer testhelper.TeardownHTTP()
210
211 testhelper.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) {
212 testhelper.TestMethod(t, r, "DELETE")
213 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
214
215 w.WriteHeader(http.StatusNoContent)
216 })
217
218 client := serviceClient()
219
220 err := Delete(client, "34")
221 if err != nil {
222 t.Fatalf("Unexpected error from Delete: %v", err)
223 }
224}