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

[s3] cleaner mocking/dependencies

This commit is contained in:
geemus (Wesley Beary) 2010-03-13 13:37:24 -08:00
parent 51c8e69701
commit 46ea4a4a99
18 changed files with 241 additions and 310 deletions

View file

@ -1,11 +1,12 @@
require 'fog/aws/s3.rb'
module Fog
module AWS
def self.dependencies
[
'fog/aws/ec2.rb',
'fog/aws/simpledb.rb',
'fog/aws/s3.rb'
'fog/aws/simpledb.rb'
]
end

View file

@ -1,10 +1,21 @@
require 'fog/collection'
require 'fog/aws/models/s3/directory'
module Fog
module AWS
class S3
module S3
class Real
def directories
Fog::AWS::S3::Directories.new(:connection => self)
end
end
class Mock
def directories
Fog::AWS::S3::Directories.new(:connection => self)
end
end
class Directories < Fog::Collection

View file

@ -1,6 +1,9 @@
require 'fog/model'
require 'fog/aws/models/s3/files'
module Fog
module AWS
class S3
module S3
class Directory < Fog::Model

View file

@ -1,6 +1,8 @@
require 'fog/model'
module Fog
module AWS
class S3
module S3
class File < Fog::Model

View file

@ -1,6 +1,9 @@
require 'fog/collection'
require 'fog/aws/models/s3/file'
module Fog
module AWS
class S3
module S3
class Files < Fog::Collection

View file

@ -1,8 +1,9 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
require 'fog/aws/parsers/s3/copy_object'
# Copy an object from one S3 bucket to another
#
@ -38,20 +39,14 @@ unless Fog.mocking?
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {})
response = Excon::Response.new
source_bucket = Fog::AWS::S3.data[:buckets][source_bucket_name]
source_bucket = @data[:buckets][source_bucket_name]
source_object = source_bucket && source_bucket[:objects][source_object_name]
target_bucket = Fog::AWS::S3.data[:buckets][target_bucket_name]
target_bucket = @data[:buckets][target_bucket_name]
if source_object && target_bucket
response.status = 200
@ -75,5 +70,4 @@ else
end
end
end
end

View file

@ -1,8 +1,7 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
# Delete an S3 bucket
#
@ -22,32 +21,26 @@ unless Fog.mocking?
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def delete_bucket(bucket_name)
response = Excon::Response.new
if Fog::AWS::S3.data[:buckets][bucket_name].nil?
if @data[:buckets][bucket_name].nil?
response.status = 404
raise(Excon::Errors.status_error({:expects => 204}, response))
elsif Fog::AWS::S3.data[:buckets][bucket_name] && !Fog::AWS::S3.data[:buckets][bucket_name][:objects].empty?
elsif @data[:buckets][bucket_name] && !@data[:buckets][bucket_name][:objects].empty?
response.status = 409
raise(Excon::Errors.status_error({:expects => 204}, response))
else
Fog::AWS::S3.data[:buckets].delete(bucket_name)
@data[:buckets].delete(bucket_name)
response.status = 204
end
response
end
end
end
end
end
end
end

View file

@ -1,8 +1,7 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
# Delete an object from S3
#
@ -25,18 +24,12 @@ unless Fog.mocking?
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def delete_object(bucket_name, object_name)
response = Excon::Response.new
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
if bucket = @data[:buckets][bucket_name]
response.status = 204
bucket[:objects].delete(object_name)
else
@ -49,5 +42,4 @@ else
end
end
end
end

View file

@ -1,8 +1,9 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
require 'fog/aws/parsers/s3/get_bucket'
# List information about objects in an S3 bucket
#
@ -54,14 +55,8 @@ unless Fog.mocking?
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
# FIXME: implement delimiter
def get_bucket(bucket_name, options = {})
@ -69,16 +64,17 @@ else
raise ArgumentError.new('bucket_name is required')
end
response = Excon::Response.new
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
if bucket = @data[:buckets][bucket_name]
response.status = 200
response.body = {
'Contents' => bucket[:objects].values.sort {|x,y| x['Key'] <=> y['Key']}.reject do |object|
(options['prefix'] && object['Key'][0...options['prefix'].length] != options['prefix']) ||
(options['marker'] && object['Key'] <= options['marker'])
end.map do |object|
data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Owner', 'Size', 'StorageClass'].include?(key)}
data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Size', 'StorageClass'].include?(key)}
data.merge!({
'LastModified' => Time.parse(data['LastModified']),
'Owner' => bucket['Owner'],
'Size' => data['Size'].to_i
})
data
@ -103,5 +99,4 @@ else
end
end
end
end

View file

@ -1,8 +1,9 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
require 'fog/aws/parsers/s3/get_bucket_location'
# Get location constraint for an S3 bucket
#
@ -26,18 +27,12 @@ unless Fog.mocking?
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def get_bucket_location(bucket_name)
response = Excon::Response.new
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
if bucket = @data[:buckets][bucket_name]
response.status = 200
response.body = {'LocationConstraint' => bucket['LocationConstraint'] }
else
@ -50,5 +45,4 @@ else
end
end
end
end

View file

@ -1,8 +1,7 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
# Get an object from S3
#
@ -56,19 +55,13 @@ unless Fog.mocking?
:headers => {},
:host => "#{bucket_name}.#{@host}",
:method => 'GET',
:path => object_name
:path => CGI.escape(object_name)
}, expires)
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def get_object(bucket_name, object_name, options = {}, &block)
unless bucket_name
@ -78,7 +71,7 @@ else
raise ArgumentError.new('object_name is required')
end
response = Excon::Response.new
if (bucket = Fog::AWS::S3.data[:buckets][bucket_name]) && (object = bucket[:objects][object_name])
if (bucket = @data[:buckets][bucket_name]) && (object = bucket[:objects][object_name])
if options['If-Match'] && options['If-Match'] != object['ETag']
response.status = 412
elsif options['If-Modified-Since'] && options['If-Modified-Since'] > Time.parse(object['LastModified'])
@ -121,5 +114,4 @@ else
end
end
end
end

View file

@ -1,8 +1,9 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
require 'fog/aws/parsers/s3/get_request_payment'
# Get configured payer for an S3 bucket
#
@ -26,18 +27,12 @@ unless Fog.mocking?
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def get_request_payment(bucket_name)
response = Excon::Response.new
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
if bucket = @data[:buckets][bucket_name]
response.status = 200
response.body = { 'Payer' => bucket['Payer'] }
else
@ -50,5 +45,4 @@ else
end
end
end
end

View file

@ -1,8 +1,9 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
require 'fog/aws/parsers/s3/get_service'
# List information about S3 buckets for authorized user
#
@ -28,19 +29,13 @@ unless Fog.mocking?
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def get_service
response = Excon::Response.new
response.headers['Status'] = 200
buckets = Fog::AWS::S3.data[:buckets].values.map do |bucket|
buckets = @data[:buckets].values.map do |bucket|
bucket.reject do |key, value|
!['CreationDate', 'Name'].include?(key)
end
@ -55,5 +50,4 @@ else
end
end
end
end

View file

@ -1,8 +1,7 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
# Get headers for an object from S3
#
@ -39,14 +38,8 @@ unless Fog.mocking?
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def head_object(bucket_name, object_name, options = {})
response = get_object(bucket_name, object_name, options)
@ -57,5 +50,4 @@ else
end
end
end
end

View file

@ -1,8 +1,7 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
# Create an S3 bucket
#
@ -17,11 +16,11 @@ unless Fog.mocking?
def put_bucket(bucket_name, options = {})
if options['LocationConstraint']
data =
<<-DATA
<<-DATA
<CreateBucketConfiguration>
<LocationConstraint>#{options['LocationConstraint']}</LocationConstraint>
</CreateBucketConfiguration>
DATA
DATA
else
data = nil
end
@ -36,14 +35,8 @@ unless Fog.mocking?
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def put_bucket(bucket_name, options = {})
response = Excon::Response.new
@ -60,8 +53,8 @@ else
else
bucket['LocationConstraint'] = ''
end
unless Fog::AWS::S3.data[:buckets][bucket_name]
Fog::AWS::S3.data[:buckets][bucket_name] = bucket
unless @data[:buckets][bucket_name]
@data[:buckets][bucket_name] = bucket
end
response
end
@ -69,5 +62,4 @@ else
end
end
end
end

View file

@ -1,8 +1,7 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
# Create an object in an S3 bucket
#
@ -25,7 +24,7 @@ unless Fog.mocking?
# * headers<~Hash>:
# * 'ETag'<~String> - etag of new object
def put_object(bucket_name, object_name, data, options = {})
data = parse_data(data)
data = Fog::AWS::S3.parse_data(data)
headers = data[:headers].merge!(options)
request({
:body => data[:body],
@ -39,19 +38,13 @@ unless Fog.mocking?
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def put_object(bucket_name, object_name, data, options = {})
data = parse_data(data)
data = Fog::AWS::S3.parse_data(data)
response = Excon::Response.new
if (bucket = Fog::AWS::S3.data[:buckets][bucket_name])
if (bucket = @data[:buckets][bucket_name])
response.status = 200
bucket[:objects][object_name] = {
:body => data[:body],
@ -72,5 +65,4 @@ else
end
end
end
end

View file

@ -1,8 +1,7 @@
unless Fog.mocking?
module Fog
module Fog
module AWS
class S3
module S3
class Real
# Change who pays for requests to an S3 bucket
#
@ -27,18 +26,12 @@ DATA
end
end
end
end
else
module Fog
module AWS
class S3
class Mock
def put_request_payment(bucket_name, payer)
response = Excon::Response.new
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
if bucket = @data[:buckets][bucket_name]
response.status = 200
bucket['Payer'] = payer
else
@ -51,6 +44,4 @@ else
end
end
end
end

View file

@ -1,49 +1,67 @@
require 'fog/aws/models/s3/directories'
require 'fog/aws/models/s3/directory'
require 'fog/aws/models/s3/files'
require 'fog/aws/models/s3/file'
require 'fog/aws/requests/s3/copy_object'
require 'fog/aws/requests/s3/delete_bucket'
require 'fog/aws/requests/s3/delete_object'
require 'fog/aws/requests/s3/get_bucket'
require 'fog/aws/requests/s3/get_bucket_location'
require 'fog/aws/requests/s3/get_object'
require 'fog/aws/requests/s3/get_request_payment'
require 'fog/aws/requests/s3/get_service'
require 'fog/aws/requests/s3/head_object'
require 'fog/aws/requests/s3/put_bucket'
require 'fog/aws/requests/s3/put_object'
require 'fog/aws/requests/s3/put_request_payment'
module Fog
module AWS
class S3
module S3
def self.new(options={})
if Fog.mocking?
def self.reset_data
Fog::AWS::S3::Mock.new(options)
else
Fog::AWS::S3::Real.new(options)
end
end
def self.parse_data(data)
metadata = {
:body => nil,
:headers => {}
}
if data.is_a?(String)
metadata[:body] = data
metadata[:headers]['Content-Length'] = metadata[:body].size.to_s
else
filename = ::File.basename(data.path)
unless (mime_types = MIME::Types.of(filename)).empty?
metadata[:headers]['Content-Type'] = mime_types.first.content_type
end
metadata[:body] = data.read
metadata[:headers]['Content-Length'] = ::File.size(data.path).to_s
end
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
metadata
end
class Mock
def reset_data
@data = { :buckets => {} }
end
def self.data
@data
end
end
def self.dependencies
[
"fog/aws/models/s3/directory.rb",
"fog/aws/models/s3/directories.rb",
"fog/aws/models/s3/file.rb",
"fog/aws/models/s3/files.rb",
"fog/aws/parsers/s3/copy_object.rb",
"fog/aws/parsers/s3/get_bucket.rb",
"fog/aws/parsers/s3/get_bucket_location.rb",
"fog/aws/parsers/s3/get_request_payment.rb",
"fog/aws/parsers/s3/get_service.rb",
"fog/aws/requests/s3/copy_object.rb",
"fog/aws/requests/s3/delete_bucket.rb",
"fog/aws/requests/s3/delete_object.rb",
"fog/aws/requests/s3/get_bucket.rb",
"fog/aws/requests/s3/get_bucket_location.rb",
"fog/aws/requests/s3/get_object.rb",
"fog/aws/requests/s3/get_request_payment.rb",
"fog/aws/requests/s3/get_service.rb",
"fog/aws/requests/s3/head_object.rb",
"fog/aws/requests/s3/put_bucket.rb",
"fog/aws/requests/s3/put_object.rb",
"fog/aws/requests/s3/put_request_payment.rb"
]
end
def self.reload
self.dependencies.each {|dependency| load(dependency)}
if Fog.mocking?
def initialize(options={})
reset_data
end
end
class Real
# Initialize connection to S3
#
# ==== Notes
@ -76,27 +94,6 @@ module Fog
private
def parse_data(data)
metadata = {
:body => nil,
:headers => {}
}
if data.is_a?(String)
metadata[:body] = data
metadata[:headers]['Content-Length'] = metadata[:body].size.to_s
else
filename = ::File.basename(data.path)
unless (mime_types = MIME::Types.of(filename)).empty?
metadata[:headers]['Content-Type'] = mime_types.first.content_type
end
metadata[:body] = data.read
metadata[:headers]['Content-Length'] = ::File.size(data.path).to_s
end
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
metadata
end
def request(params)
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
@ -107,15 +104,6 @@ module Fog
response
end
def url(params, expires)
params[:headers]['Date'] = expires.to_i
query = [params[:query]].compact
query << "AWSAccessKeyId=#{@aws_access_key_id}"
query << "Signature=#{CGI.escape(signature(params))}"
query << "Expires=#{params[:headers]['Date']}"
"http://#{params[:host]}/#{params[:path]}?#{query.join('&')}"
end
def signature(params)
string_to_sign =
<<-DATA
@ -163,8 +151,16 @@ DATA
signature = Base64.encode64(hmac.digest).chomp!
end
def url(params, expires)
params[:headers]['Date'] = expires.to_i
query = [params[:query]].compact
query << "AWSAccessKeyId=#{@aws_access_key_id}"
query << "Signature=#{CGI.escape(signature(params))}"
query << "Expires=#{params[:headers]['Date']}"
"http://#{params[:host]}/#{params[:path]}?#{query.join('&')}"
end
end
end
end
end
Fog::AWS::S3.reload