/
virtual_machine_spec.rb
240 lines (206 loc) · 7.62 KB
/
virtual_machine_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
require 'spec_helper'
module PrlBackup
describe VirtualMachine do
before do
VirtualMachine.any_instance.stub(:run).and_return('')
@uuid = '{deadbeef}'
@vm = VirtualMachine.new('foo')
@vm.stub(:uuid).and_return(@uuid)
@vm.stub(:logger).and_return(stub(:info => nil))
end
describe '.each' do
it 'should iterate over all virtual machines' do
virtual_machines = []
3.times { |i| virtual_machines << mock("virtual_machine_#{i}") }
virtual_machines.map { |vm| vm.should_receive(:foo) }
VirtualMachine.stub(:all).and_return(virtual_machines)
VirtualMachine.each { |vm| vm.foo }
end
end
describe '.all' do
before do
@stdout = %q{UUID
{264eab18-563e-4ccb-935d-50269130c592}
{b0749d89-27c5-4d0f-88e4-b8aeab95cd35}
{b893da77-f465-4de4-ab3f-f77e16f0c485} }
@uuids = ['{264eab18-563e-4ccb-935d-50269130c592}',
'{b0749d89-27c5-4d0f-88e4-b8aeab95cd35}',
'{b893da77-f465-4de4-ab3f-f77e16f0c485}']
end
it 'should return a list of all virtual machines' do
vm = mock('virtual_machine')
VirtualMachine.stub(:run).and_return(@stdout)
VirtualMachine.stub(:new).and_return(vm)
VirtualMachine.all.should eql([vm, vm, vm])
end
it 'should return an empty list if no virtual machines exist' do
VirtualMachine.stub(:run).and_return('')
VirtualMachine.all.should eql([])
end
it 'should instantiate all virtual machines by their uuid' do
VirtualMachine.stub(:run).and_return(@stdout)
@uuids.each { |uuid| VirtualMachine.should_receive(:new).with(uuid) }
VirtualMachine.all
end
it 'should query a list of all virtual machines via command' do
cmd = %w{prlctl list --all --output uuid}
VirtualMachine.should_receive(:run).with(*cmd).and_return(@stdout)
VirtualMachine.stub(:new)
VirtualMachine.all
end
end
describe '#config' do
it 'should return the global config' do
PrlBackup.should_receive(:config).and_return({:foo => 'bar'})
@vm.config.should eql({:foo => 'bar'})
end
end
%w[start stop].each do |cmd|
describe "##{cmd}" do
it "should #{cmd} the virtual machine" do
@vm.should_receive(:conditionally_run).with('prlctl', cmd, @uuid).and_return('')
@vm.send(cmd)
end
end
end
describe '#backup' do
it 'should create an incremental backup by default' do
@vm.stub(:config).and_return({})
@vm.should_receive(:conditionally_run).with('prlctl', 'backup', @uuid)
@vm.instance_eval { backup }
end
it 'should create a full backup when configured' do
@vm.stub(:config).and_return({:full => true})
@vm.should_receive(:conditionally_run).with('prlctl', 'backup', @uuid, '--full')
@vm.instance_eval { backup }
end
end
describe '#safely_backup' do
it 'should stop the VM during the backup' do
@vm.stub(:stopped?).and_return(false)
@vm.should_receive(:stop).ordered
@vm.should_receive(:backup).ordered
@vm.should_receive(:start).ordered
@vm.safely_backup
end
it 'should not stop the VM when already shutdown' do
@vm.stub(:stopped?).and_return(true)
@vm.should_receive(:backup)
@vm.safely_backup
end
end
describe '#cleanup' do
before do
@old_backup = double('old backup')
@new_backup = double('new backup')
@vm.stub(:full_backups).and_return([@old_backup, @new_backup])
end
it 'should delete 2 backups when there are 2 more backups than to keep' do
@vm.stub(:config).and_return({:keep_only => 0})
@old_backup.should_receive(:delete).once
@new_backup.should_receive(:delete).once
@vm.cleanup
end
it 'should delete the oldest backup when there is 1 more backup than to keep' do
@vm.stub(:config).and_return({:keep_only => 1})
@old_backup.should_receive(:delete).once
@new_backup.should_not_receive(:delete)
@vm.cleanup
end
it 'should not delete any backups when there are as many backups as to keep' do
@vm.stub(:config).and_return({:keep_only => 2})
@old_backup.should_not_receive(:delete)
@new_backup.should_not_receive(:delete)
@vm.cleanup
end
end
describe '#full_backups' do
before do
Backup.stub(:all).and_return([])
end
it 'should query list of backups for given UUID once' do
Backup.should_receive(:all).with(@vm.uuid).once
2.times { @vm.instance_eval { full_backups } }
end
it 'should return only full backups' do
full_backup = double('full backup')
full_backup.stub(:full?).and_return(true)
incremental_backup = double('incremental backup')
incremental_backup.stub(:full?).and_return(false)
Backup.stub(:all).and_return([full_backup, incremental_backup])
@vm.instance_eval { full_backups }.should eql([full_backup])
end
end
describe '#name' do
it 'should return the name of the virtual machine' do
@vm.stub(:info).and_return('Name: foo')
@vm.name.should eql('foo')
end
it 'should return nil if the name cannot be parsed' do
@vm.stub(:info).and_return(nil)
@vm.name.should be_nil
end
end
describe '#uuid' do
before do
@vm = VirtualMachine.new('foo')
end
it "should return the virtual machine's UUID" do
@vm.stub(:info).and_return('ID: {423dba54-45e3-46f1-9aa2-87d61ce6b757}')
@vm.uuid.should eql('{423dba54-45e3-46f1-9aa2-87d61ce6b757}')
end
it 'should return nil if the uuid cannot be parsed' do
@vm.stub(:info).and_return(nil)
@vm.uuid.should be_nil
end
end
describe '#update_info' do
it 'should query infos about the virtual machine' do
@vm.should_receive(:run).with('prlctl', 'list', '--info', 'foo')
@vm.instance_eval { update_info }
end
it 'should update and return the infos' do
@vm.stub(:run).and_return('Foo: Bar', 'Foo: Baz')
@vm.instance_eval { update_info }.should eql('Foo: Bar')
@vm.instance_eval { info }.should eql('Foo: Bar')
@vm.instance_eval { update_info }.should eql('Foo: Baz')
@vm.instance_eval { info }.should eql('Foo: Baz')
end
end
describe '#stopped?' do
it 'should return true when virtual machine is stopped' do
@vm.stub!(:update_info).and_return('State: stopped')
@vm.should be_stopped
end
it 'should return false when virtual machine is not stopped' do
@vm.stub!(:update_info).and_return('State: running')
@vm.should_not be_stopped
end
end
describe '#==' do
before do
@other_vm = VirtualMachine.new('other_vm')
end
it 'should be true when UUIDs are equal' do
@other_vm.stub(:uuid).and_return(@vm.uuid)
@vm.should == @other_vm
@other_vm.should == @vm
end
it 'should be false when UUIDs are not equal' do
@other_vm.stub(:uuid).and_return('{a-completely-different-uuid}')
@vm.should_not == @other_vm
@other_vm.should_not == @vm
end
end
describe '#to_s' do
it 'should return the name' do
@vm.should_receive('name').and_return('name_of_the_vm')
@vm.to_s.should eql('name_of_the_vm')
end
it 'should return "Unknown VM" if name is nil' do
@vm.should_receive(:name).and_return(nil)
@vm.to_s.should eql('Unknown VM')
end
end
end
end