virtualbox 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -5,51 +5,61 @@ class AbstractModelTest < Test::Unit::TestCase
5
5
  def self.set_relationship(caller, old_value, new_value)
6
6
  new_value
7
7
  end
8
-
8
+
9
9
  def self.validate_relationship(caller, data)
10
10
  end
11
11
  end
12
-
12
+
13
13
  class Bar; end
14
-
14
+
15
15
  class FakeModel < VirtualBox::AbstractModel
16
16
  attribute :foo
17
17
  attribute :bar
18
18
  relationship :foos, Foo
19
19
  relationship :bars, Bar, :dependent => :destroy
20
20
  end
21
-
21
+
22
+ context "inspecting" do
23
+ setup do
24
+ @model = FakeModel.new
25
+ end
26
+
27
+ should "generate the proper inspect string" do
28
+ assert_equal "#<AbstractModelTest::FakeModel :foo=nil, :bar=nil, :foos=..., :bars=...>", @model.inspect
29
+ end
30
+ end
31
+
22
32
  context "validation" do
23
33
  setup do
24
34
  @model = FakeModel.new
25
35
  end
26
-
36
+
27
37
  should "clear all previous errors" do
28
38
  @model.expects(:clear_errors).once
29
39
  @model.validate
30
40
  end
31
-
41
+
32
42
  should "call validate_relationship on each relationship class" do
33
43
  Foo.expects(:validate_relationship).once.with(@model, nil)
34
44
  @model.validate
35
45
  end
36
-
46
+
37
47
  should "forward arguments to validate_relationship" do
38
48
  Foo.expects(:validate_relationship).once.with(@model, nil, "HELLO")
39
49
  @model.validate("HELLO")
40
50
  end
41
-
51
+
42
52
  should "succeed if all relationships succeeded" do
43
53
  Foo.expects(:validate_relationship).returns(true)
44
54
  assert @model.validate
45
55
  end
46
-
56
+
47
57
  should "fail if one relationship failed to validate" do
48
58
  Foo.expects(:validate_relationship).returns(true)
49
59
  Bar.expects(:validate_relationship).returns(false)
50
60
  assert !@model.validate
51
61
  end
52
-
62
+
53
63
  context "errors" do
54
64
  should "return the errors of the relationships, as well as the model itself" do
55
65
  @model.foo = nil
@@ -64,7 +74,7 @@ class AbstractModelTest < Test::Unit::TestCase
64
74
  end
65
75
  end
66
76
  end
67
-
77
+
68
78
  context "new/existing records" do
69
79
  setup do
70
80
  @model = FakeModel.new
@@ -73,18 +83,18 @@ class AbstractModelTest < Test::Unit::TestCase
73
83
  should "be a new record by default" do
74
84
  assert @model.new_record?
75
85
  end
76
-
86
+
77
87
  should "not be a new record if populate_attributes is called" do
78
88
  @model.populate_attributes({})
79
89
  assert !@model.new_record?
80
90
  end
81
-
91
+
82
92
  should "not be a new record after saving" do
83
93
  assert @model.new_record?
84
94
  @model.save
85
95
  assert !@model.new_record?
86
96
  end
87
-
97
+
88
98
  should "become a new record again if new_record! is called" do
89
99
  assert @model.new_record?
90
100
  @model.save
@@ -92,19 +102,19 @@ class AbstractModelTest < Test::Unit::TestCase
92
102
  @model.new_record!
93
103
  assert @model.new_record?
94
104
  end
95
-
105
+
96
106
  should "become an existing record if existing_record! is called" do
97
107
  assert @model.new_record?
98
108
  @model.existing_record!
99
109
  assert !@model.new_record?
100
110
  end
101
111
  end
102
-
112
+
103
113
  context "subclasses" do
104
114
  class FakeTwoModel < FakeModel
105
115
  attribute :baz
106
116
  end
107
-
117
+
108
118
  setup do
109
119
  @model = FakeTwoModel.new
110
120
  @model.populate_attributes({
@@ -113,32 +123,32 @@ class AbstractModelTest < Test::Unit::TestCase
113
123
  :baz => "baz"
114
124
  })
115
125
  end
116
-
126
+
117
127
  should "have access to parents attributes" do
118
128
  assert_nothing_raised do
119
129
  assert_equal "foo", @model.foo
120
130
  end
121
131
  end
122
132
  end
123
-
133
+
124
134
  context "destroying" do
125
135
  setup do
126
136
  @model = FakeModel.new
127
137
  end
128
-
138
+
129
139
  should "call destroy_relationship only for dependent relationships" do
130
140
  Foo.expects(:destroy_relationship).never
131
141
  Bar.expects(:destroy_relationship).once
132
-
142
+
133
143
  @model.destroy
134
144
  end
135
-
145
+
136
146
  should "forward any arguments to the destroy method" do
137
147
  Bar.expects(:destroy_relationship).with(@model, anything, "HELLO").once
138
148
  @model.destroy("HELLO")
139
149
  end
140
150
  end
141
-
151
+
142
152
  context "saving" do
143
153
  setup do
144
154
  @model = FakeModel.new
@@ -147,7 +157,7 @@ class AbstractModelTest < Test::Unit::TestCase
147
157
  :bar => "bar"
148
158
  })
149
159
  end
150
-
160
+
151
161
  should "call save_attribute for only attributes which have changed" do
152
162
  @model.foo = "foo2"
153
163
  assert @model.foo_changed?
@@ -155,66 +165,66 @@ class AbstractModelTest < Test::Unit::TestCase
155
165
  @model.expects(:save_attribute).with(:foo, "foo2").once
156
166
  @model.save
157
167
  end
158
-
168
+
159
169
  should "call save_relationships" do
160
170
  @model.expects(:save_relationships).once
161
171
  @model.save
162
172
  end
163
-
173
+
164
174
  should "clear dirty state once saved" do
165
175
  @model.foo = "foo2"
166
176
  assert @model.foo_changed?
167
177
  @model.save
168
178
  assert !@model.foo_changed?
169
179
  end
170
-
180
+
171
181
  should "forward parameters through" do
172
182
  @model.expects(:save_attribute).with(:foo, "foo2", "YES").once
173
183
  Foo.expects(:save_relationship).with(@model, anything, "YES").once
174
-
184
+
175
185
  @model.foo = "foo2"
176
186
  @model.save("YES")
177
187
  end
178
188
  end
179
-
189
+
180
190
  context "populating relationships and attributes" do
181
191
  setup do
182
192
  @model = FakeModel.new
183
193
  end
184
-
194
+
185
195
  should "populate relationships at the same time as attributes" do
186
196
  Foo.expects(:populate_relationship).once
187
197
  @model.populate_attributes({})
188
198
  end
189
199
  end
190
-
200
+
191
201
  context "integrating relatable" do
192
202
  setup do
193
203
  @model = FakeModel.new
194
204
  end
195
-
205
+
196
206
  should "set dirty state when a relationship is set" do
197
207
  assert !@model.changed?
198
208
  @model.foos = "foo"
199
209
  assert @model.changed?
200
210
  end
201
211
  end
202
-
212
+
203
213
  context "integrating attributable and dirty" do
204
214
  setup do
205
215
  @model = FakeModel.new
206
216
  end
207
-
217
+
208
218
  should "not affect dirtiness with populate_attributes" do
209
219
  assert !@model.changed?
210
220
  @model.populate_attributes({
211
221
  :foo => "foo2"
212
222
  })
213
-
223
+
214
224
  assert !@model.changed?
215
225
  assert_equal "foo2", @model.foo
216
226
  end
217
-
227
+
218
228
  should "mark dirtiness on write_attribute" do
219
229
  assert !@model.changed?
220
230
  @model.write_attribute(:foo, "foo2")
@@ -10,15 +10,15 @@ class AttachedDeviceTest < Test::Unit::TestCase
10
10
 
11
11
  @vm = mock("vm")
12
12
  @vm.stubs(:name).returns("foo")
13
-
13
+
14
14
  @caller = mock("caller")
15
15
  @caller.stubs(:parent).returns(@vm)
16
16
  @caller.stubs(:name).returns("Foo Controller")
17
-
17
+
18
18
  # Stub execute to make sure nothing actually happens
19
19
  VirtualBox::Command.stubs(:execute).returns('')
20
20
  end
21
-
21
+
22
22
  context "validations" do
23
23
  setup do
24
24
  @ad = VirtualBox::AttachedDevice.new
@@ -26,49 +26,49 @@ class AttachedDeviceTest < Test::Unit::TestCase
26
26
  @ad.port = 7
27
27
  @ad.added_to_relationship(@caller)
28
28
  end
29
-
29
+
30
30
  should "be valid with all fields" do
31
31
  assert @ad.valid?
32
32
  end
33
-
33
+
34
34
  should "be invalid with no image" do
35
35
  @ad.image = nil
36
36
  assert !@ad.valid?
37
37
  end
38
-
38
+
39
39
  should "be invalid with no port" do
40
40
  @ad.port = nil
41
41
  assert !@ad.valid?
42
42
  end
43
-
43
+
44
44
  should "be invalid if not in a relationship" do
45
45
  @ad.write_attribute(:parent, nil)
46
46
  assert !@ad.valid?
47
47
  end
48
48
  end
49
-
49
+
50
50
  context "medium" do
51
51
  setup do
52
52
  @ad = VirtualBox::AttachedDevice.new
53
53
  @hd = VirtualBox::HardDrive.new
54
54
  @hd.write_attribute(:uuid, @uuid)
55
55
  end
56
-
56
+
57
57
  should "be 'none' when image is nil" do
58
58
  assert_equal "none", @ad.medium
59
59
  end
60
-
60
+
61
61
  should "be the uuid of the image if its not nil" do
62
62
  @ad.image = @hd
63
63
  assert_equal @hd.uuid, @ad.medium
64
64
  end
65
-
65
+
66
66
  should "be 'emptydrive' if the image is an empty drive" do
67
67
  @ad.image = VirtualBox::DVD.empty_drive
68
68
  assert_equal "emptydrive", @ad.medium
69
69
  end
70
70
  end
71
-
71
+
72
72
  context "saving an existing device" do
73
73
  setup do
74
74
  @value = VirtualBox::AttachedDevice.populate_relationship(@caller, @data)
@@ -76,46 +76,46 @@ class AttachedDeviceTest < Test::Unit::TestCase
76
76
  @value.image = VirtualBox::DVD.empty_drive
77
77
  assert @value.changed?
78
78
  end
79
-
79
+
80
80
  should "not do anything if the device isn't change" do
81
81
  @value.clear_dirty!
82
82
  assert !@value.changed?
83
-
83
+
84
84
  VirtualBox::Command.expects(:vboxmanage).never
85
85
  @value.save
86
86
  end
87
-
87
+
88
88
  should "call vboxmanage" do
89
89
  VirtualBox::Command.expects(:vboxmanage).once
90
90
  @value.save
91
91
  end
92
-
92
+
93
93
  should "return false and not call vboxmanage if invalid" do
94
94
  VirtualBox::Command.expects(:vboxmanage).never
95
95
  @value.expects(:valid?).returns(false)
96
96
  assert !@value.save
97
97
  end
98
-
98
+
99
99
  should "not call destroy if the port didn't change" do
100
100
  @value.expects(:destroy).never
101
101
  assert !@value.port_changed?
102
102
  assert @value.save
103
103
  end
104
-
104
+
105
105
  should "call destroy with the old port if the port changed" do
106
106
  @value.expects(:destroy).with({:port => @value.port}, false)
107
107
  @value.port = 7
108
108
  assert @value.port_changed?
109
109
  assert @value.save
110
110
  end
111
-
111
+
112
112
  should "call destroy with the raise errors flag" do
113
113
  @value.expects(:destroy).with(anything, true).once
114
114
  @value.port = 7
115
115
  @value.save(true)
116
116
  end
117
117
  end
118
-
118
+
119
119
  context "creating a new attached device" do
120
120
  setup do
121
121
  @image = VirtualBox::HardDrive.new
@@ -123,26 +123,26 @@ class AttachedDeviceTest < Test::Unit::TestCase
123
123
  @ad.image = @image
124
124
  @ad.port = 3
125
125
  end
126
-
126
+
127
127
  should "return false and not call vboxmanage if invalid" do
128
128
  VirtualBox::Command.expects(:vboxmanage).never
129
129
  @ad.expects(:valid?).returns(false)
130
130
  assert !@ad.save
131
131
  end
132
-
132
+
133
133
  should "raise a ValidationFailedException if invalid and raise_errors is true" do
134
134
  @ad.expects(:valid?).returns(false)
135
135
  assert_raises(VirtualBox::Exceptions::ValidationFailedException) {
136
136
  @ad.save(true)
137
137
  }
138
138
  end
139
-
139
+
140
140
  context "has a parent" do
141
141
  setup do
142
142
  @ad.added_to_relationship(@caller)
143
143
  VirtualBox::Command.stubs(:vboxmanage)
144
144
  end
145
-
145
+
146
146
  should "not call destroy since its a new record" do
147
147
  @ad.expects(:destroy).never
148
148
  assert @ad.save
@@ -161,20 +161,20 @@ class AttachedDeviceTest < Test::Unit::TestCase
161
161
  should "return true if the command was a success" do
162
162
  assert @ad.save
163
163
  end
164
-
164
+
165
165
  should "raise an exception if true sent to save and error occured" do
166
166
  VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
167
167
  assert_raises(VirtualBox::Exceptions::CommandFailedException) {
168
168
  @ad.save(true)
169
169
  }
170
170
  end
171
-
171
+
172
172
  should "not be a new record after saving" do
173
173
  assert @ad.new_record?
174
174
  assert @ad.save
175
175
  assert !@ad.new_record?
176
176
  end
177
-
177
+
178
178
  should "not be changed after saving" do
179
179
  assert @ad.changed?
180
180
  assert @ad.save
@@ -182,40 +182,40 @@ class AttachedDeviceTest < Test::Unit::TestCase
182
182
  end
183
183
  end
184
184
  end
185
-
185
+
186
186
  context "constructor" do
187
187
  should "call populate_from_data if 3 args are given" do
188
188
  VirtualBox::AttachedDevice.any_instance.expects(:populate_from_data).with(1,2,3).once
189
189
  VirtualBox::AttachedDevice.new(1,2,3)
190
190
  end
191
-
191
+
192
192
  should "call populate_attributes if 1 arg is given" do
193
193
  VirtualBox::AttachedDevice.any_instance.expects(:populate_attributes).with(1).once
194
194
  ad = VirtualBox::AttachedDevice.new(1)
195
195
  assert ad.new_record?
196
196
  end
197
-
197
+
198
198
  should "raise a NoMethodError if anything other than 0,1,or 3 arguments" do
199
199
  # 9 seems like a reasonable max (maybe just a bit unreasonable!)
200
200
  2.upto(9) do |i|
201
201
  next if i == 3
202
202
  args = Array.new(i, "A")
203
-
203
+
204
204
  assert_raises(NoMethodError) {
205
205
  VirtualBox::AttachedDevice.new(*args)
206
206
  }
207
207
  end
208
208
  end
209
209
  end
210
-
210
+
211
211
  context "destroying" do
212
212
  setup do
213
213
  @value = VirtualBox::AttachedDevice.populate_relationship(@caller, @data)
214
214
  @value = @value[0]
215
-
215
+
216
216
  @image = mock("image")
217
217
  @value.stubs(:image).returns(@image)
218
-
218
+
219
219
  VirtualBox::Command.stubs(:execute)
220
220
  end
221
221
 
@@ -228,7 +228,7 @@ class AttachedDeviceTest < Test::Unit::TestCase
228
228
 
229
229
  VirtualBox::AttachedDevice.destroy_relationship(self, [obj_one, obj_two], "HELLO")
230
230
  end
231
-
231
+
232
232
  should "shell escape VM name and storage controller name" do
233
233
  shell_seq = sequence("shell_seq")
234
234
  VirtualBox::Command.expects(:shell_escape).with(@vm.name).in_sequence(shell_seq)
@@ -236,69 +236,69 @@ class AttachedDeviceTest < Test::Unit::TestCase
236
236
  VirtualBox::Command.expects(:vboxmanage).in_sequence(shell_seq)
237
237
  @value.destroy
238
238
  end
239
-
239
+
240
240
  should "destroy with the specified port if set" do
241
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")
242
242
  @value.destroy(:port => 80)
243
243
  end
244
-
244
+
245
245
  should "destroy with the default port if not other port is specified" do
246
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")
247
247
  @value.destroy
248
248
  end
249
-
249
+
250
250
  should "not destroy image by default" do
251
251
  @image.expects(:destroy).never
252
252
  @value.destroy
253
253
  end
254
-
254
+
255
255
  should "destroy image if flag is set" do
256
256
  @image.expects(:destroy).once
257
257
  @value.destroy({
258
258
  :destroy_image => true
259
259
  })
260
260
  end
261
-
261
+
262
262
  should "ignore destroy image flag if image is nil" do
263
263
  @value.expects(:image).once.returns(nil)
264
264
  @value.destroy({
265
265
  :destroy_image => true
266
266
  })
267
267
  end
268
-
268
+
269
269
  should "return false if destroy failed" do
270
270
  VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
271
271
  assert !@value.destroy
272
272
  end
273
-
273
+
274
274
  should "raise an exception if destroy failed and an error occured" do
275
275
  VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
276
276
  assert_raises(VirtualBox::Exceptions::CommandFailedException) {
277
277
  @value.destroy({}, true)
278
278
  }
279
279
  end
280
-
280
+
281
281
  should "forward raise_errors flag to image.destroy" do
282
282
  @image.expects(:destroy).with(true).once
283
283
  @value.destroy({:destroy_image => true}, true)
284
284
  end
285
285
  end
286
-
286
+
287
287
  context "populating relationships" do
288
288
  setup do
289
289
  @value = VirtualBox::AttachedDevice.populate_relationship(@caller, @data)
290
290
  end
291
-
291
+
292
292
  should "create the correct amount of objects" do
293
293
  assert_equal 2, @value.length
294
294
  end
295
-
295
+
296
296
  should "create objects with proper values" do
297
297
  obj = @value[0]
298
298
  assert_equal "none", obj.medium
299
299
  assert_equal "322f79fd-7da6-416f-a16f-e70066ccf165", obj.uuid
300
300
  assert_equal 0, obj.port
301
-
301
+
302
302
  obj = @value[1]
303
303
  assert_equal "none", obj.medium
304
304
  assert_nil obj.uuid