vagrant 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/.gitignore +10 -0
  2. data/CHANGELOG.md +48 -0
  3. data/Gemfile +16 -0
  4. data/Gemfile.lock +49 -0
  5. data/README.md +2 -2
  6. data/Rakefile +22 -0
  7. data/bin/.gitignore +0 -0
  8. data/lib/vagrant/provisioners/chef_solo.rb +1 -1
  9. data/lib/vagrant/util/template_renderer.rb +2 -2
  10. data/lib/vagrant/version.rb +1 -1
  11. data/templates/Vagrantfile.erb +1 -1
  12. data/templates/nfs/exports.erb +4 -2
  13. data/test/test_helper.rb +128 -0
  14. data/test/vagrant/action/box/destroy_test.rb +30 -0
  15. data/test/vagrant/action/box/download_test.rb +141 -0
  16. data/test/vagrant/action/box/package_test.rb +25 -0
  17. data/test/vagrant/action/box/unpackage_test.rb +103 -0
  18. data/test/vagrant/action/box/verify_test.rb +39 -0
  19. data/test/vagrant/action/builder_test.rb +218 -0
  20. data/test/vagrant/action/env/error_halt_test.rb +21 -0
  21. data/test/vagrant/action/env/set_test.rb +24 -0
  22. data/test/vagrant/action/environment_test.rb +45 -0
  23. data/test/vagrant/action/exception_catcher_test.rb +30 -0
  24. data/test/vagrant/action/general/package_test.rb +254 -0
  25. data/test/vagrant/action/vm/boot_test.rb +83 -0
  26. data/test/vagrant/action/vm/check_box_test.rb +48 -0
  27. data/test/vagrant/action/vm/check_guest_additions_test.rb +9 -0
  28. data/test/vagrant/action/vm/clean_machine_folder_test.rb +82 -0
  29. data/test/vagrant/action/vm/clear_forwarded_ports_test.rb +72 -0
  30. data/test/vagrant/action/vm/clear_nfs_exports_test.rb +22 -0
  31. data/test/vagrant/action/vm/clear_shared_folders_test.rb +49 -0
  32. data/test/vagrant/action/vm/customize_test.rb +29 -0
  33. data/test/vagrant/action/vm/destroy_test.rb +26 -0
  34. data/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb +46 -0
  35. data/test/vagrant/action/vm/disable_networks_test.rb +39 -0
  36. data/test/vagrant/action/vm/discard_state_test.rb +36 -0
  37. data/test/vagrant/action/vm/export_test.rb +135 -0
  38. data/test/vagrant/action/vm/forward_ports_helpers_test.rb +70 -0
  39. data/test/vagrant/action/vm/forward_ports_test.rb +191 -0
  40. data/test/vagrant/action/vm/halt_test.rb +69 -0
  41. data/test/vagrant/action/vm/import_test.rb +50 -0
  42. data/test/vagrant/action/vm/match_mac_address_test.rb +28 -0
  43. data/test/vagrant/action/vm/network_test.rb +246 -0
  44. data/test/vagrant/action/vm/nfs_helpers_test.rb +23 -0
  45. data/test/vagrant/action/vm/nfs_test.rb +269 -0
  46. data/test/vagrant/action/vm/package_test.rb +25 -0
  47. data/test/vagrant/action/vm/package_vagrantfile_test.rb +46 -0
  48. data/test/vagrant/action/vm/persist_test.rb +50 -0
  49. data/test/vagrant/action/vm/provision_test.rb +134 -0
  50. data/test/vagrant/action/vm/resume_test.rb +35 -0
  51. data/test/vagrant/action/vm/share_folders_test.rb +215 -0
  52. data/test/vagrant/action/vm/suspend_test.rb +35 -0
  53. data/test/vagrant/action_test.rb +111 -0
  54. data/test/vagrant/active_list_test.rb +173 -0
  55. data/test/vagrant/box_test.rb +166 -0
  56. data/test/vagrant/command_test.rb +53 -0
  57. data/test/vagrant/commands/base_test.rb +139 -0
  58. data/test/vagrant/commands/box/add_test.rb +34 -0
  59. data/test/vagrant/commands/box/list_test.rb +32 -0
  60. data/test/vagrant/commands/box/remove_test.rb +41 -0
  61. data/test/vagrant/commands/box/repackage_test.rb +52 -0
  62. data/test/vagrant/commands/destroy_test.rb +44 -0
  63. data/test/vagrant/commands/halt_test.rb +50 -0
  64. data/test/vagrant/commands/init_test.rb +71 -0
  65. data/test/vagrant/commands/package_test.rb +97 -0
  66. data/test/vagrant/commands/provision_test.rb +60 -0
  67. data/test/vagrant/commands/reload_test.rb +47 -0
  68. data/test/vagrant/commands/resume_test.rb +44 -0
  69. data/test/vagrant/commands/ssh_config_test.rb +77 -0
  70. data/test/vagrant/commands/ssh_test.rb +129 -0
  71. data/test/vagrant/commands/status_test.rb +40 -0
  72. data/test/vagrant/commands/suspend_test.rb +44 -0
  73. data/test/vagrant/commands/up_test.rb +49 -0
  74. data/test/vagrant/config_test.rb +307 -0
  75. data/test/vagrant/downloaders/base_test.rb +28 -0
  76. data/test/vagrant/downloaders/file_test.rb +33 -0
  77. data/test/vagrant/downloaders/http_test.rb +70 -0
  78. data/test/vagrant/environment_test.rb +770 -0
  79. data/test/vagrant/hosts/base_test.rb +46 -0
  80. data/test/vagrant/hosts/bsd_test.rb +56 -0
  81. data/test/vagrant/hosts/linux_test.rb +56 -0
  82. data/test/vagrant/provisioners/base_test.rb +36 -0
  83. data/test/vagrant/provisioners/chef_server_test.rb +182 -0
  84. data/test/vagrant/provisioners/chef_solo_test.rb +197 -0
  85. data/test/vagrant/provisioners/chef_test.rb +178 -0
  86. data/test/vagrant/ssh_session_test.rb +46 -0
  87. data/test/vagrant/ssh_test.rb +317 -0
  88. data/test/vagrant/systems/linux_test.rb +179 -0
  89. data/test/vagrant/util/busy_test.rb +106 -0
  90. data/test/vagrant/util/plain_logger_test.rb +17 -0
  91. data/test/vagrant/util/platform_test.rb +18 -0
  92. data/test/vagrant/util/resource_logger_test.rb +145 -0
  93. data/test/vagrant/util/stacked_proc_runner_test.rb +43 -0
  94. data/test/vagrant/util/template_renderer_test.rb +145 -0
  95. data/test/vagrant/util/translator_test.rb +61 -0
  96. data/test/vagrant/util_test.rb +27 -0
  97. data/test/vagrant/vm_test.rb +228 -0
  98. data/vagrant.gemspec +34 -0
  99. metadata +158 -43
@@ -0,0 +1,28 @@
1
+ require "test_helper"
2
+
3
+ class BaseDownloaderTest < Test::Unit::TestCase
4
+ should "include the util class so subclasses have access to it" do
5
+ assert Vagrant::Downloaders::Base.include?(Vagrant::Util)
6
+ end
7
+
8
+ context "base instance" do
9
+ setup do
10
+ @env = mock_environment
11
+ @base = Vagrant::Downloaders::Base.new(@env)
12
+ end
13
+
14
+ should "implement prepare which does nothing" do
15
+ assert_nothing_raised do
16
+ assert @base.respond_to?(:prepare)
17
+ @base.prepare("source")
18
+ end
19
+ end
20
+
21
+ should "implement download! which does nothing" do
22
+ assert_nothing_raised do
23
+ assert @base.respond_to?(:download!)
24
+ @base.download!("source", "destination")
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ require "test_helper"
2
+
3
+ class FileDownloaderTest < Test::Unit::TestCase
4
+ setup do
5
+ @downloader, @tempfile = mock_downloader(Vagrant::Downloaders::File)
6
+ @uri = "foo.box"
7
+ end
8
+
9
+ context "preparing" do
10
+ should "raise an exception if the file does not exist" do
11
+ File.expects(:file?).with(@uri).returns(false)
12
+ @downloader.prepare(@uri)
13
+ assert @downloader.env.error?
14
+ assert_equal :downloader_file_doesnt_exist, @downloader.env.error.first
15
+ end
16
+ end
17
+
18
+ context "downloading" do
19
+ should "cp the file" do
20
+ path = '/path'
21
+ @tempfile.expects(:path).returns(path)
22
+ FileUtils.expects(:cp).with(@uri, path)
23
+ @downloader.download!(@uri, @tempfile)
24
+ end
25
+ end
26
+
27
+ context "matching a uri" do
28
+ should "return true if the File exists on the file system" do
29
+ File.expects(:exists?).with('foo').returns(true)
30
+ assert Vagrant::Downloaders::File.match?('foo')
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,70 @@
1
+ require "test_helper"
2
+
3
+ class HttpDownloaderTest < Test::Unit::TestCase
4
+ setup do
5
+ @downloader, @tempfile = mock_downloader(Vagrant::Downloaders::HTTP)
6
+ @downloader.stubs(:report_progress)
7
+ @downloader.stubs(:complete_progress)
8
+ @uri = "http://google.com/"
9
+ end
10
+
11
+ context "downloading" do
12
+ setup do
13
+ @parsed_uri = URI.parse(@uri)
14
+ @http = Net::HTTP.new(@parsed_uri.host, @parsed_uri.port)
15
+ Net::HTTP.stubs(:new).returns(@http)
16
+ @http.stubs(:start)
17
+ end
18
+
19
+ should "create a proper net/http object" do
20
+ Net::HTTP.expects(:new).with(@parsed_uri.host, @parsed_uri.port).once.returns(@http)
21
+ @http.expects(:start)
22
+ @downloader.download!(@uri, @tempfile)
23
+ end
24
+
25
+ should "enable SSL if scheme is https" do
26
+ @uri = "https://google.com/"
27
+ @http.expects(:use_ssl=).with(true).once
28
+ @downloader.download!(@uri, @tempfile)
29
+ end
30
+
31
+ should "read the body of the response and place each segment into the file" do
32
+ h = mock("http")
33
+ response = mock("response")
34
+ response.stubs(:content_length)
35
+ segment = mock("segment")
36
+ segment.stubs(:length).returns(7)
37
+
38
+ @http.stubs(:start).yields(h)
39
+ h.expects(:request_get).with(@parsed_uri.request_uri).once.yields(response)
40
+ response.expects(:read_body).once.yields(segment)
41
+ @tempfile.expects(:write).with(segment).once
42
+
43
+ @downloader.download!(@uri, @tempfile)
44
+ end
45
+
46
+ should "error environment if invalid URL given" do
47
+ Net::HTTP.expects(:new).raises(SocketError.new)
48
+ @downloader.download!(@uri, @tempfile)
49
+
50
+ assert @downloader.env.error?
51
+ assert_equal :box_download_http_socket_error, @downloader.env.error.first
52
+ end
53
+ end
54
+
55
+ context "matching the uri" do
56
+ should "use extract to verify that the string is in fact a uri" do
57
+ URI.expects(:extract).returns(['foo'])
58
+ assert Vagrant::Downloaders::HTTP.match?('foo')
59
+ end
60
+
61
+ should "return false if there are no extract results" do
62
+ URI.expects(:extract).returns([])
63
+ assert !Vagrant::Downloaders::HTTP.match?('foo')
64
+ end
65
+ end
66
+
67
+ context "reporting progress" do
68
+ # TODO: Testing for this, probably
69
+ end
70
+ end
@@ -0,0 +1,770 @@
1
+ require "test_helper"
2
+
3
+ class EnvironmentTest < Test::Unit::TestCase
4
+ context "class method check virtualbox version" do
5
+ setup do
6
+ VirtualBox.stubs(:version).returns("3.1.4")
7
+ end
8
+
9
+ should "not error and exit if everything is good" do
10
+ VirtualBox.expects(:version).returns("3.2.4")
11
+ Vagrant::Environment.expects(:error_and_exit).never
12
+ Vagrant::Environment.check_virtualbox!
13
+ end
14
+
15
+ should "error and exit if VirtualBox is not installed or detected" do
16
+ Vagrant::Environment.expects(:error_and_exit).with(:virtualbox_not_detected).once
17
+ VirtualBox.expects(:version).returns(nil)
18
+ Vagrant::Environment.check_virtualbox!
19
+ end
20
+
21
+ should "error and exit if VirtualBox is lower than version 3.2" do
22
+ version = "3.1.12r1041"
23
+ Vagrant::Environment.expects(:error_and_exit).with(:virtualbox_invalid_version, :version => version.to_s).once
24
+ VirtualBox.expects(:version).returns(version)
25
+ Vagrant::Environment.check_virtualbox!
26
+ end
27
+
28
+ should "error and exit for OSE VirtualBox" do
29
+ version = "3.2.6_OSE"
30
+ Vagrant::Environment.expects(:error_and_exit).with(:virtualbox_invalid_ose, :version => version.to_s).once
31
+ VirtualBox.expects(:version).returns(version)
32
+ Vagrant::Environment.check_virtualbox!
33
+ end
34
+ end
35
+
36
+ context "class method load!" do
37
+ setup do
38
+ @cwd = mock('cwd')
39
+
40
+ @env = mock('env')
41
+ @env.stubs(:load!).returns(@env)
42
+ end
43
+
44
+ should "create the environment with given cwd, load it, and return it" do
45
+ Vagrant::Environment.expects(:new).with(:cwd => @cwd).once.returns(@env)
46
+ @env.expects(:load!).returns(@env)
47
+ assert_equal @env, Vagrant::Environment.load!(@cwd)
48
+ end
49
+
50
+ should "work without a given cwd" do
51
+ Vagrant::Environment.expects(:new).with(:cwd => nil).returns(@env)
52
+
53
+ assert_nothing_raised {
54
+ env = Vagrant::Environment.load!
55
+ assert_equal env, @env
56
+ }
57
+ end
58
+ end
59
+
60
+ context "initialization" do
61
+ should "set the cwd if given" do
62
+ cwd = "foobarbaz"
63
+ env = Vagrant::Environment.new(:cwd => cwd)
64
+ assert_equal cwd, env.cwd
65
+ end
66
+
67
+ should "default to pwd if cwd is nil" do
68
+ env = Vagrant::Environment.new
69
+ assert_equal Dir.pwd, env.cwd
70
+ end
71
+ end
72
+
73
+ context "paths" do
74
+ setup do
75
+ @env = mock_environment
76
+ end
77
+
78
+ context "cwd" do
79
+ should "default to Dir.pwd" do
80
+ assert_equal Dir.pwd, @env.cwd
81
+ end
82
+
83
+ should "return cwd if set" do
84
+ @env.cwd = "foo"
85
+ assert_equal "foo", @env.cwd
86
+ end
87
+ end
88
+
89
+ context "dotfile path" do
90
+ setup do
91
+ @env.stubs(:root_path).returns("foo")
92
+ end
93
+
94
+ should "build up the dotfile out of the root path and the dotfile name" do
95
+ assert_equal File.join(@env.root_path, @env.config.vagrant.dotfile_name), @env.dotfile_path
96
+ end
97
+ end
98
+
99
+ context "home path" do
100
+ should "return nil if config is not yet loaded" do
101
+ @env.stubs(:config).returns(nil)
102
+ assert_nil @env.home_path
103
+ end
104
+
105
+ should "return the home path if it loaded" do
106
+ assert_equal @env.config.vagrant.home, @env.home_path
107
+ end
108
+ end
109
+
110
+ context "temp path" do
111
+ should "return the home path joined with 'tmp'" do
112
+ home_path = "foo"
113
+ @env.stubs(:home_path).returns(home_path)
114
+ assert_equal File.join("foo", "tmp"), @env.tmp_path
115
+ end
116
+ end
117
+
118
+ context "boxes path" do
119
+ should "return the home path joined with 'tmp'" do
120
+ home_path = "foo"
121
+ @env.stubs(:home_path).returns(home_path)
122
+ assert_equal File.join("foo", "boxes"), @env.boxes_path
123
+ end
124
+ end
125
+ end
126
+
127
+ context "primary VM helper" do
128
+ setup do
129
+ @env = mock_environment
130
+ @env.stubs(:multivm?).returns(true)
131
+ end
132
+
133
+ should "return the first VM if not multivm" do
134
+ result = mock("result")
135
+
136
+ @env.stubs(:multivm?).returns(false)
137
+ @env.stubs(:vms).returns({:default => result})
138
+
139
+ assert_equal result, @env.primary_vm
140
+ end
141
+
142
+ should "call and return the primary VM from the parent if has one" do
143
+ result = mock("result")
144
+ parent = mock("parent")
145
+ parent.expects(:primary_vm).returns(result)
146
+
147
+ @env.stubs(:parent).returns(parent)
148
+ assert_equal result, @env.primary_vm
149
+ end
150
+
151
+ should "return nil if no VM is marked as primary" do
152
+ @env.config.vm.define(:foo)
153
+ @env.config.vm.define(:bar)
154
+ @env.config.vm.define(:baz)
155
+
156
+ assert @env.primary_vm.nil?
157
+ end
158
+
159
+ should "return the primary VM" do
160
+ @env.config.vm.define(:foo)
161
+ @env.config.vm.define(:bar, :primary => true)
162
+ @env.config.vm.define(:baz)
163
+
164
+ result = mock("result")
165
+ vms = {
166
+ :foo => :foo,
167
+ :bar => result,
168
+ :baz => :baz
169
+ }
170
+ @env.stubs(:vms).returns(vms)
171
+
172
+ assert_equal result, @env.primary_vm
173
+ end
174
+ end
175
+
176
+ context "multivm? helper" do
177
+ setup do
178
+ @env = mock_environment
179
+ end
180
+
181
+ context "with a parent" do
182
+ setup do
183
+ @parent = mock('parent')
184
+ @env.stubs(:parent).returns(@parent)
185
+ end
186
+
187
+ should "return the value of multivm? from the parent" do
188
+ result = mock("result")
189
+ @parent.stubs(:multivm?).returns(result)
190
+ assert_equal result, @env.multivm?
191
+ end
192
+ end
193
+
194
+ context "without a parent" do
195
+ setup do
196
+ @env.stubs(:parent).returns(nil)
197
+ end
198
+
199
+ should "return true if VM length greater than 1" do
200
+ @env.stubs(:vms).returns([1,2,3])
201
+ assert @env.multivm?
202
+ end
203
+
204
+ should "return false if VM length is 1" do
205
+ @env.stubs(:vms).returns([1])
206
+ assert !@env.multivm?
207
+ end
208
+ end
209
+ end
210
+
211
+ context "loading" do
212
+ setup do
213
+ @env = mock_environment
214
+ end
215
+
216
+ context "overall load method" do
217
+ should "load! should call proper sequence and return itself" do
218
+ call_seq = sequence("call_sequence")
219
+ @env.expects(:load_logger!).once.in_sequence(call_seq)
220
+ @env.expects(:load_root_path!).once.in_sequence(call_seq)
221
+ @env.expects(:load_config!).once.in_sequence(call_seq)
222
+ @env.expects(:load_home_directory!).once.in_sequence(call_seq)
223
+ @env.expects(:load_host!).once.in_sequence(call_seq)
224
+ @env.expects(:load_box!).once.in_sequence(call_seq)
225
+ @env.expects(:load_config!).once.in_sequence(call_seq)
226
+ Vagrant::Environment.expects(:check_virtualbox!).once.in_sequence(call_seq)
227
+ @env.expects(:load_vm!).once.in_sequence(call_seq)
228
+ @env.expects(:load_active_list!).once.in_sequence(call_seq)
229
+ @env.expects(:load_commands!).once.in_sequence(call_seq)
230
+ @env.expects(:load_actions!).once.in_sequence(call_seq)
231
+ assert_equal @env, @env.load!
232
+ end
233
+ end
234
+
235
+ context "loading the root path" do
236
+ setup do
237
+ @env.cwd = "/foo"
238
+ end
239
+
240
+ should "default the path to the cwd instance var if nil" do
241
+ @path = mock("path")
242
+ @path.stubs(:root?).returns(true)
243
+ File.expects(:expand_path).with(@env.cwd).returns(@env.cwd)
244
+ Pathname.expects(:new).with(@env.cwd).returns(@path)
245
+ @env.load_root_path!(nil)
246
+ end
247
+
248
+ should "not default the path to pwd if its not nil" do
249
+ @path = mock("path")
250
+ @path.stubs(:to_s).returns("/")
251
+ File.expects(:expand_path).with(@path).returns("/")
252
+ Pathname.expects(:new).with("/").returns(@path)
253
+ @path.stubs(:root?).returns(true)
254
+ @env.load_root_path!(@path)
255
+ end
256
+
257
+ should "should walk the parent directories looking for rootfile" do
258
+ paths = [
259
+ Pathname.new("/foo/bar/baz"),
260
+ Pathname.new("/foo/bar"),
261
+ Pathname.new("/foo")
262
+ ]
263
+
264
+ search_seq = sequence("search_seq")
265
+ paths.each do |path|
266
+ # NOTE File.expect(:expand_path) causes tests to hang in windows below is the interim solution
267
+ File.expects(:exist?).with("#{File.expand_path(path)}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(false).in_sequence(search_seq)
268
+ end
269
+
270
+ assert !@env.load_root_path!(paths.first)
271
+ end
272
+
273
+ should "return false if not found" do
274
+ path = Pathname.new("/")
275
+ assert !@env.load_root_path!(path)
276
+ end
277
+
278
+ should "return false if not found on windows-style root" do
279
+ # TODO: Is there _any_ way to test this on unix machines? The
280
+ # expand path doesn't work [properly for the test] on unix machines.
281
+ if RUBY_PLATFORM.downcase.include?("mswin")
282
+ # Note the escaped back slash
283
+ path = Pathname.new("C:\\")
284
+ assert !@env.load_root_path!(path)
285
+ end
286
+ end
287
+
288
+ should "should set the path for the rootfile" do
289
+ # NOTE File.expect(:expand_path) causes tests to hang in windows below is the interim solution
290
+ path = File.expand_path("/foo")
291
+ File.expects(:exist?).with("#{path}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(true)
292
+
293
+ assert @env.load_root_path!(Pathname.new(path))
294
+ assert_equal path, @env.root_path
295
+ end
296
+ end
297
+
298
+ context "loading config" do
299
+ setup do
300
+ @root_path = "/foo"
301
+ @home_path = "/bar"
302
+ @env.stubs(:root_path).returns(@root_path)
303
+ @env.stubs(:home_path).returns(@home_path)
304
+ @env.stubs(:load_logger!)
305
+
306
+ @parent_env = mock_environment
307
+
308
+ File.stubs(:exist?).returns(false)
309
+ end
310
+
311
+ should "reset the configuration object" do
312
+ Vagrant::Config.expects(:reset!).with(@env).once
313
+ @env.load_config!
314
+ end
315
+
316
+ should "load from the project root" do
317
+ File.expects(:exist?).with(File.join(PROJECT_ROOT, "config", "default.rb")).once
318
+ @env.load_config!
319
+ end
320
+
321
+ should "load from the root path" do
322
+ File.expects(:exist?).with(File.join(@root_path, Vagrant::Environment::ROOTFILE_NAME)).once
323
+ @env.load_config!
324
+ end
325
+
326
+ should "not load from the root path if nil" do
327
+ @env.stubs(:root_path).returns(nil)
328
+ File.expects(:exist?).with(File.join(@root_path, Vagrant::Environment::ROOTFILE_NAME)).never
329
+ @env.load_config!
330
+ end
331
+
332
+ should "load from the home directory" do
333
+ File.expects(:exist?).with(File.join(@env.home_path, Vagrant::Environment::ROOTFILE_NAME)).once
334
+ @env.load_config!
335
+ end
336
+
337
+ should "not load from the home directory if the config is nil" do
338
+ @env.stubs(:home_path).returns(nil)
339
+ File.expects(:exist?).twice.returns(false)
340
+ @env.load_config!
341
+ end
342
+
343
+ should "not load from the box directory if it is nil" do
344
+ @env.expects(:box).once.returns(nil)
345
+ File.expects(:exist?).twice.returns(false)
346
+ @env.load_config!
347
+ end
348
+
349
+ should "load from the box directory if it is not nil" do
350
+ dir = "foo"
351
+ box = mock("box")
352
+ box.stubs(:directory).returns(dir)
353
+ @env.expects(:box).twice.returns(box)
354
+ File.expects(:exist?).with(File.join(dir, Vagrant::Environment::ROOTFILE_NAME)).once
355
+ @env.load_config!
356
+ end
357
+
358
+ should "load a sub-VM configuration if specified" do
359
+ vm_name = :foo
360
+ sub_box = :YO
361
+ @parent_env.config.vm.box = :NO
362
+ @parent_env.config.vm.define(vm_name) do |config|
363
+ config.vm.box = sub_box
364
+ end
365
+
366
+ # Sanity
367
+ assert_equal :NO, @parent_env.config.vm.box
368
+
369
+ @env.stubs(:vm_name).returns(vm_name)
370
+ @env.stubs(:parent).returns(@parent_env)
371
+
372
+ @env.load_config!
373
+
374
+ assert_equal sub_box, @env.config.vm.box
375
+ end
376
+
377
+ should "load the files only if exist? returns true" do
378
+ File.expects(:exist?).once.returns(true)
379
+ @env.expects(:load).once
380
+ @env.load_config!
381
+ end
382
+
383
+ should "not load the files if exist? returns false" do
384
+ @env.expects(:load).never
385
+ @env.load_config!
386
+ end
387
+
388
+ should "execute after loading and set result to environment config" do
389
+ result = mock("result")
390
+ File.expects(:exist?).once.returns(true)
391
+ @env.expects(:load).once
392
+ Vagrant::Config.expects(:execute!).once.returns(result)
393
+ @env.load_config!
394
+ assert_equal result, @env.config
395
+ end
396
+
397
+ should "reload the logger after executing" do
398
+ load_seq = sequence("load_seq")
399
+ Vagrant::Config.expects(:execute!).once.returns(nil).in_sequence(load_seq)
400
+ @env.expects(:load_logger!).once.in_sequence(load_seq)
401
+ @env.load_config!
402
+ end
403
+ end
404
+
405
+ context "loading logger" do
406
+ setup do
407
+ @env = mock_environment
408
+ @env.stubs(:vm_name).returns(nil)
409
+ end
410
+
411
+ should "use 'vagrant' by default" do
412
+ assert @env.vm_name.nil? # sanity
413
+ @env.load_logger!
414
+ assert_equal "vagrant", @env.logger.resource
415
+ end
416
+
417
+ should "use the vm name if available" do
418
+ name = "foo"
419
+ @env.stubs(:vm_name).returns(name)
420
+ @env.load_logger!
421
+ assert_equal name, @env.logger.resource
422
+ end
423
+ end
424
+
425
+ context "loading home directory" do
426
+ setup do
427
+ @env = mock_environment
428
+ @home_dir = File.expand_path(@env.config.vagrant.home)
429
+
430
+ File.stubs(:directory?).returns(true)
431
+ FileUtils.stubs(:mkdir_p)
432
+ end
433
+
434
+ should "create each directory if it doesn't exist" do
435
+ create_seq = sequence("create_seq")
436
+ File.stubs(:directory?).returns(false)
437
+ Vagrant::Environment::HOME_SUBDIRS.each do |subdir|
438
+ FileUtils.expects(:mkdir_p).with(File.join(@home_dir, subdir)).in_sequence(create_seq)
439
+ end
440
+
441
+ @env.load_home_directory!
442
+ end
443
+
444
+ should "not create directories if they exist" do
445
+ File.stubs(:directory?).returns(true)
446
+ FileUtils.expects(:mkdir_p).never
447
+ @env.load_home_directory!
448
+ end
449
+ end
450
+
451
+ context "loading host" do
452
+ setup do
453
+ @env = mock_environment
454
+ end
455
+
456
+ should "load the host by calling the load method on Host::Base" do
457
+ result = mock("result")
458
+ Vagrant::Hosts::Base.expects(:load).with(@env, @env.config.vagrant.host).once.returns(result)
459
+ @env.load_host!
460
+ assert_equal result, @env.host
461
+ end
462
+ end
463
+
464
+ context "loading box" do
465
+ setup do
466
+ @box = mock("box")
467
+ @box.stubs(:env=)
468
+
469
+ @env = mock_environment
470
+ @env.stubs(:root_path).returns("foo")
471
+ end
472
+
473
+ should "do nothing if the root path is nil" do
474
+ Vagrant::Box.expects(:find).never
475
+ @env.stubs(:root_path).returns(nil)
476
+ @env.load_box!
477
+ end
478
+
479
+ should "not load the box if its not set" do
480
+ @env = mock_environment do |config|
481
+ config.vm.box = nil
482
+ end
483
+
484
+ Vagrant::Box.expects(:find).never
485
+ @env.load_box!
486
+ end
487
+
488
+ should "set the box to what is found by the Box class" do
489
+ Vagrant::Box.expects(:find).with(@env, @env.config.vm.box).once.returns(@box)
490
+ @env.load_box!
491
+ assert @box.equal?(@env.box)
492
+ end
493
+ end
494
+
495
+ context "loading the UUID out from the persisted dotfile" do
496
+ setup do
497
+ @env = mock_environment
498
+ @env.stubs(:root_path).returns("foo")
499
+
500
+ File.stubs(:file?).returns(true)
501
+ end
502
+
503
+ should "blank the VMs" do
504
+ load_seq = sequence("load_seq")
505
+ @env.stubs(:root_path).returns("foo")
506
+ @env.expects(:load_blank_vms!).in_sequence(load_seq)
507
+ File.expects(:open).in_sequence(load_seq)
508
+ @env.load_vm!
509
+ end
510
+
511
+ should "load the UUID if the JSON parsing fails" do
512
+ vm = mock("vm")
513
+
514
+ filemock = mock("filemock")
515
+ filemock.expects(:read).returns("foo")
516
+ Vagrant::VM.expects(:find).with("foo", @env, Vagrant::Environment::DEFAULT_VM).returns(vm)
517
+ File.expects(:open).with(@env.dotfile_path).once.yields(filemock)
518
+ File.expects(:file?).with(@env.dotfile_path).once.returns(true)
519
+ @env.load_vm!
520
+
521
+ assert_equal vm, @env.vms.values.first
522
+ end
523
+
524
+ should "load all the VMs from the dotfile" do
525
+ vms = { :foo => "bar", :bar => "baz" }
526
+ results = {}
527
+
528
+ filemock = mock("filemock")
529
+ filemock.expects(:read).returns(vms.to_json)
530
+ File.expects(:open).with(@env.dotfile_path).once.yields(filemock)
531
+ File.expects(:file?).with(@env.dotfile_path).once.returns(true)
532
+
533
+ vms.each do |key, value|
534
+ vm = mock("vm#{key}")
535
+ Vagrant::VM.expects(:find).with(value, @env, key.to_sym).returns(vm)
536
+ results[key] = vm
537
+ end
538
+
539
+ @env.load_vm!
540
+
541
+ results.each do |key, value|
542
+ assert_equal value, @env.vms[key]
543
+ end
544
+ end
545
+
546
+ should "do nothing if the vm_name is set" do
547
+ @env.stubs(:vm_name).returns(:foo)
548
+ File.expects(:open).never
549
+ @env.load_vm!
550
+ end
551
+
552
+ should "do nothing if the dotfile is nil" do
553
+ @env.stubs(:dotfile_path).returns(nil)
554
+ File.expects(:open).never
555
+
556
+ assert_nothing_raised {
557
+ @env.load_vm!
558
+ }
559
+ end
560
+
561
+ should "do nothing if dotfile is not a file" do
562
+ File.expects(:file?).returns(false)
563
+ File.expects(:open).never
564
+ @env.load_vm!
565
+ end
566
+
567
+ should "uuid should be nil if dotfile didn't exist" do
568
+ File.expects(:open).raises(Errno::ENOENT)
569
+ @env.load_vm!
570
+ assert_nil @env.vm
571
+ end
572
+ end
573
+
574
+ context "loading blank VMs" do
575
+ setup do
576
+ @env = mock_environment
577
+ end
578
+
579
+ should "blank the VMs" do
580
+ @env = mock_environment do |config|
581
+ config.vm.define :foo do |config|
582
+ end
583
+
584
+ config.vm.define :bar do |config|
585
+ end
586
+ end
587
+
588
+ @env.load_blank_vms!
589
+
590
+ assert_equal 2, @env.vms.length
591
+ assert(@env.vms.all? { |name, vm| !vm.created? })
592
+
593
+ sorted_vms = @env.vms.keys.sort { |a,b| a.to_s <=> b.to_s }
594
+ assert_equal [:bar, :foo], sorted_vms
595
+ end
596
+
597
+ should "load the default VM blank if no multi-VMs are specified" do
598
+ assert @env.config.vm.defined_vms.empty? # sanity
599
+
600
+ @env.load_blank_vms!
601
+
602
+ assert_equal 1, @env.vms.length
603
+ assert !@env.vms.values.first.created?
604
+ end
605
+ end
606
+
607
+ context "loading the active list" do
608
+ setup do
609
+ @env = mock_environment
610
+ end
611
+
612
+ should "initialize the ActiveList object with the given environment" do
613
+ active_list = mock("active_list")
614
+ Vagrant::ActiveList.expects(:new).with(@env).returns(active_list)
615
+ @env.load_active_list!
616
+ assert_equal active_list, @env.active_list
617
+ end
618
+ end
619
+
620
+ context "loading the commands" do
621
+ setup do
622
+ @env = mock_environment
623
+ end
624
+
625
+ should "initialize the Commands object with the given environment" do
626
+ commands = mock("commands")
627
+ Vagrant::Command.expects(:new).with(@env).returns(commands)
628
+ @env.load_commands!
629
+ assert_equal commands, @env.commands
630
+ end
631
+ end
632
+
633
+ context "loading actions" do
634
+ setup do
635
+ @env = mock_environment
636
+ end
637
+
638
+ should "initialize the Action object with the given environment" do
639
+ result = mock("result")
640
+ Vagrant::Action.expects(:new).with(@env).returns(result)
641
+ @env.load_actions!
642
+ assert_equal result, @env.actions
643
+ end
644
+ end
645
+ end
646
+
647
+ context "requiring properties" do
648
+ setup do
649
+ @env = mock_environment
650
+ end
651
+
652
+ context "requiring root_path" do
653
+ should "error and exit if no root_path is set" do
654
+ @env.expects(:root_path).returns(nil)
655
+ @env.expects(:error_and_exit).with(:rootfile_not_found).once
656
+ @env.require_root_path
657
+ end
658
+
659
+ should "not error and exit if root_path is set" do
660
+ @env.expects(:root_path).returns("foo")
661
+ @env.expects(:error_and_exit).never
662
+ @env.require_root_path
663
+ end
664
+ end
665
+
666
+ context "requiring a persisted VM" do
667
+ setup do
668
+ @env.stubs(:vm).returns("foo")
669
+ @env.stubs(:require_root_path)
670
+ end
671
+
672
+ should "require a root path" do
673
+ @env.expects(:require_root_path).once
674
+ @env.expects(:error_and_exit).never
675
+ @env.require_persisted_vm
676
+ end
677
+
678
+ should "error and exit if the VM is not set" do
679
+ @env.expects(:vm).returns(nil)
680
+ @env.expects(:error_and_exit).once
681
+ @env.require_persisted_vm
682
+ end
683
+ end
684
+ end
685
+
686
+ context "managing VM" do
687
+ setup do
688
+ @env = mock_environment
689
+
690
+ @dotfile_path = "foo"
691
+ @env.stubs(:dotfile_path).returns(@dotfile_path)
692
+ end
693
+
694
+ def mock_vm
695
+ @vm = mock("vm")
696
+ @vm.stubs(:uuid).returns("foo")
697
+ @env.stubs(:vm).returns(@vm)
698
+ end
699
+ end
700
+
701
+ context "updating the dotfile" do
702
+ setup do
703
+ @env = mock_environment
704
+ @env.stubs(:parent).returns(nil)
705
+ @env.stubs(:dotfile_path).returns("foo")
706
+ File.stubs(:open)
707
+ File.stubs(:exist?).returns(true)
708
+ end
709
+
710
+ def create_vm(created)
711
+ vm = mock("vm")
712
+ vm.stubs(:created?).returns(created)
713
+ vm.stubs(:uuid).returns("foo")
714
+ vm
715
+ end
716
+
717
+ should "call parent if exists" do
718
+ parent = mock("parent")
719
+ @env.stubs(:parent).returns(parent)
720
+ parent.expects(:update_dotfile).once
721
+
722
+ @env.update_dotfile
723
+ end
724
+
725
+ should "remove the dotfile if the data is empty" do
726
+ vms = {
727
+ :foo => create_vm(false)
728
+ }
729
+
730
+ @env.stubs(:vms).returns(vms)
731
+ File.expects(:delete).with(@env.dotfile_path).once
732
+ @env.update_dotfile
733
+ end
734
+
735
+ should "not remove the dotfile if it doesn't exist" do
736
+ vms = {
737
+ :foo => create_vm(false)
738
+ }
739
+
740
+ @env.stubs(:vms).returns(vms)
741
+ File.expects(:exist?).with(@env.dotfile_path).returns(false)
742
+ File.expects(:delete).never
743
+ assert_nothing_raised { @env.update_dotfile }
744
+ end
745
+
746
+ should "write the proper data to dotfile" do
747
+ vms = {
748
+ :foo => create_vm(false),
749
+ :bar => create_vm(true),
750
+ :baz => create_vm(true)
751
+ }
752
+
753
+ f = mock("f")
754
+ @env.stubs(:vms).returns(vms)
755
+ File.expects(:open).with(@env.dotfile_path, 'w+').yields(f)
756
+ f.expects(:write).with() do |json|
757
+ assert_nothing_raised {
758
+ data = JSON.parse(json)
759
+ assert_equal 2, data.length
760
+ assert_equal vms[:bar].uuid, data["bar"]
761
+ assert_equal vms[:baz].uuid, data["baz"]
762
+ }
763
+
764
+ true
765
+ end
766
+
767
+ @env.update_dotfile
768
+ end
769
+ end
770
+ end