1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

finish most of the other s3 mocks

This commit is contained in:
Wesley Beary 2009-08-08 14:46:13 -07:00
parent 5a156e5a0b
commit 175e5802f7
11 changed files with 270 additions and 141 deletions

View file

@ -1,3 +1,5 @@
unless Fog.mocking?
module Fog module Fog
module AWS module AWS
class S3 class S3
@ -7,8 +9,8 @@ module Fog
# ==== Parameters # ==== Parameters
# * source_bucket_name<~String> - Name of source bucket # * source_bucket_name<~String> - Name of source bucket
# * source_object_name<~String> - Name of source object # * source_object_name<~String> - Name of source object
# * destination_bucket_name<~String> - Name of bucket to create copy in # * target_bucket_name<~String> - Name of bucket to create copy in
# * destination_object_name<~String> - Name for new copy of object # * target_object_name<~String> - Name for new copy of object
# * options<~Hash>: # * options<~Hash>:
# * 'x-amz-metadata-directive'<~String> - Specifies whether to copy metadata from source or replace with data in request. Must be in ['COPY', 'REPLACE'] # * 'x-amz-metadata-directive'<~String> - Specifies whether to copy metadata from source or replace with data in request. Must be in ['COPY', 'REPLACE']
# * 'x-amz-copy_source-if-match'<~String> - Copies object if its etag matches this value # * 'x-amz-copy_source-if-match'<~String> - Copies object if its etag matches this value
@ -23,18 +25,62 @@ module Fog
# * 'LastModified'<~Time> - date object was last modified # * 'LastModified'<~Time> - date object was last modified
# #
# TODO: allow specifying new metadata (support all/some of put_object?) # TODO: allow specifying new metadata (support all/some of put_object?)
def copy_object(source_bucket_name, source_object_name, destination_bucket_name, destination_object_name, options = {}) def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {})
headers = { 'x-amz-copy-source' => "/#{source_bucket_name}/#{source_object_name}" }.merge!(options) headers = { 'x-amz-copy-source' => "/#{source_bucket_name}/#{source_object_name}" }.merge!(options)
request({ request({
:expects => 200, :expects => 200,
:headers => headers, :headers => headers,
:host => "#{destination_bucket_name}.#{@host}", :host => "#{target_bucket_name}.#{@host}",
:method => 'PUT', :method => 'PUT',
:parser => Fog::Parsers::AWS::S3::CopyObject.new, :parser => Fog::Parsers::AWS::S3::CopyObject.new,
:path => destination_object_name :path => target_object_name
}) })
end end
end end
end end
end end
else
module Fog
module AWS
class S3
def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {})
response = Fog::Response.new
source_bucket_status = get_bucket(source_bucket_name).status
target_bucket_status = get_bucket(target_bucket_name).status
source_object_status = get_object(source_bucket_name, source_object_name).status
if source_bucket_status != 200
response.status = source_bucket_status
elsif target_bucket_status != 200
response.status = target_bucket_status
elsif source_bucket_status != 200
response.status = source_bucket_status
else
response.status = 200
end
if response.status == 200
source_bucket = @data['Buckets'].select {|bucket| bucket['Name'] == source_bucket_name}.first
source_object = source_bucket['Contents'].select {|object| object['Key'] == source_object_name}.first
target_bucket = @data['Buckets'].select {|bucket| bucket['Name'] == target_bucket_name}.first
target_object = source_object.dup
target_object['Name'] = target_object_name
target_bucket['Contents'] << target_object
response.body = {
'ETag' => target_object['ETag'],
'LastModified' => Time.parse(target_object['LastModified'])
}
end
response
end
end
end
end
end

View file

@ -70,7 +70,9 @@ else
response.body = { response.body = {
'IsTruncated' => false, 'IsTruncated' => false,
'Contents' => bucket['Contents'].map do |object| 'Contents' => bucket['Contents'].map do |object|
object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Owner', 'StorageClass'].include?(key)} data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Owner', 'StorageClass'].include?(key)}
data['LastModified'] = Time.parse(data['LastModified'])
data
end end
} }
end end

View file

@ -1,3 +1,5 @@
unless Fog.mocking?
module Fog module Fog
module AWS module AWS
class S3 class S3
@ -25,3 +27,28 @@ module Fog
end end
end end
end end
else
module Fog
module AWS
class S3
def get_request_payment(bucket_name)
response = Fog::Response.new
bucket_status = get_bucket(bucket_name).status
if bucket_status == 200
response.status = 200
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
response.body = { 'Payer' => bucket['Payer'] }
else
response.status = bucket_status
end
response
end
end
end
end
end

View file

@ -1,3 +1,5 @@
unless Fog.mocking?
module Fog module Fog
module AWS module AWS
class S3 class S3
@ -39,3 +41,21 @@ module Fog
end end
end end
end end
else
module Fog
module AWS
class S3
def head_object(bucket_name, object_name, options = {})
response = get_object(bucket_name, object_name, options)
response.body = nil
response
end
end
end
end
end

View file

@ -50,7 +50,8 @@ else
@data['Buckets'] << { @data['Buckets'] << {
'Name' => bucket_name, 'Name' => bucket_name,
'Contents' => [], 'Contents' => [],
'CreationDate' => Time.now 'CreationDate' => Time.now,
'Payer' => 'BucketOwner'
} }
response response
end end

View file

@ -1,3 +1,5 @@
unless Fog.mocking?
module Fog module Fog
module AWS module AWS
class S3 class S3
@ -27,3 +29,29 @@ DATA
end end
end end
end end
else
module Fog
module AWS
class S3
def put_request_payment(bucket_name, payer)
response = Fog::Response.new
bucket_status = get_bucket(bucket_name).status
if bucket_status == 200
response.status = 200
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
bucket['Payer'] = payer
else
response.status = bucket_status
end
response
end
end
end
end
end

View file

@ -3,21 +3,22 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe 'S3.copy_object' do describe 'S3.copy_object' do
before(:all) do before(:all) do
s3.put_bucket('fogcopyobjectsource') @s3 = s3
@s3.put_bucket('fogcopyobjectsource')
file = File.open(File.dirname(__FILE__) + '/../../lorem.txt', 'r') file = File.open(File.dirname(__FILE__) + '/../../lorem.txt', 'r')
s3.put_object('fogcopyobjectsource', 'fog_copy_object_source', file) @s3.put_object('fogcopyobjectsource', 'fog_copy_object_source', file)
s3.put_bucket('fogcopyobjectdestination') @s3.put_bucket('fogcopyobjectdestination')
end end
after(:all) do after(:all) do
s3.delete_object('fogcopyobjectdestination', 'fog_copy_object_destination') @s3.delete_object('fogcopyobjectdestination', 'fog_copy_object_destination')
s3.delete_bucket('fogcopyobjectdestination') @s3.delete_bucket('fogcopyobjectdestination')
s3.delete_object('fogcopyobjectsource', 'fog_copy_object_source') @s3.delete_object('fogcopyobjectsource', 'fog_copy_object_source')
s3.delete_bucket('fogcopyobjectsource') @s3.delete_bucket('fogcopyobjectsource')
end end
it 'should return proper attributes' do it 'should return proper attributes' do
actual = s3.copy_object( actual = @s3.copy_object(
'fogcopyobjectsource', 'fog_copy_object_source', 'fogcopyobjectsource', 'fog_copy_object_source',
'fogcopyobjectdestination', 'fog_copy_object_destination' 'fogcopyobjectdestination', 'fog_copy_object_destination'
) )

View file

@ -3,18 +3,19 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe 'S3.get_bucket' do describe 'S3.get_bucket' do
before(:all) do before(:all) do
s3.put_bucket('foggetbucket') @s3 = s3
@s3.put_bucket('foggetbucket')
file = File.open(File.dirname(__FILE__) + '/../../lorem.txt', 'r') file = File.open(File.dirname(__FILE__) + '/../../lorem.txt', 'r')
s3.put_object('foggetbucket', 'fog_get_bucket', file) @s3.put_object('foggetbucket', 'fog_get_bucket', file)
end end
after(:all) do after(:all) do
s3.delete_object('foggetbucket', 'fog_get_bucket') @s3.delete_object('foggetbucket', 'fog_get_bucket')
s3.delete_bucket('foggetbucket') @s3.delete_bucket('foggetbucket')
end end
it 'should return proper attributes' do it 'should return proper attributes' do
actual = s3.get_bucket('foggetbucket') actual = @s3.get_bucket('foggetbucket')
actual.body['IsTruncated'].should == false actual.body['IsTruncated'].should == false
actual.body['Marker'].should be_a(String) actual.body['Marker'].should be_a(String)
actual.body['MaxKeys'].should be_an(Integer) actual.body['MaxKeys'].should be_an(Integer)

View file

@ -3,15 +3,16 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe 'S3.get_request_payment' do describe 'S3.get_request_payment' do
before(:all) do before(:all) do
s3.put_bucket('foggetrequestpayment') @s3 = s3
@s3.put_bucket('foggetrequestpayment')
end end
after(:all) do after(:all) do
s3.delete_bucket('foggetrequestpayment') @s3.delete_bucket('foggetrequestpayment')
end end
it 'should return proper attributes' do it 'should return proper attributes' do
actual = s3.get_request_payment('foggetrequestpayment') actual = @s3.get_request_payment('foggetrequestpayment')
actual.status.should == 200 actual.status.should == 200
actual.body['Payer'].should == 'BucketOwner' actual.body['Payer'].should == 'BucketOwner'
end end

View file

@ -3,18 +3,19 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe 'S3.head_object' do describe 'S3.head_object' do
before(:all) do before(:all) do
s3.put_bucket('fogheadobject') @s3 = s3
@s3.put_bucket('fogheadobject')
file = File.open(File.dirname(__FILE__) + '/../../lorem.txt', 'r') file = File.open(File.dirname(__FILE__) + '/../../lorem.txt', 'r')
s3.put_object('fogheadobject', 'fog_head_object', file) @s3.put_object('fogheadobject', 'fog_head_object', file)
end end
after(:all) do after(:all) do
s3.delete_object('fogheadobject', 'fog_head_object') @s3.delete_object('fogheadobject', 'fog_head_object')
s3.delete_bucket('fogheadobject') @s3.delete_bucket('fogheadobject')
end end
it 'should return proper attributes' do it 'should return proper attributes' do
actual = s3.head_object('fogheadobject', 'fog_head_object') actual = @s3.head_object('fogheadobject', 'fog_head_object')
actual.status.should == 200 actual.status.should == 200
file = File.open(File.dirname(__FILE__) + '/../../lorem.txt', 'r') file = File.open(File.dirname(__FILE__) + '/../../lorem.txt', 'r')
data = file.read data = file.read

View file

@ -3,15 +3,16 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe 'S3.put_request_payment' do describe 'S3.put_request_payment' do
before(:all) do before(:all) do
s3.put_bucket('fogputrequestpayment') @s3 = s3
@s3.put_bucket('fogputrequestpayment')
end end
after(:all) do after(:all) do
s3.delete_bucket('fogputrequestpayment') @s3.delete_bucket('fogputrequestpayment')
end end
it 'should return proper attributes' do it 'should return proper attributes' do
actual = s3.put_request_payment('fogputrequestpayment', 'Requester') actual = @s3.put_request_payment('fogputrequestpayment', 'Requester')
actual.status.should == 200 actual.status.should == 200
end end