vagrant 0.2.0 → 0.3.0

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 (149) hide show
  1. data/Gemfile +4 -4
  2. data/Rakefile +3 -3
  3. data/VERSION +1 -1
  4. data/bin/vagrant +5 -13
  5. data/config/default.rb +1 -0
  6. data/keys/README.md +8 -1
  7. data/keys/vagrant.ppk +26 -0
  8. data/lib/vagrant.rb +3 -3
  9. data/lib/vagrant/actions/base.rb +15 -4
  10. data/lib/vagrant/actions/box/add.rb +1 -1
  11. data/lib/vagrant/actions/box/download.rb +72 -66
  12. data/lib/vagrant/actions/box/unpackage.rb +1 -4
  13. data/lib/vagrant/actions/runner.rb +1 -1
  14. data/lib/vagrant/actions/vm/boot.rb +5 -7
  15. data/lib/vagrant/actions/vm/customize.rb +2 -2
  16. data/lib/vagrant/actions/vm/destroy.rb +2 -2
  17. data/lib/vagrant/actions/vm/down.rb +7 -0
  18. data/lib/vagrant/actions/vm/export.rb +10 -4
  19. data/lib/vagrant/actions/vm/forward_ports.rb +5 -15
  20. data/lib/vagrant/actions/vm/halt.rb +5 -3
  21. data/lib/vagrant/actions/vm/import.rb +10 -3
  22. data/lib/vagrant/actions/vm/move_hard_drive.rb +1 -3
  23. data/lib/vagrant/actions/vm/package.rb +33 -10
  24. data/lib/vagrant/actions/vm/provision.rb +4 -4
  25. data/lib/vagrant/actions/vm/reload.rb +1 -1
  26. data/lib/vagrant/actions/vm/resume.rb +1 -1
  27. data/lib/vagrant/actions/vm/shared_folders.rb +7 -7
  28. data/lib/vagrant/actions/vm/start.rb +3 -2
  29. data/lib/vagrant/actions/vm/suspend.rb +2 -2
  30. data/lib/vagrant/actions/vm/up.rb +7 -17
  31. data/lib/vagrant/active_list.rb +52 -45
  32. data/lib/vagrant/box.rb +18 -11
  33. data/lib/vagrant/busy.rb +7 -0
  34. data/lib/vagrant/command.rb +27 -0
  35. data/lib/vagrant/commands/base.rb +163 -0
  36. data/lib/vagrant/commands/box.rb +16 -0
  37. data/lib/vagrant/commands/box/add.rb +24 -0
  38. data/lib/vagrant/commands/box/list.rb +30 -0
  39. data/lib/vagrant/commands/box/remove.rb +31 -0
  40. data/lib/vagrant/commands/destroy.rb +23 -0
  41. data/lib/vagrant/commands/down.rb +16 -0
  42. data/lib/vagrant/commands/halt.rb +23 -0
  43. data/lib/vagrant/commands/init.rb +32 -0
  44. data/lib/vagrant/commands/package.rb +46 -0
  45. data/lib/vagrant/commands/reload.rb +22 -0
  46. data/lib/vagrant/commands/resume.rb +22 -0
  47. data/lib/vagrant/commands/ssh.rb +22 -0
  48. data/lib/vagrant/commands/ssh_config.rb +30 -0
  49. data/lib/vagrant/commands/status.rb +58 -0
  50. data/lib/vagrant/commands/suspend.rb +23 -0
  51. data/lib/vagrant/commands/up.rb +26 -0
  52. data/lib/vagrant/config.rb +21 -11
  53. data/lib/vagrant/downloaders/file.rb +5 -5
  54. data/lib/vagrant/downloaders/http.rb +10 -15
  55. data/lib/vagrant/environment.rb +259 -0
  56. data/lib/vagrant/provisioners/base.rb +7 -0
  57. data/lib/vagrant/provisioners/chef.rb +24 -9
  58. data/lib/vagrant/provisioners/chef_server.rb +23 -48
  59. data/lib/vagrant/provisioners/chef_solo.rb +48 -22
  60. data/lib/vagrant/ssh.rb +95 -46
  61. data/lib/vagrant/util.rb +2 -2
  62. data/lib/vagrant/util/errors.rb +36 -0
  63. data/lib/vagrant/util/platform.rb +12 -0
  64. data/lib/vagrant/util/progress_meter.rb +33 -0
  65. data/lib/vagrant/util/stacked_proc_runner.rb +35 -0
  66. data/lib/vagrant/util/template_renderer.rb +83 -0
  67. data/lib/vagrant/vm.rb +1 -0
  68. data/templates/{Vagrantfile → Vagrantfile.erb} +2 -2
  69. data/templates/chef_server_client.erb +16 -0
  70. data/templates/chef_solo_solo.erb +4 -0
  71. data/templates/errors.yml +157 -0
  72. data/templates/package_Vagrantfile.erb +11 -0
  73. data/templates/ssh_config.erb +7 -0
  74. data/test/test_helper.rb +12 -15
  75. data/test/vagrant/actions/box/add_test.rb +1 -2
  76. data/test/vagrant/actions/box/destroy_test.rb +0 -1
  77. data/test/vagrant/actions/box/download_test.rb +40 -15
  78. data/test/vagrant/actions/box/unpackage_test.rb +2 -3
  79. data/test/vagrant/actions/collection_test.rb +8 -5
  80. data/test/vagrant/actions/runner_test.rb +8 -6
  81. data/test/vagrant/actions/vm/boot_test.rb +12 -11
  82. data/test/vagrant/actions/vm/customize_test.rb +2 -3
  83. data/test/vagrant/actions/vm/destroy_test.rb +2 -3
  84. data/test/vagrant/actions/vm/down_test.rb +16 -3
  85. data/test/vagrant/actions/vm/export_test.rb +4 -5
  86. data/test/vagrant/actions/vm/forward_ports_test.rb +6 -5
  87. data/test/vagrant/actions/vm/halt_test.rb +8 -2
  88. data/test/vagrant/actions/vm/import_test.rb +5 -5
  89. data/test/vagrant/actions/vm/move_hard_drive_test.rb +4 -6
  90. data/test/vagrant/actions/vm/package_test.rb +60 -22
  91. data/test/vagrant/actions/vm/provision_test.rb +7 -16
  92. data/test/vagrant/actions/vm/reload_test.rb +3 -2
  93. data/test/vagrant/actions/vm/resume_test.rb +0 -1
  94. data/test/vagrant/actions/vm/shared_folders_test.rb +17 -12
  95. data/test/vagrant/actions/vm/start_test.rb +10 -3
  96. data/test/vagrant/actions/vm/suspend_test.rb +1 -2
  97. data/test/vagrant/actions/vm/up_test.rb +19 -11
  98. data/test/vagrant/active_list_test.rb +148 -129
  99. data/test/vagrant/box_test.rb +26 -14
  100. data/test/vagrant/busy_test.rb +15 -6
  101. data/test/vagrant/command_test.rb +53 -0
  102. data/test/vagrant/commands/base_test.rb +118 -0
  103. data/test/vagrant/commands/box/add_test.rb +34 -0
  104. data/test/vagrant/commands/box/list_test.rb +32 -0
  105. data/test/vagrant/commands/box/remove_test.rb +41 -0
  106. data/test/vagrant/commands/destroy_test.rb +32 -0
  107. data/test/vagrant/commands/down_test.rb +17 -0
  108. data/test/vagrant/commands/halt_test.rb +28 -0
  109. data/test/vagrant/commands/init_test.rb +55 -0
  110. data/test/vagrant/commands/package_test.rb +84 -0
  111. data/test/vagrant/commands/reload_test.rb +28 -0
  112. data/test/vagrant/commands/resume_test.rb +33 -0
  113. data/test/vagrant/commands/ssh_config_test.rb +54 -0
  114. data/test/vagrant/commands/ssh_test.rb +32 -0
  115. data/test/vagrant/commands/status_test.rb +20 -0
  116. data/test/vagrant/commands/suspend_test.rb +33 -0
  117. data/test/vagrant/commands/up_test.rb +41 -0
  118. data/test/vagrant/config_test.rb +42 -17
  119. data/test/vagrant/downloaders/file_test.rb +7 -0
  120. data/test/vagrant/downloaders/http_test.rb +12 -0
  121. data/test/vagrant/environment_test.rb +595 -0
  122. data/test/vagrant/provisioners/base_test.rb +7 -1
  123. data/test/vagrant/provisioners/chef_server_test.rb +41 -51
  124. data/test/vagrant/provisioners/chef_solo_test.rb +93 -62
  125. data/test/vagrant/provisioners/chef_test.rb +61 -15
  126. data/test/vagrant/ssh_test.rb +166 -38
  127. data/test/vagrant/util/errors_test.rb +57 -0
  128. data/test/vagrant/util/progress_meter_test.rb +33 -0
  129. data/test/vagrant/{stacked_proc_runner_test.rb → util/stacked_proc_runner_test.rb} +3 -3
  130. data/test/vagrant/util/template_renderer_test.rb +138 -0
  131. data/test/vagrant/vm_test.rb +3 -2
  132. data/vagrant.gemspec +88 -33
  133. metadata +94 -51
  134. data/bin/vagrant-box +0 -34
  135. data/bin/vagrant-down +0 -27
  136. data/bin/vagrant-halt +0 -28
  137. data/bin/vagrant-init +0 -27
  138. data/bin/vagrant-package +0 -29
  139. data/bin/vagrant-reload +0 -29
  140. data/bin/vagrant-resume +0 -27
  141. data/bin/vagrant-ssh +0 -27
  142. data/bin/vagrant-status +0 -29
  143. data/bin/vagrant-suspend +0 -27
  144. data/bin/vagrant-up +0 -29
  145. data/lib/vagrant/commands.rb +0 -234
  146. data/lib/vagrant/env.rb +0 -189
  147. data/lib/vagrant/stacked_proc_runner.rb +0 -33
  148. data/test/vagrant/commands_test.rb +0 -269
  149. data/test/vagrant/env_test.rb +0 -418
@@ -23,4 +23,11 @@ class FileDownloaderTest < Test::Unit::TestCase
23
23
  @downloader.download!(@uri, @tempfile)
24
24
  end
25
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
26
33
  end
@@ -34,6 +34,18 @@ class HttpDownloaderTest < Test::Unit::TestCase
34
34
  end
35
35
  end
36
36
 
37
+ context "matching the uri" do
38
+ should "use extract to verify that the string is in fact a uri" do
39
+ URI.expects(:extract).returns(['foo'])
40
+ assert Vagrant::Downloaders::HTTP.match?('foo')
41
+ end
42
+
43
+ should "return false if there are no extract results" do
44
+ URI.expects(:extract).returns([])
45
+ assert !Vagrant::Downloaders::HTTP.match?('foo')
46
+ end
47
+ end
48
+
37
49
  context "reporting progress" do
38
50
  # TODO: Testing for this, probably
39
51
  end
@@ -0,0 +1,595 @@
1
+ require File.join(File.dirname(__FILE__), '..', '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.1.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.1" do
22
+ version = "3.0.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
+ end
28
+
29
+ context "class method load!" do
30
+ setup do
31
+ @cwd = mock('cwd')
32
+
33
+ @env = mock('env')
34
+ @env.stubs(:load!).returns(@env)
35
+ end
36
+
37
+ should "create the environment with given cwd, load it, and return it" do
38
+ Vagrant::Environment.expects(:new).with(@cwd).once.returns(@env)
39
+ @env.expects(:load!).returns(@env)
40
+ assert_equal @env, Vagrant::Environment.load!(@cwd)
41
+ end
42
+
43
+ should "work without a given cwd" do
44
+ Vagrant::Environment.expects(:new).with(nil).returns(@env)
45
+
46
+ assert_nothing_raised {
47
+ env = Vagrant::Environment.load!
48
+ assert_equal env, @env
49
+ }
50
+ end
51
+ end
52
+
53
+ context "initialization" do
54
+ should "set the cwd if given" do
55
+ cwd = "foobarbaz"
56
+ env = Vagrant::Environment.new(cwd)
57
+ assert_equal cwd, env.cwd
58
+ end
59
+
60
+ should "default to pwd if cwd is nil" do
61
+ env = Vagrant::Environment.new
62
+ assert_equal Dir.pwd, env.cwd
63
+ end
64
+ end
65
+
66
+ context "paths" do
67
+ setup do
68
+ @env = mock_environment
69
+ end
70
+
71
+ context "cwd" do
72
+ should "default to Dir.pwd" do
73
+ assert_equal Dir.pwd, @env.cwd
74
+ end
75
+
76
+ should "return cwd if set" do
77
+ @env.cwd = "foo"
78
+ assert_equal "foo", @env.cwd
79
+ end
80
+ end
81
+
82
+ context "dotfile path" do
83
+ setup do
84
+ @env.stubs(:root_path).returns("foo")
85
+ end
86
+
87
+ should "build up the dotfile out of the root path and the dotfile name" do
88
+ assert_equal File.join(@env.root_path, @env.config.vagrant.dotfile_name), @env.dotfile_path
89
+ end
90
+ end
91
+
92
+ context "home path" do
93
+ should "return nil if config is not yet loaded" do
94
+ @env.stubs(:config).returns(nil)
95
+ assert_nil @env.home_path
96
+ end
97
+
98
+ should "return the home path if it loaded" do
99
+ assert_equal @env.config.vagrant.home, @env.home_path
100
+ end
101
+ end
102
+
103
+ context "temp path" do
104
+ should "return the home path joined with 'tmp'" do
105
+ home_path = "foo"
106
+ @env.stubs(:home_path).returns(home_path)
107
+ assert_equal File.join("foo", "tmp"), @env.tmp_path
108
+ end
109
+ end
110
+
111
+ context "boxes path" do
112
+ should "return the home path joined with 'tmp'" do
113
+ home_path = "foo"
114
+ @env.stubs(:home_path).returns(home_path)
115
+ assert_equal File.join("foo", "boxes"), @env.boxes_path
116
+ end
117
+ end
118
+ end
119
+
120
+ context "loading" do
121
+ setup do
122
+ @env = mock_environment
123
+ end
124
+
125
+ context "overall load method" do
126
+ should "load! should call proper sequence and return itself" do
127
+ call_seq = sequence("call_sequence")
128
+ @env.expects(:load_root_path!).once.in_sequence(call_seq)
129
+ @env.expects(:load_config!).once.in_sequence(call_seq)
130
+ @env.expects(:load_home_directory!).once.in_sequence(call_seq)
131
+ @env.expects(:load_box!).once.in_sequence(call_seq)
132
+ @env.expects(:load_config!).once.in_sequence(call_seq)
133
+ Vagrant::Environment.expects(:check_virtualbox!).once.in_sequence(call_seq)
134
+ @env.expects(:load_vm!).once.in_sequence(call_seq)
135
+ @env.expects(:load_ssh!).once.in_sequence(call_seq)
136
+ @env.expects(:load_active_list!).once.in_sequence(call_seq)
137
+ @env.expects(:load_commands!).once.in_sequence(call_seq)
138
+ assert_equal @env, @env.load!
139
+ end
140
+ end
141
+
142
+ context "loading the root path" do
143
+ setup do
144
+ @env.cwd = "/foo"
145
+ end
146
+
147
+ should "default the path to the cwd instance var if nil" do
148
+ @path = mock("path")
149
+ @path.stubs(:root?).returns(true)
150
+ File.expects(:expand_path).with(@env.cwd).returns(@env.cwd)
151
+ Pathname.expects(:new).with(@env.cwd).returns(@path)
152
+ @env.load_root_path!(nil)
153
+ end
154
+
155
+ should "not default the path to pwd if its not nil" do
156
+ @path = mock("path")
157
+ @path.stubs(:to_s).returns("/")
158
+ File.expects(:expand_path).with(@path).returns("/")
159
+ Pathname.expects(:new).with("/").returns(@path)
160
+ @path.stubs(:root?).returns(true)
161
+ @env.load_root_path!(@path)
162
+ end
163
+
164
+ should "should walk the parent directories looking for rootfile" do
165
+ paths = [
166
+ Pathname.new("/foo/bar/baz"),
167
+ Pathname.new("/foo/bar"),
168
+ Pathname.new("/foo")
169
+ ]
170
+
171
+ search_seq = sequence("search_seq")
172
+ paths.each do |path|
173
+ # NOTE File.expect(:expand_path) causes tests to hang in windows below is the interim solution
174
+ File.expects(:exist?).with("#{File.expand_path(path)}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(false).in_sequence(search_seq)
175
+ end
176
+
177
+ assert !@env.load_root_path!(paths.first)
178
+ end
179
+
180
+ should "return false if not found" do
181
+ path = Pathname.new("/")
182
+ assert !@env.load_root_path!(path)
183
+ end
184
+
185
+ should "return false if not found on windows-style root" do
186
+ # TODO: Is there _any_ way to test this on unix machines? The
187
+ # expand path doesn't work [properly for the test] on unix machines.
188
+ if RUBY_PLATFORM.downcase.include?("mswin")
189
+ # Note the escaped back slash
190
+ path = Pathname.new("C:\\")
191
+ assert !@env.load_root_path!(path)
192
+ end
193
+ end
194
+
195
+ should "should set the path for the rootfile" do
196
+ # NOTE File.expect(:expand_path) causes tests to hang in windows below is the interim solution
197
+ path = File.expand_path("/foo")
198
+ File.expects(:exist?).with("#{path}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(true)
199
+
200
+ assert @env.load_root_path!(Pathname.new(path))
201
+ assert_equal path, @env.root_path
202
+ end
203
+ end
204
+
205
+ context "loading config" do
206
+ setup do
207
+ @root_path = "/foo"
208
+ @home_path = "/bar"
209
+ @env.stubs(:root_path).returns(@root_path)
210
+ @env.stubs(:home_path).returns(@home_path)
211
+
212
+ File.stubs(:exist?).returns(false)
213
+ Vagrant::Config.stubs(:execute!)
214
+ Vagrant::Config.stubs(:reset!)
215
+ end
216
+
217
+ should "reset the configuration object" do
218
+ Vagrant::Config.expects(:reset!).with(@env).once
219
+ @env.load_config!
220
+ end
221
+
222
+ should "load from the project root" do
223
+ File.expects(:exist?).with(File.join(PROJECT_ROOT, "config", "default.rb")).once
224
+ @env.load_config!
225
+ end
226
+
227
+ should "load from the root path" do
228
+ File.expects(:exist?).with(File.join(@root_path, Vagrant::Environment::ROOTFILE_NAME)).once
229
+ @env.load_config!
230
+ end
231
+
232
+ should "not load from the root path if nil" do
233
+ @env.stubs(:root_path).returns(nil)
234
+ File.expects(:exist?).with(File.join(@root_path, Vagrant::Environment::ROOTFILE_NAME)).never
235
+ @env.load_config!
236
+ end
237
+
238
+ should "load from the home directory" do
239
+ File.expects(:exist?).with(File.join(@env.home_path, Vagrant::Environment::ROOTFILE_NAME)).once
240
+ @env.load_config!
241
+ end
242
+
243
+ should "not load from the home directory if the config is nil" do
244
+ @env.stubs(:home_path).returns(nil)
245
+ File.expects(:exist?).twice.returns(false)
246
+ @env.load_config!
247
+ end
248
+
249
+ should "not load from the box directory if it is nil" do
250
+ @env.expects(:box).once.returns(nil)
251
+ File.expects(:exist?).twice.returns(false)
252
+ @env.load_config!
253
+ end
254
+
255
+ should "load from the box directory if it is not nil" do
256
+ dir = "foo"
257
+ box = mock("box")
258
+ box.stubs(:directory).returns(dir)
259
+ @env.expects(:box).twice.returns(box)
260
+ File.expects(:exist?).with(File.join(dir, Vagrant::Environment::ROOTFILE_NAME)).once
261
+ @env.load_config!
262
+ end
263
+
264
+ should "load the files only if exist? returns true" do
265
+ File.expects(:exist?).once.returns(true)
266
+ @env.expects(:load).once
267
+ @env.load_config!
268
+ end
269
+
270
+ should "not load the files if exist? returns false" do
271
+ @env.expects(:load).never
272
+ @env.load_config!
273
+ end
274
+
275
+ should "execute after loading and set result to environment config" do
276
+ result = mock("result")
277
+ File.expects(:exist?).once.returns(true)
278
+ @env.expects(:load).once
279
+ Vagrant::Config.expects(:execute!).once.returns(result)
280
+ @env.load_config!
281
+ assert_equal result, @env.config
282
+ end
283
+ end
284
+
285
+ context "loading home directory" do
286
+ setup do
287
+ @env = mock_environment
288
+ @home_dir = File.expand_path(@env.config.vagrant.home)
289
+
290
+ File.stubs(:directory?).returns(true)
291
+ FileUtils.stubs(:mkdir_p)
292
+ end
293
+
294
+ should "create each directory if it doesn't exist" do
295
+ create_seq = sequence("create_seq")
296
+ File.stubs(:directory?).returns(false)
297
+ Vagrant::Environment::HOME_SUBDIRS.each do |subdir|
298
+ FileUtils.expects(:mkdir_p).with(File.join(@home_dir, subdir)).in_sequence(create_seq)
299
+ end
300
+
301
+ @env.load_home_directory!
302
+ end
303
+
304
+ should "not create directories if they exist" do
305
+ File.stubs(:directory?).returns(true)
306
+ FileUtils.expects(:mkdir_p).never
307
+ @env.load_home_directory!
308
+ end
309
+ end
310
+
311
+ context "loading box" do
312
+ setup do
313
+ @box = mock("box")
314
+ @box.stubs(:env=)
315
+
316
+ @env = mock_environment
317
+ @env.stubs(:root_path).returns("foo")
318
+ end
319
+
320
+ should "do nothing if the root path is nil" do
321
+ Vagrant::Box.expects(:find).never
322
+ @env.stubs(:root_path).returns(nil)
323
+ @env.load_box!
324
+ end
325
+
326
+ should "not load the box if its not set" do
327
+ @env = mock_environment do |config|
328
+ config.vm.box = nil
329
+ end
330
+
331
+ Vagrant::Box.expects(:find).never
332
+ @env.load_box!
333
+ end
334
+
335
+ should "set the box to what is found by the Box class" do
336
+ Vagrant::Box.expects(:find).with(@env, @env.config.vm.box).once.returns(@box)
337
+ @env.load_box!
338
+ assert @box.equal?(@env.box)
339
+ end
340
+ end
341
+
342
+ context "loading the UUID out from the persisted dotfile" do
343
+ setup do
344
+ @env = mock_environment
345
+ @env.stubs(:root_path).returns("foo")
346
+
347
+ File.stubs(:file?).returns(true)
348
+ end
349
+
350
+ should "loading of the uuid from the dotfile" do
351
+ vm = mock("vm")
352
+ vm.expects(:env=).with(@env)
353
+
354
+ filemock = mock("filemock")
355
+ filemock.expects(:read).returns("foo")
356
+ Vagrant::VM.expects(:find).with("foo").returns(vm)
357
+ File.expects(:open).with(@env.dotfile_path).once.yields(filemock)
358
+ File.expects(:file?).with(@env.dotfile_path).once.returns(true)
359
+ @env.load_vm!
360
+
361
+ assert_equal vm, @env.vm
362
+ end
363
+
364
+ should "not set the environment if the VM is nil" do
365
+ filemock = mock("filemock")
366
+ filemock.expects(:read).returns("foo")
367
+ Vagrant::VM.expects(:find).with("foo").returns(nil)
368
+ File.expects(:open).with(@env.dotfile_path).once.yields(filemock)
369
+ File.expects(:file?).with(@env.dotfile_path).once.returns(true)
370
+
371
+ assert_nothing_raised { @env.load_vm! }
372
+ assert_nil @env.vm
373
+ end
374
+
375
+ should "do nothing if the root path is nil" do
376
+ File.expects(:open).never
377
+ @env.stubs(:root_path).returns(nil)
378
+ @env.load_vm!
379
+ end
380
+
381
+ should "do nothing if dotfile is not a file" do
382
+ File.expects(:file?).returns(false)
383
+ File.expects(:open).never
384
+ @env.load_vm!
385
+ end
386
+
387
+ should "uuid should be nil if dotfile didn't exist" do
388
+ File.expects(:open).raises(Errno::ENOENT)
389
+ @env.load_vm!
390
+ assert_nil @env.vm
391
+ end
392
+ end
393
+
394
+ context "loading SSH" do
395
+ setup do
396
+ @env = mock_environment
397
+ end
398
+
399
+ should "initialize the SSH object with the given environment" do
400
+ ssh = mock("ssh")
401
+ Vagrant::SSH.expects(:new).with(@env).returns(ssh)
402
+ @env.load_ssh!
403
+ assert_equal ssh, @env.ssh
404
+ end
405
+ end
406
+
407
+ context "loading the active list" do
408
+ setup do
409
+ @env = mock_environment
410
+ end
411
+
412
+ should "initialize the ActiveList object with the given environment" do
413
+ active_list = mock("active_list")
414
+ Vagrant::ActiveList.expects(:new).with(@env).returns(active_list)
415
+ @env.load_active_list!
416
+ assert_equal active_list, @env.active_list
417
+ end
418
+ end
419
+
420
+ context "loading the commands" do
421
+ setup do
422
+ @env = mock_environment
423
+ end
424
+
425
+ should "initialize the Commands object with the given environment" do
426
+ commands = mock("commands")
427
+ Vagrant::Command.expects(:new).with(@env).returns(commands)
428
+ @env.load_commands!
429
+ assert_equal commands, @env.commands
430
+ end
431
+ end
432
+ end
433
+
434
+ context "requiring properties" do
435
+ setup do
436
+ @env = mock_environment
437
+ end
438
+
439
+ context "requiring boxes" do
440
+ setup do
441
+ reconfig_environment
442
+ end
443
+
444
+ def reconfig_environment
445
+ @env = mock_environment do |config|
446
+ yield config if block_given?
447
+ end
448
+
449
+ @env.stubs(:require_root_path)
450
+ @env.stubs(:error_and_exit)
451
+ end
452
+
453
+ should "require root path" do
454
+ @env.expects(:require_root_path).once
455
+ @env.require_box
456
+ end
457
+
458
+ should "error and exit if no box is specified" do
459
+ reconfig_environment do |config|
460
+ config.vm.box = nil
461
+ end
462
+
463
+ @env.expects(:box).returns(nil)
464
+ @env.expects(:error_and_exit).once.with(:box_not_specified)
465
+ @env.require_box
466
+ end
467
+
468
+ should "error and exit if box is specified but doesn't exist" do
469
+ reconfig_environment do |config|
470
+ config.vm.box = "foo"
471
+ end
472
+
473
+ @env.expects(:box).returns(nil)
474
+ @env.expects(:error_and_exit).once.with(:box_specified_doesnt_exist, :box_name => "foo")
475
+ @env.require_box
476
+ end
477
+ end
478
+
479
+ context "requiring root_path" do
480
+ should "error and exit if no root_path is set" do
481
+ @env.expects(:root_path).returns(nil)
482
+ @env.expects(:error_and_exit).with(:rootfile_not_found).once
483
+ @env.require_root_path
484
+ end
485
+
486
+ should "not error and exit if root_path is set" do
487
+ @env.expects(:root_path).returns("foo")
488
+ @env.expects(:error_and_exit).never
489
+ @env.require_root_path
490
+ end
491
+ end
492
+
493
+ context "requiring a persisted VM" do
494
+ setup do
495
+ @env.stubs(:vm).returns("foo")
496
+ @env.stubs(:require_root_path)
497
+ end
498
+
499
+ should "require a root path" do
500
+ @env.expects(:require_root_path).once
501
+ @env.expects(:error_and_exit).never
502
+ @env.require_persisted_vm
503
+ end
504
+
505
+ should "error and exit if the VM is not set" do
506
+ @env.expects(:vm).returns(nil)
507
+ @env.expects(:error_and_exit).once
508
+ @env.require_persisted_vm
509
+ end
510
+ end
511
+ end
512
+
513
+ context "managing VM" do
514
+ setup do
515
+ @env = mock_environment
516
+
517
+ @dotfile_path = "foo"
518
+ @env.stubs(:dotfile_path).returns(@dotfile_path)
519
+ end
520
+
521
+ def mock_vm
522
+ @vm = mock("vm")
523
+ @vm.stubs(:uuid).returns("foo")
524
+ @env.stubs(:vm).returns(@vm)
525
+ end
526
+
527
+ context "creating a new VM" do
528
+ should "create a new VM" do
529
+ assert_nil @env.vm
530
+ @env.create_vm
531
+ assert !@env.vm.nil?
532
+ assert @env.vm.is_a?(Vagrant::VM)
533
+ end
534
+
535
+ should "set the new VM's environment to the env" do
536
+ @env.create_vm
537
+ assert_equal @env, @env.vm.env
538
+ end
539
+
540
+ should "return the new VM" do
541
+ result = @env.create_vm
542
+ assert result.is_a?(Vagrant::VM)
543
+ end
544
+ end
545
+
546
+ context "persisting the VM into a file" do
547
+ setup do
548
+ mock_vm
549
+
550
+ File.stubs(:open)
551
+ @env.active_list.stubs(:add)
552
+ end
553
+
554
+ should "should save it to the dotfile path" do
555
+ filemock = mock("filemock")
556
+ filemock.expects(:write).with(@vm.uuid)
557
+ File.expects(:open).with(@env.dotfile_path, 'w+').once.yields(filemock)
558
+ @env.persist_vm
559
+ end
560
+
561
+ should "add the VM to the activelist" do
562
+ @env.active_list.expects(:add).with(@vm)
563
+ @env.persist_vm
564
+ end
565
+ end
566
+
567
+ context "depersisting the VM" do
568
+ setup do
569
+ mock_vm
570
+
571
+ File.stubs(:exist?).returns(false)
572
+ File.stubs(:delete)
573
+
574
+ @env.active_list.stubs(:remove)
575
+ end
576
+
577
+ should "remove the dotfile if it exists" do
578
+ File.expects(:exist?).with(@env.dotfile_path).returns(true)
579
+ File.expects(:delete).with(@env.dotfile_path).once
580
+ @env.depersist_vm
581
+ end
582
+
583
+ should "not remove the dotfile if it doesn't exist" do
584
+ File.expects(:exist?).returns(false)
585
+ File.expects(:delete).never
586
+ @env.depersist_vm
587
+ end
588
+
589
+ should "remove from the active list" do
590
+ @env.active_list.expects(:remove).with(@vm)
591
+ @env.depersist_vm
592
+ end
593
+ end
594
+ end
595
+ end