virtualbox 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/.gitignore +2 -1
  2. data/Gemfile +1 -1
  3. data/Rakefile +1 -1
  4. data/Readme.md +5 -21
  5. data/VERSION +1 -1
  6. data/docs/WhatsNew.md +9 -47
  7. data/lib/virtualbox.rb +7 -30
  8. data/lib/virtualbox/abstract_model.rb +25 -5
  9. data/lib/virtualbox/abstract_model/attributable.rb +5 -1
  10. data/lib/virtualbox/abstract_model/dirty.rb +2 -0
  11. data/lib/virtualbox/abstract_model/interface_attributes.rb +96 -0
  12. data/lib/virtualbox/abstract_model/relatable.rb +19 -8
  13. data/lib/virtualbox/appliance.rb +59 -0
  14. data/lib/virtualbox/audio_adapter.rb +44 -0
  15. data/lib/virtualbox/bios.rb +44 -0
  16. data/lib/virtualbox/com.rb +23 -0
  17. data/lib/virtualbox/com/abstract_enum.rb +42 -0
  18. data/lib/virtualbox/com/abstract_implementer.rb +43 -0
  19. data/lib/virtualbox/com/abstract_interface.rb +165 -0
  20. data/lib/virtualbox/com/ffi/interface.rb +141 -0
  21. data/lib/virtualbox/com/ffi/interfaces.rb +42 -0
  22. data/lib/virtualbox/com/ffi/util.rb +101 -0
  23. data/lib/virtualbox/com/ffi/vboxxpcomc.rb +31 -0
  24. data/lib/virtualbox/com/ffi_interface.rb +65 -0
  25. data/lib/virtualbox/com/implementer/base.rb +52 -0
  26. data/lib/virtualbox/com/implementer/ffi.rb +350 -0
  27. data/lib/virtualbox/com/implementer/mscom.rb +165 -0
  28. data/lib/virtualbox/com/implementer/nil.rb +10 -0
  29. data/lib/virtualbox/com/interface/appliance.rb +20 -0
  30. data/lib/virtualbox/com/interface/audio_adapter.rb +13 -0
  31. data/lib/virtualbox/com/interface/audio_controller_type.rb +9 -0
  32. data/lib/virtualbox/com/interface/audio_driver_type.rb +9 -0
  33. data/lib/virtualbox/com/interface/bios_boot_menu_mode.rb +9 -0
  34. data/lib/virtualbox/com/interface/bios_settings.rb +19 -0
  35. data/lib/virtualbox/com/interface/clipboard_mode.rb +9 -0
  36. data/lib/virtualbox/com/interface/console.rb +48 -0
  37. data/lib/virtualbox/com/interface/cpu_property_type.rb +9 -0
  38. data/lib/virtualbox/com/interface/device_type.rb +9 -0
  39. data/lib/virtualbox/com/interface/dhcp_server.rb +20 -0
  40. data/lib/virtualbox/com/interface/firmware_type.rb +9 -0
  41. data/lib/virtualbox/com/interface/guest_os_type.rb +21 -0
  42. data/lib/virtualbox/com/interface/host.rb +40 -0
  43. data/lib/virtualbox/com/interface/host_network_interface.rb +28 -0
  44. data/lib/virtualbox/com/interface/host_network_interface_medium_type.rb +9 -0
  45. data/lib/virtualbox/com/interface/host_network_interface_status.rb +9 -0
  46. data/lib/virtualbox/com/interface/host_network_interface_type.rb +9 -0
  47. data/lib/virtualbox/com/interface/host_usb_device.rb +11 -0
  48. data/lib/virtualbox/com/interface/host_usb_device_filter.rb +11 -0
  49. data/lib/virtualbox/com/interface/hw_virt_ex_property_type.rb +9 -0
  50. data/lib/virtualbox/com/interface/machine.rb +103 -0
  51. data/lib/virtualbox/com/interface/machine_state.rb +12 -0
  52. data/lib/virtualbox/com/interface/medium.rb +48 -0
  53. data/lib/virtualbox/com/interface/medium_attachment.rb +16 -0
  54. data/lib/virtualbox/com/interface/medium_format.rb +16 -0
  55. data/lib/virtualbox/com/interface/medium_state.rb +9 -0
  56. data/lib/virtualbox/com/interface/medium_type.rb +9 -0
  57. data/lib/virtualbox/com/interface/medium_variant.rb +9 -0
  58. data/lib/virtualbox/com/interface/network_adapter.rb +28 -0
  59. data/lib/virtualbox/com/interface/network_adapter_type.rb +9 -0
  60. data/lib/virtualbox/com/interface/network_attachment_type.rb +9 -0
  61. data/lib/virtualbox/com/interface/nsiexception.rb +21 -0
  62. data/lib/virtualbox/com/interface/nsisupports.rb +13 -0
  63. data/lib/virtualbox/com/interface/parallel_port.rb +15 -0
  64. data/lib/virtualbox/com/interface/port_mode.rb +9 -0
  65. data/lib/virtualbox/com/interface/progress.rb +58 -0
  66. data/lib/virtualbox/com/interface/serial_port.rb +17 -0
  67. data/lib/virtualbox/com/interface/session.rb +16 -0
  68. data/lib/virtualbox/com/interface/session_state.rb +9 -0
  69. data/lib/virtualbox/com/interface/session_type.rb +9 -0
  70. data/lib/virtualbox/com/interface/shared_folder.rb +15 -0
  71. data/lib/virtualbox/com/interface/snapshot.rb +18 -0
  72. data/lib/virtualbox/com/interface/storage_bus.rb +9 -0
  73. data/lib/virtualbox/com/interface/storage_controller.rb +21 -0
  74. data/lib/virtualbox/com/interface/storage_controller_type.rb +9 -0
  75. data/lib/virtualbox/com/interface/system_properties.rb +35 -0
  76. data/lib/virtualbox/com/interface/usb_controller.rb +18 -0
  77. data/lib/virtualbox/com/interface/usb_device.rb +22 -0
  78. data/lib/virtualbox/com/interface/usb_device_filter.rb +21 -0
  79. data/lib/virtualbox/com/interface/usb_device_filter_action.rb +9 -0
  80. data/lib/virtualbox/com/interface/usb_device_state.rb +9 -0
  81. data/lib/virtualbox/com/interface/virtual_box_error_info.rb +15 -0
  82. data/lib/virtualbox/com/interface/virtual_system_description.rb +17 -0
  83. data/lib/virtualbox/com/interface/virtual_system_description_type.rb +12 -0
  84. data/lib/virtualbox/com/interface/virtual_system_description_value_type.rb +9 -0
  85. data/lib/virtualbox/com/interface/virtualbox.rb +54 -0
  86. data/lib/virtualbox/com/interface/vrdp_auth_type.rb +9 -0
  87. data/lib/virtualbox/com/interface/vrdp_server.rb +17 -0
  88. data/lib/virtualbox/com/mscom_interface.rb +22 -0
  89. data/lib/virtualbox/com/util.rb +18 -0
  90. data/lib/virtualbox/dvd.rb +7 -94
  91. data/lib/virtualbox/exceptions.rb +24 -0
  92. data/lib/virtualbox/ext/glob_loader.rb +22 -0
  93. data/lib/virtualbox/ext/logger.rb +38 -0
  94. data/lib/virtualbox/ext/platform.rb +1 -1
  95. data/lib/virtualbox/extra_data.rb +25 -37
  96. data/lib/virtualbox/forwarded_port.rb +35 -13
  97. data/lib/virtualbox/global.rb +22 -80
  98. data/lib/virtualbox/hard_drive.rb +30 -97
  99. data/lib/virtualbox/lib.rb +82 -0
  100. data/lib/virtualbox/media.rb +7 -6
  101. data/lib/virtualbox/medium.rb +138 -0
  102. data/lib/virtualbox/medium_attachment.rb +61 -0
  103. data/lib/virtualbox/network_adapter.rb +134 -0
  104. data/lib/virtualbox/shared_folder.rb +53 -78
  105. data/lib/virtualbox/storage_controller.rb +76 -20
  106. data/lib/virtualbox/system_properties.rb +74 -0
  107. data/lib/virtualbox/usb_controller.rb +55 -0
  108. data/lib/virtualbox/version.rb +15 -0
  109. data/lib/virtualbox/virtual_system_description.rb +47 -0
  110. data/lib/virtualbox/vm.rb +160 -272
  111. data/test/test_helper.rb +0 -108
  112. data/test/virtualbox/abstract_model/attributable_test.rb +7 -1
  113. data/test/virtualbox/abstract_model/dirty_test.rb +1 -1
  114. data/test/virtualbox/abstract_model/interface_attributes_test.rb +169 -0
  115. data/test/virtualbox/abstract_model/relatable_test.rb +20 -0
  116. data/test/virtualbox/abstract_model_test.rb +40 -5
  117. data/test/virtualbox/appliance_test.rb +152 -0
  118. data/test/virtualbox/audio_adapter_test.rb +83 -0
  119. data/test/virtualbox/bios_test.rb +83 -0
  120. data/test/virtualbox/com/abstract_enum_test.rb +48 -0
  121. data/test/virtualbox/com/abstract_implementer_test.rb +39 -0
  122. data/test/virtualbox/com/abstract_interface_test.rb +139 -0
  123. data/test/virtualbox/com/ffi/interface_test.rb +249 -0
  124. data/test/virtualbox/com/ffi/util_test.rb +86 -0
  125. data/test/virtualbox/com/ffi_interface_test.rb +42 -0
  126. data/test/virtualbox/com/implementer/base_test.rb +37 -0
  127. data/test/virtualbox/com/implementer/ffi_test.rb +519 -0
  128. data/test/virtualbox/com/implementer/mscom_test.rb +208 -0
  129. data/test/virtualbox/com/mscom_interface_test.rb +17 -0
  130. data/test/virtualbox/com/util_test.rb +17 -0
  131. data/test/virtualbox/dvd_test.rb +4 -95
  132. data/test/virtualbox/ext/platform_test.rb +8 -0
  133. data/test/virtualbox/extra_data_test.rb +78 -102
  134. data/test/virtualbox/forwarded_port_test.rb +57 -7
  135. data/test/virtualbox/global_test.rb +25 -115
  136. data/test/virtualbox/hard_drive_test.rb +49 -212
  137. data/test/virtualbox/lib_test.rb +93 -0
  138. data/test/virtualbox/medium_attachment_test.rb +147 -0
  139. data/test/virtualbox/medium_test.rb +192 -0
  140. data/test/virtualbox/network_adapter_test.rb +160 -0
  141. data/test/virtualbox/shared_folder_test.rb +144 -160
  142. data/test/virtualbox/storage_controller_test.rb +166 -45
  143. data/test/virtualbox/system_properties_test.rb +87 -0
  144. data/test/virtualbox/usb_controller_test.rb +104 -0
  145. data/test/virtualbox/version_test.rb +34 -0
  146. data/test/virtualbox/virtual_system_description_test.rb +61 -0
  147. data/test/virtualbox/vm_test.rb +288 -322
  148. data/test/virtualbox_test.rb +1 -9
  149. data/virtualbox.gemspec +139 -23
  150. metadata +143 -27
  151. data/lib/virtualbox/attached_device.rb +0 -249
  152. data/lib/virtualbox/command.rb +0 -109
  153. data/lib/virtualbox/image.rb +0 -137
  154. data/lib/virtualbox/nic.rb +0 -111
  155. data/lib/virtualbox/system_property.rb +0 -55
  156. data/lib/virtualbox/usb.rb +0 -72
  157. data/test/virtualbox/attached_device_test.rb +0 -303
  158. data/test/virtualbox/command_test.rb +0 -152
  159. data/test/virtualbox/image_test.rb +0 -190
  160. data/test/virtualbox/nic_test.rb +0 -76
  161. data/test/virtualbox/system_property_test.rb +0 -71
  162. data/test/virtualbox/usb_test.rb +0 -35
@@ -0,0 +1,93 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class LibTest < Test::Unit::TestCase
4
+ context "the virtualbox library file path" do
5
+ setup do
6
+ VirtualBox::Lib.lib_path = nil
7
+ end
8
+
9
+ should "return the path if its set" do
10
+ File.expects(:expand_path).with("foo").returns("expanded_foo")
11
+ VirtualBox::Lib.lib_path = "foo"
12
+ assert_equal "expanded_foo", VirtualBox::Lib.lib_path
13
+ end
14
+
15
+ should "return Mac-path if on mac" do
16
+ result = ["/Applications/VirtualBox.app/Contents/MacOS/VBoxXPCOMC.dylib"]
17
+ VirtualBox::Platform.stubs(:mac?).returns(true)
18
+
19
+ assert_equal result, VirtualBox::Lib.lib_path
20
+ end
21
+
22
+ should "return Windows-path if on windows" do
23
+ result = "Unknown"
24
+ VirtualBox::Platform.stubs(:mac?).returns(false)
25
+ VirtualBox::Platform.stubs(:linux?).returns(false)
26
+ VirtualBox::Platform.stubs(:windows?).returns(true)
27
+
28
+ assert_equal result, VirtualBox::Lib.lib_path
29
+ end
30
+
31
+ should "return Linux-path if on linux" do
32
+ result = ["/opt/VirtualBox/VBoxXPCOMC.so", "/usr/lib/virtualbox/VBoxXPCOMC.so"]
33
+ VirtualBox::Platform.stubs(:mac?).returns(false)
34
+ VirtualBox::Platform.stubs(:windows?).returns(false)
35
+ VirtualBox::Platform.stubs(:linux?).returns(true)
36
+
37
+ assert_equal result, VirtualBox::Lib.lib_path
38
+ end
39
+
40
+ should "return 'unknown' otherwise" do
41
+ result = "Unknown"
42
+ VirtualBox::Platform.stubs(:mac?).returns(false)
43
+ VirtualBox::Platform.stubs(:windows?).returns(false)
44
+ VirtualBox::Platform.stubs(:linux?).returns(false)
45
+
46
+ assert_equal result, VirtualBox::Lib.lib_path
47
+ end
48
+ end
49
+
50
+ context "accessing the lib" do
51
+ setup do
52
+ @lib_path = "foo"
53
+ VirtualBox::Lib.stubs(:lib_path).returns(@lib_path)
54
+ VirtualBox::Lib.reset!
55
+ end
56
+
57
+ should "create a new Lib instance with the lib path once" do
58
+ instance = mock("instance")
59
+ VirtualBox::Lib.expects(:new).once.returns(instance)
60
+ assert_equal instance, VirtualBox::Lib.lib
61
+ assert_equal instance, VirtualBox::Lib.lib
62
+ assert_equal instance, VirtualBox::Lib.lib
63
+ end
64
+ end
65
+
66
+ context "init-ing" do
67
+ setup do
68
+ @lib_path = "foo"
69
+
70
+ @virtualbox = mock("virtualbox")
71
+ @session = mock("session")
72
+ @interface = mock("interface")
73
+ @interface.stubs(:virtualbox).returns(@virtualbox)
74
+ @interface.stubs(:session).returns(@session)
75
+ end
76
+
77
+ should "call init on FFI with the lib path for mac" do
78
+ VirtualBox::Platform.stubs(:windows?).returns(false)
79
+ VirtualBox::COM::FFIInterface.expects(:create).with(@lib_path).once.returns(@interface)
80
+ lib = VirtualBox::Lib.new(@lib_path)
81
+ assert_equal @virtualbox, lib.virtualbox
82
+ assert_equal @session, lib.session
83
+ end
84
+
85
+ should "init MSCOM for windows" do
86
+ VirtualBox::Platform.stubs(:windows?).returns(true)
87
+ VirtualBox::COM::MSCOMInterface.expects(:new).once.returns(@interface)
88
+ lib = VirtualBox::Lib.new(@lib_path)
89
+ assert_equal @virtualbox, lib.virtualbox
90
+ assert_equal @session, lib.session
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,147 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class MediumAttachmentTest < Test::Unit::TestCase
4
+ setup do
5
+ @interface = mock("interface")
6
+ @interface.stubs(:medium)
7
+ @parent = mock("parent")
8
+
9
+ @klass = VirtualBox::MediumAttachment
10
+ end
11
+
12
+ context "class methods" do
13
+ context "populating relationships" do
14
+ setup do
15
+ @instance = mock("instance")
16
+
17
+ @interface.stubs(:medium_attachments).returns([])
18
+
19
+ @klass.stubs(:device_type).returns(:all)
20
+ @klass.stubs(:new).returns(@instance)
21
+ end
22
+
23
+ should "return a proxied collection" do
24
+ result = @klass.populate_relationship(nil, @interface)
25
+ assert result.is_a?(VirtualBox::Proxies::Collection)
26
+ end
27
+
28
+ should "call new for every medium if device type is all" do
29
+ attachments = []
30
+ @interface.stubs(:medium_attachments).returns(attachments)
31
+ 5.times { |i| attachments << mock("a#{i}") }
32
+
33
+ expected_result = []
34
+ new_seq = sequence("new_seq")
35
+ attachments.each do |attachment|
36
+ expected_value = "instance-#{attachment.inspect}"
37
+ @klass.expects(:new).with(@parent, attachment).in_sequence(new_seq).returns(expected_value)
38
+ expected_result << expected_value
39
+ end
40
+
41
+ assert_equal expected_result, @klass.populate_relationship(@parent, @interface)
42
+ end
43
+ end
44
+ end
45
+
46
+ context "initializing" do
47
+ should "load attributes from the machine" do
48
+ @klass.any_instance.expects(:initialize_attributes).with(@interface).once
49
+ @klass.new(@parent, @interface)
50
+ end
51
+ end
52
+
53
+ context "initializing attributes" do
54
+ setup do
55
+ @klass.any_instance.stubs(:load_interface_attributes)
56
+ @klass.any_instance.stubs(:populate_relationship)
57
+ end
58
+
59
+ should "load interface attribtues" do
60
+ @klass.any_instance.expects(:load_interface_attributes).with(@interface).once
61
+ @klass.new(@parent, @interface)
62
+ end
63
+
64
+ should "populate relationships" do
65
+ @klass.any_instance.expects(:populate_relationship).twice
66
+ @klass.new(@parent, @interface)
67
+ end
68
+
69
+ should "not be dirty" do
70
+ @instance = @klass.new(@parent, @interface)
71
+ assert !@instance.changed?
72
+ end
73
+
74
+ should "be existing record" do
75
+ @instance = @klass.new(@parent, @interface)
76
+ assert !@instance.new_record?
77
+ end
78
+ end
79
+
80
+ context "instance methods" do
81
+ setup do
82
+ @klass.any_instance.stubs(:initialize_attributes)
83
+
84
+ @parent = mock("parent")
85
+ @interface = mock("interface")
86
+ @instance = @klass.new(@parent, @interface)
87
+ end
88
+
89
+ context "detaching" do
90
+ setup do
91
+ @machine = mock("machine")
92
+ @session = mock("session")
93
+ @session.stubs(:machine).returns(@machine)
94
+
95
+ @sc = mock("storage_controller")
96
+ @sc.stubs(:name).returns("foo")
97
+ @instance.stubs(:storage_controller).returns(@sc)
98
+ @instance.stubs(:port).returns(7)
99
+ @instance.stubs(:device).returns(12)
100
+ end
101
+
102
+ should "open session, detach, then save" do
103
+ det_seq = sequence("detach_seq")
104
+ @parent.expects(:with_open_session).yields(@session).in_sequence(det_seq)
105
+ @machine.expects(:detach_device).with(@sc.name, @instance.port, @instance.device).in_sequence(det_seq)
106
+
107
+ @instance.detach
108
+ end
109
+ end
110
+
111
+ context "destroying" do
112
+ setup do
113
+ @medium = mock("medium")
114
+ @instance.stubs(:medium).returns(@medium)
115
+ end
116
+
117
+ should "just detach" do
118
+ @instance.expects(:detach).once
119
+ @instance.destroy
120
+ end
121
+
122
+ should "not destroy medium if nil but specified" do
123
+ @instance.stubs(:medium).returns(nil)
124
+
125
+ @instance.expects(:detach).once
126
+ @medium.expects(:destroy).with(false).never
127
+ assert_nothing_raised {
128
+ @instance.destroy(:destroy_medium => true)
129
+ }
130
+ end
131
+
132
+ should "destroy medium if specified" do
133
+ destroy_seq = sequence("destroy_seq")
134
+ @instance.expects(:detach).once.in_sequence(destroy_seq)
135
+ @medium.expects(:destroy).with(false).once.in_sequence(destroy_seq)
136
+ @instance.destroy(:destroy_medium => true)
137
+ end
138
+
139
+ should "destroy medium and backing store if specified" do
140
+ destroy_seq = sequence("destroy_seq")
141
+ @instance.expects(:detach).once.in_sequence(destroy_seq)
142
+ @medium.expects(:destroy).with(true).once.in_sequence(destroy_seq)
143
+ @instance.destroy(:destroy_medium => :delete)
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,192 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class MediumTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = VirtualBox::Medium
6
+ @imedium = mock("IMedium")
7
+ @interface = @imedium
8
+ @imedium.stubs(:refresh_state)
9
+ end
10
+
11
+ context "class methods" do
12
+ context "device type" do
13
+ should "be all on the medium" do
14
+ assert_equal :all, @klass.device_type
15
+ end
16
+ end
17
+
18
+ context "populating relationships" do
19
+ setup do
20
+ @caller = mock("caller")
21
+ end
22
+
23
+ should "call populate_array_relationship for arrays" do
24
+ @klass.expects(:populate_array_relationship).with(@caller, []).once
25
+ @klass.populate_relationship(@caller, [])
26
+ end
27
+
28
+ should "call populate_single_relationship for non-arrays" do
29
+ @klass.expects(:populate_single_relationship).with(@caller, nil).once
30
+ @klass.populate_relationship(@caller, nil)
31
+ end
32
+ end
33
+
34
+ context "populating array relationship" do
35
+ setup do
36
+ @instance = mock("instance")
37
+
38
+ @klass.stubs(:device_type).returns(:all)
39
+ @klass.stubs(:new).returns(@instance)
40
+ end
41
+
42
+ def mock_medium(device_type)
43
+ medium = mock("medium")
44
+ medium.stubs(:device_type).returns(device_type)
45
+ medium
46
+ end
47
+
48
+ should "return a proxied collection" do
49
+ result = @klass.populate_array_relationship(nil, [])
50
+ assert result.is_a?(VirtualBox::Proxies::Collection)
51
+ end
52
+
53
+ should "call new for every medium if device type is all" do
54
+ media = []
55
+ 5.times { |i| media << mock_medium("m#{i}") }
56
+
57
+ expected_result = []
58
+ new_seq = sequence("new_seq")
59
+ media.each do |medium|
60
+ expected_value = "instance-#{medium.inspect}"
61
+ @klass.expects(:new).with(medium).in_sequence(new_seq).returns(expected_value)
62
+ expected_result << expected_value
63
+ end
64
+
65
+ assert_equal expected_result, @klass.populate_array_relationship(nil, media)
66
+ end
67
+
68
+ should "ignore non-matching devices if device_type is not :all" do
69
+ @klass.stubs(:device_type).returns(:foo)
70
+
71
+ media = [mock_medium(:foo), mock_medium(:bar)]
72
+ result = @klass.populate_array_relationship(nil, media)
73
+ assert_equal 1, result.length
74
+ end
75
+ end
76
+
77
+ context "populating a single relationship" do
78
+ setup do
79
+ @subclasses = []
80
+ @klass.stubs(:subclasses).returns(@subclasses)
81
+ @imedium.stubs(:get_device_type).returns(nil)
82
+ end
83
+
84
+ def mock_subclass(device_type)
85
+ subclass = mock("subclass")
86
+ subclass.stubs(:device_type).returns(device_type)
87
+ subclass
88
+ end
89
+
90
+ should "instantiate the matching subclass" do
91
+ type = :foo
92
+ result = mock("result")
93
+ matching = mock_subclass(type)
94
+ matching.expects(:new).with(@imedium).once.returns(result)
95
+ @subclasses << matching
96
+ @imedium.stubs(:device_type).returns(type)
97
+
98
+ assert_equal result, @klass.populate_single_relationship(nil, @imedium)
99
+ end
100
+
101
+ should "return a Medium if nothing matches" do
102
+ result = mock("result")
103
+ @klass.expects(:new).with(@imedium).returns(result)
104
+ assert_equal result, @klass.populate_single_relationship(nil, @imedium)
105
+ end
106
+
107
+ should "return nil if medium given is nil" do
108
+ @klass.expects(:new).never
109
+ assert_nil @klass.populate_single_relationship(nil, nil)
110
+ end
111
+ end
112
+ end
113
+
114
+ context "initializing" do
115
+ should "load attributes from the machine" do
116
+ @klass.any_instance.expects(:initialize_attributes).with(@interface).once
117
+ @klass.new(@interface)
118
+ end
119
+
120
+ should "set the interface as the interface attribute" do
121
+ @klass.any_instance.stubs(:initialize_attributes)
122
+ instance = @klass.new(@interface)
123
+ assert_equal @interface, instance.interface
124
+ end
125
+ end
126
+
127
+ context "initializing attributes" do
128
+ setup do
129
+ @interface.stubs(:refresh_state)
130
+ @klass.any_instance.stubs(:load_interface_attributes)
131
+ end
132
+
133
+ should "refresh state then load interface attribtues" do
134
+ init_seq = sequence("init_seq")
135
+ @interface.expects(:refresh_state).in_sequence(init_seq)
136
+ @klass.any_instance.expects(:load_interface_attributes).with(@interface).once.in_sequence(init_seq)
137
+ @klass.new(@interface)
138
+ end
139
+
140
+ should "not be dirty" do
141
+ @instance = @klass.new(@interface)
142
+ assert !@instance.changed?
143
+ end
144
+
145
+ should "be existing record" do
146
+ @instance = @klass.new(@interface)
147
+ assert !@instance.new_record?
148
+ end
149
+ end
150
+
151
+ context "with an instance" do
152
+ setup do
153
+ @klass.any_instance.stubs(:initialize_attributes)
154
+ @instance = @klass.new(@imedium)
155
+ end
156
+
157
+ context "filename" do
158
+ setup do
159
+ @location = "/foo/bar/baz.rb"
160
+ @instance.stubs(:location).returns(@location)
161
+ end
162
+
163
+ should "return the basename of the location" do
164
+ assert_equal File.basename(@location), @instance.filename
165
+ end
166
+ end
167
+
168
+ context "destroy storage" do
169
+ should "delete the storage then wait for it to complete" do
170
+ progress = mock("progress")
171
+
172
+ @imedium.expects(:delete_storage).once.returns(progress)
173
+ progress.expects(:wait_for_completion).with(-1).once
174
+
175
+ @instance.destroy_storage
176
+ end
177
+ end
178
+
179
+ context "destroying" do
180
+ should "just close the medium if first argument is not specified" do
181
+ @instance.expects(:destroy_storage).never
182
+ @imedium.expects(:close).once
183
+ @instance.destroy
184
+ end
185
+
186
+ should "destroy the storage if first argument is true" do
187
+ @instance.expects(:destroy_storage).once
188
+ @instance.destroy(true)
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,160 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class NetworkAdapterTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = VirtualBox::NetworkAdapter
6
+ @interface = mock("interface")
7
+ @parent = mock("parent")
8
+ end
9
+
10
+ context "initializing" do
11
+ should "load attributes from the machine" do
12
+ @klass.any_instance.expects(:initialize_attributes).with(@parent, @interface).once
13
+ @klass.new(@parent, @interface)
14
+ end
15
+ end
16
+
17
+ context "initializing attributes" do
18
+ setup do
19
+ @klass.any_instance.stubs(:load_interface_attributes)
20
+ @klass.any_instance.stubs(:populate_relationships)
21
+
22
+ @instance = @klass.new(@parent, @interface)
23
+ end
24
+
25
+ should "load interface attribtues" do
26
+ @klass.any_instance.expects(:load_interface_attributes).with(@interface).once
27
+ @klass.new(@parent, @interface)
28
+ end
29
+
30
+ should "setup the parent" do
31
+ assert_equal @parent, @instance.parent
32
+ end
33
+
34
+ should "setup the interface" do
35
+ assert_equal @interface, @instance.interface
36
+ end
37
+
38
+ should "not be dirty" do
39
+ assert !@instance.changed?
40
+ end
41
+
42
+ should "be existing record" do
43
+ assert !@instance.new_record?
44
+ end
45
+ end
46
+
47
+ context "class methods" do
48
+ context "populating relationship" do
49
+ setup do
50
+ @instance = mock("instance")
51
+ @klass.stubs(:new).returns(@instance)
52
+
53
+ @count = 5
54
+ @vbox = mock("vbox")
55
+ @system_properties = mock("sys_props")
56
+ @interface.stubs(:parent).returns(@vbox)
57
+ @vbox.stubs(:system_properties).returns(@system_properties)
58
+ @system_properties.stubs(:network_adapter_count).returns(@count)
59
+ end
60
+
61
+ should "return a proxied collection" do
62
+ @system_properties.stubs(:network_adapter_count).returns(0)
63
+ result = @klass.populate_relationship(nil, @interface)
64
+ assert result.is_a?(VirtualBox::Proxies::Collection)
65
+ end
66
+
67
+ should "call new for every shared folder" do
68
+ expected_result = []
69
+ new_seq = sequence("new_seq")
70
+ @count.times do |i|
71
+ expected_value = "instance-#{i}"
72
+ adapter = mock("adapter#{i}")
73
+ @interface.expects(:get_network_adapter).with(i).returns(adapter).in_sequence(new_seq)
74
+ @klass.expects(:new).with(@parent, adapter).in_sequence(new_seq).returns(expected_value)
75
+ expected_result << expected_value
76
+ end
77
+
78
+ assert_equal expected_result, @klass.populate_relationship(@parent, @interface)
79
+ end
80
+ end
81
+
82
+ context "saving relationship" do
83
+ should "call save on each item" do
84
+ items = (1..5).to_a.collect do |i|
85
+ item = mock("item-#{i}")
86
+ item.expects(:save).once
87
+ item
88
+ end
89
+
90
+ @klass.save_relationship(nil, items)
91
+ end
92
+ end
93
+ end
94
+
95
+ context "instance methods" do
96
+ setup do
97
+ @klass.any_instance.stubs(:load_interface_attributes)
98
+ @instance = @klass.new(@parent, @interface)
99
+ end
100
+
101
+ context "saving" do
102
+ setup do
103
+ @adapter = mock("adapter")
104
+ @instance.stubs(:modify_adapter).yields(@adapter)
105
+ end
106
+
107
+ should "save the attachment type and interface attributes on the open adapter" do
108
+ @instance.expects(:save_attachment_type).with(@adapter).once
109
+ @instance.expects(:save_changed_interface_attributes).with(@adapter).once
110
+ @instance.save
111
+ end
112
+ end
113
+
114
+ context "saving attachment type" do
115
+ setup do
116
+ @adapter = mock("adapter")
117
+ @instance.attachment_type = :nat
118
+ end
119
+
120
+ should "do nothing if attachment type is not changed" do
121
+ @instance.clear_dirty!
122
+ assert !@instance.attachment_type_changed?
123
+ @instance.expects(:attach_to_nat).never
124
+
125
+ @instance.save_attachment_type(@adapter)
126
+ end
127
+
128
+ should "run the proper method if it has changed" do
129
+ @adapter.expects(:attach_to_nat).once
130
+ @instance.save_attachment_type(@adapter)
131
+ end
132
+
133
+ should "clear the dirty state" do
134
+ @adapter.stubs(:attach_to_nat)
135
+ @instance.save_attachment_type(@adapter)
136
+ assert !@instance.attachment_type_changed?
137
+ end
138
+ end
139
+
140
+ context "modifying the adapter" do
141
+ setup do
142
+ @machine = mock("machine")
143
+ @session = mock("session")
144
+ @session.stubs(:machine).returns(@machine)
145
+
146
+ @adapter = mock("adapter")
147
+ end
148
+
149
+ should "open a session, yield the proper adapter, then save settings" do
150
+ seq = sequence("sequence")
151
+ @parent.expects(:with_open_session).yields(@session).in_sequence(seq)
152
+ @machine.expects(:get_network_adapter).with(@instance.slot).returns(@adapter).in_sequence(seq)
153
+
154
+ @instance.modify_adapter do |adapter|
155
+ assert_equal @adapter, adapter
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end