blob: 14bbe6af5622f3ee42dc1ffc56d3446267f88f80 [file] [log] [blame]
Ash Wilsonbdfc3302014-09-04 10:16:28 -04001package endpoints
2
3import (
4 "fmt"
5 "net/http"
Ash Wilsonbdfc3302014-09-04 10:16:28 -04006 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
9 "github.com/gophercloud/gophercloud/pagination"
Jon Perrittdb0ae142016-03-13 00:33:41 -060010 th "github.com/gophercloud/gophercloud/testhelper"
Jon Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud/testhelper/client"
Ash Wilsonbdfc3302014-09-04 10:16:28 -040012)
13
Ash Wilsonbdfc3302014-09-04 10:16:28 -040014func TestCreateSuccessful(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060015 th.SetupHTTP()
16 defer th.TeardownHTTP()
Ash Wilsonbdfc3302014-09-04 10:16:28 -040017
Jon Perrittdb0ae142016-03-13 00:33:41 -060018 th.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
19 th.TestMethod(t, r, "POST")
20 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
21 th.TestJSONRequest(t, r, `
Ash Wilsonbdfc3302014-09-04 10:16:28 -040022 {
23 "endpoint": {
24 "interface": "public",
25 "name": "the-endiest-of-points",
26 "region": "underground",
27 "url": "https://1.2.3.4:9000/",
28 "service_id": "asdfasdfasdfasdf"
29 }
30 }
31 `)
32
Ash Wilson989ce542014-09-04 10:52:49 -040033 w.WriteHeader(http.StatusCreated)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040034 fmt.Fprintf(w, `
35 {
36 "endpoint": {
37 "id": "12",
38 "interface": "public",
39 "links": {
40 "self": "https://localhost:5000/v3/endpoints/12"
41 },
42 "name": "the-endiest-of-points",
43 "region": "underground",
44 "service_id": "asdfasdfasdfasdf",
45 "url": "https://1.2.3.4:9000/"
46 }
47 }
48 `)
49 })
50
Jon Perrittdb0ae142016-03-13 00:33:41 -060051 actual, err := Create(client.ServiceClient(), CreateOpts{
Ash Wilsonefac18b2014-09-10 14:44:42 -040052 Availability: gophercloud.AvailabilityPublic,
53 Name: "the-endiest-of-points",
54 Region: "underground",
55 URL: "https://1.2.3.4:9000/",
56 ServiceID: "asdfasdfasdfasdf",
Ash Wilson3f59ade2014-10-02 09:22:23 -040057 }).Extract()
Jon Perrittdb0ae142016-03-13 00:33:41 -060058 th.AssertNoErr(t, err)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040059
Ash Wilson989ce542014-09-04 10:52:49 -040060 expected := &Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -040061 ID: "12",
62 Availability: gophercloud.AvailabilityPublic,
63 Name: "the-endiest-of-points",
64 Region: "underground",
65 ServiceID: "asdfasdfasdfasdf",
66 URL: "https://1.2.3.4:9000/",
Ash Wilsonbdfc3302014-09-04 10:16:28 -040067 }
68
Jon Perrittdb0ae142016-03-13 00:33:41 -060069 th.AssertDeepEquals(t, expected, actual)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040070}
71
72func TestListEndpoints(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060073 th.SetupHTTP()
74 defer th.TeardownHTTP()
Ash Wilsonbdfc3302014-09-04 10:16:28 -040075
Jon Perrittdb0ae142016-03-13 00:33:41 -060076 th.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
77 th.TestMethod(t, r, "GET")
78 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonbdfc3302014-09-04 10:16:28 -040079
Ash Wilsonab6be612014-09-15 15:51:22 -040080 w.Header().Add("Content-Type", "application/json")
Ash Wilsonbdfc3302014-09-04 10:16:28 -040081 fmt.Fprintf(w, `
Ash Wilson8df23c82014-09-05 14:18:20 -040082 {
83 "endpoints": [
84 {
85 "id": "12",
86 "interface": "public",
87 "links": {
88 "self": "https://localhost:5000/v3/endpoints/12"
89 },
90 "name": "the-endiest-of-points",
91 "region": "underground",
92 "service_id": "asdfasdfasdfasdf",
93 "url": "https://1.2.3.4:9000/"
Ash Wilsonbdfc3302014-09-04 10:16:28 -040094 },
Ash Wilson8df23c82014-09-05 14:18:20 -040095 {
96 "id": "13",
97 "interface": "internal",
98 "links": {
99 "self": "https://localhost:5000/v3/endpoints/13"
100 },
101 "name": "shhhh",
102 "region": "underground",
103 "service_id": "asdfasdfasdfasdf",
104 "url": "https://1.2.3.4:9001/"
105 }
106 ],
107 "links": {
108 "next": null,
109 "previous": null
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400110 }
Ash Wilson8df23c82014-09-05 14:18:20 -0400111 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400112 `)
113 })
114
Ash Wilson6269f252014-09-12 14:33:56 -0400115 count := 0
Ash Wilsonea8c7f32014-10-22 09:23:04 -0400116 List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson6269f252014-09-12 14:33:56 -0400117 count++
118 actual, err := ExtractEndpoints(page)
119 if err != nil {
120 t.Errorf("Failed to extract endpoints: %v", err)
Ash Wilson6b35e502014-09-12 15:15:23 -0400121 return false, err
Ash Wilson6269f252014-09-12 14:33:56 -0400122 }
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400123
Ash Wilson6269f252014-09-12 14:33:56 -0400124 expected := []Endpoint{
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400125 Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400126 ID: "12",
127 Availability: gophercloud.AvailabilityPublic,
128 Name: "the-endiest-of-points",
129 Region: "underground",
130 ServiceID: "asdfasdfasdfasdf",
131 URL: "https://1.2.3.4:9000/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400132 },
133 Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400134 ID: "13",
135 Availability: gophercloud.AvailabilityInternal,
136 Name: "shhhh",
137 Region: "underground",
138 ServiceID: "asdfasdfasdfasdf",
139 URL: "https://1.2.3.4:9001/",
Ash Wilson32c0e8d2014-09-04 10:53:08 -0400140 },
Ash Wilson6269f252014-09-12 14:33:56 -0400141 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600142 th.AssertDeepEquals(t, expected, actual)
Ash Wilson6b35e502014-09-12 15:15:23 -0400143 return true, nil
Ash Wilson6269f252014-09-12 14:33:56 -0400144 })
Jon Perrittdb0ae142016-03-13 00:33:41 -0600145 th.AssertEquals(t, 1, count)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400146}
147
148func TestUpdateEndpoint(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600149 th.SetupHTTP()
150 defer th.TeardownHTTP()
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400151
Jon Perrittdb0ae142016-03-13 00:33:41 -0600152 th.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) {
153 th.TestMethod(t, r, "PATCH")
154 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
155 th.TestJSONRequest(t, r, `
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400156 {
157 "endpoint": {
158 "name": "renamed",
159 "region": "somewhere-else"
160 }
161 }
162 `)
163
164 fmt.Fprintf(w, `
165 {
166 "endpoint": {
167 "id": "12",
168 "interface": "public",
169 "links": {
170 "self": "https://localhost:5000/v3/endpoints/12"
171 },
172 "name": "renamed",
173 "region": "somewhere-else",
174 "service_id": "asdfasdfasdfasdf",
175 "url": "https://1.2.3.4:9000/"
176 }
177 }
178 `)
179 })
180
Jon Perrittdb0ae142016-03-13 00:33:41 -0600181 actual, err := Update(client.ServiceClient(), "12", UpdateOpts{
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400182 Name: "renamed",
183 Region: "somewhere-else",
Ash Wilson3f59ade2014-10-02 09:22:23 -0400184 }).Extract()
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400185 if err != nil {
186 t.Fatalf("Unexpected error from Update: %v", err)
187 }
188
Ash Wilsonf04a74c2014-09-04 11:16:20 -0400189 expected := &Endpoint{
Ash Wilsonefac18b2014-09-10 14:44:42 -0400190 ID: "12",
191 Availability: gophercloud.AvailabilityPublic,
192 Name: "renamed",
193 Region: "somewhere-else",
194 ServiceID: "asdfasdfasdfasdf",
195 URL: "https://1.2.3.4:9000/",
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400196 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600197 th.AssertDeepEquals(t, expected, actual)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400198}
199
200func TestDeleteEndpoint(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600201 th.SetupHTTP()
202 defer th.TeardownHTTP()
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400203
Jon Perrittdb0ae142016-03-13 00:33:41 -0600204 th.Mux.HandleFunc("/endpoints/34", func(w http.ResponseWriter, r *http.Request) {
205 th.TestMethod(t, r, "DELETE")
206 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400207
208 w.WriteHeader(http.StatusNoContent)
209 })
210
Jamie Hannaford3c086742014-10-27 11:32:16 +0100211 res := Delete(client.ServiceClient(), "34")
Jon Perrittdb0ae142016-03-13 00:33:41 -0600212 th.AssertNoErr(t, res.Err)
Ash Wilsonbdfc3302014-09-04 10:16:28 -0400213}