blob: 80687c4cb7018cef6a62c875dfc115d700fd9832 [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"
Ash Wilson3c8cc772014-09-16 11:40:49 -040010 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonbdfc3302014-09-04 10:16:28 -040011 "github.com/rackspace/gophercloud/testhelper"
Ash Wilsonea8c7f32014-10-22 09:23:04 -040012 "github.com/rackspace/gophercloud/testhelper/client"
Ash Wilsonbdfc3302014-09-04 10:16:28 -040013)
14
Ash Wilsonbdfc3302014-09-04 10:16:28 -040015func TestCreateSuccessful(t *testing.T) {
16 testhelper.SetupHTTP()
17 defer testhelper.TeardownHTTP()
18
19 testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
20 testhelper.TestMethod(t, r, "POST")
Ash Wilsonea8c7f32014-10-22 09:23:04 -040021 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040022 testhelper.TestJSONRequest(t, r, `
23 {
24 "endpoint": {
25 "interface": "public",
26 "name": "the-endiest-of-points",
27 "region": "underground",
28 "url": "https://1.2.3.4:9000/",
29 "service_id": "asdfasdfasdfasdf"
30 }
31 }
32 `)
33
Ash Wilson989ce542014-09-04 10:52:49 -040034 w.WriteHeader(http.StatusCreated)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040035 fmt.Fprintf(w, `
36 {
37 "endpoint": {
38 "id": "12",
39 "interface": "public",
40 "links": {
41 "self": "https://localhost:5000/v3/endpoints/12"
42 },
43 "name": "the-endiest-of-points",
44 "region": "underground",
45 "service_id": "asdfasdfasdfasdf",
46 "url": "https://1.2.3.4:9000/"
47 }
48 }
49 `)
50 })
51
Ash Wilsonea8c7f32014-10-22 09:23:04 -040052 actual, err := Create(client.ServiceClient(), EndpointOpts{
Ash Wilsonefac18b2014-09-10 14:44:42 -040053 Availability: gophercloud.AvailabilityPublic,
54 Name: "the-endiest-of-points",
55 Region: "underground",
56 URL: "https://1.2.3.4:9000/",
57 ServiceID: "asdfasdfasdfasdf",
Ash Wilson3f59ade2014-10-02 09:22:23 -040058 }).Extract()
Ash Wilsonbdfc3302014-09-04 10:16:28 -040059 if err != nil {
60 t.Fatalf("Unable to create an endpoint: %v", err)
61 }
62
Ash Wilson989ce542014-09-04 10:52:49 -040063 expected := &Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -040064 ID: "12",
65 Availability: gophercloud.AvailabilityPublic,
66 Name: "the-endiest-of-points",
67 Region: "underground",
68 ServiceID: "asdfasdfasdfasdf",
69 URL: "https://1.2.3.4:9000/",
Ash Wilsonbdfc3302014-09-04 10:16:28 -040070 }
71
Ash Wilson3f59ade2014-10-02 09:22:23 -040072 if !reflect.DeepEqual(actual, expected) {
73 t.Errorf("Expected %#v, was %#v", expected, actual)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040074 }
75}
76
77func TestListEndpoints(t *testing.T) {
78 testhelper.SetupHTTP()
79 defer testhelper.TeardownHTTP()
80
81 testhelper.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
82 testhelper.TestMethod(t, r, "GET")
Ash Wilsonea8c7f32014-10-22 09:23:04 -040083 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040084
Ash Wilsonab6be612014-09-15 15:51:22 -040085 w.Header().Add("Content-Type", "application/json")
Ash Wilsonbdfc3302014-09-04 10:16:28 -040086 fmt.Fprintf(w, `
Ash Wilson8df23c82014-09-05 14:18:20 -040087 {
88 "endpoints": [
89 {
90 "id": "12",
91 "interface": "public",
92 "links": {
93 "self": "https://localhost:5000/v3/endpoints/12"
94 },
95 "name": "the-endiest-of-points",
96 "region": "underground",
97 "service_id": "asdfasdfasdfasdf",
98 "url": "https://1.2.3.4:9000/"
Ash Wilsonbdfc3302014-09-04 10:16:28 -040099 },
Ash Wilson8df23c82014-09-05 14:18:20 -0400100 {
101 "id": "13",
102 "interface": "internal",
103 "links": {
104 "self": "https://localhost:5000/v3/endpoints/13"
105 },
106 "name": "shhhh",
107 "region": "underground",
108 "service_id": "asdfasdfasdfasdf",
109 "url": "https://1.2.3.4:9001/"
110 }
111 ],
112 "links": {
113 "next": null,
114 "previous": null
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400115 }
Ash Wilson8df23c82014-09-05 14:18:20 -0400116 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400117 `)
118 })
119
Ash Wilson6269f252014-09-12 14:33:56 -0400120 count := 0
Ash Wilsonea8c7f32014-10-22 09:23:04 -0400121 List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson6269f252014-09-12 14:33:56 -0400122 count++
123 actual, err := ExtractEndpoints(page)
124 if err != nil {
125 t.Errorf("Failed to extract endpoints: %v", err)
Ash Wilson6b35e502014-09-12 15:15:23 -0400126 return false, err
Ash Wilson6269f252014-09-12 14:33:56 -0400127 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400128
Ash Wilson6269f252014-09-12 14:33:56 -0400129 expected := []Endpoint{
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400130 Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400131 ID: "12",
132 Availability: gophercloud.AvailabilityPublic,
133 Name: "the-endiest-of-points",
134 Region: "underground",
135 ServiceID: "asdfasdfasdfasdf",
136 URL: "https://1.2.3.4:9000/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400137 },
138 Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400139 ID: "13",
140 Availability: gophercloud.AvailabilityInternal,
141 Name: "shhhh",
142 Region: "underground",
143 ServiceID: "asdfasdfasdfasdf",
144 URL: "https://1.2.3.4:9001/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400145 },
Ash Wilson6269f252014-09-12 14:33:56 -0400146 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400147
Ash Wilson6269f252014-09-12 14:33:56 -0400148 if !reflect.DeepEqual(expected, actual) {
149 t.Errorf("Expected %#v, got %#v", expected, actual)
150 }
151
Ash Wilson6b35e502014-09-12 15:15:23 -0400152 return true, nil
Ash Wilson6269f252014-09-12 14:33:56 -0400153 })
154 if count != 1 {
155 t.Errorf("Expected 1 page, got %d", count)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400156 }
157}
158
159func TestUpdateEndpoint(t *testing.T) {
160 testhelper.SetupHTTP()
161 defer testhelper.TeardownHTTP()
162
163 testhelper.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) {
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400164 testhelper.TestMethod(t, r, "PATCH")
Ash Wilsonea8c7f32014-10-22 09:23:04 -0400165 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400166 testhelper.TestJSONRequest(t, r, `
167 {
168 "endpoint": {
169 "name": "renamed",
170 "region": "somewhere-else"
171 }
172 }
173 `)
174
175 fmt.Fprintf(w, `
176 {
177 "endpoint": {
178 "id": "12",
179 "interface": "public",
180 "links": {
181 "self": "https://localhost:5000/v3/endpoints/12"
182 },
183 "name": "renamed",
184 "region": "somewhere-else",
185 "service_id": "asdfasdfasdfasdf",
186 "url": "https://1.2.3.4:9000/"
187 }
188 }
189 `)
190 })
191
Ash Wilsonea8c7f32014-10-22 09:23:04 -0400192 actual, err := Update(client.ServiceClient(), "12", EndpointOpts{
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400193 Name: "renamed",
194 Region: "somewhere-else",
Ash Wilson3f59ade2014-10-02 09:22:23 -0400195 }).Extract()
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400196 if err != nil {
197 t.Fatalf("Unexpected error from Update: %v", err)
198 }
199
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400200 expected := &Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400201 ID: "12",
202 Availability: gophercloud.AvailabilityPublic,
203 Name: "renamed",
204 Region: "somewhere-else",
205 ServiceID: "asdfasdfasdfasdf",
206 URL: "https://1.2.3.4:9000/",
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400207 }
208 if !reflect.DeepEqual(expected, actual) {
209 t.Errorf("Expected %#v, was %#v", expected, actual)
210 }
211}
212
213func TestDeleteEndpoint(t *testing.T) {
214 testhelper.SetupHTTP()
215 defer testhelper.TeardownHTTP()
216
217 testhelper.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) {
218 testhelper.TestMethod(t, r, "DELETE")
Ash Wilsonea8c7f32014-10-22 09:23:04 -0400219 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400220
221 w.WriteHeader(http.StatusNoContent)
222 })
223
Jamie Hannaford3c086742014-10-27 11:32:16 +0100224 res := Delete(client.ServiceClient(), "34")
225 testhelper.AssertNoErr(t, res.Err)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400226}