/
object_spec.rb
66 lines (52 loc) · 2.35 KB
/
object_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# encoding: utf-8
require File.expand_path('../../spec_helper.rb', __FILE__)
describe Backup::Storage::Object do
describe '#initialize' do
it 'uses storage type only as YAML filename if no storage_id' do
[nil, '', ' '].each do |storage_id|
object = Backup::Storage::Object.new(:s3, storage_id)
object.storage_file.should == File.join(Backup::DATA_PATH, Backup::TRIGGER, 's3.yml')
end
end
it 'appends optional storage_id' do
object = Backup::Storage::Object.new(:s3, 'foo')
object.storage_file.should == File.join(Backup::DATA_PATH, Backup::TRIGGER, 's3-foo.yml')
end
it 'sanitizes user-defined storage_id for use as filename' do
[ ['Backup Server #1', 'Backup_Server__1'],
[' {Special} Storage ', '_Special__Storage'],
['Cloud (!@$%^&*) #9', 'Cloud____________9'] ].each do |input, sanitized|
object = Backup::Storage::Object.new(:s3, input)
object.storage_file.should == File.join(Backup::DATA_PATH, Backup::TRIGGER, "s3-#{sanitized}.yml")
end
end
end
describe '#load' do
let(:object) { Backup::Storage::Object.new(:s3, nil) }
it 'should return an array with objects' do
File.expects(:exist?).returns(true)
YAML.expects(:load_file).with(
File.join(Backup::DATA_PATH, Backup::TRIGGER, 's3.yml')
).returns(YAML.load([Backup::Storage::S3.new, Backup::Storage::S3.new].to_yaml))
objects = object.load
objects.should be_an(Array)
objects.first.should be_an_instance_of(Backup::Storage::S3)
end
it 'should load them sorted by time descending (newest backup is first in the array)' do
obj_1 = Backup::Storage::S3.new; obj_1.time = '2007.00.00.00.00.00'
obj_2 = Backup::Storage::S3.new; obj_2.time = '2009.00.00.00.00.00'
obj_3 = Backup::Storage::S3.new; obj_3.time = '2011.00.00.00.00.00'
[obj_1, obj_2, obj_3].permutation.each do |perm|
File.expects(:exist?).returns(true)
File.expects(:zero?).returns(false)
YAML.expects(:load_file).with(
File.join(Backup::DATA_PATH, Backup::TRIGGER, 's3.yml')
).returns(YAML.load(perm.to_yaml))
objects = object.load
objects[0].time.should == '2011.00.00.00.00.00'
objects[1].time.should == '2009.00.00.00.00.00'
objects[2].time.should == '2007.00.00.00.00.00'
end
end
end
end