require File.expand_path("../../lib/selbackup", __FILE__) describe SelBackup do before (:each) do @selbackup = SelBackup.new('key', 'secret', 'bucket') end describe '#initialize' do it 'sets the access_key' do expect(@selbackup.access_key).to eq 'key' end it 'sets the access_secret' do expect(@selbackup.access_secret).to eq 'secret' end it 'sets the bucket_name' do expect(@selbackup.bucket_name).to eq 'bucket' end it 'sets the region' do @selbackup = SelBackup.new('key', 'secret', 'bucket', 'region') expect(@selbackup.region).to eq 'region' end it 'does not set the region' do expect(@selbackup.region).to eq 'eu-west-1' end end describe '#gen_daily_file' do it 'pushes a file to daily' do allow(File).to receive(:open).and_return(nil) file = '2013-01-01-file.tgz' expectation = { myname: 'file.tgz', key: 'file.tgz/2013-01-01-daily-file.tgz', body: nil } expect(@selbackup.gen_daily_file(file)).to include( expectation ) end it 'has not an extension' do allow(File).to receive(:open).and_return(nil) file = '2013-01-01-file' expectation = { myname: 'file', key: 'file/2013-01-01-daily-file', body: nil } expect(@selbackup.gen_daily_file(file)).to include( expectation ) end it 'has not a date' do allow(File).to receive(:open).and_return(nil) file = 'file.tgz' expectation = { myname: 'file.tgz', key: "file.tgz/#{Date.today.to_s}-daily-file.tgz", body: nil } expect(@selbackup.gen_daily_file(file)).to include( expectation ) end it 'has not a date and an extension' do allow(File).to receive(:open).and_return(nil) file = 'file' expectation = { myname: 'file', key: "file/#{Date.today.to_s}-daily-file", body: nil } expect(@selbackup.gen_daily_file(file)).to include( expectation ) end it 'has an different extension and not a date' do allow(File).to receive(:open).and_return(nil) file = 'file.myextension' expectation = { myname: 'file.myextension', key: "file.myextension/#{Date.today.to_s}-daily-file.myextension", body: nil } expect(@selbackup.gen_daily_file(file)).to include( expectation ) end it 'has a different extension' do allow(File).to receive(:open).and_return(nil) file = '2013-01-01-file.myextension' expectation = { myname: 'file.myextension', key: 'file.myextension/2013-01-01-daily-file.myextension', body: nil } expect(@selbackup.gen_daily_file(file)).to include( expectation ) end it 'has only a date and an extension' do allow(File).to receive(:open).and_return(nil) file = '2013-01-01.tgz' expect(@selbackup.gen_daily_file(file)).to eq false end it 'has only a date' do allow(File).to receive(:open).and_return(nil) file = '2013-01-01' expect(@selbackup.gen_daily_file(file)).to eq false end end describe '#daily_good?' do it 'gets 7 daily files' do files = ['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7'] expect(@selbackup.daily_good?(files, 7)).to eq true end it 'gets more than 7 daily files' do files = ['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7', 'file8'] expect(@selbackup.daily_good?(files, 7)).to eq false end end describe '#should_weekly?' do it 'has less than one week date diff' do file1 = 'file.tgz/2013-01-02-daily-file.tgz' file2 = 'file.tgz/2013-01-01-weekly-file.tgz' expect(@selbackup.should_weekly?(file1, file2)).to eq false end it 'has less than one week date diff and different syntax' do file1 = 'file.tgz/daily-2013-01-02-file.tgz' file2 = 'file.tgz/2013-01-01-weekly-file.tgz' expect(@selbackup.should_weekly?(file1, file2)).to eq false end it 'has less than one week date diff and different syntax' do file1 = 'file.tgz/daily-2013-01-02-file.tgz' file2 = 'file.tgz/weekly-2013-01-01-file.tgz' expect(@selbackup.should_weekly?(file1, file2)).to eq false end it 'has less than one week date diff and different syntax' do file1 = 'file.tgz/2013-01-02-daily-file.tgz' file2 = 'file.tgz/weekly-2013-01-01-file.tgz' expect(@selbackup.should_weekly?(file1, file2)).to eq false end it 'has more than one week date diff' do file1 = 'file.tgz/2013-01-09-daily-file.tgz' file2 = 'file.tgz/2013-01-01-weekly-file.tgz' expect(@selbackup.should_weekly?(file1, file2)).to eq true end it 'has more than one week date diff and different syntax' do file1 = 'file.tgz/daily-2013-01-09-file.tgz' file2 = 'file.tgz/2013-01-01-weekly-file.tgz' expect(@selbackup.should_weekly?(file1, file2)).to eq true end it 'has more than one week date diff and different syntax' do file1 = 'file.tgz/2013-01-09-daily-file.tgz' file2 = 'file.tgz/weekly-2013-01-01-file.tgz' expect(@selbackup.should_weekly?(file1, file2)).to eq true end it 'has more than one week date diff and different syntax' do file1 = 'file.tgz/daily-2013-01-09-file.tgz' file2 = 'file.tgz/weekly-2013-01-01-file.tgz' expect(@selbackup.should_weekly?(file1, file2)).to eq true end end describe '#gen_weekly_file' do it 'pushes a file to weekly' do file = { key: 'file.tgz/2013-01-01-daily-file.tgz', body: 'file content'} expectation = { key: 'file.tgz/2013-01-01-weekly-file.tgz', body: 'file content'} expect(@selbackup.gen_weekly_file(file)).to include( expectation ) end end describe '#weekly_good?' do it 'gets 4 weekly files' do files = ['file1', 'file2', 'file3', 'file4'] expect(@selbackup.weekly_good?(files, 4)).to eq true end it 'gets more than 4 weekly files' do files = ['file1', 'file2', 'file3', 'file4', 'file5'] expect(@selbackup.weekly_good?(files, 4)).to eq false end end describe '#should_monthly?' do it 'has less than one month date diff' do file1 = 'file.tgz/2013-02-02-weekly-file.tgz' file2 = 'file.tgz/2013-02-01-monthly-file.tgz' expect(@selbackup.should_monthly?(file1, file2)).to eq false end it 'has less than one month date diff and different syntax ' do file1 = 'file.tgz/weekly-2013-02-02-file.tgz' file2 = 'file.tgz/monthly-2013-02-01-file.tgz' expect(@selbackup.should_monthly?(file1, file2)).to eq false end it 'has less than one month date diff and different syntax' do file1 = 'file.tgz/weekly-2013-02-02-file.tgz' file2 = 'file.tgz/2013-02-01-monthly-file.tgz' expect(@selbackup.should_monthly?(file1, file2)).to eq false end it 'has less than one month date diff and different syntax' do file1 = 'file.tgz/2013-02-02-weekly-file.tgz' file2 = 'file.tgz/monthly-2013-02-01-file.tgz' expect(@selbackup.should_monthly?(file1, file2)).to eq false end it 'has more than one month date diff' do file1 = 'file.tgz/2013-02-02-weekly-file.tgz' file2 = 'file.tgz/2013-01-01-monthly-file.tgz' expect(@selbackup.should_monthly?(file1, file2)).to eq true end it 'has more than one month date diff and different syntax' do file1 = 'file.tgz/2013-02-02-weekly-file.tgz' file2 = 'file.tgz/monthly-2013-01-01-file.tgz' expect(@selbackup.should_monthly?(file1, file2)).to eq true end it 'has more than one month date diff and different syntax' do file1 = 'file.tgz/weekly-2013-02-02-file.tgz' file2 = 'file.tgz/2013-01-01-monthly-file.tgz' expect(@selbackup.should_monthly?(file1, file2)).to eq true end it 'has more than one month date diff and different syntax' do file1 = 'file.tgz/weekly-2013-02-02-file.tgz' file2 = 'file.tgz/monthly-2013-01-01-file.tgz' expect(@selbackup.should_monthly?(file1, file2)).to eq true end end describe '#gen_monthly_file' do it 'pushes a file to monthly' do file = { key: 'file.tgz/2013-01-01-weekly-file.tgz', body: 'file content'} expectation = { key: 'file.tgz/2013-01-01-monthly-file.tgz', body: 'file content'} expect(@selbackup.gen_monthly_file(file)).to include( expectation ) end end describe '#monthly_good?' do it 'gets 3 montly files' do files = ['file1', 'file2', 'file3'] expect(@selbackup.monthly_good?(files, 3)).to eq true end it 'gets more than 3 monthly files' do files = ['file1', 'file2', 'file3', 'file4'] expect(@selbackup.monthly_good?(files, 3)).to eq false end end end