virtualbox 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,6 +19,161 @@ class AttachedDeviceTest < Test::Unit::TestCase
19
19
  VirtualBox::Command.stubs(:execute).returns('')
20
20
  end
21
21
 
22
+ context "medium" do
23
+ setup do
24
+ @ad = VirtualBox::AttachedDevice.new
25
+ @hd = VirtualBox::HardDrive.new
26
+ @hd.write_attribute(:uuid, @uuid)
27
+ end
28
+
29
+ should "be 'none' when image is nil" do
30
+ assert_equal "none", @ad.medium
31
+ end
32
+
33
+ should "be the uuid of the image if its not nil" do
34
+ @ad.image = @hd
35
+ assert_equal @hd.uuid, @ad.medium
36
+ end
37
+
38
+ should "be 'emptydrive' if the image is an empty drive" do
39
+ @ad.image = VirtualBox::DVD.empty_drive
40
+ assert_equal "emptydrive", @ad.medium
41
+ end
42
+ end
43
+
44
+ context "saving an existing device" do
45
+ setup do
46
+ @value = VirtualBox::AttachedDevice.populate_relationship(@caller, @data)
47
+ @value = @value[0]
48
+ @value.image = VirtualBox::DVD.empty_drive
49
+ assert @value.changed?
50
+ end
51
+
52
+ should "not do anything if the device isn't change" do
53
+ @value.clear_dirty!
54
+ assert !@value.changed?
55
+
56
+ VirtualBox::Command.expects(:vboxmanage).never
57
+ @value.save
58
+ end
59
+
60
+ should "call vboxmanage" do
61
+ VirtualBox::Command.expects(:vboxmanage).once
62
+ @value.save
63
+ end
64
+
65
+ should "not call destroy if the port didn't change" do
66
+ @value.expects(:destroy).never
67
+ assert !@value.port_changed?
68
+ assert @value.save
69
+ end
70
+
71
+ should "call destroy with the old port if the port changed" do
72
+ @value.expects(:destroy).with({:port => @value.port}, false)
73
+ @value.port = 7
74
+ assert @value.port_changed?
75
+ assert @value.save
76
+ end
77
+
78
+ should "call destroy with the raise errors flag" do
79
+ @value.expects(:destroy).with(anything, true).once
80
+ @value.port = 7
81
+ @value.save(true)
82
+ end
83
+ end
84
+
85
+ context "creating a new attached device" do
86
+ setup do
87
+ @image = VirtualBox::HardDrive.new
88
+ @ad = VirtualBox::AttachedDevice.new
89
+ @ad.image = @image
90
+ @ad.port = 3
91
+ end
92
+
93
+ should "raise a NoParentException if it wasn't added to a relationship" do
94
+ assert_raises(VirtualBox::Exceptions::NoParentException) {
95
+ @ad.save
96
+ }
97
+ end
98
+
99
+ context "has a parent" do
100
+ setup do
101
+ @ad.added_to_relationship(@caller)
102
+ VirtualBox::Command.stubs(:vboxmanage)
103
+ end
104
+
105
+ should "not call destroy since its a new record" do
106
+ @ad.expects(:destroy).never
107
+ assert @ad.save
108
+ end
109
+
110
+ should "not raise an InvalidObjectException if no image is set" do
111
+ @ad.image = nil
112
+ assert_raises(VirtualBox::Exceptions::InvalidObjectException) {
113
+ @ad.save
114
+ }
115
+ end
116
+
117
+ should "call the proper vboxcommand" do
118
+ 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}")
119
+ @ad.save
120
+ end
121
+
122
+ should "return false if the command failed" do
123
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
124
+ assert !@ad.save
125
+ end
126
+
127
+ should "return true if the command was a success" do
128
+ assert @ad.save
129
+ end
130
+
131
+ should "raise an exception if true sent to save and error occured" do
132
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
133
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
134
+ @ad.save(true)
135
+ }
136
+ end
137
+
138
+ should "not be a new record after saving" do
139
+ assert @ad.new_record?
140
+ assert @ad.save
141
+ assert !@ad.new_record?
142
+ end
143
+
144
+ should "not be changed after saving" do
145
+ assert @ad.changed?
146
+ assert @ad.save
147
+ assert !@ad.changed?
148
+ end
149
+ end
150
+ end
151
+
152
+ context "constructor" do
153
+ should "call populate_from_data if 3 args are given" do
154
+ VirtualBox::AttachedDevice.any_instance.expects(:populate_from_data).with(1,2,3).once
155
+ VirtualBox::AttachedDevice.new(1,2,3)
156
+ end
157
+
158
+ should "call populate_attributes if 1 arg is given" do
159
+ VirtualBox::AttachedDevice.any_instance.expects(:populate_attributes).with(1).once
160
+ ad = VirtualBox::AttachedDevice.new(1)
161
+ assert ad.new_record?
162
+ end
163
+
164
+ should "raise a NoMethodError if anything other than 0,1,or 3 arguments" do
165
+ # 9 seems like a reasonable max (maybe just a bit unreasonable!)
166
+ 2.upto(9) do |i|
167
+ next if i == 3
168
+ args = Array.new(i, "A")
169
+
170
+ assert_raises(NoMethodError) {
171
+ VirtualBox::AttachedDevice.new(*args)
172
+ }
173
+ end
174
+ end
175
+ end
176
+
22
177
  context "destroying" do
23
178
  setup do
24
179
  @value = VirtualBox::AttachedDevice.populate_relationship(@caller, @data)
@@ -48,6 +203,16 @@ class AttachedDeviceTest < Test::Unit::TestCase
48
203
  @value.destroy
49
204
  end
50
205
 
206
+ should "destroy with the specified port if set" do
207
+ 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")
208
+ @value.destroy(:port => 80)
209
+ end
210
+
211
+ should "destroy with the default port if not other port is specified" do
212
+ 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")
213
+ @value.destroy
214
+ end
215
+
51
216
  should "not destroy image by default" do
52
217
  @image.expects(:destroy).never
53
218
  @value.destroy
@@ -66,6 +231,23 @@ class AttachedDeviceTest < Test::Unit::TestCase
66
231
  :destroy_image => true
67
232
  })
68
233
  end
234
+
235
+ should "return false if destroy failed" do
236
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
237
+ assert !@value.destroy
238
+ end
239
+
240
+ should "raise an exception if destroy failed and an error occured" do
241
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
242
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
243
+ @value.destroy({}, true)
244
+ }
245
+ end
246
+
247
+ should "forward raise_errors flag to image.destroy" do
248
+ @image.expects(:destroy).with(true).once
249
+ @value.destroy({:destroy_image => true}, true)
250
+ end
69
251
  end
70
252
 
71
253
  context "populating relationships" do
@@ -79,12 +261,12 @@ class AttachedDeviceTest < Test::Unit::TestCase
79
261
 
80
262
  should "create objects with proper values" do
81
263
  obj = @value[0]
82
- assert_equal "foomedium", obj.medium
264
+ assert_equal "none", obj.medium
83
265
  assert_equal "322f79fd-7da6-416f-a16f-e70066ccf165", obj.uuid
84
266
  assert_equal 0, obj.port
85
267
 
86
268
  obj = @value[1]
87
- assert_equal "barmedium", obj.medium
269
+ assert_equal "none", obj.medium
88
270
  assert_nil obj.uuid
89
271
  assert_equal 1, obj.port
90
272
  end
@@ -9,6 +9,42 @@ class CommandTest < Test::Unit::TestCase
9
9
  end
10
10
  end
11
11
 
12
+ context "executing commands" do
13
+ should "use backticks to execute the command" do
14
+ VirtualBox::Command.expects(:`).with("foo").once
15
+ VirtualBox::Command.execute("foo")
16
+ end
17
+
18
+ should "return the result of the execution" do
19
+ VirtualBox::Command.expects(:`).with("foo").returns("bar").once
20
+ assert_equal "bar", VirtualBox::Command.execute("foo")
21
+ end
22
+ end
23
+
24
+ context "vbox commands" do
25
+ should "call 'vboxmanage' followed by command" do
26
+ VirtualBox::Command.expects(:execute).with("VBoxManage foo")
27
+ VirtualBox::Command.stubs(:success?).returns(true)
28
+ VirtualBox::Command.vboxmanage("foo")
29
+ end
30
+
31
+ should "call the custom vboxmanage executable if set" do
32
+ VirtualBox::Command.vboxmanage = "barf"
33
+ VirtualBox::Command.expects(:execute).with("barf foo")
34
+ VirtualBox::Command.stubs(:success?).returns(true)
35
+ VirtualBox::Command.vboxmanage("foo")
36
+ VirtualBox::Command.vboxmanage = "VBoxManage"
37
+ end
38
+
39
+ should "raise a CommandFailedException if it failed" do
40
+ VirtualBox::Command.expects(:execute).with("VBoxManage foo")
41
+ VirtualBox::Command.stubs(:success?).returns(false)
42
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
43
+ VirtualBox::Command.vboxmanage("foo")
44
+ }
45
+ end
46
+ end
47
+
12
48
  context "testing command results" do
13
49
  setup do
14
50
  @command = "foo"
@@ -1,6 +1,49 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
2
 
3
3
  class DVDTest < Test::Unit::TestCase
4
+ setup do
5
+ VirtualBox::Command.stubs(:execute)
6
+ end
7
+
8
+ context "destroying a dvd" do
9
+ setup do
10
+ @dvd = VirtualBox::DVD.new
11
+ end
12
+
13
+ should "return false if attempting to destroy an empty drive" do
14
+ assert !VirtualBox::DVD.empty_drive.destroy
15
+ end
16
+
17
+ should "call vboxmanage to destroy it" do
18
+ VirtualBox::Command.expects(:vboxmanage).with("closemedium dvd #{@dvd.uuid} --delete")
19
+ assert @dvd.destroy
20
+ end
21
+
22
+ should "return false if destroy failed" do
23
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
24
+ assert !@dvd.destroy
25
+ end
26
+
27
+ should "raise an exception if failed and flag is set" do
28
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
29
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
30
+ @dvd.destroy(true)
31
+ }
32
+ end
33
+ end
34
+
35
+ context "empty drive" do
36
+ should "return an empty drive instance by calling new with :empty_drive" do
37
+ dvd = VirtualBox::DVD.new(:empty_drive)
38
+ assert dvd.empty_drive?
39
+ end
40
+
41
+ should "call new with :empty_drive with empty_drive class method" do
42
+ dvd = VirtualBox::DVD.empty_drive
43
+ assert dvd.empty_drive?
44
+ end
45
+ end
46
+
4
47
  context "retrieving all dvds" do
5
48
  setup do
6
49
  @expectations = {
@@ -23,10 +23,33 @@ raw
23
23
  VirtualBox::Command.stubs(:vboxmanage).with("showhdinfo #{@name}").returns(@find_raw)
24
24
  end
25
25
 
26
+ context "destroying a hard drive" do
27
+ setup do
28
+ @hd = VirtualBox::HardDrive.find(@name)
29
+ end
30
+
31
+ should "call vboxmanage to destroy it" do
32
+ VirtualBox::Command.expects(:vboxmanage).with("closemedium disk #{@hd.uuid} --delete")
33
+ assert @hd.destroy
34
+ end
35
+
36
+ should "return false if destroy failed" do
37
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
38
+ assert !@hd.destroy
39
+ end
40
+
41
+ should "raise an exception if failed and flag is set" do
42
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
43
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
44
+ @hd.destroy(true)
45
+ }
46
+ end
47
+ end
48
+
26
49
  context "cloning a hard drive" do
27
50
  setup do
28
51
  @hd = VirtualBox::HardDrive.find(@name)
29
- VirtualBox::Command.expects(:vboxmanage).with("clonehd #{@hd.uuid} bar --format VDI --remember").returns(@find_raw)
52
+ VirtualBox::Command.stubs(:vboxmanage).with("clonehd #{@hd.uuid} bar --format VDI --remember").returns(@find_raw)
30
53
  end
31
54
 
32
55
  should "call vboxmanage with the clone command" do
@@ -39,6 +62,18 @@ raw
39
62
  VirtualBox::HardDrive.expects(:find).returns(@new_hd)
40
63
  assert_equal @new_hd, @hd.clone("bar")
41
64
  end
65
+
66
+ should "return false on failure" do
67
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
68
+ assert @hd.clone("bar").nil?
69
+ end
70
+
71
+ should "raise an exception if raise_errors is true and failed" do
72
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
73
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
74
+ @hd.clone("bar", "VDI", true)
75
+ }
76
+ end
42
77
  end
43
78
 
44
79
  context "creating a hard drive" do
@@ -85,6 +120,22 @@ raw
85
120
 
86
121
  assert_equal "foo", @hd.uuid
87
122
  end
123
+
124
+ should "return true if the command was a success" do
125
+ assert @hd.save
126
+ end
127
+
128
+ should "return failure if the command failed" do
129
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
130
+ assert !@hd.save
131
+ end
132
+
133
+ should "raise an exception if flag is set" do
134
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
135
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
136
+ @hd.save(true)
137
+ }
138
+ end
88
139
  end
89
140
 
90
141
  context "finding a single hard drive" do
@@ -1,6 +1,85 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
2
 
3
3
  class ImageTest < Test::Unit::TestCase
4
+ context "image type and empty drive" do
5
+ setup do
6
+ @image = VirtualBox::Image.new
7
+ end
8
+
9
+ should "raise an exception if image_type is called on Image" do
10
+ assert_raises(RuntimeError) { @image.image_type }
11
+ end
12
+
13
+ should "return false by default on empty_drive?" do
14
+ assert !@image.empty_drive?
15
+ end
16
+ end
17
+
18
+ context "in a relationship" do
19
+ class ImageRelationshipModel < VirtualBox::AbstractModel
20
+ relationship :image, VirtualBox::Image
21
+ end
22
+
23
+ setup do
24
+ @model = ImageRelationshipModel.new
25
+ end
26
+
27
+ context "populating a relationship" do
28
+ should "return 'emptydrive' if the medium is an empty drive" do
29
+ result = VirtualBox::Image.populate_relationship(@model, {
30
+ :medium => "emptydrive"
31
+ })
32
+
33
+ assert result.is_a?(VirtualBox::DVD)
34
+ assert result.empty_drive?
35
+ end
36
+
37
+ should "return nil if uuid is nil and medium isn't empty" do
38
+ result = VirtualBox::Image.populate_relationship(@model, {})
39
+ assert result.nil?
40
+ end
41
+
42
+ should "result a matching image from subclasses if uuid" do
43
+ uuid = "foo'"
44
+
45
+ subobject = mock("subobject")
46
+ subobject.stubs(:uuid).returns(uuid)
47
+
48
+ subclass = mock("subclass")
49
+ subclass.stubs(:all).returns([subobject])
50
+
51
+ VirtualBox::Image.expects(:subclasses).returns([subclass])
52
+
53
+ result = VirtualBox::Image.populate_relationship(@model, { :uuid => uuid })
54
+ assert_equal subobject, result
55
+ end
56
+
57
+ should "result in nil if suboject can't be found" do
58
+ VirtualBox::Image.expects(:subclasses).returns([])
59
+ assert_nil VirtualBox::Image.populate_relationship(@model, { :uuid => "foo" })
60
+ end
61
+ end
62
+
63
+ context "setting relationship object" do
64
+ should "raise an InvalidRelationshipObjectException if new value is not an image" do
65
+ assert_raises(VirtualBox::Exceptions::InvalidRelationshipObjectException) {
66
+ @model.image = "foo"
67
+ }
68
+ end
69
+
70
+ should "not raise an exception if setting to nil" do
71
+ assert_nothing_raised { @model.image = nil }
72
+ end
73
+
74
+ should "just return the new value if it is an image" do
75
+ image = VirtualBox::Image.new
76
+ assert_nil @model.image
77
+ assert_nothing_raised { @model.image = image }
78
+ assert_equal image, @model.image
79
+ end
80
+ end
81
+ end
82
+
4
83
  context "recording subclasses" do
5
84
  should "list all subclasses" do
6
85
  assert_nothing_raised { VirtualBox::Image.subclasses }
@@ -48,6 +48,16 @@ raw
48
48
  assert nic.nictype_changed?
49
49
  nic.save(@vmname)
50
50
  end
51
+
52
+ should "raise a CommandFailedException if it fails" do
53
+ VirtualBox::Command.stubs(:vboxmanage).raises(VirtualBox::Exceptions::CommandFailedException)
54
+
55
+ nic = @nic[0]
56
+ nic.nictype = "ZOO"
57
+ assert_raises(VirtualBox::Exceptions::CommandFailedException) {
58
+ nic.save(@vmname)
59
+ }
60
+ end
51
61
  end
52
62
 
53
63
  context "populating relationships" do
@@ -0,0 +1,45 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class CollectionTest < Test::Unit::TestCase
4
+ setup do
5
+ @parent = mock("parent")
6
+ @collection = VirtualBox::Proxies::Collection.new(@parent)
7
+ end
8
+
9
+ should "be a subclass of Array" do
10
+ assert @collection.is_a?(Array)
11
+ end
12
+
13
+ context "element callbacks" do
14
+ setup do
15
+ @item = mock("item")
16
+ end
17
+
18
+ should "not call added_to_relationship if it doesn't exist" do
19
+ assert_nothing_raised { @collection << @item }
20
+ end
21
+
22
+ should "not call removed_from_relationship if it doesn't exist" do
23
+ @collection << @item
24
+ assert_nothing_raised { @collection.delete(@item) }
25
+ end
26
+
27
+ should "call added_to_relationship on the item when its added to a collection" do
28
+ @item.expects(:added_to_relationship).with(@parent).once
29
+ @collection << @item
30
+ end
31
+
32
+ should "call removed_from_relationship on the item when its deleted" do
33
+ @collection << @item
34
+ @item.expects(:removed_from_relationship).with(@parent).once
35
+ @collection.delete(@item)
36
+ end
37
+
38
+ should "call removed_from_relationship if clear is called" do
39
+ @collection << @item
40
+ @item.expects(:removed_from_relationship).with(@parent).once
41
+ @collection.clear
42
+ assert @collection.empty?
43
+ end
44
+ end
45
+ end
@@ -15,6 +15,18 @@ class StorageControllerTest < Test::Unit::TestCase
15
15
  @caller = mock("caller")
16
16
  end
17
17
 
18
+ context "saving" do
19
+ setup do
20
+ @value = VirtualBox::StorageController.populate_relationship(@caller, @data)
21
+ @value = @value[0]
22
+ end
23
+
24
+ should "save relationship" do
25
+ VirtualBox::AttachedDevice.expects(:save_relationship).once
26
+ @value.save
27
+ end
28
+ end
29
+
18
30
  context "destroying" do
19
31
  setup do
20
32
  @value = VirtualBox::StorageController.populate_relationship(@caller, @data)