fission 0.4.0 → 0.5.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.ruby-gemset +1 -0
  2. data/.ruby-version +1 -0
  3. data/.travis.yml +7 -0
  4. data/CHANGELOG.md +12 -3
  5. data/README.md +45 -19
  6. data/bin/fission +1 -1
  7. data/fission.gemspec +3 -2
  8. data/lib/fission.rb +15 -0
  9. data/lib/fission/action/shell_executor.rb +37 -0
  10. data/lib/fission/action/snapshot/creator.rb +81 -0
  11. data/lib/fission/action/snapshot/deleter.rb +85 -0
  12. data/lib/fission/action/snapshot/lister.rb +90 -0
  13. data/lib/fission/action/snapshot/reverter.rb +81 -0
  14. data/lib/fission/action/vm/cloner.rb +191 -0
  15. data/lib/fission/action/vm/deleter.rb +73 -0
  16. data/lib/fission/action/vm/lister.rb +138 -0
  17. data/lib/fission/action/vm/starter.rb +88 -0
  18. data/lib/fission/action/vm/stopper.rb +79 -0
  19. data/lib/fission/action/vm/suspender.rb +68 -0
  20. data/lib/fission/cli.rb +21 -117
  21. data/lib/fission/command.rb +39 -0
  22. data/lib/fission/command/clone.rb +11 -6
  23. data/lib/fission/command/delete.rb +11 -6
  24. data/lib/fission/command/info.rb +62 -0
  25. data/lib/fission/command/snapshot_create.rb +9 -3
  26. data/lib/fission/command/snapshot_delete.rb +38 -0
  27. data/lib/fission/command/snapshot_list.rb +9 -3
  28. data/lib/fission/command/snapshot_revert.rb +9 -3
  29. data/lib/fission/command/start.rb +11 -6
  30. data/lib/fission/command/status.rb +9 -1
  31. data/lib/fission/command/stop.rb +9 -3
  32. data/lib/fission/command/suspend.rb +11 -6
  33. data/lib/fission/command_helpers.rb +18 -4
  34. data/lib/fission/command_line_parser.rb +189 -0
  35. data/lib/fission/config.rb +1 -1
  36. data/lib/fission/fusion.rb +3 -4
  37. data/lib/fission/lease.rb +2 -1
  38. data/lib/fission/metadata.rb +6 -1
  39. data/lib/fission/response.rb +17 -9
  40. data/lib/fission/version.rb +1 -1
  41. data/lib/fission/vm.rb +142 -382
  42. data/lib/fission/vm_configuration.rb +79 -0
  43. data/spec/fission/action/execute_shell_command_spec.rb +25 -0
  44. data/spec/fission/action/snapshot/creator_spec.rb +77 -0
  45. data/spec/fission/action/snapshot/deleter_spec.rb +84 -0
  46. data/spec/fission/action/snapshot/lister_spec.rb +67 -0
  47. data/spec/fission/action/snapshot/reverter_spec.rb +76 -0
  48. data/spec/fission/action/vm/cloner_spec.rb +198 -0
  49. data/spec/fission/action/vm/deleter_spec.rb +79 -0
  50. data/spec/fission/action/vm/lister_spec.rb +164 -0
  51. data/spec/fission/action/vm/starter_spec.rb +88 -0
  52. data/spec/fission/action/vm/stopper_spec.rb +71 -0
  53. data/spec/fission/action/vm/suspender_spec.rb +59 -0
  54. data/spec/fission/cli_spec.rb +32 -157
  55. data/spec/fission/command/clone_spec.rb +9 -3
  56. data/spec/fission/command/delete_spec.rb +11 -3
  57. data/spec/fission/command/info_spec.rb +130 -0
  58. data/spec/fission/command/snapshot_create_spec.rb +11 -3
  59. data/spec/fission/command/snapshot_delete_spec.rb +74 -0
  60. data/spec/fission/command/snapshot_list_spec.rb +11 -3
  61. data/spec/fission/command/snapshot_revert_spec.rb +11 -3
  62. data/spec/fission/command/start_spec.rb +11 -3
  63. data/spec/fission/command/status_spec.rb +16 -5
  64. data/spec/fission/command/stop_spec.rb +11 -3
  65. data/spec/fission/command/suspend_spec.rb +11 -3
  66. data/spec/fission/command_helpers_spec.rb +27 -5
  67. data/spec/fission/command_line_parser_spec.rb +267 -0
  68. data/spec/fission/command_spec.rb +16 -1
  69. data/spec/fission/config_spec.rb +3 -3
  70. data/spec/fission/fusion_spec.rb +11 -6
  71. data/spec/fission/lease_spec.rb +81 -45
  72. data/spec/fission/metadata_spec.rb +29 -6
  73. data/spec/fission/response_spec.rb +20 -9
  74. data/spec/fission/ui_spec.rb +1 -1
  75. data/spec/fission/vm_configuration_spec.rb +132 -0
  76. data/spec/fission/vm_spec.rb +393 -750
  77. data/spec/helpers/command_helpers.rb +1 -1
  78. metadata +93 -15
  79. data/.rvmrc +0 -1
@@ -1,9 +1,8 @@
1
- require File.expand_path('../../spec_helper.rb', __FILE__)
1
+ require 'spec_helper'
2
2
 
3
3
  describe Fission::VM do
4
4
  before do
5
5
  @vm = Fission::VM.new('foo')
6
- @vm.stub!(:conf_file).and_return(File.join(@vm.path, 'foo.vmx'))
7
6
  @conf_file_path = File.join(@vm.path, 'foo.vmx')
8
7
  @vmrun_cmd = Fission.config['vmrun_cmd']
9
8
  @conf_file_response_mock = mock('conf_file_response')
@@ -15,366 +14,193 @@ describe Fission::VM do
15
14
  end
16
15
  end
17
16
 
18
- describe 'start' do
17
+ describe 'delete' do
19
18
  before do
20
- @running_response_mock = mock('running?')
21
-
22
- @vm.stub(:exists?).and_return(true)
23
- @vm.stub(:running?).and_return(@running_response_mock)
24
- @vm.stub(:conf_file).and_return(@conf_file_response_mock)
25
- @running_response_mock.stub_as_successful false
26
- @conf_file_response_mock.stub_as_successful @conf_file_path
19
+ @vm_deleter = mock 'vm deleter'
20
+ @delete_response_mock = mock 'vm delete response'
21
+ Fission::Action::VM::Deleter.stub(:new).and_return(@vm_deleter)
22
+ @vm_deleter.should_receive(:delete).
23
+ and_return(@delete_response_mock)
27
24
  end
28
25
 
29
- it "should return an unsuccessful response if the vm doesn't exist" do
30
- @vm.stub(:exists?).and_return(false)
31
- @vm.start.should be_an_unsuccessful_response 'VM does not exist'
26
+ it 'should return an unsuccessful response when unable to delete the vm' do
27
+ @delete_response_mock.stub_as_unsuccessful
28
+ @vm.delete.should be_an_unsuccessful_response
32
29
  end
33
30
 
34
- it 'should return an unsuccessful response if the vm is already running' do
35
- @running_response_mock.stub_as_successful true
36
- @vm.start.should be_an_unsuccessful_response 'VM is already running'
31
+ it 'should return a successful response when deleting' do
32
+ @delete_response_mock.stub_as_successful
33
+ @vm.delete.should be_a_successful_response
37
34
  end
35
+ end
38
36
 
39
- it 'should return an unsuccessful response if unable to determine if running' do
40
- @running_response_mock.stub_as_unsuccessful
41
- @vm.start.should be_an_unsuccessful_response
37
+ describe 'start' do
38
+ before do
39
+ @vm_starter = mock 'vm starter'
40
+ @start_response_mock = mock 'vm start response'
41
+ Fission::Action::VM::Starter.stub(:new).and_return(@vm_starter)
42
42
  end
43
43
 
44
- it 'should return an unsuccessful response if unable to figure out the conf file' do
45
- @conf_file_response_mock.stub_as_unsuccessful
44
+ it 'should return an unsuccessful response when unable to start the vm' do
45
+ @vm_starter.should_receive(:start).
46
+ and_return(@start_response_mock)
47
+ @start_response_mock.stub_as_unsuccessful
46
48
  @vm.start.should be_an_unsuccessful_response
47
49
  end
48
50
 
49
- describe 'when the fusion gui is not running' do
50
- before do
51
- Fission::Fusion.stub(:running?).and_return(false)
52
- end
53
-
54
- it 'should start the VM and return a successful response' do
55
- $?.should_receive(:exitstatus).and_return(0)
56
- @vm.should_receive(:`).
57
- with("#{@vmrun_cmd} start #{@conf_file_path.gsub(' ', '\ ')} gui 2>&1").
58
- and_return("it's all good")
59
-
60
- @vm.start.should be_a_successful_response
61
- end
62
-
63
- it 'should successfully start the vm headless' do
64
- $?.should_receive(:exitstatus).and_return(0)
65
- @vm.should_receive(:`).
66
- with("#{@vmrun_cmd} start #{@conf_file_path.gsub(' ', '\ ')} nogui 2>&1").
67
- and_return("it's all good")
68
-
69
- @vm.start(:headless => true).should be_a_successful_response
70
- end
71
-
72
- it 'should return an unsuccessful response if there was an error starting the VM' do
73
- $?.should_receive(:exitstatus).and_return(1)
74
- @vm.should_receive(:`).
75
- with("#{@vmrun_cmd} start #{@conf_file_path.gsub(' ', '\ ')} gui 2>&1").
76
- and_return("it blew up")
77
-
78
- @vm.start.should be_an_unsuccessful_response
79
- end
51
+ it 'should return a successful response when starting headless' do
52
+ @vm_starter.should_receive(:start).
53
+ with(:headless => true).
54
+ and_return(@start_response_mock)
55
+ @start_response_mock.stub_as_successful
56
+ @vm.start(:headless => true).should be_a_successful_response
80
57
  end
81
58
 
82
- describe 'when the fusion gui is running' do
83
- before do
84
- Fission::Fusion.stub(:running?).and_return(true)
85
- end
86
-
87
- it 'should return an unsuccessful response if starting headless' do
88
- response = @vm.start :headless => true
89
-
90
- error_string = 'It looks like the Fusion GUI is currently running. '
91
- error_string << 'A VM cannot be started in headless mode when the Fusion GUI is running. '
92
- error_string << 'Exit the Fusion GUI and try again.'
93
-
94
- response.should be_an_unsuccessful_response error_string
95
- end
59
+ it 'should return a successful response when starting' do
60
+ @vm_starter.should_receive(:start).
61
+ and_return(@start_response_mock)
62
+ @start_response_mock.stub_as_successful
63
+ @vm.start.should be_a_successful_response
96
64
  end
97
-
98
65
  end
99
66
 
100
67
  describe 'stop' do
101
68
  before do
102
- @running_response_mock = mock('running?')
103
-
104
- @vm.stub(:exists?).and_return(true)
105
- @vm.stub(:running?).and_return(@running_response_mock)
106
- @vm.stub(:conf_file).and_return(@conf_file_response_mock)
107
- @running_response_mock.stub_as_successful true
108
- @conf_file_response_mock.stub_as_successful @conf_file_path
109
- end
110
-
111
- it "should return an unsuccessful response if the vm doesn't exist" do
112
- @vm.stub(:exists?).and_return(false)
113
- @vm.stop.should be_an_unsuccessful_response 'VM does not exist'
114
- end
115
-
116
- it 'should return an unsuccessful response if the vm is not running' do
117
- @running_response_mock.stub_as_successful false
118
- @vm.stop.should be_an_unsuccessful_response 'VM is not running'
69
+ @vm_stopper = mock 'vm stopper'
70
+ @stop_response_mock = mock 'vm stop response'
71
+ Fission::Action::VM::Stopper.stub(:new).and_return(@vm_stopper)
119
72
  end
120
73
 
121
- it 'should return an unsuccessful response if unable to determine if running' do
122
- @running_response_mock.stub_as_unsuccessful
123
- @vm.stop.should be_an_unsuccessful_response
74
+ it 'should return a successful response when stopping' do
75
+ @vm_stopper.should_receive(:stop).
76
+ and_return(@stop_response_mock)
77
+ @stop_response_mock.stub_as_successful
78
+ @vm.stop.should be_a_successful_response
124
79
  end
125
80
 
126
- it 'should return an unsuccessful response if unable to figure out the conf file' do
127
- @conf_file_response_mock.stub_as_unsuccessful
81
+ it 'should return an unsuccessful response when unable to stop the vm' do
82
+ @vm_stopper.should_receive(:stop).
83
+ and_return(@stop_response_mock)
84
+ @stop_response_mock.stub_as_unsuccessful
128
85
  @vm.stop.should be_an_unsuccessful_response
129
86
  end
130
87
 
131
- it 'should return a successful response and stop the vm' do
132
- $?.should_receive(:exitstatus).and_return(0)
133
- @vm.should_receive(:`).
134
- with("#{@vmrun_cmd} stop #{@conf_file_path.gsub ' ', '\ '} 2>&1").
135
- and_return("it's all good")
136
-
137
- @vm.stop.should be_a_successful_response
138
- end
139
-
140
- it 'should return a suscessful response and hard stop the vm' do
141
- $?.should_receive(:exitstatus).and_return(0)
142
- @vm.should_receive(:`).
143
- with("#{@vmrun_cmd} stop #{@conf_file_path.gsub ' ', '\ '} hard 2>&1").
144
- and_return("it's all good")
145
-
88
+ it 'should return a successful response when stopping hard' do
89
+ @vm_stopper.should_receive(:stop).
90
+ with(:hard => true).
91
+ and_return(@stop_response_mock)
92
+ @stop_response_mock.stub_as_successful
146
93
  @vm.stop(:hard => true).should be_a_successful_response
147
94
  end
148
-
149
- it 'it should return an unsuccessful response if unable to stop the vm' do
150
- $?.should_receive(:exitstatus).and_return(1)
151
- @vm.should_receive(:`).
152
- with("#{@vmrun_cmd} stop #{@conf_file_path.gsub ' ', '\ '} 2>&1").
153
- and_return("it blew up")
154
-
155
- @vm.stop.should be_an_unsuccessful_response
156
- end
157
95
  end
158
96
 
159
97
  describe 'suspend' do
160
98
  before do
161
- @running_response_mock = mock('running?')
162
-
163
- @vm.stub(:exists?).and_return(true)
164
- @vm.stub(:running?).and_return(@running_response_mock)
165
- @vm.stub(:conf_file).and_return(@conf_file_response_mock)
166
- @running_response_mock.stub_as_successful true
167
- @conf_file_response_mock.stub_as_successful @conf_file_path
168
- end
169
-
170
- it "should return an unsuccessful response if the vm doesn't exist" do
171
- @vm.stub(:exists?).and_return(false)
172
- @vm.suspend.should be_an_unsuccessful_response 'VM does not exist'
173
- end
174
-
175
- it 'should return an unsuccessful response if the vm is not running' do
176
- @running_response_mock.stub_as_successful false
177
- @vm.suspend.should be_an_unsuccessful_response 'VM is not running'
99
+ @vm_suspender = mock 'vm suspender'
100
+ @suspend_response_mock = mock 'vm suspend response'
101
+ Fission::Action::VM::Suspender.stub(:new).and_return(@vm_suspender)
102
+ @vm_suspender.should_receive(:suspend).
103
+ and_return(@suspend_response_mock)
178
104
  end
179
105
 
180
- it 'should return an unsuccessful response if unable to determine if running' do
181
- @running_response_mock.stub_as_unsuccessful
182
- @vm.suspend.should be_an_unsuccessful_response
183
- end
184
-
185
- it 'should return an unsuccessful response if unable to figure out the conf file' do
186
- @conf_file_response_mock.stub_as_unsuccessful
187
- @vm.suspend.should be_an_unsuccessful_response
188
- end
189
-
190
- it 'should return a successful response' do
191
- $?.should_receive(:exitstatus).and_return(0)
192
- @vm.should_receive(:`).
193
- with("#{@vmrun_cmd} suspend #{@conf_file_path.gsub ' ', '\ '} 2>&1").
194
- and_return("it's all good")
195
-
106
+ it 'should return a successful response when suspending' do
107
+ @suspend_response_mock.stub_as_successful
196
108
  @vm.suspend.should be_a_successful_response
197
109
  end
198
110
 
199
- it 'it should return an unsuccessful response if unable to suspend the vm' do
200
- $?.should_receive(:exitstatus).and_return(1)
201
- @vm.should_receive(:`).
202
- with("#{@vmrun_cmd} suspend #{@conf_file_path.gsub ' ', '\ '} 2>&1").
203
- and_return("it blew up")
204
-
111
+ it 'should return an unsuccessful response when unable to suspend the vm' do
112
+ @suspend_response_mock.stub_as_unsuccessful
205
113
  @vm.suspend.should be_an_unsuccessful_response
206
114
  end
207
115
  end
208
116
 
209
117
  describe 'snapshots' do
210
118
  before do
211
- @vm.stub(:exists?).and_return(true)
212
- @vm.stub(:conf_file).and_return(@conf_file_response_mock)
213
- @conf_file_response_mock.stub_as_successful @conf_file_path
119
+ @snapshot_lister_mock = mock 'snapshot lister'
120
+ @snapshots_response_mock = mock 'snapshots mock'
121
+ Fission::Action::Snapshot::Lister.stub(:new).and_return(@snapshot_lister_mock)
122
+ @snapshot_lister_mock.should_receive(:snapshots).
123
+ and_return(@snapshots_response_mock)
214
124
  end
215
125
 
216
- it "should return an unsuccessful repsonse when the vm doesn't exist" do
217
- @vm.stub(:exists?).and_return(false)
218
- @vm.snapshots.should be_an_unsuccessful_response 'VM does not exist'
126
+ it "should return an unsuccessful repsonse when unable to list the snapshots" do
127
+ @snapshots_response_mock.stub_as_unsuccessful
128
+ @vm.snapshots.should be_an_unsuccessful_response
219
129
  end
220
130
 
221
131
  it 'should return a successful response with the list of snapshots' do
222
- $?.should_receive(:exitstatus).and_return(0)
223
- @vm.should_receive(:`).
224
- with("#{@vmrun_cmd} listSnapshots #{@conf_file_path.gsub ' ', '\ '} 2>&1").
225
- and_return("Total snapshots: 3\nsnap foo\nsnap bar\nsnap baz\n")
226
-
132
+ @snapshots_response_mock.stub_as_successful ['snap_1', 'snap_2']
227
133
  response = @vm.snapshots
228
134
  response.should be_a_successful_response
229
- response.data.should == ['snap foo', 'snap bar', 'snap baz']
230
- end
231
-
232
- it 'should return an unsuccessful response if unable to figure out the conf file' do
233
- @conf_file_response_mock.stub_as_unsuccessful
234
- @vm.snapshots.should be_an_unsuccessful_response
235
- end
236
-
237
- it 'should return an unsuccessful response if there was a problem getting the list of snapshots' do
238
- $?.should_receive(:exitstatus).and_return(1)
239
- @vm.should_receive(:`).
240
- with("#{@vmrun_cmd} listSnapshots #{@conf_file_path.gsub ' ', '\ '} 2>&1").
241
- and_return("it blew up")
242
-
243
- @vm.snapshots.should be_an_unsuccessful_response
135
+ response.data.should == ['snap_1', 'snap_2']
244
136
  end
245
137
  end
246
138
 
247
139
  describe 'create_snapshot' do
248
140
  before do
249
- @snapshots_response_mock = mock('snapshots')
250
- @running_response_mock = mock('running?')
251
-
252
- @running_response_mock.stub_as_successful true
253
- @conf_file_response_mock.stub_as_successful @conf_file_path
254
- @snapshots_response_mock.stub_as_successful []
255
-
256
- @vm.stub(:exists?).and_return(true)
257
- @vm.stub(:snapshots).and_return(@snapshots_response_mock)
258
- @vm.stub(:running?).and_return(@running_response_mock)
259
- @vm.stub(:conf_file).and_return(@conf_file_response_mock)
141
+ @snapshot_creator_mock = mock 'snapshot creator'
142
+ @snapshot_create_response_mock = mock 'snapshot create response'
143
+ Fission::Action::Snapshot::Creator.stub(:new).and_return(@snapshot_creator_mock)
144
+ @snapshot_creator_mock.should_receive(:create_snapshot).
145
+ with('snap_1').
146
+ and_return(@snapshot_create_response_mock)
260
147
  end
261
148
 
262
- it "should return an unsuccessful response if the vm doesn't exist" do
263
- @vm.stub(:exists?).and_return(false)
264
- @vm.create_snapshot('snap_1').should be_an_unsuccessful_response 'VM does not exist'
265
- end
266
-
267
- it 'should return an unsuccessful response if the vm is not running' do
268
- @running_response_mock.stub_as_successful false
269
-
270
- response = @vm.create_snapshot 'snap_1'
271
- error_message = 'The VM must be running in order to take a snapshot.'
272
- response.should be_an_unsuccessful_response error_message
273
- end
274
-
275
- it 'should return an unsuccessful response if unable to determine if running' do
276
- @running_response_mock.stub_as_unsuccessful
149
+ it 'should return an unsuccessful response when unable to create the snapshot' do
150
+ @snapshot_create_response_mock.stub_as_unsuccessful
277
151
  @vm.create_snapshot('snap_1').should be_an_unsuccessful_response
278
152
  end
279
153
 
280
- it 'should return an unsuccessful response if unable to figure out the conf file' do
281
- @conf_file_response_mock.stub_as_unsuccessful
282
- @vm.create_snapshot('snap_1').should be_an_unsuccessful_response
154
+ it 'should return a successful response when creating the snapshot' do
155
+ @snapshot_create_response_mock.stub_as_successful
156
+ @vm.create_snapshot('snap_1').should be_a_successful_response
283
157
  end
284
- it 'should return a successful response and create a snapshot' do
285
- $?.should_receive(:exitstatus).and_return(0)
286
- @vm.should_receive(:`).
287
- with("#{@vmrun_cmd} snapshot #{@conf_file_path.gsub ' ', '\ '} \"bar\" 2>&1").
288
- and_return("")
158
+ end
289
159
 
290
- @vm.create_snapshot('bar').should be_a_successful_response
291
- end
160
+ describe 'delete_snapshot' do
161
+ before do
162
+ @snapshot_deleter_mock = mock 'snapshot deleter'
163
+ @snapshot_delete_response_mock = mock 'snapshot delete response'
292
164
 
293
- it 'should return an unsuccessful response if the snapshot name is a duplicate' do
294
- @snapshots_response_mock.stub_as_successful ['snap_1']
295
- response = @vm.create_snapshot 'snap_1'
296
- response.should be_an_unsuccessful_response "There is already a snapshot named 'snap_1'."
165
+ Fission::Action::Snapshot::Deleter.stub(:new).
166
+ and_return(@snapshot_deleter_mock)
167
+ @snapshot_deleter_mock.should_receive(:delete_snapshot).
168
+ with('snap_1').
169
+ and_return(@snapshot_delete_response_mock)
297
170
  end
298
171
 
299
- it 'should return an unsuccessful response if there was a problem listing the existing snapshots' do
300
- @snapshots_response_mock.stub_as_unsuccessful
301
- @vm.create_snapshot('snap_1').should be_an_unsuccessful_response
172
+ it 'should return an unsuccessful response when unable to delete the snapshot' do
173
+ @snapshot_delete_response_mock.stub_as_unsuccessful
174
+ @vm.delete_snapshot('snap_1').should be_an_unsuccessful_response
302
175
  end
303
176
 
304
- it 'should return and unsuccessful response if there was a problem creating the snapshot' do
305
- $?.should_receive(:exitstatus).and_return(1)
306
- @vm.should_receive(:`).
307
- with("#{@vmrun_cmd} snapshot #{@conf_file_path.gsub ' ', '\ '} \"bar\" 2>&1").
308
- and_return("it blew up")
309
-
310
- @vm.create_snapshot('bar').should be_an_unsuccessful_response
177
+ it 'should return a successful response when deleteing the snapshot' do
178
+ @snapshot_delete_response_mock.stub_as_successful
179
+ @vm.delete_snapshot('snap_1').should be_a_successful_response
311
180
  end
312
181
  end
313
182
 
314
183
  describe 'revert_to_snapshot' do
315
184
  before do
316
- @snapshots_response_mock = mock('snapshots')
317
-
318
- @conf_file_response_mock.stub_as_successful @conf_file_path
319
- @snapshots_response_mock.stub_as_successful ['snap_1']
320
-
321
- @vm.stub(:exists?).and_return(true)
322
- @vm.stub(:snapshots).and_return(@snapshots_response_mock)
323
- @vm.stub(:conf_file).and_return(@conf_file_response_mock)
324
- Fission::Fusion.stub(:running?).and_return(false)
325
- end
326
-
327
- it "should return an unsuccessful response if the vm doesn't exist" do
328
- @vm.stub(:exists?).and_return(false)
329
- @vm.revert_to_snapshot('snap_1').should be_an_unsuccessful_response 'VM does not exist'
330
- end
331
-
332
- it 'should return an unsuccessful response if the Fusion GUI is running' do
333
- Fission::Fusion.stub(:running?).and_return(true)
334
-
335
- response = @vm.revert_to_snapshot 'snap_1'
185
+ @snapshot_reverter_mock = mock 'snapshot reverter'
186
+ @snapshot_revert_response_mock = 'snapshot revert response'
336
187
 
337
- error_string = 'It looks like the Fusion GUI is currently running. '
338
- error_string << 'A VM cannot be reverted to a snapshot when the Fusion GUI is running. '
339
- error_string << 'Exit the Fusion GUI and try again.'
340
-
341
- response.should be_an_unsuccessful_response error_string
188
+ Fission::Action::Snapshot::Reverter.stub(:new).
189
+ and_return(@snapshot_reverter_mock)
190
+ @snapshot_reverter_mock.should_receive(:revert_to_snapshot).
191
+ with('snap_1').
192
+ and_return(@snapshot_revert_response_mock)
342
193
  end
343
194
 
344
- it 'should return an unsuccessful response if unable to figure out the conf file' do
345
- @conf_file_response_mock.stub_as_unsuccessful
195
+ it 'should return an unsuccessful response when unable to revert the snapshot' do
196
+ @snapshot_revert_response_mock.stub_as_unsuccessful
346
197
  @vm.revert_to_snapshot('snap_1').should be_an_unsuccessful_response
347
198
  end
348
199
 
349
- it 'should return a successful response and revert to the provided snapshot' do
350
- $?.should_receive(:exitstatus).and_return(0)
351
- @vm.should_receive(:`).
352
- with("#{@vmrun_cmd} revertToSnapshot #{@conf_file_path.gsub ' ', '\ '} \"snap_1\" 2>&1").
353
- and_return("")
354
-
200
+ it 'should return a successful response when reverting the snapshot' do
201
+ @snapshot_revert_response_mock.stub_as_successful
355
202
  @vm.revert_to_snapshot('snap_1').should be_a_successful_response
356
203
  end
357
-
358
- it 'should return an unsuccessful response if the snapshot cannot be found' do
359
- @snapshots_response_mock.stub_as_successful []
360
- response = @vm.revert_to_snapshot 'snap_1'
361
- response.should be_an_unsuccessful_response "Unable to find a snapshot named 'snap_1'."
362
- end
363
-
364
- it 'should return an unsuccessful response if unable to list the existing snapshots' do
365
- @snapshots_response_mock.stub_as_unsuccessful
366
- @vm.revert_to_snapshot('snap_1').should be_an_unsuccessful_response
367
- end
368
-
369
- it 'should return and unsuccessful response if unable to revert to the snapshot' do
370
- $?.should_receive(:exitstatus).and_return(1)
371
- @vm.should_receive(:`).
372
- with("#{@vmrun_cmd} revertToSnapshot #{@conf_file_path.gsub ' ', '\ '} \"snap_1\" 2>&1").
373
- and_return("it blew up")
374
-
375
- @vm.revert_to_snapshot('snap_1').should be_an_unsuccessful_response
376
- end
377
-
378
204
  end
379
205
 
380
206
  describe 'exists?' do
@@ -394,6 +220,62 @@ describe Fission::VM do
394
220
  end
395
221
  end
396
222
 
223
+ describe 'hardware_info' do
224
+ before do
225
+ @vm_config_data_response_mock = mock 'vm config data response'
226
+ @vm.stub(:conf_file_data).and_return(@vm_config_data_response_mock)
227
+ @config_data = { 'numvcpus' => '2',
228
+ 'replay.supported' => "TRUE",
229
+ 'replay.filename' => '',
230
+ 'memsize' => '2048',
231
+ 'scsi0:0.redo' => '' }
232
+ end
233
+
234
+ context 'when successful getting the vm config data' do
235
+ before do
236
+ @vm_config_data_response_mock.stub_as_successful @config_data
237
+ end
238
+
239
+ context 'when the number of cpus is not specified in the conf file' do
240
+ before do
241
+ @config_data.delete 'numvcpus'
242
+ end
243
+
244
+ it 'should return a successful response with a single cpu' do
245
+ response = @vm.hardware_info
246
+
247
+ response.should be_a_successful_response
248
+ response.data.should have_key 'cpus'
249
+ response.data['cpus'].should == 1
250
+ end
251
+ end
252
+
253
+ it 'should return a successful response with the number of cpus' do
254
+ response = @vm.hardware_info
255
+
256
+ response.should be_a_successful_response
257
+ response.data.should have_key 'cpus'
258
+ response.data['cpus'].should == 2
259
+ end
260
+
261
+ it 'should return a successful response with the amount of memory' do
262
+ response = @vm.hardware_info
263
+
264
+ response.should be_a_successful_response
265
+ response.data.should have_key 'memory'
266
+ response.data['memory'].should == 2048
267
+ end
268
+ end
269
+
270
+ context 'when unsuccessfully getting the vm config data' do
271
+ it 'should return an unsuccessful response' do
272
+ @vm_config_data_response_mock.stub_as_unsuccessful
273
+ @vm.hardware_info.should be_an_unsuccessful_response
274
+ end
275
+ end
276
+
277
+ end
278
+
397
279
  describe 'mac_addresses' do
398
280
  before do
399
281
  @network_info_mock = mock('network_info')
@@ -410,7 +292,7 @@ describe Fission::VM do
410
292
  response = @vm.mac_addresses
411
293
 
412
294
  response.should be_a_successful_response
413
- response.data.should == ['00:0c:29:1d:6a:64', '00:0c:29:1d:6a:75']
295
+ response.data.should =~ ['00:0c:29:1d:6a:64', '00:0c:29:1d:6a:75']
414
296
  end
415
297
 
416
298
  it 'should return a successful response with an empty list if no mac addresses were found' do
@@ -435,150 +317,185 @@ describe Fission::VM do
435
317
 
436
318
  describe 'network_info' do
437
319
  before do
438
- @conf_file_response_mock.stub_as_successful @conf_file_path
439
-
440
- @vm.should_receive(:conf_file).and_return(@conf_file_response_mock)
441
- @conf_file_io = StringIO.new
442
320
  @lease_1_response_mock = mock('lease_1_response')
443
321
  @lease_2_response_mock = mock('lease_1_response')
444
- end
322
+ @vm_config_data_response_mock = mock 'vm config data response'
323
+
324
+ @vm.stub(:conf_file_data).and_return(@vm_config_data_response_mock)
325
+ @config_data = { 'ide1:0.deviceType' => 'cdrom-image',
326
+ 'ethernet0.present' => 'TRUE',
327
+ 'ethernet1.address' => '00:0c:29:1d:6a:75',
328
+ 'ethernet0.connectionType' => 'nat',
329
+ 'ethernet0.generatedAddress' => '00:0c:29:1d:6a:64',
330
+ 'ethernet0.virtualDev' => 'e1000',
331
+ 'ethernet0.wakeOnPcktRcv' => 'FALSE',
332
+ 'ethernet0.addressType' => 'generated',
333
+ 'ethernet0.linkStatePropagatio.enable' => 'TRUE',
334
+ 'ethernet0.generatedAddressenable' => 'TRUE',
335
+ 'ethernet1.generatedAddressenable' => 'TRUE' }
336
+ end
337
+
338
+ context 'when successful getting the vm config data' do
339
+ before do
340
+ @vm_config_data_response_mock.stub_as_successful @config_data
341
+ end
445
342
 
446
- it 'should return a successful response with the list of interfaces, macs, and ips' do
447
- @conf_file_response_mock.stub_as_successful @conf_file_path
343
+ it 'should return a successful response with the list of interfaces, macs, and ips' do
344
+ @lease_1 = Fission::Lease.new :ip_address => '127.0.0.1',
345
+ :mac_address => '00:0c:29:1d:6a:64'
346
+ @lease_1_response_mock.stub_as_successful @lease_1
448
347
 
449
- @lease_1 = Fission::Lease.new :ip_address => '127.0.0.1',
450
- :mac_address => '00:0c:29:1d:6a:64'
451
- @lease_1_response_mock.stub_as_successful @lease_1
348
+ @lease_2 = Fission::Lease.new :ip_address => '127.0.0.2',
349
+ :mac_address => '00:0c:29:1d:6a:75'
350
+ @lease_2_response_mock.stub_as_successful @lease_2
452
351
 
453
- @lease_2 = Fission::Lease.new :ip_address => '127.0.0.2',
454
- :mac_address => '00:0c:29:1d:6a:75'
455
- @lease_2_response_mock.stub_as_successful @lease_2
352
+ Fission::Lease.should_receive(:find_by_mac_address).
353
+ with('00:0c:29:1d:6a:64').
354
+ and_return(@lease_1_response_mock)
355
+ Fission::Lease.should_receive(:find_by_mac_address).
356
+ with('00:0c:29:1d:6a:75').
357
+ and_return(@lease_2_response_mock)
456
358
 
457
- vmx_content = 'ide1:0.deviceType = "cdrom-image"
458
- ethernet0.present = "TRUE"
459
- ethernet1.address = "00:0c:29:1d:6a:75"
460
- ethernet0.connectionType = "nat"
461
- ethernet0.generatedAddress = "00:0c:29:1d:6a:64"
462
- ethernet0.virtualDev = "e1000"
463
- ethernet0.wakeOnPcktRcv = "FALSE"
464
- ethernet0.addressType = "generated"
465
- ethernet0.linkStatePropagation.enable = "TRUE"
466
- ethernet0.generatedAddressenable = "TRUE"
467
- ethernet1.generatedAddressenable = "TRUE"'
359
+ response = @vm.network_info
360
+ response.should be_a_successful_response
361
+ response.data.should == { 'ethernet0' => { 'mac_address' => '00:0c:29:1d:6a:64',
362
+ 'ip_address' => '127.0.0.1' },
363
+ 'ethernet1' => { 'mac_address' => '00:0c:29:1d:6a:75',
364
+ 'ip_address' => '127.0.0.2' } }
365
+ end
468
366
 
469
- @conf_file_io.string = vmx_content
367
+ it 'should return a successful response with an empty list if there are no macs' do
368
+ @config_data.delete 'ethernet0.generatedAddress'
369
+ @config_data.delete 'ethernet1.address'
470
370
 
471
- File.should_receive(:open).with(@conf_file_path, 'r').
472
- and_yield(@conf_file_io)
371
+ response = @vm.network_info
372
+ response.should be_a_successful_response
373
+ response.data.should == {}
374
+ end
473
375
 
474
- Fission::Lease.should_receive(:find_by_mac_address).
475
- with('00:0c:29:1d:6a:64').
476
- and_return(@lease_1_response_mock)
477
- Fission::Lease.should_receive(:find_by_mac_address).
478
- with('00:0c:29:1d:6a:75').
479
- and_return(@lease_2_response_mock)
376
+ it 'should return a successful response without ip addresses if none were found' do
377
+ @lease_1_response_mock.stub_as_successful nil
378
+ @lease_2_response_mock.stub_as_successful nil
480
379
 
481
- response = @vm.network_info
482
- response.should be_a_successful_response
483
- response.data.should == { 'ethernet0' => { 'mac_address' => '00:0c:29:1d:6a:64',
484
- 'ip_address' => '127.0.0.1' },
485
- 'ethernet1' => { 'mac_address' => '00:0c:29:1d:6a:75',
486
- 'ip_address' => '127.0.0.2' } }
487
- end
380
+ Fission::Lease.should_receive(:find_by_mac_address).
381
+ with('00:0c:29:1d:6a:64').
382
+ and_return(@lease_1_response_mock)
383
+ Fission::Lease.should_receive(:find_by_mac_address).
384
+ with('00:0c:29:1d:6a:75').
385
+ and_return(@lease_2_response_mock)
488
386
 
489
- it 'should return a successful response with an empty list if there are no macs' do
387
+ response = @vm.network_info
388
+ response.should be_a_successful_response
389
+ response.data.should == { 'ethernet0' => { 'mac_address' => '00:0c:29:1d:6a:64',
390
+ 'ip_address' => nil },
391
+ 'ethernet1' => { 'mac_address' => '00:0c:29:1d:6a:75',
392
+ 'ip_address' => nil } }
393
+ end
490
394
 
491
- vmx_content = 'ide1:0.deviceType = "cdrom-image"
492
- pciBridge7.virtualDev = "pcieRootPort"
493
- pciBridge7.functions = "8"
494
- vmci0.present = "TRUE"
495
- roamingVM.exitBehavior = "go"
496
- tools.syncTime = "TRUE"'
395
+ context 'when unsuccessful getting the ip info' do
396
+ it 'should return an unsuccessful response if there was an error getting the ip information' do
397
+ @lease_1_response_mock.stub_as_unsuccessful
398
+ @lease_2_response_mock.stub_as_successful nil
497
399
 
498
- @conf_file_io.string = vmx_content
400
+ Fission::Lease.should_receive(:find_by_mac_address).
401
+ with('00:0c:29:1d:6a:64').
402
+ and_return(@lease_1_response_mock)
403
+ Fission::Lease.should_receive(:find_by_mac_address).
404
+ with('00:0c:29:1d:6a:75').
405
+ and_return(@lease_2_response_mock)
499
406
 
500
- File.should_receive(:open).with(@conf_file_path, 'r').
501
- and_yield(@conf_file_io)
407
+ @vm.network_info.should be_an_unsuccessful_response
408
+ end
409
+ end
502
410
 
503
- response = @vm.network_info
504
- response.should be_a_successful_response
505
- response.data.should == {}
506
- end
507
-
508
- it 'should return a successful response without ip addresses if none were found' do
509
- @lease_1_response_mock.stub_as_successful nil
510
- @lease_2_response_mock.stub_as_successful nil
511
-
512
- vmx_content = 'ide1:0.deviceType = "cdrom-image"
513
- ethernet0.present = "TRUE"
514
- ethernet1.address = "00:0c:29:1d:6a:75"
515
- ethernet0.connectionType = "nat"
516
- ethernet0.generatedAddress = "00:0c:29:1d:6a:64"
517
- ethernet0.virtualDev = "e1000"
518
- ethernet0.wakeOnPcktRcv = "FALSE"
519
- ethernet0.addressType = "generated"
520
- ethernet0.linkStatePropagation.enable = "TRUE"
521
- ethernet0.generatedAddressenable = "TRUE"
522
- ethernet1.generatedAddressenable = "TRUE"'
523
-
524
- @conf_file_io.string = vmx_content
525
-
526
- File.should_receive(:open).with(@conf_file_path, 'r').
527
- and_yield(@conf_file_io)
528
-
529
- Fission::Lease.should_receive(:find_by_mac_address).
530
- with('00:0c:29:1d:6a:64').
531
- and_return(@lease_1_response_mock)
532
- Fission::Lease.should_receive(:find_by_mac_address).
533
- with('00:0c:29:1d:6a:75').
534
- and_return(@lease_2_response_mock)
535
-
536
- response = @vm.network_info
537
- response.should be_a_successful_response
538
- response.data.should == { 'ethernet0' => { 'mac_address' => '00:0c:29:1d:6a:64',
539
- 'ip_address' => nil },
540
- 'ethernet1' => { 'mac_address' => '00:0c:29:1d:6a:75',
541
- 'ip_address' => nil } }
542
411
  end
543
412
 
544
- it 'should return an unsuccessful response with an error if no conf file was found' do
545
- @conf_file_response_mock.stub_as_unsuccessful
413
+ context 'when unsuccessfully getting the vm config data' do
414
+ it 'should return an unsuccessful response' do
415
+ @vm_config_data_response_mock.stub_as_unsuccessful
416
+ @vm.guestos.should be_an_unsuccessful_response
417
+ end
418
+ end
546
419
 
547
- File.should_not_receive(:open)
420
+ end
548
421
 
549
- @vm.network_info.should be_an_unsuccessful_response
422
+ describe 'guestos' do
423
+ before do
424
+ @vm_config_data_response_mock = mock 'vm config data response'
425
+ @vm.stub(:conf_file_data).and_return(@vm_config_data_response_mock)
426
+ @config_data = { 'cleanShutdown' => 'TRUE',
427
+ 'guestOS' => 'debian5',
428
+ 'replay.filename' => '',
429
+ 'scsi0:0.redo' => '' }
550
430
  end
551
431
 
552
- it 'should return an unsuccessful response if there was an error getting the ip information' do
553
- @lease_1_response_mock.stub_as_unsuccessful
554
- @lease_2_response_mock.stub_as_successful nil
432
+ context 'when successful getting the vm config data' do
433
+ it 'should return a successful response with a string when a guestos is defined' do
434
+ @vm_config_data_response_mock.stub_as_successful @config_data
555
435
 
556
- vmx_content = 'ide1:0.deviceType = "cdrom-image"
557
- ethernet0.present = "TRUE"
558
- ethernet1.address = "00:0c:29:1d:6a:75"
559
- ethernet0.connectionType = "nat"
560
- ethernet0.generatedAddress = "00:0c:29:1d:6a:64"
561
- ethernet0.virtualDev = "e1000"
562
- ethernet0.wakeOnPcktRcv = "FALSE"
563
- ethernet0.addressType = "generated"
564
- ethernet0.linkStatePropagation.enable = "TRUE"
565
- ethernet0.generatedAddressenable = "TRUE"
566
- ethernet1.generatedAddressenable = "TRUE"'
436
+ response = @vm.guestos
437
+ response.should be_a_successful_response
438
+ response.data.should == 'debian5'
439
+ end
567
440
 
568
- @conf_file_io.string = vmx_content
441
+ it 'should return a successful response with an empty string if guestos is not set' do
442
+ @config_data.delete 'guestOS'
443
+ @vm_config_data_response_mock.stub_as_successful @config_data
569
444
 
570
- File.should_receive(:open).with(@conf_file_path, 'r').
571
- and_yield(@conf_file_io)
445
+ response = @vm.guestos
446
+ response.should be_a_successful_response
447
+ response.data.should == ''
448
+ end
449
+ end
572
450
 
573
- Fission::Lease.should_receive(:find_by_mac_address).
574
- with('00:0c:29:1d:6a:64').
575
- and_return(@lease_1_response_mock)
576
- Fission::Lease.should_receive(:find_by_mac_address).
577
- with('00:0c:29:1d:6a:75').
578
- and_return(@lease_2_response_mock)
451
+ context 'when unsuccessfully getting the vm config data' do
452
+ it 'should return an unsuccessful response' do
453
+ @vm_config_data_response_mock.stub_as_unsuccessful
454
+ @vm.guestos.should be_an_unsuccessful_response
455
+ end
456
+ end
457
+ end
579
458
 
580
- @vm.network_info.should be_an_unsuccessful_response
459
+ describe 'uuids' do
460
+ before do
461
+ @vm_config_data_response_mock = mock 'vm config data response'
462
+ @vm.stub(:conf_file_data).and_return(@vm_config_data_response_mock)
463
+ @config_data = { 'uuid.location' => '56 4d d8 9c f8 ec 95 73-2e ea a0 f3 7a 1d 6f c8',
464
+ 'uuid.bios' => '56 4d d8 9c f8 ec 95 73-2e ea a0 f3 7a 1d 6f c8',
465
+ 'checkpoint.vmState' => '',
466
+ 'cleanShutdown' => 'TRUE',
467
+ 'replay.supported' => "TRUE",
468
+ 'replay.filename' => '',
469
+ 'scsi0:0.redo' =>'' }
470
+ end
471
+
472
+ context 'when successful getting the vm config data' do
473
+ it 'should return a successful response with a hash when uuids are defined' do
474
+ @vm_config_data_response_mock.stub_as_successful @config_data
475
+
476
+ response = @vm.uuids
477
+ response.should be_a_successful_response
478
+ response.data.should == { 'bios' => '56 4d d8 9c f8 ec 95 73-2e ea a0 f3 7a 1d 6f c8',
479
+ 'location' => '56 4d d8 9c f8 ec 95 73-2e ea a0 f3 7a 1d 6f c8' }
480
+ end
481
+
482
+ it 'should return a successful response with empty hash if no uuids are defined' do
483
+ ['location', 'bios'].each { |i| @config_data.delete "uuid.#{i}" }
484
+ @vm_config_data_response_mock.stub_as_successful @config_data
485
+
486
+ response = @vm.uuids
487
+ response.should be_a_successful_response
488
+ response.data.should == {}
489
+ end
581
490
  end
491
+
492
+ context 'when unsuccessfully getting the vm config data' do
493
+ it 'should return an unsuccessful response' do
494
+ @vm_config_data_response_mock.stub_as_unsuccessful
495
+ @vm.uuids.should be_an_unsuccessful_response
496
+ end
497
+ end
498
+
582
499
  end
583
500
 
584
501
  describe 'path' do
@@ -730,6 +647,33 @@ ethernet1.generatedAddressenable = "TRUE"'
730
647
 
731
648
  end
732
649
 
650
+ describe 'conf_file_data' do
651
+ before do
652
+ @vm_config_mock = mock 'vm config'
653
+ @vm_config_response_mock = mock 'vm config response'
654
+
655
+ Fission::VMConfiguration.should_receive(:new).with(@vm).
656
+ and_return(@vm_config_mock)
657
+ end
658
+
659
+ it 'should return a successful response with the data' do
660
+ @vm_config_response_mock.stub_as_successful({ 'numvcpus' => '2' })
661
+
662
+ @vm_config_mock.should_receive(:config_data).
663
+ and_return(@vm_config_response_mock)
664
+ config_data = @vm.conf_file_data
665
+ config_data.should be_a_successful_response
666
+ config_data.data.should == { 'numvcpus' => '2' }
667
+ end
668
+
669
+ it 'should return an unsuccessful response' do
670
+ @vm_config_mock.should_receive(:config_data).
671
+ and_return(@vm_config_response_mock)
672
+ @vm_config_response_mock.stub_as_unsuccessful
673
+ @vm.conf_file_data.should be_an_unsuccessful_response
674
+ end
675
+ end
676
+
733
677
  describe 'conf_file' do
734
678
  before do
735
679
  FakeFS.activate!
@@ -796,390 +740,89 @@ ethernet1.generatedAddressenable = "TRUE"'
796
740
 
797
741
  describe "self.all" do
798
742
  before do
799
- @vm_1_mock = mock('vm_1')
800
- @vm_2_mock = mock('vm_2')
801
- end
802
-
803
- it "should return a successful object with the list of VM objects" do
804
- vm_root = Fission.config['vm_dir']
805
- Dir.should_receive(:[]).
806
- and_return(["#{File.join vm_root, 'foo.vmwarevm' }", "#{File.join vm_root, 'bar.vmwarevm' }"])
807
-
808
- vm_root = Fission.config['vm_dir']
809
- File.should_receive(:directory?).with("#{File.join vm_root, 'foo.vmwarevm'}").
810
- and_return(true)
811
- File.should_receive(:directory?).with("#{File.join vm_root, 'bar.vmwarevm'}").
812
- and_return(true)
813
-
814
- Fission::VM.should_receive(:new).with('foo').and_return(@vm_1_mock)
815
- Fission::VM.should_receive(:new).with('bar').and_return(@vm_2_mock)
816
-
817
- response = Fission::VM.all
818
- response.should be_a_successful_response
819
- response.data.should == [@vm_1_mock, @vm_2_mock]
743
+ @lister = mock 'lister'
744
+ @all_response_mock = mock 'all response'
745
+ Fission::Action::VM::Lister.stub(:new).and_return(@lister)
746
+ @lister.should_receive(:all).
747
+ and_return(@all_response_mock)
820
748
  end
821
749
 
822
- it "should return a successful object and not return an item in the list if it isn't a directory" do
823
- vm_root = Fission.config['vm_dir']
824
- Dir.should_receive(:[]).
825
- and_return((['foo', 'bar', 'baz'].map { |i| File.join vm_root, "#{i}.vmwarevm"}))
826
- File.should_receive(:directory?).
827
- with("#{File.join vm_root, 'foo.vmwarevm'}").and_return(true)
828
- File.should_receive(:directory?).
829
- with("#{File.join vm_root, 'bar.vmwarevm'}").and_return(true)
830
- File.should_receive(:directory?).
831
- with("#{File.join vm_root, 'baz.vmwarevm'}").and_return(false)
832
-
833
- Fission::VM.should_receive(:new).with('foo').and_return(@vm_1_mock)
834
- Fission::VM.should_receive(:new).with('bar').and_return(@vm_2_mock)
835
-
836
- response = Fission::VM.all
837
- response.should be_a_successful_response
838
- response.data.should == [@vm_1_mock, @vm_2_mock]
750
+ it 'should return an unsuccessful response when unable to delete the vm' do
751
+ @all_response_mock.stub_as_unsuccessful
752
+ Fission::VM.all.should be_an_unsuccessful_response
839
753
  end
840
754
 
841
- it "should only query for items with an extension of .vmwarevm" do
842
- dir_arg = File.join Fission.config['vm_dir'], '*.vmwarevm'
843
- Dir.should_receive(:[]).with(dir_arg).
844
- and_return(['foo.vmwarevm', 'bar.vmwarevm'])
845
- Fission::VM.all
755
+ it 'should return a successful response when deleting' do
756
+ @all_response_mock.stub_as_successful
757
+ Fission::VM.all.should be_a_successful_response
846
758
  end
847
759
  end
848
760
 
849
761
  describe 'self.all_running' do
850
762
  before do
851
- @vm_1 = Fission::VM.new 'foo'
852
- @vm_2 = Fission::VM.new 'bar'
853
- @vm_3 = Fission::VM.new 'baz'
854
- @vm_names_and_objs = { 'foo' => @vm_1, 'bar' => @vm_2, 'baz' => @vm_3 }
763
+ @lister = mock 'lister'
764
+ @all_running_response_mock = mock 'all running response'
765
+ Fission::Action::VM::Lister.stub(:new).and_return(@lister)
766
+ @lister.should_receive(:all_running).
767
+ and_return(@all_running_response_mock)
855
768
  end
856
769
 
857
- it 'should return a successful response with the list of running vms' do
858
- list_output = "Total running VMs: 2\n/vm/foo.vmwarevm/foo.vmx\n"
859
- list_output << "/vm/bar.vmwarevm/bar.vmx\n/vm/baz.vmwarevm/baz.vmx\n"
860
-
861
- $?.should_receive(:exitstatus).and_return(0)
862
- Fission::VM.should_receive(:`).
863
- with("#{@vmrun_cmd} list").
864
- and_return(list_output)
865
- [ 'foo', 'bar', 'baz'].each do |vm|
866
- File.should_receive(:exists?).with("/vm/#{vm}.vmwarevm/#{vm}.vmx").
867
- and_return(true)
868
-
869
- Fission::VM.should_receive(:new).with(vm).
870
- and_return(@vm_names_and_objs[vm])
871
- end
872
-
873
- response = Fission::VM.all_running
874
- response.should be_a_successful_response
875
- response.data.should == [@vm_1, @vm_2, @vm_3]
876
- end
877
-
878
- it 'should return a successful response with the VM dir name if it differs from the .vmx file name' do
879
- vm_dir_file = { 'foo' => 'foo', 'bar' => 'diff', 'baz' => 'baz'}
880
- list_output = "Total running VMs: 3\n"
881
- vm_dir_file.each_pair do |dir, file|
882
- list_output << "/vm/#{dir}.vmwarevm/#{file}.vmx\n"
883
- File.should_receive(:exists?).with("/vm/#{dir}.vmwarevm/#{file}.vmx").
884
- and_return(true)
885
- Fission::VM.should_receive(:new).with(dir).
886
- and_return(@vm_names_and_objs[dir])
887
- end
888
-
889
- $?.should_receive(:exitstatus).and_return(0)
890
- Fission::VM.should_receive(:`).
891
- with("#{@vmrun_cmd} list").
892
- and_return(list_output)
893
-
894
- response = Fission::VM.all_running
895
- response.should be_a_successful_response
896
- response.data.should == [@vm_1, @vm_2, @vm_3]
770
+ it 'should return an unsuccessful response when unable to delete the vm' do
771
+ @all_running_response_mock.stub_as_unsuccessful
772
+ Fission::VM.all_running.should be_an_unsuccessful_response
897
773
  end
898
774
 
899
- it 'should return an unsuccessful response if unable to get the list of running vms' do
900
- $?.should_receive(:exitstatus).and_return(1)
901
- Fission::VM.should_receive(:`).
902
- with("#{@vmrun_cmd} list").
903
- and_return("it blew up")
904
- Fission.stub!(:ui).and_return(Fission::UI.new(@string_io))
905
-
906
- Fission::VM.all_running.should be_an_unsuccessful_response
775
+ it 'should return a successful response when deleting' do
776
+ @all_running_response_mock.stub_as_successful
777
+ Fission::VM.all_running.should be_a_successful_response
907
778
  end
908
779
  end
909
780
 
910
781
  describe "self.clone" do
911
782
  before do
912
- @source_vm = Fission::VM.new 'foo'
913
- @target_vm = Fission::VM.new 'bar'
914
- @source_path = @source_vm.path
915
- @target_path = @target_vm.path
916
-
917
- @clone_response_mock = mock('clone_response')
918
- @vm_files = ['.vmx', '.vmxf', '.vmdk', '-s001.vmdk', '-s002.vmdk', '.vmsd']
919
-
920
- FakeFS.activate!
921
-
922
- FileUtils.mkdir_p @source_path
923
-
924
- @vm_files.each do |file|
925
- FileUtils.touch "#{@source_path}/#{@source_vm.name}#{file}"
926
- end
927
-
928
- ['.vmx', '.vmxf', '.vmdk'].each do |ext|
929
- File.open("#{@source_path}/foo#{ext}", 'w') { |f| f.write 'foo.vmdk'}
930
- end
931
-
932
- @source_vm.stub(:exists?).and_return(true)
933
- @target_vm.stub(:exists?).and_return(false)
934
-
935
- Fission::VM.stub(:new).with(@source_vm.name).
936
- and_return(@source_vm)
937
- Fission::VM.stub(:new).with(@target_vm.name).
938
- and_return(@target_vm)
939
-
940
- vmx_content = 'ide1:0.deviceType = "cdrom-image"
941
- nvram = "foo.nvram"
942
- ethernet0.present = "TRUE"
943
- ethernet1.address = "00:0c:29:1d:6a:75"
944
- ethernet0.connectionType = "nat"
945
- ethernet0.generatedAddress = "00:0c:29:1d:6a:64"
946
- ethernet0.virtualDev = "e1000"
947
- tools.remindInstall = "TRUE"
948
- ethernet0.wakeOnPcktRcv = "FALSE"
949
- ethernet0.addressType = "generated"
950
- uuid.action = "keep"
951
- ethernet0.linkStatePropagation.enable = "TRUE"
952
- ethernet0.generatedAddressenable = "TRUE"
953
- ethernet1.generatedAddressenable = "TRUE"'
954
-
955
- File.open("#{@source_path}/#{@source_vm.name}.vmx", 'w') do |f|
956
- f.write vmx_content
957
- end
958
-
959
- ['.vmx', '.vmxf'].each do |ext|
960
- File.stub(:binary?).
961
- with("#{@target_path}/#{@target_vm.name}#{ext}").
962
- and_return(false)
963
- end
964
-
965
- File.stub(:binary?).
966
- with("#{@target_path}/#{@target_vm.name}.vmdk").
967
- and_return(true)
968
- end
969
-
970
- after do
971
- FakeFS.deactivate!
972
- FakeFS::FileSystem.clear
973
- end
974
-
975
- it "should return an unsuccessful response if the source vm doesn't exist" do
976
- @source_vm.stub(:exists?).and_return(false)
977
- response = Fission::VM.clone @source_vm.name, @target_vm.name
978
- response.should be_an_unsuccessful_response 'VM does not exist'
979
- end
980
-
981
- it "should return an unsuccessful response if the target vm exists" do
982
- @target_vm.stub(:exists?).and_return(true)
983
- response = Fission::VM.clone @source_vm.name, @target_vm.name
984
- response.should be_an_unsuccessful_response 'VM already exists'
985
- end
986
-
987
- it 'should copy the vm files to the target' do
988
- Fission::VM.clone @source_vm.name, @target_vm.name
989
-
990
- File.directory?(@target_path).should == true
991
-
992
- @vm_files.each do |file|
993
- File.file?("#{@target_path}/bar#{file}").should == true
994
- end
995
- end
996
-
997
- it "should copy the vm files to the target if a file name doesn't match the directory" do
998
- FileUtils.touch "#{@source_path}/other_name.nvram"
999
-
1000
- Fission::VM.clone @source_vm.name, @target_vm.name
1001
-
1002
- File.directory?(@target_path).should == true
1003
-
1004
- @vm_files.each do |file|
1005
- File.file?("#{@target_path}/#{@target_vm.name}#{file}").should == true
1006
- end
1007
-
1008
- File.file?("#{@target_path}/bar.nvram").should == true
783
+ @vm_1 = mock 'vm 1'
784
+ @vm_2 = mock 'vm 2'
785
+ @vm_cloner = mock 'vm cloner'
786
+ @clone_response_mock = mock 'vm clone response'
787
+ Fission::Action::VM::Cloner.should_receive(:new).
788
+ with(@vm_1, @vm_2).
789
+ and_return(@vm_cloner)
790
+ Fission::VM.should_receive(:new).with('vm_1').and_return(@vm_1)
791
+ Fission::VM.should_receive(:new).with('vm_2').and_return(@vm_2)
792
+ @vm_cloner.should_receive(:clone).
793
+ and_return(@clone_response_mock)
1009
794
  end
1010
795
 
1011
- it "should copy the vm files to the target if a sparse disk file name doesn't match the directory" do
1012
- FileUtils.touch "#{@source_path}/other_name-s003.vmdk"
1013
-
1014
- Fission::VM.clone @source_vm.name, @target_vm.name
1015
-
1016
- File.directory?(@target_path).should == true
1017
-
1018
- @vm_files.each do |file|
1019
- File.file?("#{@target_path}/#{@target_vm.name}#{file}").should == true
1020
- end
1021
-
1022
- File.file?("#{@target_path}/bar-s003.vmdk").should == true
796
+ it 'should return an unsuccessful response when unable to clone the vm' do
797
+ @clone_response_mock.stub_as_unsuccessful
798
+ Fission::VM.clone('vm_1', 'vm_2').should be_an_unsuccessful_response
1023
799
  end
1024
800
 
1025
- it 'should update the target vm config files' do
1026
- Fission::VM.clone @source_vm.name, @target_vm.name
1027
-
1028
- ['.vmx', '.vmxf'].each do |ext|
1029
- File.read("#{@target_path}/bar#{ext}").should_not match /foo/
1030
- File.read("#{@target_path}/bar#{ext}").should match /bar/
1031
- end
801
+ it 'should return a successful response when cloning' do
802
+ @clone_response_mock.stub_as_successful
803
+ Fission::VM.clone('vm_1', 'vm_2').should be_a_successful_response
1032
804
  end
1033
-
1034
- it 'should disable VMware tools warning in the conf file' do
1035
- Fission::VM.clone @source_vm.name, @target_vm.name
1036
-
1037
- pattern = /^tools\.remindInstall = "FALSE"/
1038
-
1039
- File.read("#{@target_path}/bar.vmx").should match pattern
1040
- end
1041
-
1042
- it 'should remove auto generated MAC addresses from the conf file' do
1043
- Fission::VM.clone @source_vm.name, @target_vm.name
1044
-
1045
- pattern = /^ethernet\.+generatedAddress.+/
1046
-
1047
- File.read("#{@target_path}/bar.vmx").should_not match pattern
1048
- end
1049
-
1050
- it 'should setup the conf file to generate a new uuid' do
1051
- Fission::VM.clone @source_vm.name, @target_vm.name
1052
-
1053
- pattern = /^uuid\.action = "create"/
1054
-
1055
- File.read("#{@target_path}/bar.vmx").should match pattern
1056
- end
1057
-
1058
- it "should not try to update the vmdk file if it's not a sparse disk" do
1059
- Fission::VM.clone @source_vm.name, @target_vm.name
1060
-
1061
- File.read("#{@target_path}/bar.vmdk").should match /foo/
1062
- end
1063
-
1064
- it 'should return a successful response if clone was successful' do
1065
- Fission::VM.clone(@source_vm.name, @target_vm.name).should be_a_successful_response
1066
- end
1067
-
1068
- describe 'when a sparse disk is found' do
1069
- it "should update the vmdk" do
1070
- File.rspec_reset
1071
- File.stub(:binary?).and_return(false)
1072
-
1073
- Fission::VM.clone @source_vm.name, @target_vm.name
1074
-
1075
- File.read("#{@target_path}/bar.vmdk").should match /bar/
1076
- end
1077
- end
1078
-
1079
805
  end
1080
806
 
1081
807
  describe 'self.all_with_status' do
1082
808
  before do
1083
- @vm_1 = Fission::VM.new 'foo'
1084
- @vm_2 = Fission::VM.new 'bar'
1085
- @vm_2.stub(:suspend_file_exists?).and_return('true')
1086
- @vm_3 = Fission::VM.new 'baz'
1087
-
1088
- @all_vms_response_mock = mock('all_vms_mock')
1089
- @all_vms_response_mock.stub_as_successful [@vm_1, @vm_2, @vm_3]
1090
-
1091
- @all_running_response_mock = mock('all_running_mock')
1092
- @all_running_response_mock.stub_as_successful [@vm_1]
1093
-
1094
- Fission::VM.stub(:all).and_return(@all_vms_response_mock)
1095
- Fission::VM.stub(:all_running).and_return(@all_running_response_mock)
1096
- end
1097
-
1098
- it 'should return a sucessful response with the VMs and their status' do
1099
- response = Fission::VM.all_with_status
1100
- response.should be_a_successful_response
1101
- response.data.should == { 'foo' => 'running',
1102
- 'bar' => 'suspended',
1103
- 'baz' => 'not running' }
1104
-
809
+ @lister = mock 'lister'
810
+ @all_status_response_mock = mock 'all status response'
811
+ Fission::Action::VM::Lister.stub(:new).and_return(@lister)
812
+ @lister.should_receive(:all_with_status).
813
+ and_return(@all_status_response_mock)
1105
814
  end
1106
815
 
1107
- it 'should return an unsuccessful response if unable to get all of the VMs' do
1108
- @all_vms_response_mock.stub_as_unsuccessful
816
+ it 'should return an unsuccessful response when unable to delete the vm' do
817
+ @all_status_response_mock.stub_as_unsuccessful
1109
818
  Fission::VM.all_with_status.should be_an_unsuccessful_response
1110
819
  end
1111
820
 
1112
- it 'should return an unsuccessful repsonse if unable to get the running VMs' do
1113
- @all_running_response_mock.stub_as_unsuccessful
1114
- Fission::VM.all_with_status.should be_an_unsuccessful_response
821
+ it 'should return a successful response when deleting' do
822
+ @all_status_response_mock.stub_as_successful
823
+ Fission::VM.all_with_status.should be_a_successful_response
1115
824
  end
1116
825
 
1117
826
  end
1118
827
 
1119
- describe 'delete' do
1120
- before do
1121
- @running_response_mock = mock('running?')
1122
- @running_response_mock.stub_as_successful false
1123
-
1124
- @vm.stub(:exists?).and_return(true)
1125
- @vm.stub(:running?).and_return(@running_response_mock)
1126
- @vm.stub(:conf_file).and_return(@conf_file_response_mock)
1127
-
1128
- @target_vm = 'foo'
1129
- @vm_files = %w{ .vmx .vmxf .vmdk -s001.vmdk -s002.vmdk .vmsd }
1130
- FakeFS.activate!
1131
-
1132
- FileUtils.mkdir_p Fission::VM.new(@target_vm).path
1133
-
1134
- @vm_files.each do |file|
1135
- FileUtils.touch File.join(Fission::VM.new(@target_vm).path, "#{@target_vm}#{file}")
1136
- end
1137
- end
1138
-
1139
- after do
1140
- FakeFS.deactivate!
1141
- end
1142
-
1143
- it "should return an unsuccessful response if the vm doesn't exist" do
1144
- @vm.stub(:exists?).and_return(false)
1145
-
1146
- response = @vm.delete
1147
- response.should be_an_unsuccessful_response 'VM does not exist'
1148
- end
1149
-
1150
- it 'should return an unsuccessful response if the vm is running' do
1151
- @running_response_mock.stub_as_successful true
1152
-
1153
- response = @vm.delete
1154
- response.should be_an_unsuccessful_response 'The VM must not be running in order to delete it.'
1155
- end
1156
-
1157
- it 'should return an unsuccessful response if unable to determine if running' do
1158
- @running_response_mock.stub_as_unsuccessful
1159
-
1160
- response = @vm.delete
1161
- response.should be_an_unsuccessful_response
1162
- end
1163
-
1164
- it "should delete the target vm files" do
1165
- Fission::Metadata.stub!(:delete_vm_info)
1166
-
1167
- @vm.delete
1168
-
1169
- @vm_files.each do |file|
1170
- File.exists?(File.join(Fission::VM.new(@target_vm).path, "#{@target_vm}#{file}")).should == false
1171
- end
1172
- end
1173
-
1174
- it 'should delete the target vm metadata' do
1175
- Fission::Metadata.should_receive(:delete_vm_info)
1176
- @vm.delete
1177
- end
1178
-
1179
- it 'should return a successful reponsse object' do
1180
- Fission::Metadata.stub!(:delete_vm_info)
1181
- @vm.delete.should be_a_successful_response
1182
- end
1183
-
1184
- end
1185
828
  end