-
Notifications
You must be signed in to change notification settings - Fork 68
/
bucket_spec.rb
651 lines (524 loc) · 21 KB
/
bucket_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# -*- encoding: utf-8 -*-
require 'spec_helper'
require 'yaml'
require 'nokogiri'
module Aliyun
module OSS
describe "Bucket" do
before :all do
@endpoint = 'oss.aliyuncs.com'
@protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy'))
@bucket = 'rubysdk-bucket'
end
def request_path
@bucket + "." + @endpoint
end
def mock_location(location)
Nokogiri::XML::Builder.new do |xml|
xml.CreateBucketConfiguration {
xml.LocationConstraint location
}
end.to_xml
end
def mock_objects(objects, more = {})
Nokogiri::XML::Builder.new do |xml|
xml.ListBucketResult {
{
:prefix => 'Prefix',
:delimiter => 'Delimiter',
:limit => 'MaxKeys',
:marker => 'Marker',
:next_marker => 'NextMarker',
:truncated => 'IsTruncated',
:encoding => 'EncodingType'
}.map do |k, v|
xml.send(v, more[k]) if more[k]
end
objects.each do |o|
xml.Contents {
xml.Key o
xml.LastModified Time.now.to_s
xml.Type 'Normal'
xml.Size 1024
xml.StorageClass 'Standard'
xml.Etag 'etag'
xml.Owner {
xml.ID '10086'
xml.DisplayName 'CMCC'
}
}
end
(more[:common_prefixes] || []).each do |p|
xml.CommonPrefixes {
xml.Prefix p
}
end
}
end.to_xml
end
def mock_acl(acl)
Nokogiri::XML::Builder.new do |xml|
xml.AccessControlPolicy {
xml.Owner {
xml.ID 'owner_id'
xml.DisplayName 'owner_name'
}
xml.AccessControlList {
xml.Grant acl
}
}
end.to_xml
end
def mock_logging(opts)
Nokogiri::XML::Builder.new do |xml|
xml.BucketLoggingStatus {
if opts.enabled?
xml.LoggingEnabled {
xml.TargetBucket opts.target_bucket
xml.TargetPrefix opts.target_prefix
}
end
}
end.to_xml
end
def mock_website(opts)
Nokogiri::XML::Builder.new do |xml|
xml.WebsiteConfiguration {
xml.IndexDocument {
xml.Suffix opts.index
}
if opts.error
xml.ErrorDocument {
xml.Key opts.error
}
end
}
end.to_xml
end
def mock_referer(opts)
Nokogiri::XML::Builder.new do |xml|
xml.RefererConfiguration {
xml.AllowEmptyReferer opts.allow_empty?
xml.RefererList {
opts.whitelist.each do |r|
xml.Referer r
end
}
}
end.to_xml
end
def mock_lifecycle(rules)
Nokogiri::XML::Builder.new do |xml|
xml.LifecycleConfiguration {
rules.each do |r|
xml.Rule {
xml.ID r.id if r.id
xml.Status r.enabled? ? 'Enabled' : 'Disabled'
xml.Prefix r.prefix
xml.Expiration {
if r.expiry.is_a?(Date)
xml.Date Time.utc(r.expiry.year, r.expiry.month, r.expiry.day)
.iso8601.sub('Z', '.000Z')
else
xml.Days r.expiry.to_i
end
}
}
end
}
end.to_xml
end
def mock_cors(rules)
Nokogiri::XML::Builder.new do |xml|
xml.CORSConfiguration {
rules.each do |r|
xml.CORSRule {
r.allowed_origins.each do |x|
xml.AllowedOrigin x
end
r.allowed_methods.each do |x|
xml.AllowedMethod x
end
r.allowed_headers.each do |x|
xml.AllowedHeader x
end
r.expose_headers.each do |x|
xml.ExposeHeader x
end
xml.MaxAgeSeconds r.max_age_seconds if r.max_age_seconds
}
end
}
end.to_xml
end
def mock_error(code, message)
Nokogiri::XML::Builder.new do |xml|
xml.Error {
xml.Code code
xml.Message message
xml.RequestId '0000'
}
end.to_xml
end
def err(msg, reqid = '0000')
"#{msg} RequestId: #{reqid}"
end
context "Create bucket" do
it "should PUT to create bucket" do
stub_request(:put, request_path)
@protocol.create_bucket(@bucket)
expect(WebMock).to have_requested(:put, request_path)
.with(:body => nil, :query => {})
end
it "should set location when create bucket" do
location = 'oss-cn-hangzhou'
stub_request(:put, request_path).with(:body => mock_location(location))
@protocol.create_bucket(@bucket, :location => 'oss-cn-hangzhou')
expect(WebMock).to have_requested(:put, request_path)
.with(:body => mock_location(location), :query => {})
end
end # create bucket
context "List objects" do
it "should list all objects" do
stub_request(:get, request_path)
@protocol.list_objects(@bucket)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => {})
end
it "should parse object response" do
return_objects = ['hello', 'world', 'foo/bar']
stub_request(:get, request_path)
.to_return(:body => mock_objects(return_objects))
objects, more = @protocol.list_objects(@bucket)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => {})
expect(objects.map {|o| o.key}).to match_array(return_objects)
expect(more).to be_empty
end
it "should list objects with prefix & delimiter" do
# Webmock cannot capture the request_path encoded query parameters,
# so we use 'foo-bar' instead of 'foo/bar' to work around
# the problem
opts = {
:marker => 'foo-bar',
:prefix => 'foo-',
:delimiter => '-',
:limit => 10,
:encoding => KeyEncoding::URL}
query = opts.clone
query['max-keys'] = query.delete(:limit)
query['encoding-type'] = query.delete(:encoding)
stub_request(:get, request_path).with(:query => query)
@protocol.list_objects(@bucket, opts)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => "", :query => query)
end
it "should parse object and common prefixes response" do
return_objects = ['hello', 'world', 'foo-bar']
return_more = {
:marker => 'foo-bar',
:prefix => 'foo-',
:delimiter => '-',
:limit => 10,
:encoding => KeyEncoding::URL,
:next_marker => 'foo-xxx',
:truncated => true,
:common_prefixes => ['foo/bar/', 'foo/xxx/']
}
opts = {
:marker => 'foo-bar',
:prefix => 'foo-',
:delimiter => '-',
:limit => 10,
:encoding => KeyEncoding::URL
}
query = opts.clone
query['max-keys'] = query.delete(:limit)
query['encoding-type'] = query.delete(:encoding)
stub_request(:get, request_path).with(:query => query).
to_return(:body => mock_objects(return_objects, return_more))
objects, more = @protocol.list_objects(@bucket, opts)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => query)
expect(objects.map {|o| o.key}).to match_array(return_objects)
expect(more).to eq(return_more)
end
it "should decode object key" do
return_objects = ['中国のruby', 'world', 'foo/bar']
return_more = {
:marker => '杭州のruby',
:prefix => 'foo-',
:delimiter => '分隔のruby',
:limit => 10,
:encoding => KeyEncoding::URL,
:next_marker => '西湖のruby',
:truncated => true,
:common_prefixes => ['玉泉のruby', '苏堤のruby']
}
es_objects = [CGI.escape('中国のruby'), 'world', 'foo/bar']
es_more = {
:marker => CGI.escape('杭州のruby'),
:prefix => 'foo-',
:delimiter => CGI.escape('分隔のruby'),
:limit => 10,
:encoding => KeyEncoding::URL,
:next_marker => CGI.escape('西湖のruby'),
:truncated => true,
:common_prefixes => [CGI.escape('玉泉のruby'), CGI.escape('苏堤のruby')]
}
stub_request(:get, request_path)
.to_return(:body => mock_objects(es_objects, es_more))
objects, more = @protocol.list_objects(@bucket)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => {})
expect(objects.map {|o| o.key}).to match_array(return_objects)
expect(more).to eq(return_more)
end
end # list objects
context "Delete bucket" do
it "should send DELETE reqeust" do
stub_request(:delete, request_path)
@protocol.delete_bucket(@bucket)
expect(WebMock).to have_requested(:delete, request_path)
.with(:body => nil, :query => {})
end
it "should raise Exception on error" do
code = "NoSuchBucket"
message = "The bucket to delete does not exist."
stub_request(:delete, request_path).to_return(
:status => 404, :body => mock_error(code, message))
expect {
@protocol.delete_bucket(@bucket)
}.to raise_error(ServerError, err(message))
end
end # delete bucket
context "acl, logging, website, referer, lifecycle" do
it "should update acl" do
query = {'acl' => ''}
stub_request(:put, request_path).with(:query => query)
@protocol.put_bucket_acl(@bucket, ACL::PUBLIC_READ)
expect(WebMock).to have_requested(:put, request_path)
.with(:query => query, :body => nil)
end
it "should get acl" do
query = {'acl' => ''}
return_acl = ACL::PUBLIC_READ
stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => mock_acl(return_acl))
acl = @protocol.get_bucket_acl(@bucket)
expect(WebMock).to have_requested(:get, request_path)
.with(:query => query, :body => nil)
expect(acl).to eq(return_acl)
end
it "should enable logging" do
query = {'logging' => ''}
stub_request(:put, request_path).with(:query => query)
logging_opts = BucketLogging.new(
:enable => true,
:target_bucket => 'target-bucket', :target_prefix => 'foo')
@protocol.put_bucket_logging(@bucket, logging_opts)
expect(WebMock).to have_requested(:put, request_path)
.with(:query => query, :body => mock_logging(logging_opts))
end
it "should disable logging" do
query = {'logging' => ''}
stub_request(:put, request_path).with(:query => query)
logging_opts = BucketLogging.new(:enable => false)
@protocol.put_bucket_logging(@bucket, logging_opts)
expect(WebMock).to have_requested(:put, request_path)
.with(:query => query, :body => mock_logging(logging_opts))
end
it "should get logging" do
query = {'logging' => ''}
logging_opts = BucketLogging.new(
:enable => true,
:target_bucket => 'target-bucket', :target_prefix => 'foo')
stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => mock_logging(logging_opts))
logging = @protocol.get_bucket_logging(@bucket)
expect(WebMock).to have_requested(:get, request_path)
.with(:query => query, :body => nil)
expect(logging.to_s).to eq(logging_opts.to_s)
end
it "should delete logging" do
query = {'logging' => ''}
stub_request(:delete, request_path).with(:query => query)
@protocol.delete_bucket_logging(@bucket)
expect(WebMock).to have_requested(:delete, request_path)
.with(:query => query, :body => nil)
end
it "should update website" do
query = {'website' => ''}
stub_request(:put, request_path).with(:query => query)
website_opts = BucketWebsite.new(
:enable => true, :index => 'index.html', :error => 'error.html')
@protocol.put_bucket_website(@bucket, website_opts)
expect(WebMock).to have_requested(:put, request_path)
.with(:query => query, :body => mock_website(website_opts))
end
it "should get website" do
query = {'website' => ''}
website_opts = BucketWebsite.new(
:enable => true, :index => 'index.html', :error => 'error.html')
stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => mock_website(website_opts))
opts = @protocol.get_bucket_website(@bucket)
expect(WebMock).to have_requested(:get, request_path)
.with(:query => query, :body => nil)
expect(opts.to_s).to eq(website_opts.to_s)
end
it "should delete website" do
query = {'website' => ''}
stub_request(:delete, request_path).with(:query => query)
@protocol.delete_bucket_website(@bucket)
expect(WebMock).to have_requested(:delete, request_path)
.with(:query => query, :body => nil)
end
it "should update referer" do
query = {'referer' => ''}
stub_request(:put, request_path).with(:query => query)
referer_opts = BucketReferer.new(
:allow_empty => true, :whitelist => ['xxx', 'yyy'])
@protocol.put_bucket_referer(@bucket, referer_opts)
expect(WebMock).to have_requested(:put, request_path)
.with(:query => query, :body => mock_referer(referer_opts))
end
it "should get referer" do
query = {'referer' => ''}
referer_opts = BucketReferer.new(
:allow_empty => true, :whitelist => ['xxx', 'yyy'])
stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => mock_referer(referer_opts))
opts = @protocol.get_bucket_referer(@bucket)
expect(WebMock).to have_requested(:get, request_path)
.with(:query => query, :body => nil)
expect(opts.to_s).to eq(referer_opts.to_s)
end
it "should update lifecycle" do
query = {'lifecycle' => ''}
stub_request(:put, request_path).with(:query => query)
rules = (1..5).map do |i|
LifeCycleRule.new(
:id => i, :enable => i % 2 == 0, :prefix => "foo#{i}",
:expiry => (i % 2 == 1 ? Date.today : 10 + i))
end
@protocol.put_bucket_lifecycle(@bucket, rules)
expect(WebMock).to have_requested(:put, request_path)
.with(:query => query, :body => mock_lifecycle(rules))
end
it "should get lifecycle" do
query = {'lifecycle' => ''}
return_rules = (1..5).map do |i|
LifeCycleRule.new(
:id => i, :enable => i % 2 == 0, :prefix => "foo#{i}",
:expiry => (i % 2 == 1 ? Date.today : 10 + i))
end
stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => mock_lifecycle(return_rules))
rules = @protocol.get_bucket_lifecycle(@bucket)
expect(WebMock).to have_requested(:get, request_path)
.with(:query => query, :body => nil)
expect(rules.map(&:to_s)).to match_array(return_rules.map(&:to_s))
end
it "should delete lifecycle" do
query = {'lifecycle' => ''}
stub_request(:delete, request_path).with(:query => query)
@protocol.delete_bucket_lifecycle(@bucket)
expect(WebMock).to have_requested(:delete, request_path)
.with(:query => query, :body => nil)
end
it "should set cors" do
query = {'cors' => ''}
stub_request(:put, request_path).with(:query => query)
rules = (1..5).map do |i|
CORSRule.new(
:allowed_origins => (1..3).map {|x| "origin-#{x}"},
:allowed_methods => ['PUT', 'GET'],
:allowed_headers => (1..3).map {|x| "header-#{x}"},
:expose_headers => (1..3).map {|x| "header-#{x}"})
end
@protocol.set_bucket_cors(@bucket, rules)
expect(WebMock).to have_requested(:put, request_path)
.with(:query => query, :body => mock_cors(rules))
end
it "should get cors" do
query = {'cors' => ''}
return_rules = (1..5).map do |i|
CORSRule.new(
:allowed_origins => (1..3).map {|x| "origin-#{x}"},
:allowed_methods => ['PUT', 'GET'],
:allowed_headers => (1..3).map {|x| "header-#{x}"},
:expose_headers => (1..3).map {|x| "header-#{x}"})
end
stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => mock_cors(return_rules))
rules = @protocol.get_bucket_cors(@bucket)
expect(WebMock).to have_requested(:get, request_path)
.with(:query => query, :body => nil)
expect(rules.map(&:to_s)).to match_array(return_rules.map(&:to_s))
end
it "should delete cors" do
query = {'cors' => ''}
stub_request(:delete, request_path).with(:query => query)
@protocol.delete_bucket_cors(@bucket)
expect(WebMock).to have_requested(:delete, request_path)
.with(:query => query, :body => nil)
end
end # acl, logging, cors, etc
context "crc" do
it "should download crc enable equal config setting" do
protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => 'true'))
expect(protocol.download_crc_enable).to eq(true)
protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => true))
expect(protocol.download_crc_enable).to eq(true)
protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => 'false'))
expect(protocol.download_crc_enable).to eq(false)
protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:download_crc_enable => false))
expect(protocol.download_crc_enable).to eq(false)
end
it "should upload crc enable equal config setting" do
protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => 'true'))
expect(protocol.upload_crc_enable).to eq(true)
protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => true))
expect(protocol.upload_crc_enable).to eq(true)
protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => 'false'))
expect(protocol.upload_crc_enable).to eq(false)
protocol = Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx', :access_key_secret => 'yyy',
:upload_crc_enable => false))
expect(protocol.upload_crc_enable).to eq(false)
end
end # crc
end # Bucket
end # OSS
end # Aliyun