blob: 051d7796364d4b5cc79f3d384c4f01caea126883 [file] [log] [blame]
Jon Perritt9da13a12015-01-20 19:25:15 -07001package services
2
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
10)
11
12func TestList(t *testing.T) {
13 th.SetupHTTP()
14 defer th.TeardownHTTP()
15
16 HandleListCDNServiceSuccessfully(t)
17
18 count := 0
19
20 err := List(fake.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
21 count++
22 actual, err := ExtractServices(page)
23 if err != nil {
24 t.Errorf("Failed to extract services: %v", err)
25 return false, err
26 }
27
28 expected := []Service{
29 Service{
30 ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
31 Name: "mywebsite.com",
32 Domains: []Domain{
33 Domain{
34 Domain: "www.mywebsite.com",
35 },
36 },
37 Origins: []Origin{
38 Origin{
39 Origin: "mywebsite.com",
40 Port: 80,
41 SSL: false,
42 },
43 },
44 Caching: []CacheRule{
45 CacheRule{
46 Name: "default",
47 TTL: 3600,
48 },
49 CacheRule{
50 Name: "home",
51 TTL: 17200,
52 Rules: []TTLRule{
53 TTLRule{
54 Name: "index",
55 RequestURL: "/index.htm",
56 },
57 },
58 },
59 CacheRule{
60 Name: "images",
61 TTL: 12800,
62 Rules: []TTLRule{
63 TTLRule{
64 Name: "images",
65 RequestURL: "*.png",
66 },
67 },
68 },
69 },
70 Restrictions: []Restriction{
71 Restriction{
72 Name: "website only",
73 Rules: []RestrictionRule{
74 RestrictionRule{
75 Name: "mywebsite.com",
76 Referrer: "www.mywebsite.com",
77 },
78 },
79 },
80 },
81 FlavorID: "asia",
82 Status: "deployed",
83 Errors: []Error{},
84 Links: []gophercloud.Link{
85 gophercloud.Link{
86 Href: "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
87 Rel: "self",
88 },
89 gophercloud.Link{
90 Href: "mywebsite.com.cdn123.poppycdn.net",
91 Rel: "access_url",
92 },
93 gophercloud.Link{
94 Href: "https://www.poppycdn.io/v1.0/flavors/asia",
95 Rel: "flavor",
96 },
97 },
98 },
99 Service{
100 ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f1",
101 Name: "myothersite.com",
102 Domains: []Domain{
103 Domain{
104 Domain: "www.myothersite.com",
105 },
106 },
107 Origins: []Origin{
108 Origin{
109 Origin: "44.33.22.11",
110 Port: 80,
111 SSL: false,
112 },
113 Origin{
114 Origin: "77.66.55.44",
115 Port: 80,
116 SSL: false,
117 Rules: []OriginRule{
118 OriginRule{
119 Name: "videos",
120 RequestURL: "^/videos/*.m3u",
121 },
122 },
123 },
124 },
125 Caching: []CacheRule{
126 CacheRule{
127 Name: "default",
128 TTL: 3600,
129 },
130 },
131 Restrictions: []Restriction{},
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500132 FlavorID: "europe",
133 Status: "deployed",
Jon Perritt9da13a12015-01-20 19:25:15 -0700134 Links: []gophercloud.Link{
135 gophercloud.Link{
136 Href: "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f1",
137 Rel: "self",
138 },
139 gophercloud.Link{
140 Href: "myothersite.com.poppycdn.net",
141 Rel: "access_url",
142 },
143 gophercloud.Link{
144 Href: "https://www.poppycdn.io/v1.0/flavors/europe",
145 Rel: "flavor",
146 },
147 },
148 },
149 }
150
151 th.CheckDeepEquals(t, expected, actual)
152
153 return true, nil
154 })
155 th.AssertNoErr(t, err)
156
157 if count != 1 {
158 t.Errorf("Expected 1 page, got %d", count)
159 }
160}
161
162func TestCreate(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500163 th.SetupHTTP()
164 defer th.TeardownHTTP()
Jon Perritt9da13a12015-01-20 19:25:15 -0700165
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500166 HandleCreateCDNServiceSuccessfully(t)
Jon Perritt9da13a12015-01-20 19:25:15 -0700167
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500168 createOpts := CreateOpts{
169 Name: "mywebsite.com",
170 Domains: []Domain{
171 Domain{
172 Domain: "www.mywebsite.com",
173 },
174 Domain{
175 Domain: "blog.mywebsite.com",
176 },
177 },
178 Origins: []Origin{
179 Origin{
180 Origin: "mywebsite.com",
181 Port: 80,
182 SSL: false,
183 },
184 },
185 Restrictions: []Restriction{
186 Restriction{
187 Name: "website only",
188 Rules: []RestrictionRule{
189 RestrictionRule{
190 Name: "mywebsite.com",
191 Referrer: "www.mywebsite.com",
192 },
193 },
194 },
195 },
196 Caching: []CacheRule{
197 CacheRule{
198 Name: "default",
199 TTL: 3600,
200 },
201 },
202 FlavorID: "cdn",
203 }
Jon Perritt9da13a12015-01-20 19:25:15 -0700204
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500205 expected := "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0"
206 actual, err := Create(fake.ServiceClient(), createOpts).Extract()
207 th.AssertNoErr(t, err)
208 th.AssertEquals(t, expected, actual)
Jon Perritt9da13a12015-01-20 19:25:15 -0700209}
210
211func TestGet(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500212 th.SetupHTTP()
213 defer th.TeardownHTTP()
Jon Perritt9da13a12015-01-20 19:25:15 -0700214
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500215 HandleGetCDNServiceSuccessfully(t)
Jon Perritt9da13a12015-01-20 19:25:15 -0700216
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500217 expected := &Service{
218 ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
219 Name: "mywebsite.com",
220 Domains: []Domain{
221 Domain{
222 Domain: "www.mywebsite.com",
223 Protocol: "http",
224 },
225 },
226 Origins: []Origin{
227 Origin{
228 Origin: "mywebsite.com",
229 Port: 80,
230 SSL: false,
231 },
232 },
233 Caching: []CacheRule{
234 CacheRule{
235 Name: "default",
236 TTL: 3600,
237 },
238 CacheRule{
239 Name: "home",
240 TTL: 17200,
241 Rules: []TTLRule{
242 TTLRule{
243 Name: "index",
244 RequestURL: "/index.htm",
245 },
246 },
247 },
248 CacheRule{
249 Name: "images",
250 TTL: 12800,
251 Rules: []TTLRule{
252 TTLRule{
253 Name: "images",
254 RequestURL: "*.png",
255 },
256 },
257 },
258 },
259 Restrictions: []Restriction{
260 Restriction{
261 Name: "website only",
262 Rules: []RestrictionRule{
263 RestrictionRule{
264 Name: "mywebsite.com",
265 Referrer: "www.mywebsite.com",
266 },
267 },
268 },
269 },
270 FlavorID: "cdn",
271 Status: "deployed",
272 Errors: []Error{},
273 Links: []gophercloud.Link{
274 gophercloud.Link{
275 Href: "https://global.cdn.api.rackspacecloud.com/v1.0/110011/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
276 Rel: "self",
277 },
278 gophercloud.Link{
279 Href: "blog.mywebsite.com.cdn1.raxcdn.com",
280 Rel: "access_url",
281 },
282 gophercloud.Link{
283 Href: "https://global.cdn.api.rackspacecloud.com/v1.0/110011/flavors/cdn",
284 Rel: "flavor",
285 },
286 },
287 }
Jon Perritt9da13a12015-01-20 19:25:15 -0700288
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500289 actual, err := Get(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0").Extract()
290 th.AssertNoErr(t, err)
291 th.AssertDeepEquals(t, expected, actual)
Jon Perritt9da13a12015-01-20 19:25:15 -0700292}
293
Jon Perritt1e5e2932015-01-27 12:13:19 -0700294func TestSuccessfulUpdate(t *testing.T) {
Jon Perritt1e5e2932015-01-27 12:13:19 -0700295 th.SetupHTTP()
296 defer th.TeardownHTTP()
297
298 HandleUpdateCDNServiceSuccessfully(t)
299
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500300 expected := "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0"
Jon Perritt31919cd2015-01-28 10:34:09 -0700301 updateOpts := UpdateOpts{
302 UpdateOpt{
303 Op: Replace,
304 Path: "/origins/0",
305 Value: map[string]interface{}{
306 "origin": "44.33.22.11",
307 "port": 80,
308 "ssl": false,
Ash Wilson163e4592015-01-29 12:03:28 -0500309 },
310 },
Jon Perritt31919cd2015-01-28 10:34:09 -0700311 UpdateOpt{
312 Op: Add,
313 Path: "/domains/0",
314 Value: map[string]interface{}{
315 "domain": "added.mocksite4.com",
316 },
Ash Wilson163e4592015-01-29 12:03:28 -0500317 },
Jon Perritt31919cd2015-01-28 10:34:09 -0700318 }
319 actual, err := Update(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", updateOpts).Extract()
320 th.AssertNoErr(t, err)
321 th.AssertEquals(t, expected, actual)
322}
323
324func TestUnsuccessfulUpdate(t *testing.T) {
325 th.SetupHTTP()
326 defer th.TeardownHTTP()
327
328 HandleUpdateCDNServiceSuccessfully(t)
329
330 updateOpts := UpdateOpts{
331 UpdateOpt{
332 Op: "Foo",
333 Path: "/origins/0",
334 Value: map[string]interface{}{
335 "origin": "44.33.22.11",
336 "port": 80,
337 "ssl": false,
338 },
339 },
340 UpdateOpt{
341 Op: Add,
342 Path: "/domains/0",
343 Value: map[string]interface{}{
344 "domain": "added.mocksite4.com",
Ash Wilson163e4592015-01-29 12:03:28 -0500345 },
346 },
347 // Remove a single CacheRule
348 Removal{
349 Index: 8,
350 Path: PathCaching,
Jon Perritt1e5e2932015-01-27 12:13:19 -0700351 },
Ash Wilsond842ae62015-01-29 13:11:50 -0500352 // Bulk removal
353 Removal{
354 All: true,
355 Path: PathCaching,
356 },
357 // Service name replacement
358 NameReplacement{
359 NewName: "differentServiceName",
360 },
Jon Perritt1e5e2932015-01-27 12:13:19 -0700361 }
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500362
363 actual, err := Update(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", ops).Extract()
364 th.AssertNoErr(t, err)
365 th.AssertEquals(t, expected, actual)
Jon Perritt1e5e2932015-01-27 12:13:19 -0700366}
367
Jon Perritt9da13a12015-01-20 19:25:15 -0700368func TestDelete(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500369 th.SetupHTTP()
370 defer th.TeardownHTTP()
Jon Perritt9da13a12015-01-20 19:25:15 -0700371
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500372 HandleDeleteCDNServiceSuccessfully(t)
Jon Perritt9da13a12015-01-20 19:25:15 -0700373
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500374 err := Delete(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0").ExtractErr()
375 th.AssertNoErr(t, err)
Jon Perritt9da13a12015-01-20 19:25:15 -0700376}