require 'rubygems' gem 'RequirePaths'; require 'require_paths' require_paths '../lib','../../..' require 'test/unit' gem 'Shoulda'; require 'shoulda' require 'ihl_ruby/misc_utils' require 'ihl_ruby/shell_extras' require 'fileutils' require 'yore/yore_core' def ensure_clean_bucket(aName) `s3cmd createbucket #{aName}` `s3cmd deleteall #{aName}` end class YoreTest < Test::Unit::TestCase def setup @yore = YoreCore::Yore.new end should "collect a list of files into a single file with a .contents file" do # create a temp dir temp_dir = MiscUtils::make_temp_dir('yore_test') # create source and dest subfolders source_dir = File.expand_path('source',temp_dir) dest_dir = File.expand_path('dest',temp_dir) FileUtils.mkdir_p([source_dir,dest_dir]) # create some dirs and files in source ['a','a/1','b','c'].each {|p| FileUtils.mkdir_p(File.expand_path(p,source_dir))} %w(a/blah.txt a/1/blahblah.txt b/apples.txt c/carrots.txt).each do |f| MiscUtils::make_temp_file(f,source_dir) end # collect into dest file # get recursive file list, without carrots filelist = MiscUtils::recursive_file_list(source_dir) {|p| not p =~ /carrots.txt/} dest_file = File.join(dest_dir,'destfile.bzb') @yore.collect(filelist,dest_file) # check contains filelist files ignoring .contents cmd = "tar --list --file=#{dest_file}" filelist_out = `#{cmd}` i = 0 filelist_out.each_line {|line| next if line==".contents\n" assert i < filelist.length assert_equal line.chomp("\n"), MiscUtils.path_debase(filelist[i],source_dir) # .bite('/') i += 1 } # could also check .contents file against actual contents end should "encrypt and unencrypt a file with a standard iv but a supplied key" do orig_content = 'abcdef123456' temp_file1 = MiscUtils.make_temp_file(nil,nil,orig_content) temp_file2 = temp_file1+'.enc' temp_file3 = temp_file1+'.dec' @yore.pack(temp_file1,temp_file2) @yore.unpack(temp_file2,temp_file3) file3_content = MiscUtils.string_from_file(temp_file3) assert_equal file3_content, orig_content end should "create a backup filename combining a prefix, date, day and standard extension, and be able to decode the filename back to a date" do @yore.configure({ :prefix => 'test', :first_hour => 4 }) now = Time.local(2009, 1, 1, 0, 0, 0, 0) exp_filename = "test-20081231.yor" calc_filename = @yore.encode_file_name(now) assert_equal calc_filename, exp_filename assert_equal Time.local(2008, 12, 31), @yore.decode_file_name(calc_filename) end should "upload and download files intact" do bucket = 'yore_test' ensure_clean_bucket(bucket) @yore.configure({ :bucket => bucket }) content = 'this is my test content' temp_dir = MiscUtils.make_temp_dir() temp_file = MiscUtils.make_temp_file(nil, temp_dir, content) puts @yore.upload(temp_file) orig_file = temp_file+'.orig' File.rename(temp_file, orig_file) puts @yore.download(temp_file) down_file_content = MiscUtils.string_from_file(temp_file) assert_equal content, down_file_content end should "backup and restore multiple directories of file" do # create a temp dir temp_dir = MiscUtils::make_temp_dir('yore_test') # create source and dest subfolders source_dir = File.expand_path('source',temp_dir) source1 = File.join(source_dir,'source1') source2 = File.join(source_dir,'source2') dest_dir = File.expand_path('dest',temp_dir) bucket = 'yore_test' FileUtils.mkdir_p([source1,source2,dest_dir]) # create some dirs and files in source ['a','a/1','b','c'].each {|p| FileUtils.mkdir_p(File.expand_path(p,source1))} %w(a/blah.txt a/1/blahblah.txt b/apples.txt c/carrots.txt).each do |f| MiscUtils::make_temp_file(f,source1) end ['w','x/1','y/2','z'].each {|p| FileUtils.mkdir_p(File.expand_path(p,source2))} %w(w/zonk.txt x/1/eggs.txt y/2/bloop.txt z/zax.txt).each do |f| MiscUtils::make_temp_file(f,source2) end # create job file job_template = < 3A63775C1E3F291B0925578165EB917E 07692FC8656F04AE5518B80D38681E038A3C12050DF6CC97CEEC33D800D5E2FE backup INFO ${BUCKET} ${SOURCE1} ${SOURCE2} EOF job_content = StringUtils::render_template(job_template,{ 'SOURCE1' => source1, 'SOURCE2' => source2, 'BUCKET' => bucket }) MiscUtils::string_to_file(job_content,job_file = MiscUtils::temp_file) ensure_clean_bucket(bucket) @yore.configure({ :bucket => bucket }) # call yore script with ruby from the command line, then download result and check contents begin result = @yore.shell("ruby #{File.expand_path('../../bin/yore',THIS_DIR)} backup #{job_file}") rescue ::StandardError => e flunk e.inspect end puts result retrieved_fname = File.expand_path(@yore.encode_file_name(), dest_dir) collection_fname = MiscUtils::temp_file puts @yore.download(retrieved_fname) puts @yore.unpack(retrieved_fname,collection_fname) filelist = MiscUtils::recursive_file_list(source_dir,false) # check contains filelist files ignoring .contents cmd = "tar --list --file=#{collection_fname}" filelist_out = @yore.shell(cmd) filelist_out = filelist_out.split("\n").sort assert_equal filelist, filelist_out # i = 0 # filelist_out.each {|line| # next if line==".contents\n" # i.should < filelist.length # line.chomp("\n").should == MiscUtils.path_debase(filelist[i],source_dir) # .bite('/') # i += 1 # } # could also check .contents file against actual contents end should "handle test_file_b" do aController = YoreCore::Yore.new # main program object job = File.expand_path('../../test/test_job_b.xml',THIS_DIR) xmlRoot = XmlUtils.get_file_root(job) srcdir = '/tmp/yoretest' FileUtils.rm_rf srcdir+'/*' FileUtils.mkdir_p srcdir ['a','a/1','b','c'].each {|p| FileUtils.mkdir_p(File.expand_path(p,srcdir))} %w(a/blah.txt a/1/blahblah.txt b/apples.txt c/carrots.txt).each do |f| MiscUtils::make_temp_file(f,srcdir) end aController.configure(xmlRoot,nil,{:basepath => File.dirname(File.expand_path(job))}) aController.backup(job) end should "provide configurable criteria for keeping old files" should "clean a folder full of files, removing files that don't match configurable criteria for keeping old files" end