vagrantup 0.5.2 → 0.5.3

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 (100) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +10 -0
  3. data/CHANGELOG.md +48 -0
  4. data/Gemfile +16 -0
  5. data/Gemfile.lock +49 -0
  6. data/README.md +2 -2
  7. data/Rakefile +22 -0
  8. data/bin/.gitignore +0 -0
  9. data/lib/vagrant/provisioners/chef_solo.rb +1 -1
  10. data/lib/vagrant/util/template_renderer.rb +2 -2
  11. data/lib/vagrant/version.rb +1 -1
  12. data/templates/Vagrantfile.erb +1 -1
  13. data/templates/nfs/exports.erb +4 -2
  14. data/test/test_helper.rb +128 -0
  15. data/test/vagrant/action/box/destroy_test.rb +30 -0
  16. data/test/vagrant/action/box/download_test.rb +141 -0
  17. data/test/vagrant/action/box/package_test.rb +25 -0
  18. data/test/vagrant/action/box/unpackage_test.rb +103 -0
  19. data/test/vagrant/action/box/verify_test.rb +39 -0
  20. data/test/vagrant/action/builder_test.rb +218 -0
  21. data/test/vagrant/action/env/error_halt_test.rb +21 -0
  22. data/test/vagrant/action/env/set_test.rb +24 -0
  23. data/test/vagrant/action/environment_test.rb +45 -0
  24. data/test/vagrant/action/exception_catcher_test.rb +30 -0
  25. data/test/vagrant/action/general/package_test.rb +254 -0
  26. data/test/vagrant/action/vm/boot_test.rb +83 -0
  27. data/test/vagrant/action/vm/check_box_test.rb +48 -0
  28. data/test/vagrant/action/vm/check_guest_additions_test.rb +9 -0
  29. data/test/vagrant/action/vm/clean_machine_folder_test.rb +82 -0
  30. data/test/vagrant/action/vm/clear_forwarded_ports_test.rb +72 -0
  31. data/test/vagrant/action/vm/clear_nfs_exports_test.rb +22 -0
  32. data/test/vagrant/action/vm/clear_shared_folders_test.rb +49 -0
  33. data/test/vagrant/action/vm/customize_test.rb +29 -0
  34. data/test/vagrant/action/vm/destroy_test.rb +26 -0
  35. data/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb +46 -0
  36. data/test/vagrant/action/vm/disable_networks_test.rb +39 -0
  37. data/test/vagrant/action/vm/discard_state_test.rb +36 -0
  38. data/test/vagrant/action/vm/export_test.rb +135 -0
  39. data/test/vagrant/action/vm/forward_ports_helpers_test.rb +70 -0
  40. data/test/vagrant/action/vm/forward_ports_test.rb +191 -0
  41. data/test/vagrant/action/vm/halt_test.rb +69 -0
  42. data/test/vagrant/action/vm/import_test.rb +50 -0
  43. data/test/vagrant/action/vm/match_mac_address_test.rb +28 -0
  44. data/test/vagrant/action/vm/network_test.rb +246 -0
  45. data/test/vagrant/action/vm/nfs_helpers_test.rb +23 -0
  46. data/test/vagrant/action/vm/nfs_test.rb +269 -0
  47. data/test/vagrant/action/vm/package_test.rb +25 -0
  48. data/test/vagrant/action/vm/package_vagrantfile_test.rb +46 -0
  49. data/test/vagrant/action/vm/persist_test.rb +50 -0
  50. data/test/vagrant/action/vm/provision_test.rb +134 -0
  51. data/test/vagrant/action/vm/resume_test.rb +35 -0
  52. data/test/vagrant/action/vm/share_folders_test.rb +215 -0
  53. data/test/vagrant/action/vm/suspend_test.rb +35 -0
  54. data/test/vagrant/action_test.rb +111 -0
  55. data/test/vagrant/active_list_test.rb +173 -0
  56. data/test/vagrant/box_test.rb +166 -0
  57. data/test/vagrant/command_test.rb +53 -0
  58. data/test/vagrant/commands/base_test.rb +139 -0
  59. data/test/vagrant/commands/box/add_test.rb +34 -0
  60. data/test/vagrant/commands/box/list_test.rb +32 -0
  61. data/test/vagrant/commands/box/remove_test.rb +41 -0
  62. data/test/vagrant/commands/box/repackage_test.rb +52 -0
  63. data/test/vagrant/commands/destroy_test.rb +44 -0
  64. data/test/vagrant/commands/halt_test.rb +50 -0
  65. data/test/vagrant/commands/init_test.rb +71 -0
  66. data/test/vagrant/commands/package_test.rb +97 -0
  67. data/test/vagrant/commands/provision_test.rb +60 -0
  68. data/test/vagrant/commands/reload_test.rb +47 -0
  69. data/test/vagrant/commands/resume_test.rb +44 -0
  70. data/test/vagrant/commands/ssh_config_test.rb +77 -0
  71. data/test/vagrant/commands/ssh_test.rb +129 -0
  72. data/test/vagrant/commands/status_test.rb +40 -0
  73. data/test/vagrant/commands/suspend_test.rb +44 -0
  74. data/test/vagrant/commands/up_test.rb +49 -0
  75. data/test/vagrant/config_test.rb +307 -0
  76. data/test/vagrant/downloaders/base_test.rb +28 -0
  77. data/test/vagrant/downloaders/file_test.rb +33 -0
  78. data/test/vagrant/downloaders/http_test.rb +70 -0
  79. data/test/vagrant/environment_test.rb +770 -0
  80. data/test/vagrant/hosts/base_test.rb +46 -0
  81. data/test/vagrant/hosts/bsd_test.rb +56 -0
  82. data/test/vagrant/hosts/linux_test.rb +56 -0
  83. data/test/vagrant/provisioners/base_test.rb +36 -0
  84. data/test/vagrant/provisioners/chef_server_test.rb +182 -0
  85. data/test/vagrant/provisioners/chef_solo_test.rb +197 -0
  86. data/test/vagrant/provisioners/chef_test.rb +178 -0
  87. data/test/vagrant/ssh_session_test.rb +46 -0
  88. data/test/vagrant/ssh_test.rb +317 -0
  89. data/test/vagrant/systems/linux_test.rb +179 -0
  90. data/test/vagrant/util/busy_test.rb +106 -0
  91. data/test/vagrant/util/plain_logger_test.rb +17 -0
  92. data/test/vagrant/util/platform_test.rb +18 -0
  93. data/test/vagrant/util/resource_logger_test.rb +145 -0
  94. data/test/vagrant/util/stacked_proc_runner_test.rb +43 -0
  95. data/test/vagrant/util/template_renderer_test.rb +145 -0
  96. data/test/vagrant/util/translator_test.rb +61 -0
  97. data/test/vagrant/util_test.rb +27 -0
  98. data/test/vagrant/vm_test.rb +228 -0
  99. data/vagrant.gemspec +34 -0
  100. metadata +130 -9
@@ -0,0 +1,70 @@
1
+ require "test_helper"
2
+
3
+ class ForwardPortsHelpersVMActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Class.new do
6
+ def initialize(env); @env = env; end
7
+ end
8
+
9
+ @klass.send(:include, Vagrant::Action::VM::ForwardPortsHelpers)
10
+ @app, @env = mock_action_data
11
+
12
+ @vm = mock("vm")
13
+ @vm.stubs(:name).returns("foo")
14
+ @env["vm"] = @vm
15
+
16
+ @instance = @klass.new(@env)
17
+ end
18
+
19
+ context "getting list of used ports" do
20
+ setup do
21
+ @vms = []
22
+ VirtualBox::VM.stubs(:all).returns(@vms)
23
+ VirtualBox.stubs(:version).returns("3.1.0")
24
+ @vm.stubs(:uuid).returns(:bar)
25
+ end
26
+
27
+ def mock_vm(options={})
28
+ options = {
29
+ :running? => true,
30
+ :uuid => :foo
31
+ }.merge(options)
32
+
33
+ vm = mock("vm")
34
+ options.each do |k,v|
35
+ vm.stubs(k).returns(v)
36
+ end
37
+
38
+ vm
39
+ end
40
+
41
+ def mock_fp(hostport)
42
+ fp = mock("fp")
43
+ fp.stubs(:hostport).returns(hostport.to_s)
44
+ fp
45
+ end
46
+
47
+ should "ignore VMs which aren't running" do
48
+ @vms << mock_vm(:running? => false)
49
+ @vms[0].expects(:forwarded_ports).never
50
+ @instance.used_ports
51
+ end
52
+
53
+ should "ignore VMs of the same uuid" do
54
+ @vms << mock_vm(:uuid => @vm.uuid)
55
+ @vms[0].expects(:forwarded_ports).never
56
+ @instance.used_ports
57
+ end
58
+
59
+ should "return the forwarded ports for VB 3.2.x" do
60
+ VirtualBox.stubs(:version).returns("3.2.4")
61
+ fps = [mock_fp(2222), mock_fp(80)]
62
+ na = mock("na")
63
+ ne = mock("ne")
64
+ na.stubs(:nat_driver).returns(ne)
65
+ ne.stubs(:forwarded_ports).returns(fps)
66
+ @vms << mock_vm(:network_adapters => [na])
67
+ assert_equal %W[2222 80], @instance.used_ports
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,191 @@
1
+ require "test_helper"
2
+
3
+ class ForwardPortsVMActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Vagrant::Action::VM::ForwardPorts
6
+ @app, @env = mock_action_data
7
+
8
+ @vm = mock("vm")
9
+ @vm.stubs(:name).returns("foo")
10
+ @env["vm"] = @vm
11
+ end
12
+
13
+ context "initializing" do
14
+ should "call proper methods" do
15
+ @klass.any_instance.expects(:threshold_check)
16
+ @klass.any_instance.expects(:external_collision_check)
17
+ @klass.new(@app, @env)
18
+ end
19
+ end
20
+
21
+ context "checking for threshold" do
22
+ setup do
23
+ @klass.any_instance.stubs(:external_collision_check)
24
+ end
25
+
26
+ should "error if has a port below threshold" do
27
+ @env.env.config.vm.forwarded_ports.clear
28
+ @env.env.config.vm.forward_port("foo", 22, 222)
29
+ @klass.new(@app, @env)
30
+ assert @env.error?
31
+ assert_equal :vm_port_below_threshold, @env.error.first
32
+ end
33
+
34
+ should "not error if ports are fine" do
35
+ @env.env.config.vm.forwarded_ports.clear
36
+ @env.env.config.vm.forward_port("foo", 22, 2222)
37
+ @klass.new(@app, @env)
38
+ assert !@env.error?
39
+ end
40
+ end
41
+
42
+ context "checking for colliding external ports" do
43
+ setup do
44
+ @env.env.config.vm.forwarded_ports.clear
45
+ @env.env.config.vm.forward_port("ssh", 22, 2222)
46
+
47
+ @used_ports = []
48
+ @klass.any_instance.stubs(:used_ports).returns(@used_ports)
49
+ @klass.any_instance.stubs(:handle_collision)
50
+ end
51
+
52
+ should "not raise any errors if no forwarded ports collide" do
53
+ @used_ports << "80"
54
+ @klass.new(@app, @env)
55
+ assert !@env.error?
56
+ end
57
+
58
+ should "handle collision if it happens" do
59
+ @used_ports << "2222"
60
+ @klass.any_instance.expects(:handle_collision).with("ssh", anything, anything).once
61
+ @klass.new(@app, @env)
62
+ assert !@env.error?
63
+ end
64
+ end
65
+
66
+ context "with instance" do
67
+ setup do
68
+ @klass.any_instance.stubs(:threshold_check)
69
+ @klass.any_instance.stubs(:external_collision_check)
70
+ @instance = @klass.new(@app, @env)
71
+ end
72
+
73
+ context "handling collisions" do
74
+ setup do
75
+ @name = :foo
76
+ @options = {
77
+ :hostport => 0,
78
+ :auto => true
79
+ }
80
+ @used_ports = [1,2,3]
81
+
82
+ @env.env.config.vm.auto_port_range = (1..5)
83
+ end
84
+
85
+ should "error if auto forwarding is disabled" do
86
+ @options[:auto] = false
87
+ @instance.handle_collision(@name, @options, @used_ports)
88
+ assert @env.error?
89
+ assert_equal :vm_port_collision, @env.error.first
90
+ end
91
+
92
+ should "set the host port to the first available port" do
93
+ assert_equal 0, @options[:hostport]
94
+ @instance.handle_collision(@name, @options, @used_ports)
95
+ assert_equal 4, @options[:hostport]
96
+ end
97
+
98
+ should "add the newly used port to the list of used ports" do
99
+ assert !@used_ports.include?(4)
100
+ @instance.handle_collision(@name, @options, @used_ports)
101
+ assert @used_ports.include?(4)
102
+ end
103
+
104
+ should "not use a host port which is being forwarded later" do
105
+ @env.env.config.vm.forward_port("http", 80, 4)
106
+
107
+ assert_equal 0, @options[:hostport]
108
+ @instance.handle_collision(@name, @options, @used_ports)
109
+ assert_equal 5, @options[:hostport]
110
+ end
111
+
112
+ should "raise an exception if there are no auto ports available" do
113
+ @env.env.config.vm.auto_port_range = (1..3)
114
+ @instance.handle_collision(@name, @options, @used_ports)
115
+ assert @env.error?
116
+ assert_equal :vm_port_auto_empty, @env.error.first
117
+ end
118
+ end
119
+
120
+ context "calling" do
121
+ should "clear all previous ports and forward new ports" do
122
+ exec_seq = sequence("exec_seq")
123
+ @instance.expects(:forward_ports).once.in_sequence(exec_seq)
124
+ @app.expects(:call).once.with(@env).in_sequence(exec_seq)
125
+ @instance.call(@env)
126
+ end
127
+ end
128
+
129
+ context "forwarding ports" do
130
+ setup do
131
+ @internal_vm = mock("internal_vm")
132
+ @vm.stubs(:vm).returns(@internal_vm)
133
+ end
134
+
135
+ should "create a port forwarding for the VM" do
136
+ forwarded_ports = mock("forwarded_ports")
137
+ network_adapter = mock("network_adapter")
138
+
139
+ @internal_vm.stubs(:network_adapters).returns([network_adapter])
140
+ network_adapter.expects(:attachment_type).returns(:nat)
141
+
142
+ @instance.expects(:forward_port).once
143
+ @internal_vm.expects(:save).once
144
+ @vm.expects(:reload!).once
145
+ @instance.forward_ports
146
+ end
147
+
148
+ should "not port forward for non NAT interfaces" do
149
+ forwarded_ports = mock("forwarded_ports")
150
+ network_adapter = mock("network_adapter")
151
+
152
+ @internal_vm.expects(:network_adapters).returns([network_adapter])
153
+ network_adapter.expects(:attachment_type).returns(:host_only)
154
+ @internal_vm.expects(:save).once
155
+ @vm.expects(:reload!).once
156
+ @instance.forward_ports
157
+ end
158
+ end
159
+
160
+ context "forwarding ports implementation" do
161
+ setup do
162
+ VirtualBox.stubs(:version).returns("3.2.8")
163
+
164
+ @internal_vm = mock("internal_vm")
165
+ @vm.stubs(:vm).returns(@internal_vm)
166
+ end
167
+
168
+ should "forward ports" do
169
+ name, opts = @env.env.config.vm.forwarded_ports.first
170
+
171
+ adapters = []
172
+ adapter = mock("adapter")
173
+ engine = mock("engine")
174
+ fps = mock("forwarded ports")
175
+ adapter.stubs(:nat_driver).returns(engine)
176
+ engine.stubs(:forwarded_ports).returns(fps)
177
+ fps.expects(:<<).with do |port|
178
+ assert_equal name, port.name
179
+ assert_equal opts[:hostport], port.hostport
180
+ assert_equal opts[:guestport], port.guestport
181
+ true
182
+ end
183
+
184
+ adapters[opts[:adapter]] = adapter
185
+ @internal_vm.stubs(:network_adapters).returns(adapters)
186
+
187
+ @instance.forward_port(name, opts)
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,69 @@
1
+ require "test_helper"
2
+
3
+ class HaltVMActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Vagrant::Action::VM::Halt
6
+ @app, @env = mock_action_data
7
+
8
+ @vm = mock("vm")
9
+ @vm.stubs(:name).returns("foo")
10
+ @vm.stubs(:ssh).returns(mock("ssh"))
11
+ @vm.stubs(:system).returns(mock("system"))
12
+ @env["vm"] = @vm
13
+
14
+ @internal_vm = mock("internal")
15
+ @vm.stubs(:vm).returns(@internal_vm)
16
+
17
+ @instance = @klass.new(@app, @env)
18
+ end
19
+
20
+ context "initializing" do
21
+ should "merge in the given options" do
22
+ @klass.new(@app, @env, :foo => :bar)
23
+ assert_equal :bar, @env[:foo]
24
+ end
25
+ end
26
+
27
+ context "calling" do
28
+ setup do
29
+ @internal_vm.stubs(:running?).returns(true)
30
+
31
+ @vm.system.stubs(:halt)
32
+ @internal_vm.stubs(:stop)
33
+ @internal_vm.stubs(:state).returns(:powered_off)
34
+ end
35
+
36
+ should "do nothing if VM not running" do
37
+ @internal_vm.stubs(:running?).returns(false)
38
+ @vm.system.expects(:halt).never
39
+ @internal_vm.expects(:stop).never
40
+ @app.expects(:call).once
41
+
42
+ @instance.call(@env)
43
+ end
44
+
45
+ should "halt with the system and NOT force VM to stop if powered off" do
46
+ @internal_vm.expects(:state).with(true).returns(:powered_off)
47
+ @vm.system.expects(:halt).once
48
+ @internal_vm.expects(:stop).never
49
+ @app.expects(:call).once
50
+
51
+ @instance.call(@env)
52
+ end
53
+
54
+ should "halt with the system and force VM to stop if NOT powered off" do
55
+ @internal_vm.expects(:state).with(true).returns(:running)
56
+ @vm.system.expects(:halt).once
57
+ @internal_vm.expects(:stop).once
58
+ @app.expects(:call).once
59
+
60
+ @instance.call(@env)
61
+ end
62
+
63
+ should "not call halt on the system if forcing" do
64
+ @env[:force] = true
65
+ @vm.system.expects(:halt).never
66
+ @instance.call(@env)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,50 @@
1
+ require "test_helper"
2
+
3
+ class ImportVMActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Vagrant::Action::VM::Import
6
+ @app, @env = mock_action_data
7
+ @instance = @klass.new(@app, @env)
8
+
9
+ ovf_file = "foo"
10
+ @box = mock("box")
11
+ @box.stubs(:ovf_file).returns(ovf_file)
12
+ @env.env.stubs(:box).returns(@box)
13
+
14
+ @env.env.vm = Vagrant::VM.new
15
+
16
+ VirtualBox::VM.stubs(:import)
17
+ end
18
+
19
+ should "call import on VirtualBox with proper base" do
20
+ VirtualBox::VM.expects(:import).once.with(@env.env.box.ovf_file).returns("foo")
21
+ @instance.call(@env)
22
+ end
23
+
24
+ should "call next in chain on success and set VM" do
25
+ vm = mock("vm")
26
+ VirtualBox::VM.stubs(:import).returns(vm)
27
+ @app.expects(:call).with(@env).once
28
+ @instance.call(@env)
29
+
30
+ assert_equal vm, @env["vm"].vm
31
+ end
32
+
33
+ should "mark environment erroneous and not continue chain on failure" do
34
+ @app.expects(:call).never
35
+ @instance.call(@env)
36
+
37
+ assert @env.error?
38
+ end
39
+
40
+ should "run the destroy action if interrupted" do
41
+ VirtualBox::VM.stubs(:import).returns(mock("vm"))
42
+ @app.expects(:call).once.with() do |env|
43
+ assert_equal @env, env
44
+ @env.error!(:interrupt)
45
+ end
46
+ @env.env.actions.expects(:run).with(:destroy).once
47
+
48
+ @instance.call(@env)
49
+ end
50
+ end
@@ -0,0 +1,28 @@
1
+ require "test_helper"
2
+
3
+ class MatchMACAddressVMActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Vagrant::Action::VM::MatchMACAddress
6
+ @app, @env = mock_action_data
7
+
8
+ @vm = mock("vm")
9
+ @env["vm"] = @vm
10
+
11
+ @internal_vm = mock("internal")
12
+ @vm.stubs(:vm).returns(@internal_vm)
13
+
14
+ @instance = @klass.new(@app, @env)
15
+ end
16
+
17
+ should "match the mac addresses" do
18
+ nic = mock("nic")
19
+ nic.expects(:mac_address=).once
20
+
21
+ update_seq = sequence("update_seq")
22
+ @internal_vm.expects(:network_adapters).returns([nic]).once.in_sequence(update_seq)
23
+ @internal_vm.expects(:save).once.in_sequence(update_seq)
24
+ @app.expects(:call).with(@env).once.in_sequence(update_seq)
25
+
26
+ @instance.call(@env)
27
+ end
28
+ end
@@ -0,0 +1,246 @@
1
+ require "test_helper"
2
+
3
+ class NetworkVMActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Vagrant::Action::VM::Network
6
+ @app, @env = mock_action_data
7
+
8
+ @vm = mock("vm")
9
+ @vm.stubs(:name).returns("foo")
10
+ @vm.stubs(:ssh).returns(mock("ssh"))
11
+ @vm.stubs(:system).returns(mock("system"))
12
+ @env["vm"] = @vm
13
+
14
+ @internal_vm = mock("internal")
15
+ @vm.stubs(:vm).returns(@internal_vm)
16
+ end
17
+
18
+ context "initializing" do
19
+ should "verify no bridge collisions for each network enabled" do
20
+ @env.env.config.vm.network("foo")
21
+ @klass.any_instance.expects(:verify_no_bridge_collision).once.with() do |options|
22
+ assert_equal "foo", options[:ip]
23
+ true
24
+ end
25
+
26
+ @klass.new(@app, @env)
27
+ end
28
+ end
29
+
30
+ context "with an instance" do
31
+ setup do
32
+ @klass.any_instance.stubs(:verify_no_bridge_collision)
33
+ @instance = @klass.new(@app, @env)
34
+
35
+ @interfaces = []
36
+ VirtualBox::Global.global.host.stubs(:network_interfaces).returns(@interfaces)
37
+ end
38
+
39
+ def mock_interface(options=nil)
40
+ options = {
41
+ :interface_type => :host_only,
42
+ :name => "foo"
43
+ }.merge(options || {})
44
+
45
+ interface = mock("interface")
46
+ options.each do |k,v|
47
+ interface.stubs(k).returns(v)
48
+ end
49
+
50
+ @interfaces << interface
51
+ interface
52
+ end
53
+
54
+ context "calling" do
55
+ setup do
56
+ @env.env.config.vm.network("foo")
57
+ @instance.stubs(:enable_network?).returns(false)
58
+ end
59
+
60
+ should "do nothing if network should not be enabled" do
61
+ @instance.expects(:assign_network).never
62
+ @app.expects(:call).with(@env).once
63
+ @vm.system.expects(:prepare_host_only_network).never
64
+ @vm.system.expects(:enable_host_only_network).never
65
+
66
+ @instance.call(@env)
67
+ end
68
+
69
+ should "assign and enable the network if networking enabled" do
70
+ @instance.stubs(:enable_network?).returns(true)
71
+
72
+ run_seq = sequence("run")
73
+ @instance.expects(:assign_network).once.in_sequence(run_seq)
74
+ @app.expects(:call).with(@env).once.in_sequence(run_seq)
75
+ @vm.system.expects(:prepare_host_only_network).once.in_sequence(run_seq)
76
+ @vm.system.expects(:enable_host_only_network).once.in_sequence(run_seq)
77
+
78
+ @instance.call(@env)
79
+ end
80
+ end
81
+
82
+ context "checking if network is enabled" do
83
+ should "return true if the network options are set" do
84
+ @env.env.config.vm.network("foo")
85
+ assert @instance.enable_network?
86
+ end
87
+
88
+ should "return false if the network was not set" do
89
+ assert !@instance.enable_network?
90
+ end
91
+ end
92
+
93
+ context "assigning the network" do
94
+ setup do
95
+ @network_name = "foo"
96
+ @instance.stubs(:network_name).returns(@network_name)
97
+
98
+ @network_adapters = []
99
+ @internal_vm.stubs(:network_adapters).returns(@network_adapters)
100
+
101
+ @options = {
102
+ :ip => "foo",
103
+ :adapter => 7
104
+ }
105
+
106
+ @env.env.config.vm.network(@options[:ip], @options)
107
+ end
108
+
109
+ should "setup the specified network adapter" do
110
+ adapter = mock("adapter")
111
+ @network_adapters[@options[:adapter]] = adapter
112
+
113
+ adapter.expects(:enabled=).with(true).once
114
+ adapter.expects(:attachment_type=).with(:host_only).once
115
+ adapter.expects(:host_interface=).with(@network_name).once
116
+ adapter.expects(:save).once
117
+
118
+ @instance.assign_network
119
+ end
120
+ end
121
+
122
+ context "network name" do
123
+ setup do
124
+ @instance.stubs(:matching_network?).returns(false)
125
+
126
+ @options = { :ip => :foo, :netmask => :bar, :name => nil }
127
+ end
128
+
129
+ should "return the network which matches" do
130
+ result = mock("result")
131
+ interface = mock_interface(:name => result)
132
+
133
+ @instance.expects(:matching_network?).with(interface, @options).returns(true)
134
+ assert_equal result, @instance.network_name(@options)
135
+ end
136
+
137
+ should "ignore non-host only interfaces" do
138
+ @options[:name] = "foo"
139
+ mock_interface(:name => @options[:name],
140
+ :interface_type => :bridged)
141
+
142
+ @instance.network_name(@options)
143
+ assert @env.error?
144
+ end
145
+
146
+ should "return the network which matches the name if given" do
147
+ @options[:name] = "foo"
148
+
149
+ interface = mock_interface(:name => @options[:name])
150
+ assert_equal @options[:name], @instance.network_name(@options)
151
+ end
152
+
153
+ should "error and exit if the given network name is not found" do
154
+ @options[:name] = "foo"
155
+
156
+ @interfaces.expects(:create).never
157
+ @instance.network_name(@options)
158
+ assert @env.error?
159
+ assert_equal :network_not_found, @env.error.first
160
+ end
161
+
162
+ should "create a network for the IP and netmask" do
163
+ result = mock("result")
164
+ network_ip = :foo
165
+
166
+ interface = mock_interface(:name => result)
167
+ interface.expects(:enable_static).with(network_ip, @options[:netmask])
168
+ @interfaces.expects(:create).returns(interface)
169
+ @instance.expects(:network_ip).with(@options[:ip], @options[:netmask]).once.returns(network_ip)
170
+
171
+ assert_equal result, @instance.network_name(@options)
172
+ end
173
+ end
174
+
175
+ context "checking for a matching network" do
176
+ setup do
177
+ @interface = mock("interface")
178
+ @interface.stubs(:network_mask).returns("foo")
179
+ @interface.stubs(:ip_address).returns("192.168.0.1")
180
+
181
+ @options = {
182
+ :netmask => "foo",
183
+ :ip => "baz"
184
+ }
185
+ end
186
+
187
+ should "return false if the netmasks don't match" do
188
+ @options[:netmask] = "bar"
189
+ assert @interface.network_mask != @options[:netmask] # sanity
190
+ assert !@instance.matching_network?(@interface, @options)
191
+ end
192
+
193
+ should "return true if the netmasks yield the same IP" do
194
+ tests = [["255.255.255.0", "192.168.0.1", "192.168.0.45"],
195
+ ["255.255.0.0", "192.168.45.1", "192.168.28.7"]]
196
+
197
+ tests.each do |netmask, interface_ip, guest_ip|
198
+ @options[:netmask] = netmask
199
+ @options[:ip] = guest_ip
200
+ @interface.stubs(:network_mask).returns(netmask)
201
+ @interface.stubs(:ip_address).returns(interface_ip)
202
+
203
+ assert @instance.matching_network?(@interface, @options)
204
+ end
205
+ end
206
+ end
207
+
208
+ context "applying the netmask" do
209
+ should "return the proper result" do
210
+ tests = {
211
+ ["192.168.0.1","255.255.255.0"] => [192,168,0,0],
212
+ ["192.168.45.10","255.255.255.0"] => [192,168,45,0]
213
+ }
214
+
215
+ tests.each do |k,v|
216
+ assert_equal v, @instance.apply_netmask(*k)
217
+ end
218
+ end
219
+ end
220
+
221
+ context "splitting an IP" do
222
+ should "return the proper result" do
223
+ tests = {
224
+ "192.168.0.1" => [192,168,0,1]
225
+ }
226
+
227
+ tests.each do |k,v|
228
+ assert_equal v, @instance.split_ip(k)
229
+ end
230
+ end
231
+ end
232
+
233
+ context "network IP" do
234
+ should "return the proper result" do
235
+ tests = {
236
+ ["192.168.0.45", "255.255.255.0"] => "192.168.0.1"
237
+ }
238
+
239
+ tests.each do |args, result|
240
+ assert_equal result, @instance.network_ip(*args)
241
+ end
242
+ end
243
+ end
244
+
245
+ end
246
+ end
@@ -0,0 +1,23 @@
1
+ require "test_helper"
2
+
3
+ class NFSHelpersVMActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Class.new
6
+ @klass.send(:include, Vagrant::Action::VM::NFSHelpers)
7
+ @app, @env = mock_action_data
8
+
9
+ @instance = @klass.new
10
+ end
11
+
12
+ should "clear NFS exports for the environment if the host exists" do
13
+ @host = mock("host")
14
+ @env.env.stubs(:host).returns(@host)
15
+ @host.expects(:nfs_cleanup).once
16
+
17
+ @instance.clear_nfs_exports(@env)
18
+ end
19
+
20
+ should "not do anything if host is nil" do
21
+ assert_nothing_raised { @instance.clear_nfs_exports(@env) }
22
+ end
23
+ end