virtualbox 0.4.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. data/.gitignore +3 -3
  2. data/Gemfile +8 -8
  3. data/Rakefile +2 -0
  4. data/Readme.md +11 -2
  5. data/VERSION +1 -1
  6. data/docs/WhatsNew.md +40 -24
  7. data/lib/virtualbox.rb +9 -0
  8. data/lib/virtualbox/abstract_model.rb +80 -10
  9. data/lib/virtualbox/abstract_model/attributable.rb +61 -1
  10. data/lib/virtualbox/abstract_model/relatable.rb +88 -6
  11. data/lib/virtualbox/attached_device.rb +18 -10
  12. data/lib/virtualbox/command.rb +13 -0
  13. data/lib/virtualbox/dvd.rb +35 -0
  14. data/lib/virtualbox/exceptions.rb +1 -0
  15. data/lib/virtualbox/extra_data.rb +10 -21
  16. data/lib/virtualbox/global.rb +126 -0
  17. data/lib/virtualbox/hard_drive.rb +33 -9
  18. data/lib/virtualbox/image.rb +2 -3
  19. data/lib/virtualbox/media.rb +19 -0
  20. data/lib/virtualbox/nic.rb +28 -67
  21. data/lib/virtualbox/shared_folder.rb +7 -12
  22. data/lib/virtualbox/storage_controller.rb +8 -36
  23. data/lib/virtualbox/system_property.rb +55 -0
  24. data/lib/virtualbox/usb.rb +72 -0
  25. data/lib/virtualbox/vm.rb +126 -25
  26. data/test/test_helper.rb +118 -12
  27. data/test/virtualbox/abstract_model/attributable_test.rb +55 -5
  28. data/test/virtualbox/abstract_model/relatable_test.rb +66 -4
  29. data/test/virtualbox/abstract_model_test.rb +140 -8
  30. data/test/virtualbox/attached_device_test.rb +10 -7
  31. data/test/virtualbox/command_test.rb +13 -0
  32. data/test/virtualbox/dvd_test.rb +50 -28
  33. data/test/virtualbox/extra_data_test.rb +11 -51
  34. data/test/virtualbox/global_test.rb +78 -0
  35. data/test/virtualbox/hard_drive_test.rb +34 -57
  36. data/test/virtualbox/image_test.rb +0 -5
  37. data/test/virtualbox/nic_test.rb +11 -64
  38. data/test/virtualbox/shared_folder_test.rb +5 -5
  39. data/test/virtualbox/storage_controller_test.rb +15 -30
  40. data/test/virtualbox/system_property_test.rb +71 -0
  41. data/test/virtualbox/usb_test.rb +35 -0
  42. data/test/virtualbox/vm_test.rb +62 -121
  43. data/virtualbox.gemspec +15 -2
  44. metadata +23 -4
@@ -17,6 +17,9 @@ class AttachedDeviceTest < Test::Unit::TestCase
17
17
 
18
18
  # Stub execute to make sure nothing actually happens
19
19
  VirtualBox::Command.stubs(:execute).returns('')
20
+
21
+ # Stub the config
22
+ VirtualBox::Global.stubs(:config).returns(mock_xml_doc)
20
23
  end
21
24
 
22
25
  context "validations" do
@@ -71,7 +74,7 @@ class AttachedDeviceTest < Test::Unit::TestCase
71
74
 
72
75
  context "saving an existing device" do
73
76
  setup do
74
- @value = VirtualBox::AttachedDevice.populate_relationship(@caller, @data)
77
+ @value = VirtualBox::AttachedDevice.populate_relationship(@caller, mock_xml_doc)
75
78
  @value = @value[0]
76
79
  @value.image = VirtualBox::DVD.empty_drive
77
80
  assert @value.changed?
@@ -210,7 +213,7 @@ class AttachedDeviceTest < Test::Unit::TestCase
210
213
 
211
214
  context "destroying" do
212
215
  setup do
213
- @value = VirtualBox::AttachedDevice.populate_relationship(@caller, @data)
216
+ @value = VirtualBox::AttachedDevice.populate_relationship(@caller, mock_xml_doc)
214
217
  @value = @value[0]
215
218
 
216
219
  @image = mock("image")
@@ -278,7 +281,7 @@ class AttachedDeviceTest < Test::Unit::TestCase
278
281
 
279
282
  context "populating relationships" do
280
283
  setup do
281
- @value = VirtualBox::AttachedDevice.populate_relationship(@caller, @data)
284
+ @value = VirtualBox::AttachedDevice.populate_relationship(@caller, mock_xml_doc)
282
285
  end
283
286
 
284
287
  should "create the correct amount of objects" do
@@ -288,13 +291,13 @@ class AttachedDeviceTest < Test::Unit::TestCase
288
291
  should "create objects with proper values" do
289
292
  obj = @value[0]
290
293
  assert_equal "none", obj.medium
291
- assert_equal "322f79fd-7da6-416f-a16f-e70066ccf165", obj.uuid
292
- assert_equal 0, obj.port
294
+ assert_equal "2c16dd48-4cf1-497e-98fa-84ed55cfe71f", obj.uuid
295
+ assert_equal "0", obj.port
293
296
 
294
297
  obj = @value[1]
295
- assert_equal "none", obj.medium
298
+ assert_equal "emptydrive", obj.medium
296
299
  assert_nil obj.uuid
297
- assert_equal 1, obj.port
300
+ assert_equal "1", obj.port
298
301
  end
299
302
  end
300
303
  end
@@ -1,6 +1,19 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
2
 
3
3
  class CommandTest < Test::Unit::TestCase
4
+ context "parsing XML" do
5
+ should "open the file, parse it, and close the file" do
6
+ arg = "foo"
7
+ file = mock("file")
8
+ seq = sequence("seq")
9
+ File.expects(:open).with(arg, "r").returns(file).in_sequence(seq)
10
+ Nokogiri.expects(:XML).in_sequence(seq)
11
+ file.expects(:close).in_sequence(seq)
12
+
13
+ VirtualBox::Command.parse_xml(arg)
14
+ end
15
+ end
16
+
4
17
  context "shell escaping" do
5
18
  should "convert value to string" do
6
19
  assert_nothing_raised do
@@ -8,6 +8,7 @@ class DVDTest < Test::Unit::TestCase
8
8
  context "destroying a dvd" do
9
9
  setup do
10
10
  @dvd = VirtualBox::DVD.new
11
+ VirtualBox::DVD.reloaded!
11
12
  end
12
13
 
13
14
  should "return false if attempting to destroy an empty drive" do
@@ -30,6 +31,11 @@ class DVDTest < Test::Unit::TestCase
30
31
  @dvd.destroy(true)
31
32
  }
32
33
  end
34
+
35
+ should "mark the global class for reloading" do
36
+ VirtualBox::Global.expects(:reload!)
37
+ assert @dvd.destroy
38
+ end
33
39
  end
34
40
 
35
41
  context "empty drive" do
@@ -46,19 +52,34 @@ class DVDTest < Test::Unit::TestCase
46
52
 
47
53
  context "retrieving all dvds" do
48
54
  setup do
49
- @expectations = {
50
- "d3252617-8176-4f8c-9d73-1c9c82b23960" => {
51
- :location => "/Users/mitchellh/Downloads/jeos-8.04.3-jeos-i386.iso",
52
- :accessible => "yes"
53
- },
54
-
55
- "4a08f52c-bca3-4908-8da4-4f48aaa4ebba" => {
56
- :location => "/Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso",
57
- :accessible => "yes"
55
+ @media = mock("media")
56
+ @media.expects(:dvds).returns([])
57
+ @global = mock("global")
58
+ @global.expects(:media).returns(@media)
59
+ end
60
+
61
+ should "return an array of DVD objects" do
62
+ VirtualBox::Global.expects(:global).returns(@global)
63
+ result = VirtualBox::DVD.all
64
+ assert result.is_a?(Array)
65
+ end
66
+ end
67
+
68
+ context "retrieving all dvds by command" do
69
+ setup do
70
+ @expectations = {
71
+ "d3252617-8176-4f8c-9d73-1c9c82b23960" => {
72
+ :location => "/Users/mitchellh/Downloads/jeos-8.04.3-jeos-i386.iso",
73
+ :accessible => "yes"
74
+ },
75
+
76
+ "4a08f52c-bca3-4908-8da4-4f48aaa4ebba" => {
77
+ :location => "/Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso",
78
+ :accessible => "yes"
79
+ }
58
80
  }
59
- }
60
81
 
61
- @valid = <<-valid
82
+ @valid = <<-valid
62
83
  UUID: d3252617-8176-4f8c-9d73-1c9c82b23960
63
84
  Path: /Users/mitchellh/Downloads/jeos-8.04.3-jeos-i386.iso
64
85
  Accessible: yes
@@ -69,29 +90,30 @@ Accessible: yes
69
90
  Usage: TestJeOS (UUID: 3d0f87b4-50f7-4fc5-ad89-93375b1b32a3)
70
91
  valid
71
92
 
72
- VirtualBox::Command.expects(:vboxmanage).with("list", "dvds").returns(@valid)
73
- end
93
+ VirtualBox::Command.expects(:vboxmanage).with("list", "dvds").returns(@valid).once
94
+ end
74
95
 
75
- should "return an array of DVD objects" do
76
- result = VirtualBox::DVD.all
77
- assert result.is_a?(Array)
96
+ should "return an array of DVD objects" do
97
+ result = VirtualBox::DVD.all_from_command
98
+ assert result.is_a?(Array)
99
+ assert_equal @expectations.length, result.length
78
100
 
79
- result.each { |v| assert v.is_a?(VirtualBox::DVD) }
80
- end
101
+ result.each { |v| assert v.is_a?(VirtualBox::DVD) }
102
+ end
81
103
 
82
- should "return the proper results" do
83
- result = VirtualBox::DVD.all
84
- assert result.is_a?(Array)
85
- assert_equal @expectations.length, result.length
104
+ should "return the proper results" do
105
+ result = VirtualBox::DVD.all_from_command
106
+ assert result.is_a?(Array)
107
+ assert_equal @expectations.length, result.length
86
108
 
87
- result.each do |image|
88
- expected_image = @expectations[image.uuid]
89
- assert expected_image
109
+ result.each do |image|
110
+ expected_image = @expectations[image.uuid]
111
+ assert expected_image
90
112
 
91
- expected_image.each do |k,v|
92
- assert_equal v, image.read_attribute(k)
113
+ expected_image.each do |k,v|
114
+ assert_equal v, image.read_attribute(k)
115
+ end
93
116
  end
94
117
  end
95
118
  end
96
- end
97
119
  end
@@ -38,23 +38,11 @@ raw
38
38
  setup do
39
39
  @caller = mock("caller")
40
40
  @caller.stubs(:name).returns("foocaller")
41
-
42
- VirtualBox::Command.stubs(:vboxmanage).returns(@raw)
43
41
  end
44
42
 
45
43
  context "populating" do
46
- should "call VBoxManage for the caller" do
47
- VirtualBox::Command.expects(:vboxmanage).with("getextradata", @caller.name, "enumerate").returns(@raw)
48
- VirtualBox::ExtraData.populate_relationship(@caller, {})
49
- end
50
-
51
- should "call pairs_to_objects with parent set to the caller" do
52
- VirtualBox::ExtraData.expects(:parse_kv_pairs).with(@raw, @caller).once
53
- VirtualBox::ExtraData.populate_relationship(@caller, {})
54
- end
55
-
56
- should "return an array of ExtraData objects" do
57
- result = VirtualBox::ExtraData.populate_relationship(@caller, {})
44
+ should "return a ExtraData object" do
45
+ result = VirtualBox::ExtraData.populate_relationship(@caller, mock_xml_doc)
58
46
  assert result.is_a?(VirtualBox::ExtraData)
59
47
  end
60
48
  end
@@ -153,24 +141,25 @@ raw
153
141
 
154
142
  context "global extra data" do
155
143
  setup do
156
- get_seq = sequence("get_seq")
157
- VirtualBox::Command.expects(:vboxmanage).with("getextradata", "global", "enumerate").once.in_sequence(get_seq)
158
- VirtualBox::ExtraData.expects(:parse_kv_pairs).returns(@ed).once.in_sequence(get_seq)
144
+ global = mock("global")
145
+ global.expects(:extra_data).once.returns("foo")
146
+ VirtualBox::Global.expects(:global).returns(global)
159
147
  @global = VirtualBox::ExtraData.global(true)
160
148
  end
161
149
 
162
- should "call the command, parse it, then turn it into objects" do
163
- assert_equal "bar", @global["foo"]
150
+ should "call the global extra data if it has never been loaded" do
151
+ assert_equal "foo", VirtualBox::ExtraData.global
164
152
  end
165
153
 
166
154
  should "return the same object if it exists for global data, rather than recreating it" do
167
- VirtualBox::Command.expects(:vboxmanage).never
155
+ VirtualBox::Global.expects(:global).never
168
156
  assert_equal @global, VirtualBox::ExtraData.global
169
157
  end
170
158
 
171
159
  should "return a new object if reload is true" do
172
- VirtualBox::Command.expects(:vboxmanage).once
173
- VirtualBox::ExtraData.expects(:parse_kv_pairs).returns(@ed.dup).once
160
+ global = mock("global")
161
+ global.expects(:extra_data).once.returns("bar")
162
+ VirtualBox::Global.expects(:global).returns(global)
174
163
  assert !@global.equal?(VirtualBox::ExtraData.global(true))
175
164
  end
176
165
  end
@@ -186,33 +175,4 @@ raw
186
175
  assert_equal "global", ed.parent
187
176
  end
188
177
  end
189
-
190
- context "parsing KV pairs" do
191
- setup do
192
- @data = VirtualBox::ExtraData.parse_kv_pairs(@raw)
193
- end
194
-
195
- should "return the proper number of items" do
196
- # Shows that it skips over non-matching lines as well
197
- assert_equal 6, @data.length
198
- end
199
-
200
- should "return as an ExtraData Hash" do
201
- assert @data.is_a?(Hash)
202
- assert @data.is_a?(VirtualBox::ExtraData)
203
- end
204
-
205
- should "return proper values, trimmed" do
206
- assert_equal "1 d, 2010-01-29, stable", @data["GUI/UpdateDate"]
207
- end
208
-
209
- should "send the 2nd param as the parent to the ED object" do
210
- @data = VirtualBox::ExtraData.parse_kv_pairs(@raw, "FOO")
211
- assert_equal "FOO", @data.parent
212
- end
213
-
214
- should "return an unchanged ED object" do
215
- assert !@data.changed?
216
- end
217
- end
218
178
  end
@@ -0,0 +1,78 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class GlobalTest < Test::Unit::TestCase
4
+ context "getting the global config" do
5
+ should "only get it once, then cache" do
6
+ VirtualBox::Global.expects(:config).returns(mock_xml_doc).once
7
+ result = VirtualBox::Global.global(true)
8
+ assert result
9
+ assert result.equal?(VirtualBox::Global.global)
10
+ end
11
+
12
+ should "reload if reload is true" do
13
+ VirtualBox::Global.expects(:config).returns(mock_xml_doc).twice
14
+ result = VirtualBox::Global.global(true)
15
+ assert result
16
+ assert !result.equal?(VirtualBox::Global.global(true))
17
+ end
18
+
19
+ should "reload if global reload flag is set" do
20
+ VirtualBox::Global.reload!
21
+ VirtualBox::Global.expects(:config).returns(mock_xml_doc).once
22
+ VirtualBox::Global.global
23
+ assert !VirtualBox::Global.reload?
24
+ end
25
+ end
26
+
27
+ context "parsing configuration XML" do
28
+ setup do
29
+ File.stubs(:exist?).returns(true)
30
+ VirtualBox::Command.stubs(:parse_xml)
31
+ end
32
+
33
+ should "check the file with the expanded path" do
34
+ File.stubs(:expand_path).returns("FOO")
35
+ File.expects(:exist?).with("FOO").returns(true)
36
+ VirtualBox::Global.config
37
+ end
38
+
39
+ should "raise an error if the config XML doesn't exist" do
40
+ File.expects(:exist?).returns(false)
41
+ assert_raises(VirtualBox::Exceptions::ConfigurationException) do
42
+ VirtualBox::Global.config
43
+ end
44
+ end
45
+
46
+ should "use Command.parse_xml to parse" do
47
+ VirtualBox::Command.expects(:parse_xml).with(anything).once
48
+ VirtualBox::Global.config
49
+ end
50
+
51
+ should "use the set vboxconfig to parse xml" do
52
+ VirtualBox::Global.vboxconfig = "/foo"
53
+ VirtualBox::Command.expects(:parse_xml).with("/foo").once
54
+ VirtualBox::Global.config
55
+ end
56
+
57
+ should "file expand path the vboxconfig path" do
58
+ VirtualBox::Global.vboxconfig = "foo"
59
+ VirtualBox::Command.expects(:parse_xml).with(File.expand_path("foo")).once
60
+ VirtualBox::Global.config
61
+ end
62
+ end
63
+
64
+ context "expanding path" do
65
+ setup do
66
+ VirtualBox::Global.vboxconfig = "/foo/bar/baz.rb"
67
+ end
68
+
69
+ should "expand the path properly" do
70
+ assert_equal "/foo/bar/vroom/rawr.bak", VirtualBox::Global.expand_path("vroom/rawr.bak")
71
+ end
72
+
73
+ should "expand the path relative to the vboxconfig directory" do
74
+ File.expects(:expand_path).with("foo", "/foo/bar").once
75
+ VirtualBox::Global.expand_path("foo")
76
+ end
77
+ end
78
+ end
@@ -19,6 +19,34 @@ raw
19
19
  VirtualBox::Command.stubs(:vboxmanage).with("showhdinfo", @name).returns(@find_raw)
20
20
  end
21
21
 
22
+ context "populating relationship" do
23
+ setup do
24
+ @xml = <<-xml
25
+ <MediaRegistry>
26
+ <HardDisks>
27
+ <HardDisk uuid="{9d2e4353-d1e9-466c-ac58-f2249264147b}" location="HardDisks/TestJeOS.vdi" format="VDI" type="Normal"/>
28
+ <HardDisk uuid="{5f7ccd06-78ef-47e9-b2bc-515aedd2f288}" location="HardDisks/hobobase.vdi" format="VDI" type="Normal"/>
29
+ </HardDisks>
30
+ </MediaRegistry>
31
+ xml
32
+ @doc = Nokogiri::XML(@xml)
33
+ end
34
+
35
+ should "create an object for each hard disk entry" do
36
+ result = VirtualBox::HardDrive.populate_relationship(nil, @doc)
37
+ assert_equal 2, result.length
38
+ end
39
+
40
+ should "properly extract uuid, location, and format" do
41
+ VirtualBox::Global.vboxconfig = "/foo/rawr.rb"
42
+ result = VirtualBox::HardDrive.populate_relationship(nil, @doc)
43
+ result = result[0]
44
+ assert_equal "9d2e4353-d1e9-466c-ac58-f2249264147b", result.uuid
45
+ assert_equal "VDI", result.format
46
+ assert_equal "/foo/HardDisks/TestJeOS.vdi", result.location
47
+ end
48
+ end
49
+
22
50
  context "validations" do
23
51
  setup do
24
52
  @hd = VirtualBox::HardDrive.new
@@ -206,64 +234,13 @@ raw
206
234
  end
207
235
 
208
236
  context "retrieving all hard drives" do
209
- setup do
210
- @valid = <<-valid
211
- UUID: 9d2e4353-d1e9-466c-ac58-f2249264147b
212
- Format: VDI
213
- Location: /Users/mitchellh/Library/VirtualBox/HardDisks/foo.vdi
214
- Accessible: yes
215
- Type: normal
216
- Usage: TestJeOS (UUID: 3d0f87b4-50f7-4fc5-ad89-93375b1b32a3)
217
-
218
- UUID: 11dedd14-57a1-4bdb-adeb-dd1d67f066e1
219
- Format: VDI
220
- Location: /Users/mitchellh/Library/VirtualBox/HardDisks/bar.vdi
221
- Accessible: yes
222
- Type: normal
223
- Usage: HoboBase (UUID: 696249ad-00b6-4087-b47f-9b82629efc31)
224
-
225
- UUID: 5e090af6-7d71-4f40-8b03-33aa665f9ecf
226
- Format: VMDK
227
- Location: /Users/mitchellh/Library/VirtualBox/HardDisks/baz.vmdk
228
- Accessible: yes
229
- Type: normal
230
- Usage: foo (UUID: 8710d3db-d96a-46ed-9004-59fa891fda90)
231
- valid
232
-
233
- VirtualBox::Command.stubs(:vboxmanage).with("list", "hdds").returns(@valid)
234
-
235
- @hd = mock("hd")
236
- @hd.stubs(:is_a?).with(VirtualBox::HardDrive).returns(true)
237
- VirtualBox::HardDrive.expects(:find).at_least(0).returns(@hd)
238
- end
239
-
240
237
  should "return an array of HardDrive objects" do
241
- result = VirtualBox::HardDrive.all
242
- assert result.is_a?(Array)
243
-
244
- result.each { |v| assert v.is_a?(VirtualBox::HardDrive) }
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
- }
238
+ media = mock("media")
239
+ media.expects(:hard_drives).returns("foo")
240
+ global = mock("global")
241
+ global.expects(:media).returns(media)
242
+ VirtualBox::Global.expects(:global).returns(global)
243
+ assert_equal "foo", VirtualBox::HardDrive.all
267
244
  end
268
245
  end
269
246
  end