virtualbox 0.4.1 → 0.4.2

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/Readme.md +9 -9
  2. data/VERSION +1 -1
  3. data/docs/GettingStarted.md +11 -11
  4. data/docs/WhatsNew.md +1 -1
  5. data/lib/virtualbox.rb +10 -1
  6. data/lib/virtualbox/abstract_model.rb +47 -29
  7. data/lib/virtualbox/abstract_model/attributable.rb +16 -16
  8. data/lib/virtualbox/abstract_model/dirty.rb +10 -10
  9. data/lib/virtualbox/abstract_model/relatable.rb +22 -22
  10. data/lib/virtualbox/abstract_model/validatable.rb +4 -4
  11. data/lib/virtualbox/attached_device.rb +23 -23
  12. data/lib/virtualbox/command.rb +9 -9
  13. data/lib/virtualbox/dvd.rb +7 -7
  14. data/lib/virtualbox/ext/subclass_listing.rb +1 -1
  15. data/lib/virtualbox/extra_data.rb +17 -17
  16. data/lib/virtualbox/forwarded_port.rb +23 -23
  17. data/lib/virtualbox/hard_drive.rb +27 -27
  18. data/lib/virtualbox/image.rb +25 -18
  19. data/lib/virtualbox/nic.rb +22 -22
  20. data/lib/virtualbox/proxies/collection.rb +4 -4
  21. data/lib/virtualbox/shared_folder.rb +25 -25
  22. data/lib/virtualbox/storage_controller.rb +16 -16
  23. data/lib/virtualbox/vm.rb +95 -42
  24. data/test/virtualbox/abstract_model/attributable_test.rb +28 -28
  25. data/test/virtualbox/abstract_model/dirty_test.rb +13 -13
  26. data/test/virtualbox/abstract_model/relatable_test.rb +36 -36
  27. data/test/virtualbox/abstract_model/validatable_test.rb +22 -22
  28. data/test/virtualbox/abstract_model_test.rb +46 -36
  29. data/test/virtualbox/attached_device_test.rb +47 -47
  30. data/test/virtualbox/command_test.rb +12 -12
  31. data/test/virtualbox/dvd_test.rb +15 -19
  32. data/test/virtualbox/ext/subclass_listing_test.rb +2 -2
  33. data/test/virtualbox/extra_data_test.rb +37 -37
  34. data/test/virtualbox/forwarded_port_test.rb +31 -31
  35. data/test/virtualbox/hard_drive_test.rb +40 -48
  36. data/test/virtualbox/image_test.rb +36 -33
  37. data/test/virtualbox/nic_test.rb +22 -22
  38. data/test/virtualbox/proxies/collection_test.rb +6 -6
  39. data/test/virtualbox/shared_folder_test.rb +36 -36
  40. data/test/virtualbox/storage_controller_test.rb +14 -14
  41. data/test/virtualbox/vm_test.rb +121 -70
  42. data/test/virtualbox_test.rb +19 -0
  43. data/virtualbox.gemspec +5 -3
  44. metadata +4 -2
@@ -3,15 +3,11 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
3
3
  class HardDriveTest < Test::Unit::TestCase
4
4
  setup do
5
5
  VirtualBox::Command.stubs(:execute)
6
-
7
- @find_raw = <<-raw
8
- VirtualBox Command Line Management Interface Version 3.1.2
9
- (C) 2005-2009 Sun Microsystems, Inc.
10
- All rights reserved.
11
6
 
7
+ @find_raw = <<-raw
12
8
  UUID: 11dedd14-57a1-4bdb-adeb-dd1d67f066e1
13
9
  Accessible: yes
14
- Description:
10
+ Description:
15
11
  Logical size: 20480 MBytes
16
12
  Current size on disk: 1218 MBytes
17
13
  Type: normal (base)
@@ -22,22 +18,22 @@ raw
22
18
  @name = "foo"
23
19
  VirtualBox::Command.stubs(:vboxmanage).with("showhdinfo #{@name}").returns(@find_raw)
24
20
  end
25
-
21
+
26
22
  context "validations" do
27
23
  setup do
28
24
  @hd = VirtualBox::HardDrive.new
29
25
  @hd.size = 2000
30
26
  end
31
-
27
+
32
28
  should "be valid with a size and format" do
33
29
  assert @hd.valid?
34
30
  end
35
-
31
+
36
32
  should "be invalid if size is nil" do
37
33
  @hd.size = nil
38
34
  assert !@hd.valid?
39
35
  end
40
-
36
+
41
37
  should "clear validations when rechecking" do
42
38
  @hd.size = nil
43
39
  assert !@hd.valid?
@@ -45,22 +41,22 @@ raw
45
41
  assert @hd.valid?
46
42
  end
47
43
  end
48
-
44
+
49
45
  context "destroying a hard drive" do
50
46
  setup do
51
47
  @hd = VirtualBox::HardDrive.find(@name)
52
48
  end
53
-
49
+
54
50
  should "call vboxmanage to destroy it" do
55
51
  VirtualBox::Command.expects(:vboxmanage).with("closemedium disk #{@hd.uuid} --delete")
56
52
  assert @hd.destroy
57
53
  end
58
-
54
+
59
55
  should "return false if destroy failed" do
60
56
  VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
61
57
  assert !@hd.destroy
62
58
  end
63
-
59
+
64
60
  should "raise an exception if failed and flag is set" do
65
61
  VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
66
62
  assert_raises(VirtualBox::Exceptions::CommandFailedException) {
@@ -68,29 +64,29 @@ raw
68
64
  }
69
65
  end
70
66
  end
71
-
67
+
72
68
  context "cloning a hard drive" do
73
69
  setup do
74
70
  @hd = VirtualBox::HardDrive.find(@name)
75
71
  VirtualBox::Command.stubs(:vboxmanage).with("clonehd #{@hd.uuid} bar --format VDI --remember").returns(@find_raw)
76
72
  end
77
-
73
+
78
74
  should "call vboxmanage with the clone command" do
79
75
  VirtualBox::HardDrive.expects(:find).returns(nil)
80
76
  @hd.clone("bar")
81
77
  end
82
-
78
+
83
79
  should "return the newly cloned hard drive" do
84
80
  @new_hd = mock("hd")
85
81
  VirtualBox::HardDrive.expects(:find).returns(@new_hd)
86
82
  assert_equal @new_hd, @hd.clone("bar")
87
83
  end
88
-
84
+
89
85
  should "return false on failure" do
90
86
  VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
91
87
  assert @hd.clone("bar").nil?
92
88
  end
93
-
89
+
94
90
  should "raise an exception if raise_errors is true and failed" do
95
91
  VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
96
92
  assert_raises(VirtualBox::Exceptions::CommandFailedException) {
@@ -98,74 +94,74 @@ raw
98
94
  }
99
95
  end
100
96
  end
101
-
97
+
102
98
  context "creating a hard drive" do
103
99
  setup do
104
100
  @location = "foo.foo"
105
101
  @size = "758"
106
102
  @format = "VDI"
107
-
103
+
108
104
  @hd = VirtualBox::HardDrive.new
109
105
  @hd.location = @location
110
106
  @hd.size = @size
111
-
107
+
112
108
  @fake_hd = mock("hd")
113
109
  @fake_hd.stubs(:attributes).returns({
114
110
  :uuid => "foo"
115
111
  })
116
-
112
+
117
113
  VirtualBox::HardDrive.stubs(:find).returns(@fake_hd)
118
114
  VirtualBox::Command.stubs(:vboxmanage).returns("UUID: FOO")
119
115
  end
120
-
116
+
121
117
  should "call create on save" do
122
118
  @hd.expects(:create).once
123
-
119
+
124
120
  assert @hd.new_record?
125
121
  @hd.save
126
122
  end
127
-
123
+
128
124
  should "call not call create on existing records" do
129
125
  @hd.save
130
126
  assert !@hd.new_record?
131
-
127
+
132
128
  @hd.expects(:create).never
133
129
  @hd.save
134
130
  end
135
-
131
+
136
132
  should "call createhd" do
137
133
  VirtualBox::Command.expects(:vboxmanage).with("createhd --filename #{@location} --size #{@size} --format #{@format} --remember")
138
134
  @hd.save
139
135
  end
140
-
136
+
141
137
  should "replace attributes with those of the newly created hard drive" do
142
138
  @hd.save
143
-
139
+
144
140
  assert_equal "foo", @hd.uuid
145
141
  end
146
-
142
+
147
143
  should "return true if the command was a success" do
148
144
  assert @hd.save
149
145
  end
150
-
146
+
151
147
  should "return failure if the command failed" do
152
148
  VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
153
149
  assert !@hd.save
154
150
  end
155
-
151
+
156
152
  should "raise an exception if flag is set" do
157
153
  VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
158
154
  assert_raises(VirtualBox::Exceptions::CommandFailedException) {
159
155
  @hd.save(true)
160
156
  }
161
157
  end
162
-
158
+
163
159
  should "not run if invalid" do
164
160
  @hd.expects(:valid?).returns(false)
165
161
  VirtualBox::Command.expects(:vboxmanage).never
166
162
  assert !@hd.save
167
163
  end
168
-
164
+
169
165
  should "raise a ValidationFailedException if invalid and raise_errors is true" do
170
166
  @hd.expects(:valid?).returns(false)
171
167
  assert_raises(VirtualBox::Exceptions::ValidationFailedException) {
@@ -173,7 +169,7 @@ raw
173
169
  }
174
170
  end
175
171
  end
176
-
172
+
177
173
  context "finding a single hard drive" do
178
174
  should "parse proper fields" do
179
175
  VirtualBox::Command.expects(:vboxmanage).with("showhdinfo #{@name}").returns(@find_raw)
@@ -184,31 +180,27 @@ raw
184
180
  :size => "20480",
185
181
  :location => "/Users/mitchellh/Library/VirtualBox/HardDisks/foo.vdi"
186
182
  }
187
-
183
+
188
184
  hd = VirtualBox::HardDrive.find(@name)
189
185
  assert hd.is_a?(VirtualBox::HardDrive)
190
-
186
+
191
187
  @expected.each do |k,v|
192
188
  assert_equal v, hd.send(k)
193
189
  end
194
190
  end
195
-
191
+
196
192
  should "return nil if finding a non-existent hard drive" do
197
193
  VirtualBox::Command.expects(:vboxmanage).with("showhdinfo 12").returns("UH OH (VERR_FILE_NOT_FOUND)")
198
-
194
+
199
195
  assert_nothing_raised do
200
196
  assert_nil VirtualBox::HardDrive.find(12)
201
197
  end
202
198
  end
203
199
  end
204
-
200
+
205
201
  context "retrieving all hard drives" do
206
202
  setup do
207
203
  @valid = <<-valid
208
- VirtualBox Command Line Management Interface Version 3.1.2
209
- (C) 2005-2009 Sun Microsystems, Inc.
210
- All rights reserved.
211
-
212
204
  UUID: 9d2e4353-d1e9-466c-ac58-f2249264147b
213
205
  Format: VDI
214
206
  Location: /Users/mitchellh/Library/VirtualBox/HardDisks/foo.vdi
@@ -232,16 +224,16 @@ Usage: foo (UUID: 8710d3db-d96a-46ed-9004-59fa891fda90)
232
224
  valid
233
225
 
234
226
  VirtualBox::Command.expects(:vboxmanage).with("list hdds").returns(@valid)
235
-
227
+
236
228
  @hd = mock("hd")
237
229
  @hd.stubs(:is_a?).with(VirtualBox::HardDrive).returns(true)
238
230
  VirtualBox::HardDrive.expects(:find).at_least(0).returns(@hd)
239
231
  end
240
-
232
+
241
233
  should "return an array of HardDrive objects" do
242
234
  result = VirtualBox::HardDrive.all
243
235
  assert result.is_a?(Array)
244
-
236
+
245
237
  result.each { |v| assert v.is_a?(VirtualBox::HardDrive) }
246
238
  end
247
239
  end
@@ -5,61 +5,61 @@ class ImageTest < Test::Unit::TestCase
5
5
  setup do
6
6
  @image = VirtualBox::Image.new
7
7
  end
8
-
8
+
9
9
  should "raise an exception if image_type is called on Image" do
10
10
  assert_raises(RuntimeError) { @image.image_type }
11
11
  end
12
-
12
+
13
13
  should "return false by default on empty_drive?" do
14
14
  assert !@image.empty_drive?
15
15
  end
16
16
  end
17
-
17
+
18
18
  context "in a relationship" do
19
19
  class ImageRelationshipModel < VirtualBox::AbstractModel
20
20
  relationship :image, VirtualBox::Image
21
21
  end
22
-
22
+
23
23
  setup do
24
24
  @model = ImageRelationshipModel.new
25
25
  end
26
-
26
+
27
27
  context "populating a relationship" do
28
28
  should "return 'emptydrive' if the medium is an empty drive" do
29
29
  result = VirtualBox::Image.populate_relationship(@model, {
30
30
  :medium => "emptydrive"
31
31
  })
32
-
32
+
33
33
  assert result.is_a?(VirtualBox::DVD)
34
34
  assert result.empty_drive?
35
35
  end
36
-
36
+
37
37
  should "return nil if uuid is nil and medium isn't empty" do
38
38
  result = VirtualBox::Image.populate_relationship(@model, {})
39
39
  assert result.nil?
40
40
  end
41
-
41
+
42
42
  should "result a matching image from subclasses if uuid" do
43
43
  uuid = "foo'"
44
44
 
45
45
  subobject = mock("subobject")
46
46
  subobject.stubs(:uuid).returns(uuid)
47
-
47
+
48
48
  subclass = mock("subclass")
49
49
  subclass.stubs(:all).returns([subobject])
50
-
50
+
51
51
  VirtualBox::Image.expects(:subclasses).returns([subclass])
52
-
52
+
53
53
  result = VirtualBox::Image.populate_relationship(@model, { :uuid => uuid })
54
54
  assert_equal subobject, result
55
55
  end
56
-
56
+
57
57
  should "result in nil if suboject can't be found" do
58
58
  VirtualBox::Image.expects(:subclasses).returns([])
59
59
  assert_nil VirtualBox::Image.populate_relationship(@model, { :uuid => "foo" })
60
60
  end
61
61
  end
62
-
62
+
63
63
  context "setting relationship object" do
64
64
  should "raise an InvalidRelationshipObjectException if new value is not an image" do
65
65
  assert_raises(VirtualBox::Exceptions::InvalidRelationshipObjectException) {
@@ -79,20 +79,16 @@ class ImageTest < Test::Unit::TestCase
79
79
  end
80
80
  end
81
81
  end
82
-
82
+
83
83
  context "recording subclasses" do
84
84
  should "list all subclasses" do
85
85
  assert_nothing_raised { VirtualBox::Image.subclasses }
86
86
  end
87
87
  end
88
-
88
+
89
89
  context "parsing raw" do
90
90
  setup do
91
91
  @raw = <<-raw
92
- VirtualBox Command Line Management Interface Version 3.1.2
93
- (C) 2005-2009 Sun Microsystems, Inc.
94
- All rights reserved.
95
-
96
92
  UUID: 9d2e4353-d1e9-466c-ac58-f2249264147b
97
93
  Format: VDI
98
94
  Location: /Users/mitchellh/Library/VirtualBox/HardDisks/TestJeOS.vdi
@@ -112,21 +108,28 @@ Format: VMDK
112
108
  Location: /Users/mitchellh/Library/VirtualBox/HardDisks/HoboBase.vmdk
113
109
  Accessible: yes
114
110
  Type: normal
115
- Usage: HoboBase_2 (UUID: 3cc36c5d-562a-4030-8acf-f061f44170c4)
111
+ Usage: HoboBase_2 (UUID: 3cc36c5d-562a-4030-8acf-f061f44170c4)
116
112
  raw
117
113
  end
118
-
114
+
119
115
  should "call parse block the correct number of times" do
120
- VirtualBox::Image.expects(:parse_block).times(4).returns({})
116
+ VirtualBox::Image.expects(:parse_block).times(3).returns({})
121
117
  VirtualBox::Image.parse_raw(@raw)
122
118
  end
123
-
119
+
124
120
  should "remove nil (invalid) blocks from result" do
125
121
  result = VirtualBox::Image.parse_raw(@raw)
126
122
  assert_equal 3, result.length
127
123
  end
124
+
125
+ should "be able to extract the filename from the location" do
126
+ result = VirtualBox::Image.parse_raw(@raw)
127
+ assert_equal "TestJeOS.vdi", result[0].filename
128
+ assert_equal "HoboBase.vdi", result[1].filename
129
+ assert_equal "HoboBase.vmdk", result[2].filename
130
+ end
128
131
  end
129
-
132
+
130
133
  context "parsing multiple blocks" do
131
134
  setup do
132
135
  @raw = <<-raw
@@ -137,7 +140,7 @@ two
137
140
  three
138
141
  raw
139
142
  end
140
-
143
+
141
144
  should "call parse block for each multiline" do
142
145
  parse_seq = sequence("parse")
143
146
  VirtualBox::Image.expects(:parse_block).with("one").in_sequence(parse_seq)
@@ -145,19 +148,19 @@ raw
145
148
  VirtualBox::Image.expects(:parse_block).with("three").in_sequence(parse_seq)
146
149
  VirtualBox::Image.parse_blocks(@raw)
147
150
  end
148
-
151
+
149
152
  should "return an array of the parses with nil ignored" do
150
153
  parse_seq = sequence("parse")
151
154
  VirtualBox::Image.expects(:parse_block).with("one").returns({}).in_sequence(parse_seq)
152
155
  VirtualBox::Image.expects(:parse_block).with("two").returns(nil).in_sequence(parse_seq)
153
156
  VirtualBox::Image.expects(:parse_block).with("three").returns({}).in_sequence(parse_seq)
154
157
  result = VirtualBox::Image.parse_blocks(@raw)
155
-
158
+
156
159
  assert result.is_a?(Array)
157
160
  assert_equal 2, result.length
158
161
  end
159
162
  end
160
-
163
+
161
164
  context "parsing a single block" do
162
165
  setup do
163
166
  @expected = {
@@ -165,24 +168,24 @@ raw
165
168
  :path => "foo",
166
169
  :accessible => "yes"
167
170
  }
168
-
171
+
169
172
  @block = ""
170
173
  @expected.each { |k,v| @block += "#{k}: #{v}\n" }
171
174
  end
172
-
175
+
173
176
  should "return nil for an invalid block" do
174
177
  assert VirtualBox::Image.parse_block("HI").nil?
175
178
  end
176
-
179
+
177
180
  should "mirror location to path" do
178
181
  result = VirtualBox::Image.parse_block(@block)
179
182
  assert_equal "foo", result[:location]
180
183
  end
181
-
184
+
182
185
  should "properly parse if all properties are available" do
183
186
  result = VirtualBox::Image.parse_block(@block)
184
187
  assert !result.nil?
185
-
188
+
186
189
  @expected.each do |k,v|
187
190
  k = :location if k == :path
188
191
  assert_equal v, result[k]