virtualbox 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
1
+ 0.4.3
@@ -160,7 +160,7 @@ module VirtualBox
160
160
  values.push("#{name.inspect}=...")
161
161
  end
162
162
 
163
- "#<#{self.class} #{values.join(", ")}>".strip
163
+ "#<#{self.class} #{values.sort.join(", ")}>".strip
164
164
  end
165
165
  end
166
166
  end
@@ -166,7 +166,7 @@ module VirtualBox
166
166
  # a new one
167
167
  destroy({:port => port_was}, raise_errors) if port_changed? && !port_was.nil?
168
168
 
169
- Command.vboxmanage("storageattach #{Command.shell_escape(parent.parent.name)} --storagectl #{Command.shell_escape(parent.name)} --port #{port} --device 0 --type #{image.image_type} --medium #{medium}")
169
+ Command.vboxmanage("storageattach", parent.parent.name, "--storagectl", parent.name, "--port", port, "--device", "0", "--type", image.image_type, "--medium", medium)
170
170
  existing_record!
171
171
  clear_dirty!
172
172
 
@@ -209,7 +209,7 @@ module VirtualBox
209
209
  # parent = storagecontroller
210
210
  # parent.parent = vm
211
211
  destroy_port = options[:port] || port
212
- Command.vboxmanage("storageattach #{Command.shell_escape(parent.parent.name)} --storagectl #{Command.shell_escape(parent.name)} --port #{destroy_port} --device 0 --medium none")
212
+ Command.vboxmanage("storageattach", parent.parent.name, "--storagectl", parent.name, "--port", destroy_port, "--device", "0", "--medium", "none")
213
213
  image.destroy(raise_errors) if options[:destroy_image] && image
214
214
  rescue Exceptions::CommandFailedException
215
215
  raise if raise_errors
@@ -23,13 +23,25 @@ module VirtualBox
23
23
 
24
24
  # Sets the path to VBoxManage, which is required for this gem to
25
25
  # work.
26
+ #
27
+ # @param [String] Full path to `VBoxManage`.
26
28
  def vboxmanage=(path)
27
29
  @@vboxmanage = path
28
30
  end
29
31
 
30
- # Runs a VBoxManage command and returns the output.
31
- def vboxmanage(command)
32
- result = execute("#{@@vboxmanage} -q #{command}")
32
+ # Runs a VBoxManage command and returns the output. This method will automatically
33
+ # shell escape all args passed to it. There is no way to avoid this at the moment
34
+ # (since it hasn't been necessary to). This will raise an {Exceptions::CommandFailedException}
35
+ # if the exit status of the command is nonzero. It is up to the caller to figure
36
+ # out how to handle this; there is no way to suppress it via a parameter to this
37
+ # call.
38
+ #
39
+ # Upon success, {vboxmanage} returns the stdout output from the command.
40
+ #
41
+ # @return [String] The data from stdout of the command.
42
+ def vboxmanage(*args)
43
+ args.collect! { |arg| shell_escape(arg.to_s) }
44
+ result = execute("#{@@vboxmanage} -q #{args.join(" ")}")
33
45
  raise Exceptions::CommandFailedException.new(result) if !Command.success?
34
46
  result
35
47
  end
@@ -22,7 +22,7 @@ module VirtualBox
22
22
  class <<self
23
23
  # Returns an array of all available DVDs as DVD objects
24
24
  def all
25
- raw = Command.vboxmanage("list dvds")
25
+ raw = Command.vboxmanage("list", "dvds")
26
26
  parse_raw(raw)
27
27
  end
28
28
 
@@ -69,7 +69,7 @@ module VirtualBox
69
69
  def destroy(raise_errors=false)
70
70
  return false if empty_drive?
71
71
 
72
- Command.vboxmanage("closemedium dvd #{uuid} --delete")
72
+ Command.vboxmanage("closemedium", "dvd", uuid, "--delete")
73
73
  true
74
74
  rescue Exceptions::CommandFailedException
75
75
  raise if raise_errors
@@ -46,7 +46,7 @@ module VirtualBox
46
46
  # @return [Array<ExtraData>]
47
47
  def global(reload=false)
48
48
  if !@@global_data || reload
49
- raw = Command.vboxmanage("getextradata global enumerate")
49
+ raw = Command.vboxmanage("getextradata", "global", "enumerate")
50
50
  @@global_data = parse_kv_pairs(raw)
51
51
  end
52
52
 
@@ -75,7 +75,7 @@ module VirtualBox
75
75
  #
76
76
  # @return [Array<ExtraData>]
77
77
  def populate_relationship(caller, data)
78
- raw = Command.vboxmanage("getextradata #{Command.shell_escape(caller.name)} enumerate")
78
+ raw = Command.vboxmanage("getextradata", caller.name, "enumerate")
79
79
  parse_kv_pairs(raw, caller)
80
80
  end
81
81
 
@@ -124,7 +124,7 @@ module VirtualBox
124
124
  # @return [Boolean] True if command was successful, false otherwise.
125
125
  def save(raise_errors=false)
126
126
  changes.each do |key, value|
127
- Command.vboxmanage("setextradata #{Command.shell_escape(parent_name)} #{Command.shell_escape(key)} #{Command.shell_escape(value[1])}")
127
+ Command.vboxmanage("setextradata", parent_name, key, value[1])
128
128
  clear_dirty!(key)
129
129
  end
130
130
 
@@ -140,7 +140,7 @@ module VirtualBox
140
140
  # will be raised if the command failed.
141
141
  # @return [Boolean] True if command was successful, false otherwise.
142
142
  def delete(key, raise_errors=false)
143
- Command.vboxmanage("setextradata #{Command.shell_escape(parent_name)} #{Command.shell_escape(key)}")
143
+ Command.vboxmanage("setextradata", parent_name, key)
144
144
  super(key)
145
145
  true
146
146
  rescue Exceptions::CommandFailedException
@@ -84,21 +84,26 @@ module VirtualBox
84
84
  # Returns an array of all available hard drives as HardDrive
85
85
  # objects.
86
86
  #
87
+ # @param [Boolean] raise_errors If true, {Exceptions::CommandFailedException}
88
+ # will be raised if the command failed.
87
89
  # @return [Array<HardDrive>]
88
- def all
89
- raw = Command.vboxmanage("list hdds")
90
- parse_blocks(raw).collect { |v| find(v[:uuid]) }
90
+ def all(raise_errors=false)
91
+ raw = Command.vboxmanage("list", "hdds")
92
+ parse_blocks(raw).collect { |v| find(v[:uuid], raise_errors) }
93
+ rescue Exceptions::CommandFailedException
94
+ raise if raise_errors
95
+ false
91
96
  end
92
97
 
93
98
  # Finds one specific hard drive by UUID or file name. If the
94
99
  # hard drive can not be found, will return `nil`.
95
100
  #
101
+ # @param [String] id The UUID or name of the hard drive
102
+ # @param [Boolean] raise_errors If true, {Exceptions::CommandFailedException}
103
+ # will be raised if the command failed.
96
104
  # @return [HardDrive]
97
- def find(id)
98
- raw = Command.vboxmanage("showhdinfo #{id}")
99
-
100
- # Return nil if the hard drive doesn't exist
101
- return nil if raw =~ /VERR_FILE_NOT_FOUND/
105
+ def find(id, raise_errors=false)
106
+ raw = Command.vboxmanage("showhdinfo", id)
102
107
 
103
108
  data = raw.split(/\n\n/).collect { |v| parse_block(v) }.find { |v| !v.nil? }
104
109
 
@@ -108,6 +113,9 @@ module VirtualBox
108
113
 
109
114
  # Return new object
110
115
  new(data)
116
+ rescue Exceptions::CommandFailedException
117
+ raise if raise_errors
118
+ nil
111
119
  end
112
120
  end
113
121
 
@@ -124,7 +132,7 @@ module VirtualBox
124
132
  # will be raised if the command failed.
125
133
  # @return [HardDrive] The new, cloned hard drive, or nil on failure.
126
134
  def clone(outputfile, format="VDI", raise_errors=false)
127
- raw = Command.vboxmanage("clonehd #{uuid} #{Command.shell_escape(outputfile)} --format #{format} --remember")
135
+ raw = Command.vboxmanage("clonehd", uuid, outputfile, "--format", format, "--remember")
128
136
  return nil unless raw =~ /UUID: (.+?)$/
129
137
 
130
138
  self.class.find($1.to_s)
@@ -159,7 +167,7 @@ module VirtualBox
159
167
  return false
160
168
  end
161
169
 
162
- raw = Command.vboxmanage("createhd --filename #{location} --size #{size} --format #{read_attribute(:format)} --remember")
170
+ raw = Command.vboxmanage("createhd", "--filename", location, "--size", size, "--format", read_attribute(:format), "--remember")
163
171
  return nil unless raw =~ /UUID: (.+?)$/
164
172
 
165
173
  # Just replace our attributes with the newly created ones. This also
@@ -201,7 +209,7 @@ module VirtualBox
201
209
  # will be raised if the command failed.
202
210
  # @return [Boolean] True if command was successful, false otherwise.
203
211
  def destroy(raise_errors=false)
204
- Command.vboxmanage("closemedium disk #{uuid} --delete")
212
+ Command.vboxmanage("closemedium", "disk", uuid, "--delete")
205
213
  true
206
214
  rescue Exceptions::CommandFailedException
207
215
  raise if raise_errors
@@ -143,7 +143,7 @@ module VirtualBox
143
143
  #
144
144
  # **This method typically won't be used except internally.**
145
145
  def save_attribute(key, value, vmname)
146
- Command.vboxmanage("modifyvm #{Command.shell_escape(vmname)} --#{key}#{@index} #{Command.shell_escape(value)}")
146
+ Command.vboxmanage("modifyvm", vmname, "--#{key}#{@index}", value)
147
147
  super
148
148
  end
149
149
  end
@@ -204,7 +204,7 @@ module VirtualBox
204
204
  # If this isn't a new record, we destroy it first
205
205
  destroy(raise_errors) if !new_record?
206
206
 
207
- Command.vboxmanage("sharedfolder add #{Command.shell_escape(parent.name)} --name #{Command.shell_escape(name)} --hostpath #{Command.shell_escape(hostpath)}")
207
+ Command.vboxmanage("sharedfolder", "add", parent.name, "--name", name, "--hostpath", hostpath)
208
208
  existing_record!
209
209
  clear_dirty!
210
210
 
@@ -233,7 +233,7 @@ module VirtualBox
233
233
  # one.
234
234
  name_value = name_changed? ? name_was : name
235
235
 
236
- Command.vboxmanage("sharedfolder remove #{Command.shell_escape(parent.name)} --name #{Command.shell_escape(name_value)}")
236
+ Command.vboxmanage("sharedfolder", "remove", parent.name, "--name", name_value)
237
237
  true
238
238
  rescue Exceptions::CommandFailedException
239
239
  raise if raise_errors
data/lib/virtualbox/vm.rb CHANGED
@@ -120,7 +120,7 @@ module VirtualBox
120
120
  #
121
121
  # @return [Array<VM>]
122
122
  def all
123
- raw = Command.vboxmanage("list vms")
123
+ raw = Command.vboxmanage("list", "vms")
124
124
  parse_vm_list(raw)
125
125
  end
126
126
 
@@ -141,7 +141,7 @@ module VirtualBox
141
141
  #
142
142
  # @return [VM] The newly imported virtual machine
143
143
  def import(source_path)
144
- raw = Command.vboxmanage("import #{Command.shell_escape(source_path)}")
144
+ raw = Command.vboxmanage("import", source_path)
145
145
  return nil unless raw
146
146
 
147
147
  find(parse_vm_name(raw))
@@ -154,7 +154,7 @@ module VirtualBox
154
154
  #
155
155
  # @return [String]
156
156
  def human_info(name)
157
- Command.vboxmanage("showvminfo #{Command.shell_escape(name)}")
157
+ Command.vboxmanage("showvminfo", name)
158
158
  end
159
159
 
160
160
  # Gets the VM info (machine readable) for a given VM and returns it
@@ -162,7 +162,7 @@ module VirtualBox
162
162
  #
163
163
  # @return [Hash] Parsed VM info.
164
164
  def raw_info(name)
165
- raw = Command.vboxmanage("showvminfo #{Command.shell_escape(name)} --machinereadable")
165
+ raw = Command.vboxmanage("showvminfo", name, "--machinereadable")
166
166
  parse_vm_info(raw)
167
167
  end
168
168
 
@@ -259,7 +259,7 @@ module VirtualBox
259
259
  #
260
260
  # **This method typically won't be used except internally.**
261
261
  def save_attribute(key, value)
262
- Command.vboxmanage("modifyvm #{Command.shell_escape(@original_name)} --#{key} #{Command.shell_escape(value.to_s)}")
262
+ Command.vboxmanage("modifyvm", @original_name, "--#{key}", value)
263
263
  super
264
264
  end
265
265
 
@@ -287,12 +287,13 @@ module VirtualBox
287
287
  # @option options [String] :eulafile (nil) License file
288
288
  def export(filename, options={}, raise_error=false)
289
289
  options = options.inject([]) do |acc, kv|
290
- acc.push("--#{kv[0]} #{Command.shell_escape(kv[1])}")
290
+ acc.push("--#{kv[0]}")
291
+ acc.push(kv[1])
291
292
  end
292
293
 
293
- options.unshift("--vsys 0") unless options.empty?
294
+ options.unshift("0").unshift("--vsys") unless options.empty?
294
295
 
295
- raw = Command.vboxmanage("export #{Command.shell_escape(@original_name)} -o #{Command.shell_escape(filename)} #{options.join(" ")}".strip)
296
+ raw = Command.vboxmanage("export", @original_name, "-o", filename, *options)
296
297
  true
297
298
  rescue Exceptions::CommandFailedException
298
299
  raise if raise_error
@@ -314,7 +315,7 @@ module VirtualBox
314
315
  # will be raised if the command failed.
315
316
  # @return [Boolean] True if command was successful, false otherwise.
316
317
  def start(mode=:gui, raise_errors=false)
317
- Command.vboxmanage("startvm #{Command.shell_escape(@original_name)} --type #{mode}")
318
+ Command.vboxmanage("startvm", @original_name, "--type", mode)
318
319
  true
319
320
  rescue Exceptions::CommandFailedException
320
321
  raise if raise_errors
@@ -364,7 +365,7 @@ module VirtualBox
364
365
  end
365
366
 
366
367
  # Saves the state of a VM and stops it. The VM can be resumed
367
- # again by calling "start" again.
368
+ # again by calling "{#start}" again.
368
369
  #
369
370
  # @param [Boolean] raise_errors If true, {Exceptions::CommandFailedException}
370
371
  # will be raised if the command failed.
@@ -373,6 +374,20 @@ module VirtualBox
373
374
  control(:savestate, raise_errors)
374
375
  end
375
376
 
377
+ # Discards any saved state on the current VM. The VM is not destroyed though
378
+ # and can still be started by calling {#start}.
379
+ #
380
+ # @param [Boolean] raise_errors If true, {Exceptions::CommandFailedException}
381
+ # will be raised if the command failed.
382
+ # @return [Boolean] True if command was successful, false otherwise.
383
+ def discard_state(raise_errors=false)
384
+ Command.vboxmanage("discardstate", @original_name)
385
+ true
386
+ rescue Exceptions::CommandFailedException
387
+ raise if raise_errors
388
+ false
389
+ end
390
+
376
391
  # Controls the virtual machine. This method is used by {#stop},
377
392
  # {#pause}, {#resume}, and {#save_state} to control the virtual machine.
378
393
  # Typically, you won't ever have to call this method and should
@@ -383,7 +398,7 @@ module VirtualBox
383
398
  # will be raised if the command failed.
384
399
  # @return [Boolean] True if command was successful, false otherwise.
385
400
  def control(command, raise_errors=false)
386
- Command.vboxmanage("controlvm #{Command.shell_escape(@original_name)} #{command}")
401
+ Command.vboxmanage("controlvm", @original_name, command)
387
402
  true
388
403
  rescue Exceptions::CommandFailedException
389
404
  raise if raise_errors
@@ -405,7 +420,7 @@ module VirtualBox
405
420
  # unregistering a VM
406
421
  super
407
422
 
408
- Command.vboxmanage("unregistervm #{Command.shell_escape(@original_name)} --delete")
423
+ Command.vboxmanage("unregistervm", @original_name, "--delete")
409
424
  end
410
425
 
411
426
  # Returns true if the virtual machine state is running
@@ -25,7 +25,7 @@ class AbstractModelTest < Test::Unit::TestCase
25
25
  end
26
26
 
27
27
  should "generate the proper inspect string" do
28
- assert_equal "#<AbstractModelTest::FakeModel :foo=nil, :bar=nil, :foos=..., :bars=...>", @model.inspect
28
+ assert_equal "#<AbstractModelTest::FakeModel :bar=nil, :bars=..., :foo=nil, :foos=...>", @model.inspect
29
29
  end
30
30
  end
31
31
 
@@ -149,7 +149,7 @@ class AttachedDeviceTest < Test::Unit::TestCase
149
149
  end
150
150
 
151
151
  should "call the proper vboxcommand" do
152
- VirtualBox::Command.expects(:vboxmanage).with("storageattach #{@vm.name} --storagectl #{VirtualBox::Command.shell_escape(@caller.name)} --port #{@ad.port} --device 0 --type #{@image.image_type} --medium #{@ad.medium}")
152
+ VirtualBox::Command.expects(:vboxmanage).with("storageattach", @vm.name, "--storagectl", @caller.name, "--port", @ad.port, "--device", "0", "--type", @image.image_type, "--medium", @ad.medium)
153
153
  @ad.save
154
154
  end
155
155
 
@@ -229,21 +229,13 @@ class AttachedDeviceTest < Test::Unit::TestCase
229
229
  VirtualBox::AttachedDevice.destroy_relationship(self, [obj_one, obj_two], "HELLO")
230
230
  end
231
231
 
232
- should "shell escape VM name and storage controller name" do
233
- shell_seq = sequence("shell_seq")
234
- VirtualBox::Command.expects(:shell_escape).with(@vm.name).in_sequence(shell_seq)
235
- VirtualBox::Command.expects(:shell_escape).with(@caller.name).in_sequence(shell_seq)
236
- VirtualBox::Command.expects(:vboxmanage).in_sequence(shell_seq)
237
- @value.destroy
238
- end
239
-
240
232
  should "destroy with the specified port if set" do
241
- VirtualBox::Command.expects(:vboxmanage).with("storageattach #{VirtualBox::Command.shell_escape(@vm.name)} --storagectl #{VirtualBox::Command.shell_escape(@caller.name)} --port 80 --device 0 --medium none")
233
+ VirtualBox::Command.expects(:vboxmanage).with("storageattach", @vm.name, "--storagectl", @caller.name, "--port", 80, "--device", "0", "--medium", "none")
242
234
  @value.destroy(:port => 80)
243
235
  end
244
236
 
245
237
  should "destroy with the default port if not other port is specified" do
246
- VirtualBox::Command.expects(:vboxmanage).with("storageattach #{VirtualBox::Command.shell_escape(@vm.name)} --storagectl #{VirtualBox::Command.shell_escape(@caller.name)} --port #{@value.port} --device 0 --medium none")
238
+ VirtualBox::Command.expects(:vboxmanage).with("storageattach", @vm.name, "--storagectl", @caller.name, "--port", @value.port, "--device", "0", "--medium", "none")
247
239
  @value.destroy
248
240
  end
249
241
 
@@ -43,6 +43,21 @@ class CommandTest < Test::Unit::TestCase
43
43
  VirtualBox::Command.vboxmanage("foo")
44
44
  }
45
45
  end
46
+
47
+ should "call vboxmanage with multiple arguments" do
48
+ VirtualBox::Command.expects(:execute).with("VBoxManage -q foo bar baz --bak bax")
49
+ VirtualBox::Command.vboxmanage("foo", "bar", "baz", "--bak", "bax")
50
+ end
51
+
52
+ should "shell escape all arguments" do
53
+ VirtualBox::Command.expects(:execute).with("VBoxManage -q foo\\ bar baz another\\ string")
54
+ VirtualBox::Command.vboxmanage("foo bar", "baz", "another string")
55
+ end
56
+
57
+ should "convert arguments to strings" do
58
+ VirtualBox::Command.expects(:execute).with("VBoxManage -q isastring 8")
59
+ VirtualBox::Command.vboxmanage(:isastring, 8)
60
+ end
46
61
  end
47
62
 
48
63
  context "testing command results" do
@@ -15,7 +15,7 @@ class DVDTest < Test::Unit::TestCase
15
15
  end
16
16
 
17
17
  should "call vboxmanage to destroy it" do
18
- VirtualBox::Command.expects(:vboxmanage).with("closemedium dvd #{@dvd.uuid} --delete")
18
+ VirtualBox::Command.expects(:vboxmanage).with("closemedium", "dvd", @dvd.uuid, "--delete")
19
19
  assert @dvd.destroy
20
20
  end
21
21
 
@@ -69,7 +69,7 @@ Accessible: yes
69
69
  Usage: TestJeOS (UUID: 3d0f87b4-50f7-4fc5-ad89-93375b1b32a3)
70
70
  valid
71
71
 
72
- VirtualBox::Command.expects(:vboxmanage).with("list dvds").returns(@valid)
72
+ VirtualBox::Command.expects(:vboxmanage).with("list", "dvds").returns(@valid)
73
73
  end
74
74
 
75
75
  should "return an array of DVD objects" do
@@ -44,7 +44,7 @@ raw
44
44
 
45
45
  context "populating" do
46
46
  should "call VBoxManage for the caller" do
47
- VirtualBox::Command.expects(:vboxmanage).with("getextradata #{@caller.name} enumerate").returns(@raw)
47
+ VirtualBox::Command.expects(:vboxmanage).with("getextradata", @caller.name, "enumerate").returns(@raw)
48
48
  VirtualBox::ExtraData.populate_relationship(@caller, {})
49
49
  end
50
50
 
@@ -75,7 +75,7 @@ raw
75
75
  end
76
76
 
77
77
  should "call the proper vbox command" do
78
- VirtualBox::Command.expects(:vboxmanage).with("setextradata global foo")
78
+ VirtualBox::Command.expects(:vboxmanage).with("setextradata", "global", "foo")
79
79
  assert @ed.delete(@key)
80
80
  end
81
81
 
@@ -111,7 +111,7 @@ raw
111
111
  end
112
112
 
113
113
  should "call the proper vbox command" do
114
- VirtualBox::Command.expects(:vboxmanage).with("setextradata global foo BAR")
114
+ VirtualBox::Command.expects(:vboxmanage).with("setextradata", "global", "foo", "BAR")
115
115
  assert @ed.save
116
116
  end
117
117
 
@@ -154,7 +154,7 @@ raw
154
154
  context "global extra data" do
155
155
  setup do
156
156
  get_seq = sequence("get_seq")
157
- VirtualBox::Command.expects(:vboxmanage).with("getextradata global enumerate").once.in_sequence(get_seq)
157
+ VirtualBox::Command.expects(:vboxmanage).with("getextradata", "global", "enumerate").once.in_sequence(get_seq)
158
158
  VirtualBox::ExtraData.expects(:parse_kv_pairs).returns(@ed).once.in_sequence(get_seq)
159
159
  @global = VirtualBox::ExtraData.global(true)
160
160
  end
@@ -16,7 +16,7 @@ In use by VMs: FooVM (UUID: 696249ad-00b6-4087-b47f-9b82629efc31)
16
16
  Location: /Users/mitchellh/Library/VirtualBox/HardDisks/foo.vdi
17
17
  raw
18
18
  @name = "foo"
19
- VirtualBox::Command.stubs(:vboxmanage).with("showhdinfo #{@name}").returns(@find_raw)
19
+ VirtualBox::Command.stubs(:vboxmanage).with("showhdinfo", @name).returns(@find_raw)
20
20
  end
21
21
 
22
22
  context "validations" do
@@ -48,7 +48,7 @@ raw
48
48
  end
49
49
 
50
50
  should "call vboxmanage to destroy it" do
51
- VirtualBox::Command.expects(:vboxmanage).with("closemedium disk #{@hd.uuid} --delete")
51
+ VirtualBox::Command.expects(:vboxmanage).with("closemedium", "disk", @hd.uuid, "--delete")
52
52
  assert @hd.destroy
53
53
  end
54
54
 
@@ -68,7 +68,7 @@ raw
68
68
  context "cloning a hard drive" do
69
69
  setup do
70
70
  @hd = VirtualBox::HardDrive.find(@name)
71
- VirtualBox::Command.stubs(:vboxmanage).with("clonehd #{@hd.uuid} bar --format VDI --remember").returns(@find_raw)
71
+ VirtualBox::Command.stubs(:vboxmanage).with("clonehd", @hd.uuid, "bar", "--format", "VDI", "--remember").returns(@find_raw)
72
72
  end
73
73
 
74
74
  should "call vboxmanage with the clone command" do
@@ -130,7 +130,7 @@ raw
130
130
  end
131
131
 
132
132
  should "call createhd" do
133
- VirtualBox::Command.expects(:vboxmanage).with("createhd --filename #{@location} --size #{@size} --format #{@format} --remember")
133
+ VirtualBox::Command.expects(:vboxmanage).with("createhd", "--filename", @location, "--size", @size, "--format", @format, "--remember")
134
134
  @hd.save
135
135
  end
136
136
 
@@ -172,7 +172,7 @@ raw
172
172
 
173
173
  context "finding a single hard drive" do
174
174
  should "parse proper fields" do
175
- VirtualBox::Command.expects(:vboxmanage).with("showhdinfo #{@name}").returns(@find_raw)
175
+ VirtualBox::Command.expects(:vboxmanage).with("showhdinfo", @name).returns(@find_raw)
176
176
 
177
177
  @expected = {
178
178
  :uuid => "11dedd14-57a1-4bdb-adeb-dd1d67f066e1",
@@ -190,12 +190,19 @@ raw
190
190
  end
191
191
 
192
192
  should "return nil if finding a non-existent hard drive" do
193
- VirtualBox::Command.expects(:vboxmanage).with("showhdinfo 12").returns("UH OH (VERR_FILE_NOT_FOUND)")
193
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
194
194
 
195
195
  assert_nothing_raised do
196
196
  assert_nil VirtualBox::HardDrive.find(12)
197
197
  end
198
198
  end
199
+
200
+ should "raise an exception if flag is set" do
201
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
202
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
203
+ VirtualBox::HardDrive.find(12, true)
204
+ }
205
+ end
199
206
  end
200
207
 
201
208
  context "retrieving all hard drives" do
@@ -223,7 +230,7 @@ Type: normal
223
230
  Usage: foo (UUID: 8710d3db-d96a-46ed-9004-59fa891fda90)
224
231
  valid
225
232
 
226
- VirtualBox::Command.expects(:vboxmanage).with("list hdds").returns(@valid)
233
+ VirtualBox::Command.stubs(:vboxmanage).with("list", "hdds").returns(@valid)
227
234
 
228
235
  @hd = mock("hd")
229
236
  @hd.stubs(:is_a?).with(VirtualBox::HardDrive).returns(true)
@@ -236,5 +243,27 @@ valid
236
243
 
237
244
  result.each { |v| assert v.is_a?(VirtualBox::HardDrive) }
238
245
  end
246
+
247
+ should "forward the raise_error flag to find" do
248
+ VirtualBox::HardDrive.expects(:find).with(anything, true).raises(VirtualBox::Exceptions::CommandFailedException)
249
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
250
+ VirtualBox::HardDrive.all(true)
251
+ }
252
+ end
253
+
254
+ should "return false if an error occured" do
255
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
256
+
257
+ assert_nothing_raised do
258
+ assert !VirtualBox::HardDrive.all
259
+ end
260
+ end
261
+
262
+ should "raise an exception if flag is set" do
263
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
264
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
265
+ VirtualBox::HardDrive.all(true)
266
+ }
267
+ end
239
268
  end
240
269
  end
@@ -25,7 +25,7 @@ raw
25
25
  end
26
26
 
27
27
  should "use the vmname strung through the save" do
28
- VirtualBox::Command.expects(:vboxmanage).with("modifyvm #{@vmname} --nic1 foo")
28
+ VirtualBox::Command.expects(:vboxmanage).with("modifyvm", @vmname, "--nic1", "foo")
29
29
 
30
30
  nic = @nic[0]
31
31
  nic.nic = "foo"
@@ -33,7 +33,7 @@ raw
33
33
  end
34
34
 
35
35
  should "use the proper index" do
36
- VirtualBox::Command.expects(:vboxmanage).with("modifyvm #{@vmname} --nic2 far")
36
+ VirtualBox::Command.expects(:vboxmanage).with("modifyvm", @vmname, "--nic2", "far")
37
37
 
38
38
  nic = @nic[1]
39
39
  nic.nic = "far"
@@ -41,7 +41,7 @@ raw
41
41
  end
42
42
 
43
43
  should "save the nictype" do
44
- VirtualBox::Command.expects(:vboxmanage).with("modifyvm #{@vmname} --nictype1 ZOO")
44
+ VirtualBox::Command.expects(:vboxmanage).with("modifyvm", @vmname, "--nictype1", "ZOO")
45
45
 
46
46
  nic = @nic[0]
47
47
  nic.nictype = "ZOO"
@@ -96,7 +96,7 @@ class SharedFolderTest < Test::Unit::TestCase
96
96
  end
97
97
 
98
98
  should "call the proper vboxcommand" do
99
- VirtualBox::Command.expects(:vboxmanage).with("sharedfolder add #{@caller.name} --name #{@sf.name} --hostpath #{@sf.hostpath}")
99
+ VirtualBox::Command.expects(:vboxmanage).with("sharedfolder", "add", @caller.name, "--name", @sf.name, "--hostpath", @sf.hostpath)
100
100
  assert @sf.save
101
101
  end
102
102
 
@@ -175,15 +175,7 @@ class SharedFolderTest < Test::Unit::TestCase
175
175
  end
176
176
 
177
177
  should "call the proper command" do
178
- VirtualBox::Command.expects(:vboxmanage).with("sharedfolder remove #{@caller.name} --name #{@value.name}").once
179
- assert @value.destroy
180
- end
181
-
182
- should "shell escape VM name and storage controller name" do
183
- shell_seq = sequence("shell_seq")
184
- VirtualBox::Command.expects(:shell_escape).with(@caller.name).in_sequence(shell_seq)
185
- VirtualBox::Command.expects(:shell_escape).with(@value.name).in_sequence(shell_seq)
186
- VirtualBox::Command.expects(:vboxmanage).in_sequence(shell_seq)
178
+ VirtualBox::Command.expects(:vboxmanage).with("sharedfolder", "remove", @caller.name, "--name", @value.name).once
187
179
  assert @value.destroy
188
180
  end
189
181
 
@@ -201,10 +193,7 @@ class SharedFolderTest < Test::Unit::TestCase
201
193
 
202
194
  should "use the old name if it was changed" do
203
195
  @value.name = "DIFFERENT"
204
- shell_seq = sequence("shell_seq")
205
- VirtualBox::Command.expects(:shell_escape).with(@caller.name).in_sequence(shell_seq)
206
- VirtualBox::Command.expects(:shell_escape).with(@value.name_was).in_sequence(shell_seq)
207
- VirtualBox::Command.expects(:vboxmanage).in_sequence(shell_seq)
196
+ VirtualBox::Command.expects(:vboxmanage)
208
197
  assert @value.destroy
209
198
  end
210
199
  end
@@ -81,11 +81,11 @@ showvminfo
81
81
  end
82
82
 
83
83
  def create_vm
84
- VirtualBox::Command.expects(:vboxmanage).with("showvminfo #{@name} --machinereadable").returns(@raw)
85
- VirtualBox::Command.expects(:vboxmanage).with("showvminfo #{@name}").returns("")
86
- VirtualBox::Command.expects(:vboxmanage).with("list hdds").returns("")
87
- VirtualBox::Command.expects(:vboxmanage).with("list dvds").returns("")
88
- VirtualBox::Command.expects(:vboxmanage).with("getextradata #{@name} enumerate").returns("")
84
+ VirtualBox::Command.expects(:vboxmanage).with("showvminfo", @name, "--machinereadable").returns(@raw)
85
+ VirtualBox::Command.expects(:vboxmanage).with("showvminfo", @name).returns("")
86
+ VirtualBox::Command.expects(:vboxmanage).with("list", "hdds").returns("")
87
+ VirtualBox::Command.expects(:vboxmanage).with("list", "dvds").returns("")
88
+ VirtualBox::Command.expects(:vboxmanage).with("getextradata", @name, "enumerate").returns("")
89
89
  vm = VirtualBox::VM.find(@name)
90
90
  assert vm
91
91
  vm
@@ -93,14 +93,9 @@ showvminfo
93
93
 
94
94
  context "human readable info" do
95
95
  should "not pass --machinereadable into the showvminfo command" do
96
- VirtualBox::Command.expects(:vboxmanage).with("showvminfo #{@name}").once
96
+ VirtualBox::Command.expects(:vboxmanage).with("showvminfo", @name).once
97
97
  VirtualBox::VM.human_info(@name)
98
98
  end
99
-
100
- should "shell escape parameter" do
101
- VirtualBox::Command.expects(:vboxmanage).with("showvminfo hello\\ world").once
102
- VirtualBox::VM.human_info("hello world")
103
- end
104
99
  end
105
100
 
106
101
  context "reading the VM state" do
@@ -147,17 +142,17 @@ showvminfo
147
142
  end
148
143
 
149
144
  should "export the VM with no options if none are passed" do
150
- VirtualBox::Command.expects(:vboxmanage).with("export #{@name} -o foo")
145
+ VirtualBox::Command.expects(:vboxmanage).with("export", @name, "-o", "foo")
151
146
  @vm.export("foo")
152
147
  end
153
148
 
154
149
  should "export the VM with specified options" do
155
- VirtualBox::Command.expects(:vboxmanage).with("export #{@name} -o foo --vsys 0 --foo bar")
150
+ VirtualBox::Command.expects(:vboxmanage).with("export", @name, "-o", "foo", "--vsys", "0", "--foo", :bar)
156
151
  @vm.export("foo", :foo => :bar)
157
152
  end
158
153
 
159
154
  should "shell escape all the options" do
160
- VirtualBox::Command.expects(:vboxmanage).with("export #{@name} -o foo --vsys 0 --foo a\\ space")
155
+ VirtualBox::Command.expects(:vboxmanage).with("export", @name, "-o", "foo", "--vsys", "0", "--foo", "a space")
161
156
  @vm.export("foo", :foo => "a space")
162
157
  end
163
158
 
@@ -185,14 +180,8 @@ showvminfo
185
180
  end
186
181
 
187
182
  context "control method" do
188
- should "shell escape the vm name" do
189
- VirtualBox::Command.expects(:vboxmanage).with("controlvm hello\\ world FOO")
190
- @vm.instance_variable_set(:@original_name, "hello world")
191
- assert @vm.control(:FOO)
192
- end
193
-
194
183
  should "run the given command when 'control' is called" do
195
- VirtualBox::Command.expects(:vboxmanage).with("controlvm #{@name} foo")
184
+ VirtualBox::Command.expects(:vboxmanage).with("controlvm", @name, :foo)
196
185
  assert @vm.control(:foo)
197
186
  end
198
187
 
@@ -210,7 +199,7 @@ showvminfo
210
199
  end
211
200
 
212
201
  should "start a VM with the given type" do
213
- VirtualBox::Command.expects(:vboxmanage).with("startvm #{@name} --type FOO")
202
+ VirtualBox::Command.expects(:vboxmanage).with("startvm", @name, "--type", :FOO)
214
203
  assert @vm.start(:FOO)
215
204
  end
216
205
 
@@ -219,12 +208,6 @@ showvminfo
219
208
  assert !@vm.start
220
209
  end
221
210
 
222
- should "shell escape the VM name on start" do
223
- VirtualBox::Command.expects(:vboxmanage).with("startvm hello\\ world --type FOO")
224
- @vm.instance_variable_set(:@original_name, "hello world")
225
- assert @vm.start(:FOO)
226
- end
227
-
228
211
  should "raise an exception if start fails and flag is set" do
229
212
  VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
230
213
  assert_raises(VirtualBox::Exceptions::CommandFailedException) {
@@ -232,7 +215,7 @@ showvminfo
232
215
  }
233
216
  end
234
217
 
235
- should "stop a VM with a 'poweroff'" do
218
+ should "stop a VM with a 'acpipowerbutton'" do
236
219
  @vm.expects(:control).with(:acpipowerbutton, false).returns(true)
237
220
  assert @vm.shutdown
238
221
  end
@@ -256,6 +239,23 @@ showvminfo
256
239
  @vm.expects(:control).with(:savestate, false).returns(true)
257
240
  assert @vm.save_state
258
241
  end
242
+
243
+ should "discard a saved state of a VM" do
244
+ VirtualBox::Command.expects(:vboxmanage).with("discardstate", @name)
245
+ assert @vm.discard_state
246
+ end
247
+
248
+ should "return false if discarding state failed" do
249
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
250
+ assert !@vm.discard_state
251
+ end
252
+
253
+ should "raise an exception if discarding state fails and flag is set" do
254
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
255
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
256
+ @vm.discard_state(true)
257
+ }
258
+ end
259
259
  end
260
260
 
261
261
  context "destroying" do
@@ -263,17 +263,10 @@ showvminfo
263
263
  @vm = create_vm
264
264
  end
265
265
 
266
- should "shell escape the name" do
267
- VirtualBox::StorageController.expects(:destroy_relationship)
268
- VirtualBox::Command.expects(:vboxmanage).with("unregistervm hello\\ world --delete")
269
- @vm.instance_variable_set(:@original_name, "hello world")
270
- @vm.destroy
271
- end
272
-
273
266
  should "destroy all storage controllers before destroying VM" do
274
267
  destroy_seq = sequence("destroy_seq")
275
268
  VirtualBox::StorageController.expects(:destroy_relationship).in_sequence(destroy_seq)
276
- VirtualBox::Command.expects(:vboxmanage).with("unregistervm #{@name} --delete").in_sequence(destroy_seq)
269
+ VirtualBox::Command.expects(:vboxmanage).with("unregistervm", @name, "--delete").in_sequence(destroy_seq)
277
270
  @vm.destroy
278
271
  end
279
272
  end
@@ -288,7 +281,7 @@ raw
288
281
 
289
282
  should "list VMs then parse them" do
290
283
  all_seq = sequence("all")
291
- VirtualBox::Command.expects(:vboxmanage).with("list vms").returns(@raw).in_sequence(all_seq)
284
+ VirtualBox::Command.expects(:vboxmanage).with("list", "vms").returns(@raw).in_sequence(all_seq)
292
285
  VirtualBox::VM.expects(:parse_vm_list).with(@raw).in_sequence(all_seq)
293
286
  VirtualBox::VM.all
294
287
  end
@@ -331,7 +324,7 @@ raw
331
324
  end
332
325
 
333
326
  should "attempt to find the imported VM" do
334
- VirtualBox::Command.expects(:vboxmanage).with("import whatever").returns(@raw)
327
+ VirtualBox::Command.expects(:vboxmanage).with("import", "whatever").returns(@raw)
335
328
  VirtualBox::VM.expects(:find).with("Base_1").once
336
329
  VirtualBox::VM.import("whatever")
337
330
  end
@@ -368,39 +361,17 @@ raw
368
361
  end
369
362
 
370
363
  should "save only the attributes which saved" do
371
- VirtualBox::Command.expects(:vboxmanage).with("modifyvm #{@name} --ostype Zubuntu")
364
+ VirtualBox::Command.expects(:vboxmanage).with("modifyvm", @name, "--ostype", "Zubuntu")
372
365
 
373
366
  @vm.ostype = "Zubuntu"
374
367
  assert @vm.save
375
368
  end
376
369
 
377
- should "shell escape saved values" do
378
- VirtualBox::Command.expects(:vboxmanage).with("modifyvm #{@name} --ostype My\\ Value")
379
-
380
- @vm.ostype = "My Value"
381
- assert @vm.save
382
- end
383
-
384
- should "shell escape the string value of a value" do
385
- VirtualBox::Command.expects(:vboxmanage).with("modifyvm #{@name} --memory 400")
386
-
387
- @vm.memory = 400
388
- assert_nothing_raised { @vm.save }
389
- end
390
-
391
- should "shell escape the VM name when saving an attribute" do
392
- VirtualBox::Command.expects(:vboxmanage).with("modifyvm hello\\ world --memory 400")
393
-
394
- @vm.memory = 400
395
- @vm.instance_variable_set(:@original_name, "hello world")
396
- assert @vm.save
397
- end
398
-
399
370
  should "save name first if changed, then following values should modify new VM" do
400
371
  save_seq = sequence("save_seq")
401
372
  new_name = "foo2"
402
- VirtualBox::Command.expects(:vboxmanage).with("modifyvm #{@name} --name #{new_name}").in_sequence(save_seq)
403
- VirtualBox::Command.expects(:vboxmanage).with("modifyvm #{new_name} --ostype Zubuntu").in_sequence(save_seq)
373
+ VirtualBox::Command.expects(:vboxmanage).with("modifyvm", @name, "--name", new_name).in_sequence(save_seq)
374
+ VirtualBox::Command.expects(:vboxmanage).with("modifyvm", new_name, "--ostype", "Zubuntu").in_sequence(save_seq)
404
375
 
405
376
  @vm.name = new_name
406
377
  @vm.ostype = "Zubuntu"
@@ -427,17 +398,12 @@ raw
427
398
  }
428
399
 
429
400
  command_seq = sequence("command_seq)")
430
- VirtualBox::Command.expects(:vboxmanage).with("showvminfo #{@name} --machinereadable").returns(@raw).in_sequence(command_seq)
431
- VirtualBox::Command.expects(:vboxmanage).with(anything).returns("").at_least(0).in_sequence(command_seq)
401
+ VirtualBox::Command.expects(:vboxmanage).with("showvminfo", @name, "--machinereadable").returns(@raw).in_sequence(command_seq)
402
+ VirtualBox::Command.expects(:vboxmanage).returns("").at_least(0).in_sequence(command_seq)
432
403
  @vm = VirtualBox::VM.find(@name)
433
404
  assert @vm
434
405
  end
435
406
 
436
- should "shell escape the VM name" do
437
- VirtualBox::Command.expects(:vboxmanage).with("showvminfo hello\\ world --machinereadable").returns(@raw)
438
- assert VirtualBox::VM.find("hello world")
439
- end
440
-
441
407
  should "return a VM object with proper attributes" do
442
408
  @expected.each do |k,v|
443
409
  assert_equal v, @vm.read_attribute(k)
data/virtualbox.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{virtualbox}
8
- s.version = "0.4.2"
8
+ s.version = "0.4.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mitchell Hashimoto"]
12
- s.date = %q{2010-01-31}
12
+ s.date = %q{2010-02-02}
13
13
  s.description = %q{Create and modify virtual machines in VirtualBox using pure ruby.}
14
14
  s.email = %q{mitchell.hashimoto@gmail.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: virtualbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitchell Hashimoto
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-31 00:00:00 -08:00
12
+ date: 2010-02-02 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15