blob: 0abc98ef3d9face16f8627d4cf6d45051d8f6f56 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Jon Perritt9da13a12015-01-20 19:25:15 -07002
3import (
4 "testing"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
jrperritt3d966162016-06-06 14:08:54 -05007 "github.com/gophercloud/gophercloud/openstack/cdn/v1/services"
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud/pagination"
9 th "github.com/gophercloud/gophercloud/testhelper"
10 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jon Perritt9da13a12015-01-20 19:25:15 -070011)
12
13func TestList(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
16
17 HandleListCDNServiceSuccessfully(t)
18
19 count := 0
20
jrperritt3d966162016-06-06 14:08:54 -050021 err := services.List(fake.ServiceClient(), &services.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt9da13a12015-01-20 19:25:15 -070022 count++
jrperritt3d966162016-06-06 14:08:54 -050023 actual, err := services.ExtractServices(page)
Jon Perritt9da13a12015-01-20 19:25:15 -070024 if err != nil {
25 t.Errorf("Failed to extract services: %v", err)
26 return false, err
27 }
28
jrperritt3d966162016-06-06 14:08:54 -050029 expected := []services.Service{
30 {
Jon Perritt9da13a12015-01-20 19:25:15 -070031 ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
32 Name: "mywebsite.com",
jrperritt3d966162016-06-06 14:08:54 -050033 Domains: []services.Domain{
34 {
Jon Perritt9da13a12015-01-20 19:25:15 -070035 Domain: "www.mywebsite.com",
36 },
37 },
jrperritt3d966162016-06-06 14:08:54 -050038 Origins: []services.Origin{
39 {
Jon Perritt9da13a12015-01-20 19:25:15 -070040 Origin: "mywebsite.com",
41 Port: 80,
42 SSL: false,
43 },
44 },
jrperritt3d966162016-06-06 14:08:54 -050045 Caching: []services.CacheRule{
46 {
Jon Perritt9da13a12015-01-20 19:25:15 -070047 Name: "default",
48 TTL: 3600,
49 },
jrperritt3d966162016-06-06 14:08:54 -050050 {
Jon Perritt9da13a12015-01-20 19:25:15 -070051 Name: "home",
52 TTL: 17200,
jrperritt3d966162016-06-06 14:08:54 -050053 Rules: []services.TTLRule{
54 {
Jon Perritt9da13a12015-01-20 19:25:15 -070055 Name: "index",
56 RequestURL: "/index.htm",
57 },
58 },
59 },
jrperritt3d966162016-06-06 14:08:54 -050060 {
Jon Perritt9da13a12015-01-20 19:25:15 -070061 Name: "images",
62 TTL: 12800,
jrperritt3d966162016-06-06 14:08:54 -050063 Rules: []services.TTLRule{
64 {
Jon Perritt9da13a12015-01-20 19:25:15 -070065 Name: "images",
66 RequestURL: "*.png",
67 },
68 },
69 },
70 },
jrperritt3d966162016-06-06 14:08:54 -050071 Restrictions: []services.Restriction{
72 {
Jon Perritt9da13a12015-01-20 19:25:15 -070073 Name: "website only",
jrperritt3d966162016-06-06 14:08:54 -050074 Rules: []services.RestrictionRule{
75 {
Jon Perritt9da13a12015-01-20 19:25:15 -070076 Name: "mywebsite.com",
77 Referrer: "www.mywebsite.com",
78 },
79 },
80 },
81 },
82 FlavorID: "asia",
83 Status: "deployed",
jrperritt3d966162016-06-06 14:08:54 -050084 Errors: []services.Error{},
Jon Perritt9da13a12015-01-20 19:25:15 -070085 Links: []gophercloud.Link{
jrperritt3d966162016-06-06 14:08:54 -050086 {
Jon Perritt9da13a12015-01-20 19:25:15 -070087 Href: "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
88 Rel: "self",
89 },
jrperritt3d966162016-06-06 14:08:54 -050090 {
Jon Perritt9da13a12015-01-20 19:25:15 -070091 Href: "mywebsite.com.cdn123.poppycdn.net",
92 Rel: "access_url",
93 },
jrperritt3d966162016-06-06 14:08:54 -050094 {
Jon Perritt9da13a12015-01-20 19:25:15 -070095 Href: "https://www.poppycdn.io/v1.0/flavors/asia",
96 Rel: "flavor",
97 },
98 },
99 },
jrperritt3d966162016-06-06 14:08:54 -0500100 {
Jon Perritt9da13a12015-01-20 19:25:15 -0700101 ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f1",
102 Name: "myothersite.com",
jrperritt3d966162016-06-06 14:08:54 -0500103 Domains: []services.Domain{
104 {
Jon Perritt9da13a12015-01-20 19:25:15 -0700105 Domain: "www.myothersite.com",
106 },
107 },
jrperritt3d966162016-06-06 14:08:54 -0500108 Origins: []services.Origin{
109 {
Jon Perritt9da13a12015-01-20 19:25:15 -0700110 Origin: "44.33.22.11",
111 Port: 80,
112 SSL: false,
113 },
jrperritt3d966162016-06-06 14:08:54 -0500114 {
Jon Perritt9da13a12015-01-20 19:25:15 -0700115 Origin: "77.66.55.44",
116 Port: 80,
117 SSL: false,
jrperritt3d966162016-06-06 14:08:54 -0500118 Rules: []services.OriginRule{
119 {
Jon Perritt9da13a12015-01-20 19:25:15 -0700120 Name: "videos",
121 RequestURL: "^/videos/*.m3u",
122 },
123 },
124 },
125 },
jrperritt3d966162016-06-06 14:08:54 -0500126 Caching: []services.CacheRule{
127 {
Jon Perritt9da13a12015-01-20 19:25:15 -0700128 Name: "default",
129 TTL: 3600,
130 },
131 },
jrperritt3d966162016-06-06 14:08:54 -0500132 Restrictions: []services.Restriction{},
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500133 FlavorID: "europe",
134 Status: "deployed",
Jon Perritt9da13a12015-01-20 19:25:15 -0700135 Links: []gophercloud.Link{
136 gophercloud.Link{
137 Href: "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f1",
138 Rel: "self",
139 },
140 gophercloud.Link{
141 Href: "myothersite.com.poppycdn.net",
142 Rel: "access_url",
143 },
144 gophercloud.Link{
145 Href: "https://www.poppycdn.io/v1.0/flavors/europe",
146 Rel: "flavor",
147 },
148 },
149 },
150 }
151
152 th.CheckDeepEquals(t, expected, actual)
153
154 return true, nil
155 })
156 th.AssertNoErr(t, err)
157
158 if count != 1 {
159 t.Errorf("Expected 1 page, got %d", count)
160 }
161}
162
163func TestCreate(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500164 th.SetupHTTP()
165 defer th.TeardownHTTP()
Jon Perritt9da13a12015-01-20 19:25:15 -0700166
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500167 HandleCreateCDNServiceSuccessfully(t)
Jon Perritt9da13a12015-01-20 19:25:15 -0700168
jrperritt3d966162016-06-06 14:08:54 -0500169 createOpts := services.CreateOpts{
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500170 Name: "mywebsite.com",
jrperritt3d966162016-06-06 14:08:54 -0500171 Domains: []services.Domain{
172 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500173 Domain: "www.mywebsite.com",
174 },
jrperritt3d966162016-06-06 14:08:54 -0500175 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500176 Domain: "blog.mywebsite.com",
177 },
178 },
jrperritt3d966162016-06-06 14:08:54 -0500179 Origins: []services.Origin{
180 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500181 Origin: "mywebsite.com",
182 Port: 80,
183 SSL: false,
184 },
185 },
jrperritt3d966162016-06-06 14:08:54 -0500186 Restrictions: []services.Restriction{
187 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500188 Name: "website only",
jrperritt3d966162016-06-06 14:08:54 -0500189 Rules: []services.RestrictionRule{
190 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500191 Name: "mywebsite.com",
192 Referrer: "www.mywebsite.com",
193 },
194 },
195 },
196 },
jrperritt3d966162016-06-06 14:08:54 -0500197 Caching: []services.CacheRule{
198 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500199 Name: "default",
200 TTL: 3600,
201 },
202 },
203 FlavorID: "cdn",
204 }
Jon Perritt9da13a12015-01-20 19:25:15 -0700205
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500206 expected := "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0"
jrperritt3d966162016-06-06 14:08:54 -0500207 actual, err := services.Create(fake.ServiceClient(), createOpts).Extract()
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500208 th.AssertNoErr(t, err)
209 th.AssertEquals(t, expected, actual)
Jon Perritt9da13a12015-01-20 19:25:15 -0700210}
211
212func TestGet(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500213 th.SetupHTTP()
214 defer th.TeardownHTTP()
Jon Perritt9da13a12015-01-20 19:25:15 -0700215
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500216 HandleGetCDNServiceSuccessfully(t)
Jon Perritt9da13a12015-01-20 19:25:15 -0700217
jrperritt3d966162016-06-06 14:08:54 -0500218 expected := &services.Service{
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500219 ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
220 Name: "mywebsite.com",
jrperritt3d966162016-06-06 14:08:54 -0500221 Domains: []services.Domain{
222 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500223 Domain: "www.mywebsite.com",
224 Protocol: "http",
225 },
226 },
jrperritt3d966162016-06-06 14:08:54 -0500227 Origins: []services.Origin{
228 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500229 Origin: "mywebsite.com",
230 Port: 80,
231 SSL: false,
232 },
233 },
jrperritt3d966162016-06-06 14:08:54 -0500234 Caching: []services.CacheRule{
235 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500236 Name: "default",
237 TTL: 3600,
238 },
jrperritt3d966162016-06-06 14:08:54 -0500239 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500240 Name: "home",
241 TTL: 17200,
jrperritt3d966162016-06-06 14:08:54 -0500242 Rules: []services.TTLRule{
243 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500244 Name: "index",
245 RequestURL: "/index.htm",
246 },
247 },
248 },
jrperritt3d966162016-06-06 14:08:54 -0500249 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500250 Name: "images",
251 TTL: 12800,
jrperritt3d966162016-06-06 14:08:54 -0500252 Rules: []services.TTLRule{
253 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500254 Name: "images",
255 RequestURL: "*.png",
256 },
257 },
258 },
259 },
jrperritt3d966162016-06-06 14:08:54 -0500260 Restrictions: []services.Restriction{
261 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500262 Name: "website only",
jrperritt3d966162016-06-06 14:08:54 -0500263 Rules: []services.RestrictionRule{
264 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500265 Name: "mywebsite.com",
266 Referrer: "www.mywebsite.com",
267 },
268 },
269 },
270 },
271 FlavorID: "cdn",
272 Status: "deployed",
jrperritt3d966162016-06-06 14:08:54 -0500273 Errors: []services.Error{},
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500274 Links: []gophercloud.Link{
jrperritt3d966162016-06-06 14:08:54 -0500275 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500276 Href: "https://global.cdn.api.rackspacecloud.com/v1.0/110011/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
277 Rel: "self",
278 },
jrperritt3d966162016-06-06 14:08:54 -0500279 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500280 Href: "blog.mywebsite.com.cdn1.raxcdn.com",
281 Rel: "access_url",
282 },
jrperritt3d966162016-06-06 14:08:54 -0500283 {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500284 Href: "https://global.cdn.api.rackspacecloud.com/v1.0/110011/flavors/cdn",
285 Rel: "flavor",
286 },
287 },
288 }
Jon Perritt9da13a12015-01-20 19:25:15 -0700289
jrperritt3d966162016-06-06 14:08:54 -0500290 actual, err := services.Get(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0").Extract()
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500291 th.AssertNoErr(t, err)
292 th.AssertDeepEquals(t, expected, actual)
Jon Perritt9da13a12015-01-20 19:25:15 -0700293}
294
Jon Perritt1e5e2932015-01-27 12:13:19 -0700295func TestSuccessfulUpdate(t *testing.T) {
Jon Perritt1e5e2932015-01-27 12:13:19 -0700296 th.SetupHTTP()
297 defer th.TeardownHTTP()
298
299 HandleUpdateCDNServiceSuccessfully(t)
300
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500301 expected := "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0"
jrperritt3d966162016-06-06 14:08:54 -0500302 ops := services.UpdateOpts{
Ash Wilson163e4592015-01-29 12:03:28 -0500303 // Append a single Domain
jrperritt3d966162016-06-06 14:08:54 -0500304 services.Append{Value: services.Domain{Domain: "appended.mocksite4.com"}},
Ash Wilson163e4592015-01-29 12:03:28 -0500305 // Insert a single Domain
jrperritt3d966162016-06-06 14:08:54 -0500306 services.Insertion{
Ash Wilson163e4592015-01-29 12:03:28 -0500307 Index: 4,
jrperritt3d966162016-06-06 14:08:54 -0500308 Value: services.Domain{Domain: "inserted.mocksite4.com"},
Jon Perritt1e5e2932015-01-27 12:13:19 -0700309 },
Ash Wilson163e4592015-01-29 12:03:28 -0500310 // Bulk addition
jrperritt3d966162016-06-06 14:08:54 -0500311 services.Append{
312 Value: services.DomainList{
313 {Domain: "bulkadded1.mocksite4.com"},
314 {Domain: "bulkadded2.mocksite4.com"},
Ash Wilson163e4592015-01-29 12:03:28 -0500315 },
316 },
317 // Replace a single Origin
jrperritt3d966162016-06-06 14:08:54 -0500318 services.Replacement{
Ash Wilson163e4592015-01-29 12:03:28 -0500319 Index: 2,
jrperritt3d966162016-06-06 14:08:54 -0500320 Value: services.Origin{Origin: "44.33.22.11", Port: 80, SSL: false},
Ash Wilson163e4592015-01-29 12:03:28 -0500321 },
322 // Bulk replace Origins
jrperritt3d966162016-06-06 14:08:54 -0500323 services.Replacement{
Ash Wilson163e4592015-01-29 12:03:28 -0500324 Index: 0, // Ignored
jrperritt3d966162016-06-06 14:08:54 -0500325 Value: services.OriginList{
326 {Origin: "44.33.22.11", Port: 80, SSL: false},
327 {Origin: "55.44.33.22", Port: 443, SSL: true},
Ash Wilson163e4592015-01-29 12:03:28 -0500328 },
329 },
330 // Remove a single CacheRule
jrperritt3d966162016-06-06 14:08:54 -0500331 services.Removal{
Ash Wilson163e4592015-01-29 12:03:28 -0500332 Index: 8,
jrperritt3d966162016-06-06 14:08:54 -0500333 Path: services.PathCaching,
Jon Perritt1e5e2932015-01-27 12:13:19 -0700334 },
Ash Wilsond842ae62015-01-29 13:11:50 -0500335 // Bulk removal
jrperritt3d966162016-06-06 14:08:54 -0500336 services.Removal{
Ash Wilsond842ae62015-01-29 13:11:50 -0500337 All: true,
jrperritt3d966162016-06-06 14:08:54 -0500338 Path: services.PathCaching,
Ash Wilsond842ae62015-01-29 13:11:50 -0500339 },
340 // Service name replacement
jrperritt3d966162016-06-06 14:08:54 -0500341 services.NameReplacement{
Ash Wilsond842ae62015-01-29 13:11:50 -0500342 NewName: "differentServiceName",
343 },
Jon Perritt1e5e2932015-01-27 12:13:19 -0700344 }
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500345
jrperritt3d966162016-06-06 14:08:54 -0500346 actual, err := services.Update(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", ops).Extract()
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500347 th.AssertNoErr(t, err)
348 th.AssertEquals(t, expected, actual)
Jon Perritt1e5e2932015-01-27 12:13:19 -0700349}
350
Jon Perritt9da13a12015-01-20 19:25:15 -0700351func TestDelete(t *testing.T) {
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500352 th.SetupHTTP()
353 defer th.TeardownHTTP()
Jon Perritt9da13a12015-01-20 19:25:15 -0700354
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500355 HandleDeleteCDNServiceSuccessfully(t)
Jon Perritt9da13a12015-01-20 19:25:15 -0700356
jrperritt3d966162016-06-06 14:08:54 -0500357 err := services.Delete(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0").ExtractErr()
Ash Wilson4cd7aff2015-01-29 10:18:09 -0500358 th.AssertNoErr(t, err)
Jon Perritt9da13a12015-01-20 19:25:15 -0700359}