blob: 8a2a2baa733a26685af65e739b2437e9f5669057 [file] [log] [blame]
Jon Perritt239e4d82015-01-23 10:50:36 -07001package services
2
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud"
7 os "github.com/rackspace/gophercloud/openstack/cdn/v1/services"
8 "github.com/rackspace/gophercloud/pagination"
9 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
11)
12
13func TestList(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
16
17 os.HandleListCDNServiceSuccessfully(t)
18
19 count := 0
20
21 err := List(fake.ServiceClient(), &os.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
22 count++
23 actual, err := os.ExtractServices(page)
24 if err != nil {
25 t.Errorf("Failed to extract services: %v", err)
26 return false, err
27 }
28
29 expected := []os.Service{
30 os.Service{
31 ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
32 Name: "mywebsite.com",
33 Domains: []os.Domain{
34 os.Domain{
35 Domain: "www.mywebsite.com",
36 },
37 },
38 Origins: []os.Origin{
39 os.Origin{
40 Origin: "mywebsite.com",
41 Port: 80,
42 SSL: false,
43 },
44 },
45 Caching: []os.CacheRule{
46 os.CacheRule{
47 Name: "default",
48 TTL: 3600,
49 },
50 os.CacheRule{
51 Name: "home",
52 TTL: 17200,
53 Rules: []os.TTLRule{
54 os.TTLRule{
55 Name: "index",
56 RequestURL: "/index.htm",
57 },
58 },
59 },
60 os.CacheRule{
61 Name: "images",
62 TTL: 12800,
63 Rules: []os.TTLRule{
64 os.TTLRule{
65 Name: "images",
66 RequestURL: "*.png",
67 },
68 },
69 },
70 },
71 Restrictions: []os.Restriction{
72 os.Restriction{
73 Name: "website only",
74 Rules: []os.RestrictionRule{
75 os.RestrictionRule{
76 Name: "mywebsite.com",
77 Referrer: "www.mywebsite.com",
78 },
79 },
80 },
81 },
82 FlavorID: "asia",
83 Status: "deployed",
84 Errors: []os.Error{},
85 Links: []gophercloud.Link{
86 gophercloud.Link{
87 Href: "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
88 Rel: "self",
89 },
90 gophercloud.Link{
91 Href: "mywebsite.com.cdn123.poppycdn.net",
92 Rel: "access_url",
93 },
94 gophercloud.Link{
95 Href: "https://www.poppycdn.io/v1.0/flavors/asia",
96 Rel: "flavor",
97 },
98 },
99 },
100 os.Service{
101 ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f1",
102 Name: "myothersite.com",
103 Domains: []os.Domain{
104 os.Domain{
105 Domain: "www.myothersite.com",
106 },
107 },
108 Origins: []os.Origin{
109 os.Origin{
110 Origin: "44.33.22.11",
111 Port: 80,
112 SSL: false,
113 },
114 os.Origin{
115 Origin: "77.66.55.44",
116 Port: 80,
117 SSL: false,
118 Rules: []os.OriginRule{
119 os.OriginRule{
120 Name: "videos",
121 RequestURL: "^/videos/*.m3u",
122 },
123 },
124 },
125 },
126 Caching: []os.CacheRule{
127 os.CacheRule{
128 Name: "default",
129 TTL: 3600,
130 },
131 },
132 Restrictions: []os.Restriction{},
133 FlavorID: "europe",
134 Status: "deployed",
135 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) {
164 th.SetupHTTP()
165 defer th.TeardownHTTP()
166
167 os.HandleCreateCDNServiceSuccessfully(t)
168
169 createOpts := os.CreateOpts{
170 Name: "mywebsite.com",
171 Domains: []os.Domain{
172 os.Domain{
173 Domain: "www.mywebsite.com",
174 },
175 os.Domain{
176 Domain: "blog.mywebsite.com",
177 },
178 },
179 Origins: []os.Origin{
180 os.Origin{
181 Origin: "mywebsite.com",
182 Port: 80,
183 SSL: false,
184 },
185 },
186 Restrictions: []os.Restriction{
187 os.Restriction{
188 Name: "website only",
189 Rules: []os.RestrictionRule{
190 os.RestrictionRule{
191 Name: "mywebsite.com",
192 Referrer: "www.mywebsite.com",
193 },
194 },
195 },
196 },
197 Caching: []os.CacheRule{
198 os.CacheRule{
199 Name: "default",
200 TTL: 3600,
201 },
202 },
203 FlavorID: "cdn",
204 }
205
206 expected := "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0"
207 actual, err := Create(fake.ServiceClient(), createOpts).Extract()
208 th.AssertNoErr(t, err)
209 th.AssertEquals(t, expected, actual)
210}
211
212func TestGet(t *testing.T) {
213 th.SetupHTTP()
214 defer th.TeardownHTTP()
215
216 os.HandleGetCDNServiceSuccessfully(t)
217
218 expected := &os.Service{
219 ID: "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
220 Name: "mywebsite.com",
221 Domains: []os.Domain{
222 os.Domain{
223 Domain: "www.mywebsite.com",
224 Protocol: "http",
225 },
226 },
227 Origins: []os.Origin{
228 os.Origin{
229 Origin: "mywebsite.com",
230 Port: 80,
231 SSL: false,
232 },
233 },
234 Caching: []os.CacheRule{
235 os.CacheRule{
236 Name: "default",
237 TTL: 3600,
238 },
239 os.CacheRule{
240 Name: "home",
241 TTL: 17200,
242 Rules: []os.TTLRule{
243 os.TTLRule{
244 Name: "index",
245 RequestURL: "/index.htm",
246 },
247 },
248 },
249 os.CacheRule{
250 Name: "images",
251 TTL: 12800,
252 Rules: []os.TTLRule{
253 os.TTLRule{
254 Name: "images",
255 RequestURL: "*.png",
256 },
257 },
258 },
259 },
260 Restrictions: []os.Restriction{
261 os.Restriction{
262 Name: "website only",
263 Rules: []os.RestrictionRule{
264 os.RestrictionRule{
265 Name: "mywebsite.com",
266 Referrer: "www.mywebsite.com",
267 },
268 },
269 },
270 },
271 FlavorID: "cdn",
272 Status: "deployed",
273 Errors: []os.Error{},
274 Links: []gophercloud.Link{
275 gophercloud.Link{
276 Href: "https://global.cdn.api.rackspacecloud.com/v1.0/110011/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0",
277 Rel: "self",
278 },
279 gophercloud.Link{
280 Href: "blog.mywebsite.com.cdn1.raxcdn.com",
281 Rel: "access_url",
282 },
283 gophercloud.Link{
284 Href: "https://global.cdn.api.rackspacecloud.com/v1.0/110011/flavors/cdn",
285 Rel: "flavor",
286 },
287 },
288 }
289
290 actual, err := Get(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0").Extract()
291 th.AssertNoErr(t, err)
292 th.AssertDeepEquals(t, expected, actual)
293}
294
Jon Perritt1e5e2932015-01-27 12:13:19 -0700295func TestSuccessfulUpdate(t *testing.T) {
Jon Perritt239e4d82015-01-23 10:50:36 -0700296 th.SetupHTTP()
297 defer th.TeardownHTTP()
298
299 os.HandleUpdateCDNServiceSuccessfully(t)
300
301 expected := "https://www.poppycdn.io/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0"
Ash Wilsonb5fcaa82015-01-29 10:34:00 -0500302 ops := []os.Patch{
303 os.Replacement{
304 Value: os.Origin{
305 Origin: "44.33.22.11",
306 Port: 80,
307 SSL: false,
Jon Perritt239e4d82015-01-23 10:50:36 -0700308 },
Ash Wilsonb5fcaa82015-01-29 10:34:00 -0500309 Index: 0,
Jon Perritt239e4d82015-01-23 10:50:36 -0700310 },
Ash Wilsonb5fcaa82015-01-29 10:34:00 -0500311 os.Addition{
312 Value: os.Domain{Domain: "added.mocksite4.com"},
Jon Perritt239e4d82015-01-23 10:50:36 -0700313 },
314 }
Ash Wilsonb5fcaa82015-01-29 10:34:00 -0500315
316 actual, err := Update(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0", ops).Extract()
Jon Perritt239e4d82015-01-23 10:50:36 -0700317 th.AssertNoErr(t, err)
318 th.AssertEquals(t, expected, actual)
319}
320
Jon Perritt239e4d82015-01-23 10:50:36 -0700321func TestDelete(t *testing.T) {
322 th.SetupHTTP()
323 defer th.TeardownHTTP()
324
325 os.HandleDeleteCDNServiceSuccessfully(t)
326
327 err := Delete(fake.ServiceClient(), "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0").ExtractErr()
328 th.AssertNoErr(t, err)
329}