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 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
|
# -*- encoding: utf-8 -*-
require 'spec_helper'
require 'yaml'
require 'nokogiri'
module Aliyun
module OSS
describe Multipart 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'
@object = 'rubysdk-object'
end
def request_path
"#{@bucket}.#{@endpoint}/#{@object}"
end
def crc_protocol
Protocol.new(
Config.new(:endpoint => @endpoint,
:access_key_id => 'xxx',
:access_key_secret => 'yyy',
:upload_crc_enable => true,
:download_crc_enable => true))
end
def mock_txn_id(txn_id)
Nokogiri::XML::Builder.new do |xml|
xml.InitiateMultipartUploadResult {
xml.Bucket @bucket
xml.Key @object
xml.UploadId txn_id
}
end.to_xml
end
def mock_multiparts(multiparts, more = {})
Nokogiri::XML::Builder.new do |xml|
xml.ListMultipartUploadsResult {
{
:prefix => 'Prefix',
:limit => 'MaxUploads',
:id_marker => 'UploadIdMarker',
:next_id_marker => 'NextUploadIdMarker',
:key_marker => 'KeyMarker',
:next_key_marker => 'NextKeyMarker',
:truncated => 'IsTruncated',
:encoding => 'EncodingType'
}.map do |k, v|
xml.send(v, more[k]) if more[k]
end
multiparts.each do |m|
xml.Upload {
xml.Key m.object
xml.UploadId m.id
xml.Initiated m.creation_time.rfc822
}
end
}
end.to_xml
end
def mock_parts(parts, more = {})
Nokogiri::XML::Builder.new do |xml|
xml.ListPartsResult {
{
:marker => 'PartNumberMarker',
:next_marker => 'NextPartNumberMarker',
:limit => 'MaxParts',
:truncated => 'IsTruncated',
:encoding => 'EncodingType'
}.map do |k, v|
xml.send(v, more[k]) if more[k]
end
parts.each do |p|
xml.Part {
xml.PartNumber p.number
xml.LastModified p.last_modified.rfc822
xml.ETag p.etag
xml.Size p.size
}
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 "Initiate multipart upload" do
it "should POST to create transaction" do
query = {'uploads' => nil}
stub_request(:post, request_path).with(:query => query)
@protocol.initiate_multipart_upload(
@bucket, @object, :metas => {
'year' => '2015',
'people' => 'mary'
})
expect(WebMock).to have_requested(:post, request_path)
.with(:body => nil, :query => query,
:headers => {
'x-oss-meta-year' => '2015',
'x-oss-meta-people' => 'mary'
})
end
it "should return transaction id" do
query = {'uploads' => nil}
return_txn_id = 'zyx'
stub_request(:post, request_path).
with(:query => query).
to_return(:body => mock_txn_id(return_txn_id))
txn_id = @protocol.initiate_multipart_upload(@bucket, @object)
expect(WebMock).to have_requested(:post, request_path)
.with(:body => nil, :query => query)
expect(txn_id).to eq(return_txn_id)
end
it "should raise Exception on error" do
query = {'uploads' => nil}
code = 'InvalidArgument'
message = 'Invalid argument.'
stub_request(:post, request_path)
.with(:query => query)
.to_return(:status => 400, :body => mock_error(code, message))
expect {
@protocol.initiate_multipart_upload(@bucket, @object)
}.to raise_error(ServerError, err(message))
expect(WebMock).to have_requested(:post, request_path)
.with(:body => nil, :query => query)
end
end # initiate multipart
context "Upload part" do
it "should PUT to upload a part" do
txn_id = 'xxxyyyzzz'
part_no = 1
query = {'partNumber' => part_no, 'uploadId' => txn_id}
stub_request(:put, request_path).with(:query => query)
@protocol.upload_part(@bucket, @object, txn_id, part_no) {}
expect(WebMock).to have_requested(:put, request_path)
.with(:body => nil, :query => query)
end
it "should return part etag" do
part_no = 1
txn_id = 'xxxyyyzzz'
query = {'partNumber' => part_no, 'uploadId' => txn_id}
return_etag = 'etag_1'
stub_request(:put, request_path)
.with(:query => query)
.to_return(:headers => {'ETag' => return_etag})
body = 'hello world'
p = @protocol.upload_part(@bucket, @object, txn_id, part_no) do |content|
content << body
end
expect(WebMock).to have_requested(:put, request_path)
.with(:body => body, :query => query)
expect(p.number).to eq(part_no)
expect(p.etag).to eq(return_etag)
end
it "should raise Exception on error" do
txn_id = 'xxxyyyzzz'
part_no = 1
query = {'partNumber' => part_no, 'uploadId' => txn_id}
code = 'InvalidArgument'
message = 'Invalid argument.'
stub_request(:put, request_path)
.with(:query => query)
.to_return(:status => 400, :body => mock_error(code, message))
expect {
@protocol.upload_part(@bucket, @object, txn_id, part_no) {}
}.to raise_error(ServerError, err(message))
expect(WebMock).to have_requested(:put, request_path)
.with(:body => nil, :query => query)
end
it "should raise crc exception on error" do
txn_id = 'xxxyyyzzz'
part_no = 1
query = {'partNumber' => part_no, 'uploadId' => txn_id}
content = "hello world"
content_crc = Aliyun::OSS::Util.crc(content)
stub_request(:put, request_path).with(:query => query).to_return(
:status => 200, :headers => {:x_oss_hash_crc64ecma => content_crc + 1})
expect {
crc_protocol.upload_part(@bucket, @object, txn_id, part_no) do |body|
body << content
end
}.to raise_error(CrcInconsistentError, "The crc of put between client and oss is not inconsistent.")
end
it "should not raise crc exception on error" do
txn_id = 'xxxyyyzzz'
part_no = 1
query = {'partNumber' => part_no, 'uploadId' => txn_id}
content = "hello world"
content_crc = Aliyun::OSS::Util.crc(content)
stub_request(:put, request_path).with(:query => query).to_return(
:status => 200, :headers => {:x_oss_hash_crc64ecma => content_crc})
expect {
crc_protocol.upload_part(@bucket, @object, txn_id, part_no) do |body|
body << content
end
}.not_to raise_error
end
end # upload part
context "Upload part by copy object" do
it "should PUT to upload a part by copy object" do
txn_id = 'xxxyyyzzz'
part_no = 1
copy_source = "/#{@bucket}/src_obj"
query = {'partNumber' => part_no, 'uploadId' => txn_id}
headers = {'x-oss-copy-source' => copy_source}
stub_request(:put, request_path)
.with(:query => query, :headers => headers)
@protocol.upload_part_by_copy(@bucket, @object, txn_id, part_no, 'src_obj')
expect(WebMock).to have_requested(:put, request_path)
.with(:body => nil, :query => query, :headers => headers)
end
it "should upload a part by copy object from different bucket" do
txn_id = 'xxxyyyzzz'
part_no = 1
src_bucket = 'source-bucket'
copy_source = "/#{src_bucket}/src_obj"
query = {'partNumber' => part_no, 'uploadId' => txn_id}
headers = {'x-oss-copy-source' => copy_source}
stub_request(:put, request_path)
.with(:query => query, :headers => headers)
@protocol.upload_part_by_copy(
@bucket, @object, txn_id, part_no, 'src_obj', :src_bucket => src_bucket)
expect(WebMock).to have_requested(:put, request_path)
.with(:body => nil, :query => query, :headers => headers)
end
it "should return part etag" do
txn_id = 'xxxyyyzzz'
part_no = 1
copy_source = "/#{@bucket}/src_obj"
query = {'partNumber' => part_no, 'uploadId' => txn_id}
headers = {'x-oss-copy-source' => copy_source}
return_etag = 'etag_1'
stub_request(:put, request_path)
.with(:query => query, :headers => headers)
.to_return(:headers => {'ETag' => return_etag})
p = @protocol.upload_part_by_copy(@bucket, @object, txn_id, part_no, 'src_obj')
expect(WebMock).to have_requested(:put, request_path)
.with(:body => nil, :query => query, :headers => headers)
expect(p.number).to eq(part_no)
expect(p.etag).to eq(return_etag)
end
it "should set range and conditions when copy" do
txn_id = 'xxxyyyzzz'
part_no = 1
copy_source = "/#{@bucket}/src_obj"
query = {'partNumber' => part_no, 'uploadId' => txn_id}
modified_since = Time.now
unmodified_since = Time.now
headers = {
'Range' => 'bytes=1-4',
'x-oss-copy-source' => copy_source,
'x-oss-copy-source-if-modified-since' => modified_since.httpdate,
'x-oss-copy-source-if-unmodified-since' => unmodified_since.httpdate,
'x-oss-copy-source-if-match' => 'me',
'x-oss-copy-source-if-none-match' => 'ume'
}
return_etag = 'etag_1'
stub_request(:put, request_path)
.with(:query => query, :headers => headers)
.to_return(:headers => {'ETag' => return_etag})
p = @protocol.upload_part_by_copy(
@bucket, @object, txn_id, part_no, 'src_obj',
{:range => [1, 5],
:condition => {
:if_modified_since => modified_since,
:if_unmodified_since => unmodified_since,
:if_match_etag => 'me',
:if_unmatch_etag => 'ume'
}})
expect(WebMock).to have_requested(:put, request_path)
.with(:body => nil, :query => query, :headers => headers)
expect(p.number).to eq(part_no)
expect(p.etag).to eq(return_etag)
end
it "should raise Exception on error" do
txn_id = 'xxxyyyzzz'
part_no = 1
copy_source = "/#{@bucket}/src_obj"
query = {'partNumber' => part_no, 'uploadId' => txn_id}
headers = {'x-oss-copy-source' => copy_source}
code = 'PreconditionFailed'
message = 'Precondition check failed.'
stub_request(:put, request_path)
.with(:query => query, :headers => headers)
.to_return(:status => 412, :body => mock_error(code, message))
expect {
@protocol.upload_part_by_copy(@bucket, @object, txn_id, part_no, 'src_obj')
}.to raise_error(ServerError, err(message))
expect(WebMock).to have_requested(:put, request_path)
.with(:body => nil, :query => query, :headers => headers)
end
end # upload part by copy object
context "Commit multipart" do
it "should POST to complete multipart" do
txn_id = 'xxxyyyzzz'
query = {'uploadId' => txn_id}
parts = (1..5).map do |i|
Multipart::Part.new(:number => i, :etag => "etag_#{i}")
end
stub_request(:post, request_path).with(:query => query)
@protocol.complete_multipart_upload(@bucket, @object, txn_id, parts)
parts_body = Nokogiri::XML::Builder.new do |xml|
xml.CompleteMultipartUpload {
parts.each do |p|
xml.Part {
xml.PartNumber p.number
xml.ETag p.etag
}
end
}
end.to_xml
expect(WebMock).to have_requested(:post, request_path)
.with(:body => parts_body, :query => query)
end
it "should raise Exception on error" do
txn_id = 'xxxyyyzzz'
query = {'uploadId' => txn_id}
code = 'InvalidDigest'
message = 'Content md5 does not match.'
stub_request(:post, request_path)
.with(:query => query)
.to_return(:status => 400, :body => mock_error(code, message))
expect {
@protocol.complete_multipart_upload(@bucket, @object, txn_id, [])
}.to raise_error(ServerError, err(message))
expect(WebMock).to have_requested(:post, request_path)
.with(:query => query)
end
end # commit multipart
context "Abort multipart" do
it "should DELETE to abort multipart" do
txn_id = 'xxxyyyzzz'
query = {'uploadId' => txn_id}
stub_request(:delete, request_path).with(:query => query)
@protocol.abort_multipart_upload(@bucket, @object, txn_id)
expect(WebMock).to have_requested(:delete, request_path)
.with(:body => nil, :query => query)
end
it "should raise Exception on error" do
txn_id = 'xxxyyyzzz'
query = {'uploadId' => txn_id}
code = 'NoSuchUpload'
message = 'The multipart transaction does not exist.'
stub_request(:delete, request_path)
.with(:query => query)
.to_return(:status => 404, :body => mock_error(code, message))
expect {
@protocol.abort_multipart_upload(@bucket, @object, txn_id)
}.to raise_error(ServerError, err(message))
expect(WebMock).to have_requested(:delete, request_path)
.with(:body => nil, :query => query)
end
end # abort multipart
context "List multiparts" do
it "should GET to list multiparts" do
request_path = "#{@bucket}.#{@endpoint}/"
query = {'uploads' => nil}
stub_request(:get, request_path).with(:query => query)
@protocol.list_multipart_uploads(@bucket)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => query)
end
it "should send extra params when list multiparts" do
request_path = "#{@bucket}.#{@endpoint}/"
query = {
'uploads' => nil,
'prefix' => 'foo-',
'upload-id-marker' => 'id-marker',
'key-marker' => 'key-marker',
'max-uploads' => 10,
'encoding-type' => KeyEncoding::URL
}
stub_request(:get, request_path).with(:query => query)
@protocol.list_multipart_uploads(
@bucket,
:prefix => 'foo-',
:id_marker => 'id-marker',
:key_marker => 'key-marker',
:limit => 10,
:encoding => KeyEncoding::URL
)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => query)
end
it "should get multipart transactions" do
request_path = "#{@bucket}.#{@endpoint}/"
query = {
'uploads' => nil,
'prefix' => 'foo-',
'upload-id-marker' => 'id-marker',
'key-marker' => 'key-marker',
'max-uploads' => 100,
'encoding-type' => KeyEncoding::URL
}
return_multiparts = (1..5).map do |i|
Multipart::Transaction.new(
:id => "id-#{i}",
:object => "key-#{i}",
:bucket => @bucket,
:creation_time => Time.parse(Time.now.rfc822))
end
return_more = {
:prefix => 'foo-',
:id_marker => 'id-marker',
:key_marker => 'key-marker',
:next_id_marker => 'next-id-marker',
:next_key_marker => 'next-key-marker',
:limit => 100,
:truncated => true
}
stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => mock_multiparts(return_multiparts, return_more))
txns, more = @protocol.list_multipart_uploads(
@bucket,
:prefix => 'foo-',
:id_marker => 'id-marker',
:key_marker => 'key-marker',
:limit => 100,
:encoding => KeyEncoding::URL)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => query)
expect(txns.map {|x| x.to_s}.join(';'))
.to eq(return_multiparts.map {|x| x.to_s}.join(';'))
expect(more).to eq(return_more)
end
it "should decode object key" do
request_path = "#{@bucket}.#{@endpoint}/"
query = {
'uploads' => nil,
'prefix' => 'foo-',
'upload-id-marker' => 'id-marker',
'key-marker' => 'key-marker',
'max-uploads' => 100,
'encoding-type' => KeyEncoding::URL
}
return_multiparts = (1..5).map do |i|
Multipart::Transaction.new(
:id => "id-#{i}",
:object => "中国-#{i}",
:bucket => @bucket,
:creation_time => Time.parse(Time.now.rfc822))
end
es_multiparts = return_multiparts.map do |x|
Multipart::Transaction.new(
:id => x.id,
:object => CGI.escape(x.object),
:creation_time => x.creation_time)
end
return_more = {
:prefix => 'foo-',
:id_marker => 'id-marker',
:key_marker => '杭州のruby',
:next_id_marker => 'next-id-marker',
:next_key_marker => '西湖のruby',
:limit => 100,
:truncated => true,
:encoding => KeyEncoding::URL
}
es_more = {
:prefix => 'foo-',
:id_marker => 'id-marker',
:key_marker => CGI.escape('杭州のruby'),
:next_id_marker => 'next-id-marker',
:next_key_marker => CGI.escape('西湖のruby'),
:limit => 100,
:truncated => true,
:encoding => KeyEncoding::URL
}
stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => mock_multiparts(es_multiparts, es_more))
txns, more = @protocol.list_multipart_uploads(
@bucket,
:prefix => 'foo-',
:id_marker => 'id-marker',
:key_marker => 'key-marker',
:limit => 100,
:encoding => KeyEncoding::URL)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => query)
expect(txns.map {|x| x.to_s}.join(';'))
.to eq(return_multiparts.map {|x| x.to_s}.join(';'))
expect(more).to eq(return_more)
end
it "should raise Exception on error" do
request_path = "#{@bucket}.#{@endpoint}/"
query = {'uploads' => nil}
code = 'InvalidArgument'
message = 'Invalid argument.'
stub_request(:get, request_path)
.with(:query => query)
.to_return(:status => 400, :body => mock_error(code, message))
expect {
@protocol.list_multipart_uploads(@bucket)
}.to raise_error(ServerError, err(message))
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => query)
end
end # list multiparts
context "List parts" do
it "should GET to list parts" do
txn_id = 'yyyxxxzzz'
query = {'uploadId' => txn_id}
stub_request(:get, request_path).with(:query => query)
@protocol.list_parts(@bucket, @object, txn_id)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => query)
end
it "should send extra params when list parts" do
txn_id = 'yyyxxxzzz'
query = {
'uploadId' => txn_id,
'part-number-marker' => 'foo-',
'max-parts' => 100,
'encoding-type' => KeyEncoding::URL
}
stub_request(:get, request_path).with(:query => query)
@protocol.list_parts(@bucket, @object, txn_id,
:marker => 'foo-',
:limit => 100,
:encoding => KeyEncoding::URL)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => query)
end
it "should get parts" do
txn_id = 'yyyxxxzzz'
query = {
'uploadId' => txn_id,
'part-number-marker' => 'foo-',
'max-parts' => 100,
'encoding-type' => KeyEncoding::URL
}
return_parts = (1..5).map do |i|
Multipart::Part.new(
:number => i,
:etag => "etag-#{i}",
:size => 1024,
:last_modified => Time.parse(Time.now.rfc822))
end
return_more = {
:marker => 'foo-',
:next_marker => 'bar-',
:limit => 100,
:truncated => true
}
stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => mock_parts(return_parts, return_more))
parts, more = @protocol.list_parts(@bucket, @object, txn_id,
:marker => 'foo-',
:limit => 100,
:encoding => KeyEncoding::URL)
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => query)
part_numbers = return_parts.map {|x| x.number}
expect(parts.map {|x| x.number}).to match_array(part_numbers)
expect(more).to eq(return_more)
end
it "should raise Exception on error" do
txn_id = 'yyyxxxzzz'
query = {'uploadId' => txn_id}
code = 'InvalidArgument'
message = 'Invalid argument.'
stub_request(:get, request_path)
.with(:query => query)
.to_return(:status => 400, :body => mock_error(code, message))
expect {
@protocol.list_parts(@bucket, @object, txn_id)
}.to raise_error(ServerError, err(message))
expect(WebMock).to have_requested(:get, request_path)
.with(:body => nil, :query => query)
end
end # list parts
end # Multipart
end # OSS
end # Aliyun
|