Permalink
Browse files

Get tests passing.

  • Loading branch information...
vitaly committed Jan 22, 2010
1 parent 4c5f757 commit 9e24a1d7e49d9fb1a8df5ec00a20c42dafe18ea1
@@ -51,7 +51,6 @@
end
it "should create backup file" do
- puts "Expecting: #{@backup}"
File.exists?(@backup).should be_true
end
@@ -47,7 +47,6 @@
end
it "should create backup file" do
- puts "Expecting: #{@backup}"
File.exists?(@backup).should be_true
end
@@ -36,10 +36,17 @@ def cloudfiles(config = def_config, backup = def_backup)
before(:each) do
@cloudfiles = cloudfiles
- @files = [4,1,3,2].to_a.map { |i| stub(o = {}).name {"aaaaa#{i}"}; o }
- mock.instance_of(CloudFiles::Container).objects(:prefix => "_kind/_id/_kind-_id.") { @files }
- stub(CloudFiles::Authentication).new { true }
-
+
+ @files = [4,1,3,2].to_a.map { |i| "aaaaa#{i}" }
+
+ @container = "container"
+
+ stub(@container).objects(:prefix => "_kind/_id/_kind-_id.") { @files }
+ stub(@container).delete_object(anything)
+
+ stub(CloudFiles::Connection).
+ new('_user', '_api_key', true, false).stub!.
+ container('_container') {@container}
end
it "should check [:keep, :cloudfiles]" do
@@ -49,111 +56,115 @@ def cloudfiles(config = def_config, backup = def_backup)
end
it "should delete extra files" do
- mock(CloudFiles::Connection).container("_container").mock!["aaaaa1"].mock!.delete
- mock(CloudFiles::Connection).container("_container").mock!["aaaaa2"].mock!.delete
+ mock(@container).delete_object('aaaaa1')
+ mock(@container).delete_object('aaaaa2')
@cloudfiles.send :cleanup
end
end
- # describe :active do
- # before(:each) do
- # @s3 = s3
- # end
- #
- # it "should be true when all params are set" do
- # @s3.should be_active
- # end
- #
- # it "should be false if bucket is missing" do
- # @s3.config[:s3].data["bucket"] = nil
- # @s3.should_not be_active
- # end
- #
- # it "should be false if key is missing" do
- # @s3.config[:s3].data["key"] = nil
- # @s3.should_not be_active
- # end
- #
- # it "should be false if secret is missing" do
- # @s3.config[:s3].data["secret"] = nil
- # @s3.should_not be_active
- # end
- # end
- #
- # describe :path do
- # before(:each) do
- # @s3 = s3
- # end
- # it "should use s3/path 1st" do
- # @s3.config[:s3].data["path"] = "s3_path"
- # @s3.config[:local] = {:path => "local_path"}
- # @s3.send(:path).should == "s3_path"
- # end
- #
- # it "should use local/path 2nd" do
- # @s3.config[:local] = {:path => "local_path"}
- # @s3.send(:path).should == "local_path"
- # end
- #
- # it "should use constant 3rd" do
- # @s3.send(:path).should == "_kind/_id"
- # end
- #
- # end
- #
- # describe :save do
- # def add_stubs(*stubs)
- # stubs.each do |s|
- # case s
- # when :connection
- # stub(AWS::S3::Base).establish_connection!(:access_key_id => "_key", :secret_access_key => "_secret", :use_ssl => true)
- # when :stat
- # stub(File).stat("foo").stub!.size {123}
- # when :create_bucket
- # stub(AWS::S3::Bucket).create
- # when :file_open
- # stub(File).open("foo") {|f, block| block.call(:opened_file)}
- # when :s3_store
- # stub(AWS::S3::S3Object).store(@full_path, :opened_file, "_bucket")
- # end
- # end
- # end
- #
- # before(:each) do
- # @s3 = s3(def_config, def_backup(:path => "foo"))
- # @full_path = "_kind/_id/backup/somewhere/_kind-_id.NOW.bar.bar"
- # end
- #
- # it "should fail if no backup.file is set" do
- # @s3.backup.path = nil
- # proc {@s3.send(:save)}.should raise_error(RuntimeError)
- # end
- #
- # it "should establish s3 connection" do
- # mock(AWS::S3::Base).establish_connection!(:access_key_id => "_key", :secret_access_key => "_secret", :use_ssl => true)
- # add_stubs(:stat, :create_bucket, :file_open, :s3_store)
- # @s3.send(:save)
- # end
- #
- # it "should open local file" do
- # add_stubs(:connection, :stat, :create_bucket)
- # mock(File).open("foo")
- # @s3.send(:save)
- # end
- #
- # it "should upload file" do
- # add_stubs(:connection, :stat, :create_bucket, :file_open)
- # mock(AWS::S3::S3Object).store(@full_path, :opened_file, "_bucket")
- # @s3.send(:save)
- # end
- #
- # it "should fail on files bigger then 5G" do
- # add_stubs(:connection)
- # mock(File).stat("foo").stub!.size {5*1024*1024*1024+1}
- # mock(STDERR).puts(anything)
- # dont_allow(Benchmark).realtime
- # @s3.send(:save)
- # end
- # end
+ describe :active do
+ before(:each) do
+ @cloudfiles = cloudfiles
+ end
+
+ it "should be true when all params are set" do
+ @cloudfiles.should be_active
+ end
+
+ it "should be false if container is missing" do
+ @cloudfiles.config[:cloudfiles].data["container"] = nil
+ @cloudfiles.should_not be_active
+ end
+
+ it "should be false if user is missing" do
+ @cloudfiles.config[:cloudfiles].data["user"] = nil
+ @cloudfiles.should_not be_active
+ end
+
+ it "should be false if api_key is missing" do
+ @cloudfiles.config[:cloudfiles].data["api_key"] = nil
+ @cloudfiles.should_not be_active
+ end
+ end
+
+ describe :path do
+ before(:each) do
+ @cloudfiles = cloudfiles
+ end
+ it "should use cloudfiles/path 1st" do
+ @cloudfiles.config[:cloudfiles].data["path"] = "cloudfiles_path"
+ @cloudfiles.config[:local] = {:path => "local_path"}
+ @cloudfiles.send(:path).should == "cloudfiles_path"
+ end
+
+ it "should use local/path 2nd" do
+ @cloudfiles.config[:local] = {:path => "local_path"}
+ @cloudfiles.send(:path).should == "local_path"
+ end
+
+ it "should use constant 3rd" do
+ @cloudfiles.send(:path).should == "_kind/_id"
+ end
+
+ end
+
+ describe :save do
+ def add_stubs(*stubs)
+ stubs.each do |s|
+ case s
+ when :connection
+ stub(CloudFiles::Authentication).new
+ stub(CloudFiles::Connection).
+ new('_user', '_api_key', true, false).stub!.
+ create_container('_container') {@container}
+ when :stat
+ stub(File).stat("foo").stub!.size {123}
+ when :create_container
+ @container = "container"
+ stub(@container).create_object("_kind/_id/backup/somewhere/_kind-_id.NOW.bar.bar", true) {@object}
+ stub(CloudFiles::Connection).create_container {@container}
+ when :file_open
+ stub(File).open("foo")
+ when :cloudfiles_store
+ @object = "object"
+ mock(@object).write(nil) {true}
+ end
+ end
+ end
+
+ before(:each) do
+ @cloudfiles = cloudfiles(def_config, def_backup(:path => "foo"))
+ @full_path = "_kind/_id/backup/somewhere/_kind-_id.NOW.bar.bar"
+ end
+
+ it "should fail if no backup.file is set" do
+ @cloudfiles.backup.path = nil
+ proc {@cloudfiles.send(:save)}.should raise_error(RuntimeError)
+ end
+
+ it "should establish Cloud Files connection" do
+ add_stubs(:connection, :stat, :create_container, :file_open, :cloudfiles_store)
+ @cloudfiles.send(:save)
+ end
+
+ it "should open local file" do
+ add_stubs(:connection, :stat, :create_container, :cloudfiles_store)
+ mock(File).open("foo")
+ @cloudfiles.send(:save)
+ end
+
+ it "should upload file" do
+ add_stubs(:connection, :stat, :create_container, :file_open, :cloudfiles_store)
+ @cloudfiles.send(:save)
+ end
+
+ it "should fail on files bigger then 5G" do
+ add_stubs(:connection)
+ mock(File).stat("foo").stub!.size {5*1024*1024*1024+1}
+ mock(STDERR).puts(anything)
+ dont_allow(Benchmark).realtime
+ @cloudfiles.send(:save)
+ end
+ end
end
@@ -2,7 +2,7 @@ module Astrails
module Safe
class Cloudfiles < Sink
MAX_CLOUDFILES_FILE_SIZE = 5368709120
-
+
protected
def active?
@@ -27,7 +27,7 @@ def save
benchmark = Benchmark.realtime do
cf_container = cf.create_container(container)
o = cf_container.create_object(full_path,true)
- o.write(open(@backup.path))
+ o.write(File.open(@backup.path))
end
puts "...done" if $_VERBOSE
puts("Upload took " + sprintf("%.2f", benchmark) + " second(s).") if $_VERBOSE
@@ -42,8 +42,7 @@ def cleanup
puts "listing files: #{container}:#{base}*" if $_VERBOSE
cf = CloudFiles::Connection.new(user, api_key, true, service_net) unless $LOCAL
cf_container = cf.container(container)
- files = cf_container.objects(:prefix => base)
- print "DEBUG: Files is #{files.to_s}\n"
+ files = cf_container.objects(:prefix => base).sort
cleanup_with_limit(files, keep) do |f|
puts "removing Cloud File #{container}:#{f}" if $DRY_RUN || $_VERBOSE

0 comments on commit 9e24a1d

Please sign in to comment.