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

[aws|s3] Added basic tests for get_bucket, fixed a bug in get_bucket with delimiter option, tests succeed for both mocked and real situation

This commit is contained in:
Erik Terpstra 2011-08-03 21:21:24 +02:00
parent 4862063e27
commit c738d59b53
2 changed files with 57 additions and 2 deletions

View file

@ -62,6 +62,7 @@ module Fog
def get_bucket(bucket_name, options = {})
prefix, marker, delimiter, max_keys = \
options['prefix'], options['marker'], options['delimiter'], options['max-keys']
common_prefixes = []
unless bucket_name
raise ArgumentError.new('bucket_name is required')
@ -71,7 +72,8 @@ module Fog
contents = bucket[:objects].values.sort {|x,y| x['Key'] <=> y['Key']}.reject do |object|
(prefix && object['Key'][0...prefix.length] != prefix) ||
(marker && object['Key'] <= marker) ||
(delimiter && object['Key'][(prefix ? prefix.length : 0)..-1].include?(delimiter))
(delimiter && object['Key'][(prefix ? prefix.length : 0)..-1].include?(delimiter) \
&& common_prefixes << object['Key'].sub(/^(#{prefix}[^#{delimiter}]+.).*/, '\1'))
end.map do |object|
data = object.reject {|key, value| !['ETag', 'Key', 'StorageClass'].include?(key)}
data.merge!({
@ -87,7 +89,7 @@ module Fog
response.status = 200
response.body = {
'CommonPrefixes' => [],
'CommonPrefixes' => common_prefixes.uniq,
'Contents' => truncated_contents,
'IsTruncated' => truncated_contents.size != contents.size,
'Marker' => marker,

View file

@ -49,6 +49,59 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', [:aws]) do
file.destroy
file1 = Fog::Storage[:aws].directories.get('fogbuckettests').files.create(:body => 'a', :key => 'a/a1/file1')
file2 = Fog::Storage[:aws].directories.get('fogbuckettests').files.create(:body => 'ab', :key => 'a/file2')
file3 = Fog::Storage[:aws].directories.get('fogbuckettests').files.create(:body => 'abc', :key => 'b/file3')
file4 = Fog::Storage[:aws].directories.get('fogbuckettests').files.create(:body => 'abcd', :key => 'file4')
tests("#get_bucket('fogbuckettests')") do
before do
@bucket = Fog::Storage[:aws].get_bucket('fogbuckettests')
end
tests(".body['Contents'].map{|n| n['Key']}").returns(["a/a1/file1", "a/file2", "b/file3", "file4"]) do
@bucket.body['Contents'].map{|n| n['Key']}
end
tests(".body['Contents'].map{|n| n['Size']}").returns([1, 2, 3, 4]) do
@bucket.body['Contents'].map{|n| n['Size']}
end
tests(".body['CommonPrefixes']").returns([]) do
@bucket.body['CommonPrefixes']
end
end
tests("#get_bucket('fogbuckettests', 'delimiter' => '/')") do
before do
@bucket = Fog::Storage[:aws].get_bucket('fogbuckettests', 'delimiter' => '/')
end
tests(".body['Contents'].map{|n| n['Key']}").returns(['file4']) do
@bucket.body['Contents'].map{|n| n['Key']}
end
tests(".body['CommonPrefixes']").returns(['a/', 'b/']) do
@bucket.body['CommonPrefixes']
end
end
tests("#get_bucket('fogbuckettests', 'delimiter' => '/', 'prefix' => 'a/')") do
before do
@bucket = Fog::Storage[:aws].get_bucket('fogbuckettests', 'delimiter' => '/', 'prefix' => 'a/')
end
tests(".body['Contents'].map{|n| n['Key']}").returns(['a/file2']) do
@bucket.body['Contents'].map{|n| n['Key']}
end
tests(".body['CommonPrefixes']").returns(['a/a1/']) do
@bucket.body['CommonPrefixes']
end
end
file1.destroy; file2.destroy; file3.destroy; file4.destroy
tests("#get_bucket_location('fogbuckettests)").formats('LocationConstraint' => NilClass) do
Fog::Storage[:aws].get_bucket_location('fogbuckettests').body
end