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

fleshing out existing mocks and adding basic object mocks

This commit is contained in:
Wesley Beary 2009-08-08 12:31:32 -07:00
parent 3e6c9703b9
commit 5a156e5a0b
11 changed files with 305 additions and 155 deletions

View file

@ -33,8 +33,13 @@ else
def delete_bucket(bucket_name)
response = Fog::Response.new
response.status = 204
@data['Buckets'].delete_if {|bucket| bucket['Name'] == bucket_name}
bucket_status = get_bucket(bucket_name).status
if bucket_status == 200
response.status = 204
@data['Buckets'].delete_if {|bucket| bucket['Name'] == bucket_name}
else
response.status = bucket_status
end
response
end

View file

@ -1,26 +1,53 @@
module Fog
module AWS
class S3
unless Fog.mocking?
module Fog
module AWS
class S3
# Delete an object from S3
#
# ==== Parameters
# * bucket_name<~String> - Name of bucket containing object to delete
# * object_name<~String> - Name of object to delete
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * status<~Integer> - 204
def delete_object(bucket_name, object_name)
request({
:expects => 204,
:headers => {},
:host => "#{bucket_name}.#{@host}",
:method => 'DELETE',
:path => object_name
})
end
# Delete an object from S3
#
# ==== Parameters
# * bucket_name<~String> - Name of bucket containing object to delete
# * object_name<~String> - Name of object to delete
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * status<~Integer> - 204
def delete_object(bucket_name, object_name)
request({
:expects => 204,
:headers => {},
:host => "#{bucket_name}.#{@host}",
:method => 'DELETE',
:path => object_name
})
end
end
end
end
else
module Fog
module AWS
class S3
def delete_object(bucket_name, object_name)
response = Fog::Response.new
object_status = get_object(bucket_name, object_name).status
if object_status == 200
response.status = 204
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
bucket['Contents'].delete_if {|object| object['Key'] == object_name}
else
response.status = object_status
end
response
end
end
end
end
end

View file

@ -1,52 +1,84 @@
module Fog
module AWS
class S3
unless Fog.mocking?
# List information about objects in an S3 bucket
#
# ==== Parameters
# * bucket_name<~String> - name of bucket to list object keys from
# * options<~Hash> - config arguments for list. Defaults to {}.
# * 'prefix'<~String> - limits object keys to those beginning with its value.
# * 'marker'<~String> - limits object keys to only those that appear
# lexicographically after its value.
# * 'max-keys'<~Integer> - limits number of object keys returned
# * 'delimiter'<~String> - causes keys with the same string between the prefix
# value and the first occurence of delimiter to be rolled up
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'Delimeter'<~String> - Delimiter specified for query
# * 'Marker'<~String> - Marker specified for query
# * 'MaxKeys'<~Integer> - Maximum number of keys specified for query
# * 'Name'<~String> - Name of the bucket
# * 'Prefix'<~String> - Prefix specified for query
# * 'Contents'<~Array>:
# * 'ETag'<~String>: Etag of object
# * 'Key'<~String>: Name of object
# * 'LastModified'<~String>: Timestamp of last modification of object
# * 'Owner'<~Hash>:
# * 'DisplayName'<~String> - Display name of object owner
# * 'ID'<~String> - Id of object owner
# * 'Size'<~Integer> - Size of object
# * 'StorageClass'<~String> - Storage class of object
def get_bucket(bucket_name, options = {})
query = '?'
for key, value in options
query << "#{key}=#{value};"
module Fog
module AWS
class S3
# List information about objects in an S3 bucket
#
# ==== Parameters
# * bucket_name<~String> - name of bucket to list object keys from
# * options<~Hash> - config arguments for list. Defaults to {}.
# * 'prefix'<~String> - limits object keys to those beginning with its value.
# * 'marker'<~String> - limits object keys to only those that appear
# lexicographically after its value.
# * 'max-keys'<~Integer> - limits number of object keys returned
# * 'delimiter'<~String> - causes keys with the same string between the prefix
# value and the first occurence of delimiter to be rolled up
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'Delimeter'<~String> - Delimiter specified for query
# * 'Marker'<~String> - Marker specified for query
# * 'MaxKeys'<~Integer> - Maximum number of keys specified for query
# * 'Name'<~String> - Name of the bucket
# * 'Prefix'<~String> - Prefix specified for query
# * 'Contents'<~Array>:
# * 'ETag'<~String>: Etag of object
# * 'Key'<~String>: Name of object
# * 'LastModified'<~String>: Timestamp of last modification of object
# * 'Owner'<~Hash>:
# * 'DisplayName'<~String> - Display name of object owner
# * 'ID'<~String> - Id of object owner
# * 'Size'<~Integer> - Size of object
# * 'StorageClass'<~String> - Storage class of object
def get_bucket(bucket_name, options = {})
query = '?'
for key, value in options
query << "#{key}=#{value};"
end
query.chop!
request({
:expects => 200,
:headers => {},
:host => "#{bucket_name}.#{@host}",
:method => 'GET',
:parser => Fog::Parsers::AWS::S3::GetBucket.new,
:query => query
})
end
query.chop!
request({
:expects => 200,
:headers => {},
:host => "#{bucket_name}.#{@host}",
:method => 'GET',
:parser => Fog::Parsers::AWS::S3::GetBucket.new,
:query => query
})
end
end
end
end
else
# TODO: IsTruncated, Marker, MaxKeys, Name, Prefix
module Fog
module AWS
class S3
def get_bucket(bucket_name, options = {})
response = Fog::Response.new
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
unless bucket
response.status = 404
else
response.status = 200
response.body = {
'IsTruncated' => false,
'Contents' => bucket['Contents'].map do |object|
object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Owner', 'StorageClass'].include?(key)}
end
}
end
response
end
end
end
end
end

View file

@ -1,40 +1,77 @@
module Fog
module AWS
class S3
unless Fog.mocking?
module Fog
module AWS
class S3
# Get an object from S3
#
# ==== Parameters
# * bucket_name<~String> - Name of bucket to read from
# * object_name<~String> - Name of object to read
# * options<~Hash>:
# * 'If-Match'<~String> - Returns object only if its etag matches this value, otherwise returns 412 (Precondition Failed).
# * 'If-Modified-Since'<~Time> - Returns object only if it has been modified since this time, otherwise returns 304 (Not Modified).
# * 'If-None-Match'<~String> - Returns object only if its etag differs from this value, otherwise returns 304 (Not Modified)
# * 'If-Unmodified-Since'<~Time> - Returns object only if it has not been modified since this time, otherwise returns 412 (Precodition Failed).
# * 'Range'<~String> - Range of object to download
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~String> - Contents of object
# * headers<~Hash>:
# * 'Content-Length'<~String> - Size of object contents
# * 'Content-Type'<~String> - MIME type of object
# * 'ETag'<~String> - Etag of object
# * 'Last-Modified'<~String> - Last modified timestamp for object
def get_object(bucket_name, object_name, options = {})
headers = {}
headers['If-Modified-Since'] = options['If-Modified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers['If-Unmodified-Since'] = options['If-Unmodified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers.merge!(options)
request({
:expects => 200,
:headers => headers,
:host => "#{bucket_name}.#{@host}",
:method => 'GET',
:path => object_name
})
end
# Get an object from S3
#
# ==== Parameters
# * bucket_name<~String> - Name of bucket to read from
# * object_name<~String> - Name of object to read
# * options<~Hash>:
# * 'If-Match'<~String> - Returns object only if its etag matches this value, otherwise returns 412 (Precondition Failed).
# * 'If-Modified-Since'<~Time> - Returns object only if it has been modified since this time, otherwise returns 304 (Not Modified).
# * 'If-None-Match'<~String> - Returns object only if its etag differs from this value, otherwise returns 304 (Not Modified)
# * 'If-Unmodified-Since'<~Time> - Returns object only if it has not been modified since this time, otherwise returns 412 (Precodition Failed).
# * 'Range'<~String> - Range of object to download
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~String> - Contents of object
# * headers<~Hash>:
# * 'Content-Length'<~String> - Size of object contents
# * 'Content-Type'<~String> - MIME type of object
# * 'ETag'<~String> - Etag of object
# * 'Last-Modified'<~String> - Last modified timestamp for object
def get_object(bucket_name, object_name, options = {})
headers = {}
headers['If-Modified-Since'] = options['If-Modified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers['If-Unmodified-Since'] = options['If-Unmodified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
headers.merge!(options)
request({
:expects => 200,
:headers => headers,
:host => "#{bucket_name}.#{@host}",
:method => 'GET',
:path => object_name
})
end
end
end
else
module Fog
module AWS
class S3
def get_object(bucket_name, object_name, options = {})
response = Fog::Response.new
bucket_status = get_bucket(bucket_name).status
if bucket_status == 200
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
object = bucket['Contents'].select {|object| object['Key'] == object_name}.first
if object
response.status = 200
response.headers = {
'Content-Length' => object['Size'],
'ETag' => object['ETag'],
'Last-Modified' => object['LastModified']
}
response.body = object[:body]
else
response.status = 404
end
else
response.status = bucket_status
end
response
end
end
end
end
end

View file

@ -39,8 +39,13 @@ else
def get_service
response = Fog::Response.new
response.headers['Status'] = 200
buckets = @data['Buckets'].map do |bucket|
bucket.reject do |key, value|
!['CreationDate', 'Name'].include?(key)
end
end
response.body = {
'Buckets' => @data['Buckets'],
'Buckets' => buckets,
'Owner' => { 'DisplayName' => 'owner', 'ID' => 'some_id'}
}
response

View file

@ -47,7 +47,11 @@ else
def put_bucket(bucket_name, options = {})
response = Fog::Response.new
response.status = 200
@data['Buckets'] << { 'Name' => bucket_name, 'CreationDate' => Time.now }
@data['Buckets'] << {
'Name' => bucket_name,
'Contents' => [],
'CreationDate' => Time.now
}
response
end

View file

@ -1,40 +1,76 @@
module Fog
module AWS
class S3
unless Fog.mocking?
module Fog
module AWS
class S3
# Create an object in an S3 bucket
#
# ==== Parameters
# * bucket_name<~String> - Name of bucket to create object in
# * object_name<~String> - Name of object to create
# * object<~String> - File to create object from
# * options<~Hash>:
# * 'Cache-Control'<~String> - Caching behaviour
# * 'Content-Disposition'<~String> - Presentational information for the object
# * 'Content-Encoding'<~String> - Encoding of object data
# * 'Content-Length'<~String> - Size of object in bytes (defaults to object.read.length)
# * 'Content-MD5'<~String> - Base64 encoded 128-bit MD5 digest of message (defaults to Base64 encoded MD5 of object.read)
# * 'Content-Type'<~String> - Standard MIME type describing contents (defaults to MIME::Types.of.first)
# * 'x-amz-acl'<~String> - Permissions, must be in ['private', 'public-read', 'public-read-write', 'authenticated-read']
# * "x-amz-meta-#{name}" - Headers to be returned with object, note total size of request without body must be less than 8 KB.
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * headers<~Hash>:
# * 'ETag'<~String> - etag of new object
def put_object(bucket_name, object_name, object, options = {})
file = parse_file(object)
headers = file[:headers].merge!(options)
request({
:body => file[:body],
:expects => 200,
:headers => headers,
:host => "#{bucket_name}.#{@host}",
:method => 'PUT',
:path => object_name
})
end
# Create an object in an S3 bucket
#
# ==== Parameters
# * bucket_name<~String> - Name of bucket to create object in
# * object_name<~String> - Name of object to create
# * object<~String> - File to create object from
# * options<~Hash>:
# * 'Cache-Control'<~String> - Caching behaviour
# * 'Content-Disposition'<~String> - Presentational information for the object
# * 'Content-Encoding'<~String> - Encoding of object data
# * 'Content-Length'<~String> - Size of object in bytes (defaults to object.read.length)
# * 'Content-MD5'<~String> - Base64 encoded 128-bit MD5 digest of message (defaults to Base64 encoded MD5 of object.read)
# * 'Content-Type'<~String> - Standard MIME type describing contents (defaults to MIME::Types.of.first)
# * 'x-amz-acl'<~String> - Permissions, must be in ['private', 'public-read', 'public-read-write', 'authenticated-read']
# * "x-amz-meta-#{name}" - Headers to be returned with object, note total size of request without body must be less than 8 KB.
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * headers<~Hash>:
# * 'ETag'<~String> - etag of new object
def put_object(bucket_name, object_name, object, options = {})
file = parse_file(object)
headers = file[:headers].merge!(options)
request({
:body => file[:body],
:expects => 200,
:headers => headers,
:host => "#{bucket_name}.#{@host}",
:method => 'PUT',
:path => object_name
})
end
end
end
else
module Fog
module AWS
class S3
def put_object(bucket_name, object_name, object, options = {})
file = parse_file(object)
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['Contents'] << {
:body => file[:body],
'ETag' => 'some_etag',
'Key' => object_name,
'LastModified' => Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000"),
'Owner' => { 'DisplayName' => 'owner', 'ID' => 'some_id'},
'Size' => file[:headers]['Content-Length'],
'StorageClass' => 'STANDARD'
}
else
response.status = bucket_status
end
response
end
end
end
end
end

View file

@ -3,17 +3,18 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe 'S3.delete_object' do
before(:all) do
s3.put_bucket('fogdeleteobject')
@s3 = s3
@s3.put_bucket('fogdeleteobject')
file = File.open(File.dirname(__FILE__) + '/../../lorem.txt', 'r')
s3.put_object('fogdeleteobject', 'fog_delete_object', file)
@s3.put_object('fogdeleteobject', 'fog_delete_object', file)
end
after(:all) do
s3.delete_bucket('fogdeleteobject')
@s3.delete_bucket('fogdeleteobject')
end
it 'should return proper attributes' do
actual = s3.delete_object('fogdeleteobject', 'fog_delete_object')
actual = @s3.delete_object('fogdeleteobject', 'fog_delete_object')
actual.status.should == 204
end

View file

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

View file

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

View file

@ -1,6 +1,7 @@
require 'spec'
current_directory = File.dirname(__FILE__)
# require "#{current_directory}/../lib/fog"
require "#{current_directory}/../lib/fog/mocking"
def credentials