blob: 7bd6fcfc260df5fa8052998c58d0a1984aa2cd11 [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
129 expected := []Endpoint{
130 Endpoint{
131 ID: "12",
132 Interface: InterfacePublic,
133 Name: "the-endiest-of-points",
134 Region: "underground",
135 ServiceID: "asdfasdfasdfasdf",
136 URL: "https://1.2.3.4:9000/",
137 },
138 Endpoint{
139 ID: "13",
140 Interface: InterfaceInternal,
141 Name: "shhhh",
142 Region: "underground",
143 ServiceID: "asdfasdfasdfasdf",
144 URL: "https://1.2.3.4:9001/",
145 },
146 }
147
148 if !reflect.DeepEqual(expected, actual) {
149 t.Errorf("Expected %#v, got %#v", expected, actual)
150 }
151}
152
153func TestUpdateEndpoint(t *testing.T) {
154 testhelper.SetupHTTP()
155 defer testhelper.TeardownHTTP()
156
157 testhelper.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) {
158 testhelper.TestMethod(t, r, "GET")
159 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
160 testhelper.TestJSONRequest(t, r, `
161 {
162 "endpoint": {
163 "name": "renamed",
164 "region": "somewhere-else"
165 }
166 }
167 `)
168
169 fmt.Fprintf(w, `
170 {
171 "endpoint": {
172 "id": "12",
173 "interface": "public",
174 "links": {
175 "self": "https://localhost:5000/v3/endpoints/12"
176 },
177 "name": "renamed",
178 "region": "somewhere-else",
179 "service_id": "asdfasdfasdfasdf",
180 "url": "https://1.2.3.4:9000/"
181 }
182 }
183 `)
184 })
185
186 client := serviceClient()
187 actual, err := Update(client, "12", EndpointOpts{
188 Name: "renamed",
189 Region: "somewhere-else",
190 })
191 if err != nil {
192 t.Fatalf("Unexpected error from Update: %v", err)
193 }
194
195 expected := Endpoint{
196 ID: "12",
197 Interface: InterfacePublic,
198 Name: "renamed",
199 Region: "somewhere-else",
200 ServiceID: "asdfasdfasdfasdf",
201 URL: "https://1.2.3.4:9000/",
202 }
203 if !reflect.DeepEqual(expected, actual) {
204 t.Errorf("Expected %#v, was %#v", expected, actual)
205 }
206}
207
208func TestDeleteEndpoint(t *testing.T) {
209 testhelper.SetupHTTP()
210 defer testhelper.TeardownHTTP()
211
212 testhelper.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) {
213 testhelper.TestMethod(t, r, "DELETE")
214 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
215
216 w.WriteHeader(http.StatusNoContent)
217 })
218
219 client := serviceClient()
220
221 err := Delete(client, "34")
222 if err != nil {
223 t.Fatalf("Unexpected error from Delete: %v", err)
224 }
225}