bmabey-vagrant 0.2.0

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