blob: 1f27b5930fd4e47d050b33c416732528f0a77d92 [file] [log] [blame]
Jon Perritt9da13a12015-01-20 19:25:15 -07001package services
2
3import (
4 "testing"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
7 "github.com/gophercloud/gophercloud/pagination"
8 th "github.com/gophercloud/gophercloud/testhelper"
9 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jon Perritt9da13a12015-01-20 19:25:15 -070010)
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 Perritt1bda9c12015-01-29 12:16:08 -0700301 ops := UpdateOpts{
Ash Wilson163e4592015-01-29 12:03:28 -0500302 // Append a single Domain
303 Append{Value: Domain{Domain: "appended.mocksite4.com"}},
304 // Insert a single Domain
305 Insertion{
306 Index: 4,
307 Value: Domain{Domain: "inserted.mocksite4.com"},
Jon Perritt1e5e2932015-01-27 12:13:19 -0700308 },
Ash Wilson163e4592015-01-29 12:03:28 -0500309 // Bulk addition
Ash Wilson299363d2015-01-29 10:49:40 -0500310 Append{
Ash Wilson163e4592015-01-29 12:03:28 -0500311 Value: DomainList{
312 Domain{Domain: "bulkadded1.mocksite4.com"},
313 Domain{Domain: "bulkadded2.mocksite4.com"},
314 },
315 },
316 // Replace a single Origin
317 Replacement{
318 Index: 2,
319 Value: Origin{Origin: "44.33.22.11", Port: 80, SSL: false},
320 },
321 // Bulk replace Origins
322 Replacement{
323 Index: 0, // Ignored
324 Value: OriginList{
325 Origin{Origin: "44.33.22.11", Port: 80, SSL: false},
326 Origin{Origin: "55.44.33.22", Port: 443, SSL: true},
327 },
328 },
329 // Remove a single CacheRule
330 Removal{
331 Index: 8,
332 Path: PathCaching,
Jon Perritt1e5e2932015-01-27 12:13:19 -0700333 },
Ash Wilsond842ae62015-01-29 13:11:50 -0500334 // Bulk removal
335 Removal{
336 All: true,
337 Path: PathCaching,
338 },
339 // Service name replacement
340 NameReplacement{
341 NewName: "differentServiceName",
342 },
Jon Perritt1e5e2932015-01-27 12:13:19 -0700343 }
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500344
345 actual, err := Update(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", ops).Extract()
346 th.AssertNoErr(t, err)
347 th.AssertEquals(t, expected, actual)
Jon Perritt1e5e2932015-01-27 12:13:19 -0700348}
349
Jon Perritt9da13a12015-01-20 19:25:15 -0700350func TestDelete(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500351 th.SetupHTTP()
352 defer th.TeardownHTTP()
Jon Perritt9da13a12015-01-20 19:25:15 -0700353
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500354 HandleDeleteCDNServiceSuccessfully(t)
Jon Perritt9da13a12015-01-20 19:25:15 -0700355
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500356 err := Delete(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0").ExtractErr()
357 th.AssertNoErr(t, err)
Jon Perritt9da13a12015-01-20 19:25:15 -0700358}