fission 0.2.0 → 0.3.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,13 @@
1
1
  require File.expand_path('../../../spec_helper.rb', __FILE__)
2
2
 
3
3
  describe Fission::Command::Status do
4
- before :each do
4
+ before do
5
5
  @string_io = StringIO.new
6
6
  Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
7
7
  end
8
8
 
9
9
  describe 'execute' do
10
- before :each do
10
+ before do
11
11
  Fission::VM.stub!(:all).and_return(['foo', 'bar', 'baz'])
12
12
  Fission::VM.stub!(:all_running).and_return(['foo', 'baz'])
13
13
  end
@@ -1,11 +1,8 @@
1
1
  require File.expand_path('../../../spec_helper.rb', __FILE__)
2
2
 
3
3
  describe Fission::Command::Stop do
4
- before :all do
4
+ before do
5
5
  @vm_info = ['foo']
6
- end
7
-
8
- before :each do
9
6
  @string_io = StringIO.new
10
7
  Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
11
8
  end
@@ -1,11 +1,8 @@
1
1
  require File.expand_path('../../../spec_helper.rb', __FILE__)
2
2
 
3
3
  describe Fission::Command::Suspend do
4
- before :all do
4
+ before do
5
5
  @vm_info = ['foo']
6
- end
7
-
8
- before :each do
9
6
  @string_io = StringIO.new
10
7
  Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
11
8
  end
@@ -56,7 +53,31 @@ describe Fission::Command::Suspend do
56
53
  command = Fission::Command::Suspend.new @vm_info
57
54
  command.execute
58
55
 
59
- @string_io.string.should match /suspending '#{@vm_info.first}'/
56
+ @string_io.string.should match /Suspending '#{@vm_info.first}'/
57
+ end
58
+
59
+ describe 'with --all' do
60
+ it 'should suspend all running VMs' do
61
+ @vm_mock_1 = mock('vm_mock_1')
62
+ @vm_mock_2 = mock('vm_mock_2')
63
+
64
+ vm_items = {'vm_1' => @vm_mock_1,
65
+ 'vm_2' => @vm_mock_2
66
+ }
67
+
68
+ Fission::VM.should_receive(:all_running).and_return(vm_items.keys)
69
+
70
+ vm_items.each_pair do |name, mock|
71
+ Fission::VM.should_receive(:new).with(name).and_return(mock)
72
+ mock.should_receive(:suspend)
73
+ end
74
+
75
+ command = Fission::Command::Suspend.new ['--all']
76
+ command.execute
77
+
78
+ @string_io.string.should match /Suspending 'vm_1'/
79
+ @string_io.string.should match /Suspending 'vm_2'/
80
+ end
60
81
  end
61
82
 
62
83
  end
@@ -65,7 +86,7 @@ describe Fission::Command::Suspend do
65
86
  it 'should output info for this command' do
66
87
  output = Fission::Command::Suspend.help
67
88
 
68
- output.should match /suspend vm/
89
+ output.should match /suspend \[vm \| --all\]/
69
90
  end
70
91
  end
71
92
  end
@@ -1,7 +1,6 @@
1
1
  require File.expand_path('../../spec_helper.rb', __FILE__)
2
2
 
3
3
  describe Fission::Command do
4
-
5
4
  describe 'new' do
6
5
  it 'should set options variable as an open struct' do
7
6
  @command = Fission::Command.new
@@ -16,6 +16,16 @@ describe Fission::Config do
16
16
  end
17
17
  end
18
18
 
19
+ it 'should use the fusion default for plist_file' do
20
+ @config = Fission::Config.new
21
+ @config.attributes['plist_file'].should == File.expand_path('~/Library/Preferences/com.vmware.fusion.plist')
22
+ end
23
+
24
+ it 'should use the fusion default for the gui bin' do
25
+ @config = Fission::Config.new
26
+ @config.attributes['gui_bin'].should == File.expand_path('/Applications/VMware Fusion.app/Contents/MacOS/vmware')
27
+ end
28
+
19
29
  it "should use the user specified dir in ~/.fissionrc" do
20
30
  FakeFS do
21
31
  File.open('~/.fissionrc', 'w') { |f| f.puts YAML.dump({ 'vm_dir' => '/var/tmp/foo' })}
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../../spec_helper.rb', __FILE__)
2
+
3
+ describe Fission::Fusion do
4
+ describe 'self.is_running?' do
5
+ before do
6
+ @cmd = "ps -ef | grep -v grep | "
7
+ @cmd << "grep -c #{Fission.config.attributes['gui_bin'].gsub(' ', '\ ')} 2>&1"
8
+ end
9
+
10
+ it 'should return true if the fusion app is running' do
11
+ Fission::Fusion.should_receive(:`).with(@cmd).
12
+ and_return("1\n")
13
+ Fission::Fusion.is_running?.should == true
14
+ end
15
+
16
+ it 'should return false if the fusion app is not running' do
17
+ Fission::Fusion.should_receive(:`).with(@cmd).
18
+ and_return("0\n")
19
+ Fission::Fusion.is_running?.should == false
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,100 @@
1
+ require File.expand_path('../../spec_helper.rb', __FILE__)
2
+
3
+ describe Fission::Metadata do
4
+ before do
5
+ @plist_mock = mock('plist_mock')
6
+ @plist_file_path = Fission.config.attributes['plist_file']
7
+ @metadata = Fission::Metadata.new
8
+ end
9
+
10
+ describe 'load' do
11
+ it 'should load the existing data' do
12
+ plist = {'vm_list' => ['1', '2', '3']}
13
+ CFPropertyList::List.should_receive(:new).
14
+ with(:file => @plist_file_path).
15
+ and_return(@plist_mock)
16
+ @plist_mock.stub!(:value).and_return(plist)
17
+ CFPropertyList.should_receive(:native_types).with(plist).
18
+ and_return([1, 2, 3])
19
+ @metadata.load
20
+ @metadata.content.should == [1, 2, 3]
21
+ end
22
+ end
23
+
24
+ describe 'delete_vm_restart_document' do
25
+ before do
26
+ @data = { 'PLRestartDocumentPaths' => ['/vm/foo.vmwarevm', '/vm/bar.vmwarevm']}
27
+ @metadata.content = @data
28
+ end
29
+
30
+ it 'should remove the vm item from the list if the vm path is in the list' do
31
+ @metadata.delete_vm_restart_document(Fission::VM.path('foo'))
32
+ @metadata.content.should == { 'PLRestartDocumentPaths' => ['/vm/bar.vmwarevm']}
33
+ end
34
+
35
+ it 'should not doing anything if the vm is not in the list' do
36
+ @metadata.delete_vm_restart_document(Fission::VM.path('baz'))
37
+ @metadata.content.should == @data
38
+ end
39
+
40
+ it 'should not do anything if the restart document list does not exist' do
41
+ other_data = { 'OtherConfigItem' => ['foo', 'bar']}
42
+ @metadata.content = other_data
43
+ @metadata.delete_vm_restart_document(Fission::VM.path('foo'))
44
+ @metadata.content.should == other_data
45
+ end
46
+ end
47
+
48
+ describe 'delete_vm_favorite_entry' do
49
+ before do
50
+ @data = { 'VMFavoritesListDefaults2' => [{'path' => '/vm/foo.vmwarevm'}] }
51
+ @metadata.content = @data
52
+ end
53
+
54
+ it 'should remove the vm item from the list' do
55
+ @metadata.delete_vm_favorite_entry(Fission::VM.path('foo'))
56
+ @metadata.content.should == { 'VMFavoritesListDefaults2' => [] }
57
+ end
58
+
59
+ it 'should not do anything if the vm is not in the list' do
60
+ @metadata.delete_vm_favorite_entry(Fission::VM.path('bar'))
61
+ @metadata.content.should == @data
62
+ end
63
+ end
64
+
65
+ describe 'self.delete_vm_info' do
66
+ before do
67
+ @md_mock = mock('metadata_mock')
68
+ @md_mock.should_receive(:load)
69
+ @md_mock.should_receive(:save)
70
+ Fission::Metadata.stub!(:new).and_return(@md_mock)
71
+ end
72
+
73
+ it 'should remove the vm from the restart document list' do
74
+ @md_mock.should_receive(:delete_vm_restart_document).with('/vm/foo.vmwarevm')
75
+ @md_mock.stub!(:delete_vm_favorite_entry)
76
+ Fission::Metadata.delete_vm_info(Fission::VM.path('foo'))
77
+ end
78
+
79
+ it 'should remove the vm from the favorite list' do
80
+ @md_mock.should_receive(:delete_vm_favorite_entry).with('/vm/foo.vmwarevm')
81
+ @md_mock.stub!(:delete_vm_restart_document)
82
+ Fission::Metadata.delete_vm_info(Fission::VM.path('foo'))
83
+ end
84
+ end
85
+
86
+ describe 'save' do
87
+ it 'should save the data' do
88
+ CFPropertyList::List.should_receive(:new).and_return(@plist_mock)
89
+ CFPropertyList.should_receive(:guess).with([1, 2, 3]).
90
+ and_return(['1', '2', '3'])
91
+
92
+ @plist_mock.should_receive(:value=).with(['1', '2', '3'])
93
+ @plist_mock.should_receive(:save).
94
+ with(@plist_file_path, CFPropertyList::List::FORMAT_BINARY)
95
+
96
+ @metadata.content = [1, 2, 3]
97
+ @metadata.save
98
+ end
99
+ end
100
+ end
@@ -1,13 +1,14 @@
1
1
  require File.expand_path('../../spec_helper.rb', __FILE__)
2
2
 
3
3
  describe Fission::UI do
4
+ before do
5
+ @string_io = StringIO.new
6
+ end
7
+
4
8
  describe 'output' do
5
9
  it 'should show the desired text' do
6
- output = capturing_output do
7
- Fission::UI.new.output "foo bar\nbaz blah"
8
- end
9
-
10
- output.should == "foo bar\nbaz blah\n"
10
+ Fission::UI.new(@string_io).output "foo bar\nbaz blah"
11
+ @string_io.string.should == "foo bar\nbaz blah\n"
11
12
  end
12
13
  end
13
14
 
@@ -22,13 +23,8 @@ describe Fission::UI do
22
23
  describe 'output_and_exit' do
23
24
  it 'should show the desired text and exit with the desired exit code' do
24
25
  Fission::UI.any_instance.should_receive(:exit).and_return(1)
25
-
26
- output = capturing_output do
27
- Fission::UI.new.output_and_exit "foo bar\nbaz blah", 1
28
- end
29
-
30
- output.should == "foo bar\nbaz blah\n"
26
+ Fission::UI.new(@string_io).output_and_exit "foo bar\nbaz blah", 1
27
+ @string_io.string.should == "foo bar\nbaz blah\n"
31
28
  end
32
-
33
29
  end
34
30
  end
@@ -1,8 +1,12 @@
1
1
  require File.expand_path('../../spec_helper.rb', __FILE__)
2
2
 
3
3
  describe Fission::VM do
4
- before :each do
4
+ before do
5
5
  @string_io = StringIO.new
6
+ Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
7
+ @vm = Fission::VM.new('foo')
8
+ @vm.stub!(:conf_file).and_return(File.join(Fission::VM.path('foo'), 'foo.vmx'))
9
+ @vmrun_cmd = Fission.config.attributes['vmrun_cmd']
6
10
  end
7
11
 
8
12
  describe 'new' do
@@ -14,36 +18,41 @@ describe Fission::VM do
14
18
  describe 'start' do
15
19
  it 'should output that it was successful' do
16
20
  $?.should_receive(:exitstatus).and_return(0)
17
- Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
18
- @vm = Fission::VM.new('foo')
19
21
  @vm.should_receive(:`).
20
- with("#{Fission.config.attributes['vmrun_cmd']} start #{@vm.conf_file.gsub(' ', '\ ')} gui 2>&1").
22
+ with("#{@vmrun_cmd} start #{@vm.conf_file.gsub(' ', '\ ')} gui 2>&1").
21
23
  and_return("it's all good")
22
24
  @vm.start
23
25
 
24
26
  @string_io.string.should match /VM started/
25
27
  end
26
28
 
27
- it 'it should output that it was unsuccessful' do
29
+ it 'should start the vm headless' do
30
+ $?.should_receive(:exitstatus).and_return(0)
31
+ @vm.should_receive(:`).
32
+ with("#{@vmrun_cmd} start #{@vm.conf_file.gsub(' ', '\ ')} nogui 2>&1").
33
+ and_return("it's all good")
34
+ @vm.start(:headless => true)
35
+
36
+ @string_io.string.should match /VM started/
37
+ end
38
+
39
+ it 'should output that it was unsuccessful' do
28
40
  $?.should_receive(:exitstatus).and_return(1)
29
- Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
30
- @vm = Fission::VM.new('foo')
31
41
  @vm.should_receive(:`).
32
- with("#{Fission.config.attributes['vmrun_cmd']} start #{@vm.conf_file.gsub(' ', '\ ')} gui 2>&1").
42
+ with("#{@vmrun_cmd} start #{@vm.conf_file.gsub(' ', '\ ')} gui 2>&1").
33
43
  and_return("it blew up")
34
44
  @vm.start
35
45
 
36
46
  @string_io.string.should match /There was a problem starting the VM.+it blew up.+/m
37
47
  end
48
+
38
49
  end
39
50
 
40
51
  describe 'stop' do
41
52
  it 'should output that it was successful' do
42
53
  $?.should_receive(:exitstatus).and_return(0)
43
- Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
44
- @vm = Fission::VM.new('foo')
45
54
  @vm.should_receive(:`).
46
- with("#{Fission.config.attributes['vmrun_cmd']} stop #{@vm.conf_file.gsub ' ', '\ '} 2>&1").
55
+ with("#{@vmrun_cmd} stop #{@vm.conf_file.gsub ' ', '\ '} 2>&1").
47
56
  and_return("it's all good")
48
57
  @vm.stop
49
58
 
@@ -52,10 +61,8 @@ describe Fission::VM do
52
61
 
53
62
  it 'it should output that it was unsuccessful' do
54
63
  $?.should_receive(:exitstatus).and_return(1)
55
- Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
56
- @vm = Fission::VM.new('foo')
57
64
  @vm.should_receive(:`).
58
- with("#{Fission.config.attributes['vmrun_cmd']} stop #{@vm.conf_file.gsub ' ', '\ '} 2>&1").
65
+ with("#{@vmrun_cmd} stop #{@vm.conf_file.gsub ' ', '\ '} 2>&1").
59
66
  and_return("it blew up")
60
67
  @vm.stop
61
68
 
@@ -66,10 +73,8 @@ describe Fission::VM do
66
73
  describe 'suspend' do
67
74
  it 'should output that it was successful' do
68
75
  $?.should_receive(:exitstatus).and_return(0)
69
- Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
70
- @vm = Fission::VM.new('foo')
71
76
  @vm.should_receive(:`).
72
- with("#{Fission.config.attributes['vmrun_cmd']} suspend #{@vm.conf_file.gsub ' ', '\ '} 2>&1").
77
+ with("#{@vmrun_cmd} suspend #{@vm.conf_file.gsub ' ', '\ '} 2>&1").
73
78
  and_return("it's all good")
74
79
  @vm.suspend
75
80
 
@@ -78,10 +83,8 @@ describe Fission::VM do
78
83
 
79
84
  it 'it should output that it was unsuccessful' do
80
85
  $?.should_receive(:exitstatus).and_return(1)
81
- Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
82
- @vm = Fission::VM.new('foo')
83
86
  @vm.should_receive(:`).
84
- with("#{Fission.config.attributes['vmrun_cmd']} suspend #{@vm.conf_file.gsub ' ', '\ '} 2>&1").
87
+ with("#{@vmrun_cmd} suspend #{@vm.conf_file.gsub ' ', '\ '} 2>&1").
85
88
  and_return("it blew up")
86
89
  @vm.suspend
87
90
 
@@ -89,55 +92,203 @@ describe Fission::VM do
89
92
  end
90
93
  end
91
94
 
95
+ describe 'snapshots' do
96
+ it 'should return the list of snapshots' do
97
+ $?.should_receive(:exitstatus).and_return(0)
98
+ @vm.should_receive(:`).
99
+ with("#{@vmrun_cmd} listSnapshots #{@vm.conf_file.gsub ' ', '\ '} 2>&1").
100
+ and_return("Total snapshots: 3\nsnap foo\nsnap bar\nsnap baz\n")
101
+ @vm.snapshots.should == ['snap foo', 'snap bar', 'snap baz']
102
+ end
103
+
104
+ it 'should print an error and exit if there was a problem getting the list of snapshots' do
105
+ $?.should_receive(:exitstatus).and_return(1)
106
+ @vm.should_receive(:`).
107
+ with("#{@vmrun_cmd} listSnapshots #{@vm.conf_file.gsub ' ', '\ '} 2>&1").
108
+ and_return("it blew up\n")
109
+ lambda { @vm.snapshots }.should raise_error SystemExit
110
+ @string_io.string.should match /error getting the list of snapshots/
111
+ @string_io.string.should match /error was.+it blew up/m
112
+ end
113
+ end
114
+
115
+ describe 'create_snapshot' do
116
+ it 'should create a snapshot' do
117
+ $?.should_receive(:exitstatus).and_return(0)
118
+ @vm.should_receive(:`).
119
+ with("#{@vmrun_cmd} snapshot #{@vm.conf_file.gsub ' ', '\ '} \"bar\" 2>&1").
120
+ and_return("")
121
+ @vm.create_snapshot('bar')
122
+ @string_io.string.should match /Snapshot 'bar' created/
123
+ end
124
+
125
+ it 'should print an error and exit if there was a problem creating the snapshot' do
126
+ $?.should_receive(:exitstatus).and_return(1)
127
+ @vm.should_receive(:`).
128
+ with("#{@vmrun_cmd} snapshot #{@vm.conf_file.gsub ' ', '\ '} \"bar\" 2>&1").
129
+ and_return("it blew up")
130
+ lambda { @vm.create_snapshot('bar') }.should raise_error SystemExit
131
+ @string_io.string.should match /error creating the snapshot/
132
+ @string_io.string.should match /error was.+it blew up/m
133
+ end
134
+ end
135
+
136
+ describe 'revert_to_snapshot' do
137
+ it 'should revert to the provided snapshot' do
138
+ $?.should_receive(:exitstatus).and_return(0)
139
+ @vm.should_receive(:`).
140
+ with("#{@vmrun_cmd} revertToSnapshot #{@vm.conf_file.gsub ' ', '\ '} \"bar\" 2>&1").
141
+ and_return("")
142
+ @vm.revert_to_snapshot('bar')
143
+ @string_io.string.should match /Reverted to snapshot 'bar'/
144
+ end
145
+
146
+ it "should print an error and exit if the snapshot doesn't exist" do
147
+ $?.should_receive(:exitstatus).and_return(1)
148
+ @vm.should_receive(:`).
149
+ with("#{@vmrun_cmd} revertToSnapshot #{@vm.conf_file.gsub ' ', '\ '} \"bar\" 2>&1").
150
+ and_return("it blew up")
151
+ lambda { @vm.revert_to_snapshot('bar') }.should raise_error SystemExit
152
+ @string_io.string.should match /error reverting to the snapshot/
153
+ @string_io.string.should match /error was.+it blew up/m
154
+ end
155
+ end
156
+
92
157
  describe 'conf_file' do
158
+ before do
159
+ FakeFS.activate!
160
+ @vm_root_dir = Fission::VM.path('foo')
161
+ FileUtils.mkdir_p(@vm_root_dir)
162
+ end
163
+
164
+ after do
165
+ FakeFS.deactivate!
166
+ FakeFS::FileSystem.clear
167
+ end
168
+
93
169
  it 'should return the path to the conf file' do
94
- Fission::VM.new('foo').conf_file.should == File.join(Fission.config.attributes['vm_dir'], 'foo.vmwarevm', 'foo.vmx')
170
+ file_path = File.join(@vm_root_dir, 'foo.vmx')
171
+ FileUtils.touch(file_path)
172
+ Fission::VM.new('foo').conf_file.should == file_path
173
+ end
174
+
175
+ it 'should output an error and exit if no vmx file was found' do
176
+ lambda {
177
+ FileUtils.mkdir_p(@vm_root_dir)
178
+ Fission::VM.new('foo').conf_file
179
+ }.should raise_error SystemExit
180
+
181
+ @string_io.string.should match /Unable to find a config file for VM 'foo' \(in '#{File.join(@vm_root_dir, '\*\.vmx')}'\)/m
182
+ end
183
+
184
+ describe 'when the VM name and conf file name do not match' do
185
+ it 'should return the path to the conf file' do
186
+ file_path = File.join(@vm_root_dir, 'bar.vmx')
187
+ FileUtils.touch(file_path)
188
+ Fission::VM.new('foo').conf_file.should == file_path
189
+ end
95
190
  end
191
+
192
+ describe 'if multiple vmx files are found' do
193
+ before do
194
+ FileUtils.mkdir_p(@vm_root_dir)
195
+ end
196
+
197
+ it 'should use the conf file which matches the VM name if it exists' do
198
+ ['foo.vmx', 'bar.vmx'].each do |file|
199
+ FileUtils.touch(File.join(@vm_root_dir, file))
200
+ end
201
+ Fission::VM.new('foo').conf_file.should == File.join(@vm_root_dir, 'foo.vmx')
202
+ end
203
+
204
+ it 'should output an error and exit' do
205
+ lambda {
206
+ ['bar.vmx', 'baz.vmx'].each do |file|
207
+ FileUtils.touch(File.join(@vm_root_dir, file))
208
+ end
209
+ Fission::VM.new('foo').conf_file
210
+ }.should raise_error SystemExit
211
+
212
+ @string_io.string.should match /Multiple config files found for VM 'foo' \('bar\.vmx', 'baz\.vmx' in '#{@vm_root_dir}'/m
213
+ end
214
+ end
215
+
96
216
  end
97
217
 
98
218
  describe "self.all" do
99
219
  it "should return the list of VMs" do
100
220
  vm_root = Fission.config.attributes['vm_dir']
101
- Dir.should_receive(:[]).and_return(["#{File.join vm_root, 'foo.vmwarevm' }", "#{File.join vm_root, 'bar.vmwarevm' }"])
221
+ Dir.should_receive(:[]).
222
+ and_return(["#{File.join vm_root, 'foo.vmwarevm' }", "#{File.join vm_root, 'bar.vmwarevm' }"])
102
223
 
103
224
  vm_root = Fission.config.attributes['vm_dir']
104
- File.should_receive(:directory?).with("#{File.join vm_root, 'foo.vmwarevm'}").and_return(true)
105
- File.should_receive(:directory?).with("#{File.join vm_root, 'bar.vmwarevm'}").and_return(true)
225
+ File.should_receive(:directory?).with("#{File.join vm_root, 'foo.vmwarevm'}").
226
+ and_return(true)
227
+ File.should_receive(:directory?).with("#{File.join vm_root, 'bar.vmwarevm'}").
228
+ and_return(true)
106
229
  Fission::VM.all.should == ['foo', 'bar']
107
230
  end
108
231
 
109
232
  it "should not return an item in the list if it isn't a directory" do
110
233
  vm_root = Fission.config.attributes['vm_dir']
111
- Dir.should_receive(:[]).and_return((['foo', 'bar', 'baz'].map { |i| File.join vm_root, "#{i}.vmwarevm"}))
112
- File.should_receive(:directory?).with("#{File.join vm_root, 'foo.vmwarevm'}").and_return(true)
113
- File.should_receive(:directory?).with("#{File.join vm_root, 'bar.vmwarevm'}").and_return(true)
114
- File.should_receive(:directory?).with("#{File.join vm_root, 'baz.vmwarevm'}").and_return(false)
234
+ Dir.should_receive(:[]).
235
+ and_return((['foo', 'bar', 'baz'].map { |i| File.join vm_root, "#{i}.vmwarevm"}))
236
+ File.should_receive(:directory?).
237
+ with("#{File.join vm_root, 'foo.vmwarevm'}").and_return(true)
238
+ File.should_receive(:directory?).
239
+ with("#{File.join vm_root, 'bar.vmwarevm'}").and_return(true)
240
+ File.should_receive(:directory?).
241
+ with("#{File.join vm_root, 'baz.vmwarevm'}").and_return(false)
115
242
  Fission::VM.all.should == ['foo', 'bar']
116
243
  end
117
244
 
118
245
  it "should only query for items with an extension of .vmwarevm" do
119
246
  dir_arg = File.join Fission.config.attributes['vm_dir'], '*.vmwarevm'
120
- Dir.should_receive(:[]).with(dir_arg).and_return(['foo.vmwarevm', 'bar.vmwarevm'])
247
+ Dir.should_receive(:[]).with(dir_arg).
248
+ and_return(['foo.vmwarevm', 'bar.vmwarevm'])
121
249
  Fission::VM.all
122
250
  end
123
251
  end
124
252
 
125
253
  describe 'self.all_running' do
126
254
  it 'should list the running vms' do
127
- list_output = "Total running VMs: 2\n/vm/foo.vmwarevm/foo.vmx\n/vm/bar.vmwarevm/bar.vmx\n/vm/baz.vmwarevm/baz.vmx\n"
255
+ list_output = "Total running VMs: 2\n/vm/foo.vmwarevm/foo.vmx\n"
256
+ list_output << "/vm/bar.vmwarevm/bar.vmx\n/vm/baz.vmwarevm/baz.vmx\n"
128
257
 
129
258
  $?.should_receive(:exitstatus).and_return(0)
130
- Fission::VM.should_receive(:`).with("#{Fission.config.attributes['vmrun_cmd']} list").and_return(list_output)
259
+ Fission::VM.should_receive(:`).
260
+ with("#{@vmrun_cmd} list").
261
+ and_return(list_output)
131
262
  [ 'foo', 'bar', 'baz'].each do |vm|
132
- File.should_receive(:exists?).with("/vm/#{vm}.vmwarevm/#{vm}.vmx").and_return(true)
263
+ File.should_receive(:exists?).with("/vm/#{vm}.vmwarevm/#{vm}.vmx").
264
+ and_return(true)
133
265
  end
134
266
 
135
267
  Fission::VM.all_running.should == ['foo', 'bar', 'baz']
136
268
  end
137
269
 
270
+ it 'should output the VM dir name if it differs from the .vmx file name' do
271
+ vm_dir_file = { 'foo' => 'foo', 'bar' => 'diff', 'baz' => 'baz'}
272
+ list_output = "Total running VMs: 3\n"
273
+ vm_dir_file.each_pair do |dir, file|
274
+ list_output << "/vm/#{dir}.vmwarevm/#{file}.vmx\n"
275
+ File.should_receive(:exists?).with("/vm/#{dir}.vmwarevm/#{file}.vmx").
276
+ and_return(true)
277
+ end
278
+
279
+ $?.should_receive(:exitstatus).and_return(0)
280
+ Fission::VM.should_receive(:`).
281
+ with("#{@vmrun_cmd} list").
282
+ and_return(list_output)
283
+
284
+ Fission::VM.all_running.should == ['foo', 'bar', 'baz']
285
+ end
286
+
138
287
  it 'should output an error and exit if unable to get the list of running vms' do
139
288
  $?.should_receive(:exitstatus).and_return(1)
140
- Fission::VM.should_receive(:`).with("#{Fission.config.attributes['vmrun_cmd']} list").and_return("it blew up")
289
+ Fission::VM.should_receive(:`).
290
+ with("#{@vmrun_cmd} list").
291
+ and_return("it blew up")
141
292
  Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
142
293
 
143
294
  lambda {
@@ -172,8 +323,7 @@ describe Fission::VM do
172
323
  end
173
324
 
174
325
  describe "self.clone" do
175
- before :each do
176
- Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
326
+ before do
177
327
  @source_vm = 'foo'
178
328
  @target_vm = 'bar'
179
329
  @vm_files = ['.vmx', '.vmxf', '.vmdk', '-s001.vmdk', '-s002.vmdk', '.vmsd']
@@ -191,16 +341,35 @@ describe Fission::VM do
191
341
  end
192
342
 
193
343
  ['.vmx', '.vmxf'].each do |ext|
194
- File.should_receive(:binary?).with(File.join(Fission::VM.path('bar'), "bar#{ext}")).and_return(false)
344
+ File.should_receive(:binary?).
345
+ with(File.join(Fission::VM.path('bar'), "bar#{ext}")).
346
+ and_return(false)
195
347
  end
196
348
  end
197
349
 
198
- after :each do
350
+ after do
199
351
  FakeFS.deactivate!
352
+ FakeFS::FileSystem.clear
200
353
  end
201
354
 
202
355
  it 'should copy the vm files to the target' do
203
- File.should_receive(:binary?).with(File.join(Fission::VM.path('bar'), "bar.vmdk")).and_return(true)
356
+ File.should_receive(:binary?).
357
+ with(File.join(Fission::VM.path('bar'), "bar.vmdk")).
358
+ and_return(true)
359
+ Fission::VM.clone @source_vm, @target_vm
360
+
361
+ File.directory?(Fission::VM.path('bar')).should == true
362
+
363
+ @vm_files.each do |file|
364
+ File.file?(File.join(Fission::VM.path('bar'), "#{@target_vm}#{file}")).should == true
365
+ end
366
+ end
367
+
368
+ it "should copy the vm files to the target if a file name doesn't match the directory" do
369
+ FileUtils.touch File.join(Fission::VM.path('foo'), 'other_name.nvram')
370
+ File.should_receive(:binary?).
371
+ with(File.join(Fission::VM.path('bar'), "bar.vmdk")).
372
+ and_return(true)
204
373
  Fission::VM.clone @source_vm, @target_vm
205
374
 
206
375
  File.directory?(Fission::VM.path('bar')).should == true
@@ -208,10 +377,31 @@ describe Fission::VM do
208
377
  @vm_files.each do |file|
209
378
  File.file?(File.join(Fission::VM.path('bar'), "#{@target_vm}#{file}")).should == true
210
379
  end
380
+
381
+ File.file?(File.join(Fission::VM.path('bar'), "bar.nvram")).should == true
382
+ end
383
+
384
+
385
+ it "should copy the vm files to the target if a sparse disk file name doesn't match the directory" do
386
+ FileUtils.touch File.join(Fission::VM.path('foo'), 'other_name-s003.vmdk')
387
+ File.should_receive(:binary?).
388
+ with(File.join(Fission::VM.path('bar'), "bar.vmdk")).
389
+ and_return(true)
390
+ Fission::VM.clone @source_vm, @target_vm
391
+
392
+ File.directory?(Fission::VM.path('bar')).should == true
393
+
394
+ @vm_files.each do |file|
395
+ File.file?(File.join(Fission::VM.path('bar'), "#{@target_vm}#{file}")).should == true
396
+ end
397
+
398
+ File.file?(File.join(Fission::VM.path('bar'), "bar-s003.vmdk")).should == true
211
399
  end
212
400
 
213
401
  it 'should update the target vm config files' do
214
- File.should_receive(:binary?).with(File.join(Fission::VM.path('bar'), "bar.vmdk")).and_return(true)
402
+ File.should_receive(:binary?).
403
+ with(File.join(Fission::VM.path('bar'), "bar.vmdk")).
404
+ and_return(true)
215
405
  Fission::VM.clone @source_vm, @target_vm
216
406
 
217
407
  ['.vmx', '.vmxf'].each do |ext|
@@ -221,27 +411,61 @@ describe Fission::VM do
221
411
  end
222
412
 
223
413
  it "should not try to update the vmdk file if it's not a sparse disk" do
224
- File.should_receive(:binary?).with(File.join(Fission::VM.path('bar'), "bar.vmdk")).and_return(true)
414
+ File.should_receive(:binary?).
415
+ with(File.join(Fission::VM.path('bar'), "bar.vmdk")).and_return(true)
225
416
  Fission::VM.clone @source_vm, @target_vm
226
417
 
227
418
  File.read(File.join(Fission::VM.path('bar'), 'bar.vmdk')).should match /foo/
228
419
  end
229
420
 
230
421
  it "should update the vmdk when a sparse disk is found" do
231
- File.should_receive(:binary?).with(File.join(Fission::VM.path('bar'), "bar.vmdk")).and_return(false)
422
+ File.should_receive(:binary?).
423
+ with(File.join(Fission::VM.path('bar'), "bar.vmdk")).and_return(false)
232
424
  Fission::VM.clone @source_vm, @target_vm
233
425
 
234
426
  File.read(File.join(Fission::VM.path('bar'), 'bar.vmdk')).should match /bar/
235
427
  end
236
428
 
237
429
  it 'should output that the clone was successful' do
238
- File.should_receive(:binary?).with(File.join(Fission::VM.path('bar'), "bar.vmdk")).and_return(true)
430
+ File.should_receive(:binary?).
431
+ with(File.join(Fission::VM.path('bar'), "bar.vmdk")).and_return(true)
239
432
  Fission::VM.clone @source_vm, @target_vm
240
433
 
241
434
  @string_io.string.should match /Cloning #{@source_vm} to #{@target_vm}/
242
- @string_io.string.should match /Configuring #{@target_vm}/
435
+ @string_io.string.should match /Configuring #{@target_vm}/
243
436
  end
244
-
245
437
  end
246
438
 
439
+ describe "self.delete" do
440
+ before do
441
+ @target_vm = 'foo'
442
+ @vm_files = %w{ .vmx .vmxf .vmdk -s001.vmdk -s002.vmdk .vmsd }
443
+ FakeFS.activate!
444
+
445
+ FileUtils.mkdir_p Fission::VM.path(@target_vm)
446
+
447
+ @vm_files.each do |file|
448
+ FileUtils.touch File.join(Fission::VM.path(@target_vm), "#{@target_vm}#{file}")
449
+ end
450
+ end
451
+
452
+ after do
453
+ FakeFS.deactivate!
454
+ end
455
+
456
+ it "should delete the target vm files" do
457
+ Fission::Metadata.stub!(:delete_vm_info)
458
+ Fission::VM.delete @target_vm
459
+ @vm_files.each do |file|
460
+ File.exists?(File.join(Fission::VM.path(@target_vm), "#{@target_vm}#{file}")).should == false
461
+ end
462
+ @string_io.string.should match /Deleting vm #{@target_vm}/
463
+ end
464
+
465
+ it 'should delete the target vm metadata' do
466
+ Fission::Metadata.should_receive(:delete_vm_info)
467
+ Fission::VM.delete @target_vm
468
+ end
469
+
470
+ end
247
471
  end