vagrant 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. data/Gemfile +2 -2
  2. data/README.md +2 -2
  3. data/Rakefile +1 -1
  4. data/VERSION +1 -1
  5. data/config/default.rb +13 -3
  6. data/lib/vagrant.rb +10 -13
  7. data/lib/vagrant/actions/base.rb +14 -2
  8. data/lib/vagrant/actions/box/download.rb +2 -7
  9. data/lib/vagrant/actions/box/verify.rb +1 -1
  10. data/lib/vagrant/actions/runner.rb +0 -1
  11. data/lib/vagrant/actions/vm/boot.rb +2 -6
  12. data/lib/vagrant/actions/vm/customize.rb +7 -5
  13. data/lib/vagrant/actions/vm/destroy.rb +4 -3
  14. data/lib/vagrant/actions/vm/down.rb +6 -3
  15. data/lib/vagrant/actions/vm/export.rb +2 -4
  16. data/lib/vagrant/actions/vm/forward_ports.rb +77 -16
  17. data/lib/vagrant/actions/vm/halt.rb +10 -2
  18. data/lib/vagrant/actions/vm/import.rb +2 -4
  19. data/lib/vagrant/actions/vm/move_hard_drive.rb +2 -2
  20. data/lib/vagrant/actions/vm/network.rb +120 -0
  21. data/lib/vagrant/actions/vm/package.rb +11 -7
  22. data/lib/vagrant/actions/vm/provision.rb +3 -3
  23. data/lib/vagrant/actions/vm/reload.rb +2 -9
  24. data/lib/vagrant/actions/vm/shared_folders.rb +19 -39
  25. data/lib/vagrant/actions/vm/start.rb +10 -2
  26. data/lib/vagrant/actions/vm/up.rb +5 -6
  27. data/lib/vagrant/active_list.rb +23 -13
  28. data/lib/vagrant/box.rb +2 -2
  29. data/lib/vagrant/busy.rb +3 -3
  30. data/lib/vagrant/command.rb +2 -2
  31. data/lib/vagrant/commands/base.rb +40 -20
  32. data/lib/vagrant/commands/destroy.rb +17 -3
  33. data/lib/vagrant/commands/halt.rb +23 -3
  34. data/lib/vagrant/commands/package.rb +54 -14
  35. data/lib/vagrant/commands/provision.rb +31 -0
  36. data/lib/vagrant/commands/reload.rb +16 -3
  37. data/lib/vagrant/commands/resume.rb +16 -3
  38. data/lib/vagrant/commands/ssh.rb +25 -3
  39. data/lib/vagrant/commands/ssh_config.rb +20 -5
  40. data/lib/vagrant/commands/status.rb +107 -40
  41. data/lib/vagrant/commands/suspend.rb +16 -3
  42. data/lib/vagrant/commands/up.rb +26 -7
  43. data/lib/vagrant/config.rb +82 -12
  44. data/lib/vagrant/downloaders/base.rb +8 -1
  45. data/lib/vagrant/downloaders/http.rb +31 -19
  46. data/lib/vagrant/environment.rb +146 -49
  47. data/lib/vagrant/provisioners/base.rb +19 -5
  48. data/lib/vagrant/provisioners/chef.rb +12 -4
  49. data/lib/vagrant/provisioners/chef_server.rb +13 -6
  50. data/lib/vagrant/provisioners/chef_solo.rb +7 -3
  51. data/lib/vagrant/resource_logger.rb +126 -0
  52. data/lib/vagrant/ssh.rb +109 -8
  53. data/lib/vagrant/systems/base.rb +70 -0
  54. data/lib/vagrant/systems/linux.rb +137 -0
  55. data/lib/vagrant/util.rb +1 -45
  56. data/lib/vagrant/util/error_helper.rb +13 -0
  57. data/lib/vagrant/util/glob_loader.rb +22 -0
  58. data/lib/vagrant/util/output_helper.rb +9 -0
  59. data/lib/vagrant/util/plain_logger.rb +12 -0
  60. data/lib/vagrant/util/platform.rb +7 -2
  61. data/lib/vagrant/util/template_renderer.rb +2 -2
  62. data/lib/vagrant/util/translator.rb +35 -0
  63. data/lib/vagrant/vm.rb +91 -10
  64. data/templates/crontab_entry.erb +1 -0
  65. data/templates/network_entry.erb +8 -0
  66. data/templates/ssh_config.erb +1 -0
  67. data/templates/{errors.yml → strings.yml} +111 -3
  68. data/templates/sync.erb +14 -0
  69. data/test/test_helper.rb +46 -3
  70. data/test/vagrant/actions/box/download_test.rb +0 -17
  71. data/test/vagrant/actions/vm/boot_test.rb +3 -10
  72. data/test/vagrant/actions/vm/customize_test.rb +6 -0
  73. data/test/vagrant/actions/vm/destroy_test.rb +6 -5
  74. data/test/vagrant/actions/vm/down_test.rb +5 -11
  75. data/test/vagrant/actions/vm/export_test.rb +1 -0
  76. data/test/vagrant/actions/vm/forward_ports_test.rb +92 -15
  77. data/test/vagrant/actions/vm/halt_test.rb +36 -4
  78. data/test/vagrant/actions/vm/import_test.rb +2 -0
  79. data/test/vagrant/actions/vm/network_test.rb +237 -0
  80. data/test/vagrant/actions/vm/package_test.rb +35 -5
  81. data/test/vagrant/actions/vm/provision_test.rb +3 -3
  82. data/test/vagrant/actions/vm/reload_test.rb +1 -1
  83. data/test/vagrant/actions/vm/shared_folders_test.rb +41 -74
  84. data/test/vagrant/actions/vm/start_test.rb +41 -3
  85. data/test/vagrant/actions/vm/up_test.rb +10 -21
  86. data/test/vagrant/active_list_test.rb +28 -43
  87. data/test/vagrant/commands/base_test.rb +25 -4
  88. data/test/vagrant/commands/destroy_test.rb +24 -12
  89. data/test/vagrant/commands/halt_test.rb +33 -11
  90. data/test/vagrant/commands/package_test.rb +77 -57
  91. data/test/vagrant/commands/provision_test.rb +50 -0
  92. data/test/vagrant/commands/reload_test.rb +27 -11
  93. data/test/vagrant/commands/resume_test.rb +25 -14
  94. data/test/vagrant/commands/ssh_config_test.rb +40 -17
  95. data/test/vagrant/commands/ssh_test.rb +52 -13
  96. data/test/vagrant/commands/status_test.rb +21 -1
  97. data/test/vagrant/commands/suspend_test.rb +25 -14
  98. data/test/vagrant/commands/up_test.rb +25 -19
  99. data/test/vagrant/config_test.rb +74 -18
  100. data/test/vagrant/downloaders/base_test.rb +2 -1
  101. data/test/vagrant/downloaders/http_test.rb +18 -8
  102. data/test/vagrant/environment_test.rb +245 -77
  103. data/test/vagrant/provisioners/base_test.rb +4 -4
  104. data/test/vagrant/provisioners/chef_server_test.rb +18 -7
  105. data/test/vagrant/provisioners/chef_solo_test.rb +17 -7
  106. data/test/vagrant/provisioners/chef_test.rb +22 -9
  107. data/test/vagrant/resource_logger_test.rb +144 -0
  108. data/test/vagrant/ssh_session_test.rb +46 -0
  109. data/test/vagrant/ssh_test.rb +42 -2
  110. data/test/vagrant/systems/linux_test.rb +174 -0
  111. data/test/vagrant/util/error_helper_test.rb +5 -0
  112. data/test/vagrant/util/output_helper_test.rb +5 -0
  113. data/test/vagrant/util/plain_logger_test.rb +17 -0
  114. data/test/vagrant/util/platform_test.rb +18 -0
  115. data/test/vagrant/util/{errors_test.rb → translator_test.rb} +25 -21
  116. data/test/vagrant/util_test.rb +12 -49
  117. data/test/vagrant/vm_test.rb +133 -11
  118. data/vagrant.gemspec +39 -15
  119. metadata +64 -40
  120. data/lib/vagrant/commands/down.rb +0 -16
  121. data/lib/vagrant/util/errors.rb +0 -36
  122. data/lib/vagrant/util/progress_meter.rb +0 -33
  123. data/test/vagrant/commands/down_test.rb +0 -17
  124. data/test/vagrant/util/progress_meter_test.rb +0 -33
@@ -52,17 +52,6 @@ class ConfigTest < Test::Unit::TestCase
52
52
  end
53
53
  end
54
54
 
55
- context "accessing configuration" do
56
- setup do
57
- Vagrant::Config.run { |config| }
58
- Vagrant::Config.execute!
59
- end
60
-
61
- should "forward config to the class method" do
62
- assert_equal Vagrant.config, Vagrant::Config.config
63
- end
64
- end
65
-
66
55
  context "initializing" do
67
56
  setup do
68
57
  Vagrant::Config.reset!
@@ -92,7 +81,14 @@ class ConfigTest < Test::Unit::TestCase
92
81
  should "return the configuration on execute!" do
93
82
  Vagrant::Config.run {}
94
83
  result = Vagrant::Config.execute!
95
- assert result.equal?(Vagrant.config)
84
+ assert result.is_a?(Vagrant::Config::Top)
85
+ end
86
+
87
+ should "use given configuration object if given" do
88
+ fake_env = mock("env")
89
+ config = Vagrant::Config::Top.new(fake_env)
90
+ result = Vagrant::Config.execute!(config)
91
+ assert_equal config.env, result.env
96
92
  end
97
93
  end
98
94
 
@@ -229,14 +225,74 @@ class ConfigTest < Test::Unit::TestCase
229
225
  @env.config.ssh.username = @username
230
226
  end
231
227
 
232
- should "include the stacked proc runner module" do
233
- assert @config.class.included_modules.include?(Vagrant::Util::StackedProcRunner)
228
+ context "defining VMs" do
229
+ should "store the proc by name but not run it" do
230
+ foo = mock("proc")
231
+ foo.expects(:call).never
232
+
233
+ proc = Proc.new { foo.call }
234
+ @config.define(:name, &proc)
235
+ assert @config.defined_vms[:name].proc_stack.include?(proc)
236
+ end
237
+
238
+ should "store the options" do
239
+ @config.define(:name, :set => true)
240
+ assert @config.defined_vms[:name].options[:set]
241
+ end
242
+
243
+ should "not have multi-VMs by default" do
244
+ assert !@config.has_multi_vms?
245
+ end
246
+
247
+ should "have multi-VMs once one is specified" do
248
+ @config.define(:foo) {}
249
+ assert @config.has_multi_vms?
250
+ end
251
+ end
252
+
253
+ context "customizing" do
254
+ should "include the stacked proc runner module" do
255
+ assert @config.class.included_modules.include?(Vagrant::Util::StackedProcRunner)
256
+ end
257
+
258
+ should "add the customize proc to the proc stack" do
259
+ proc = Proc.new {}
260
+ @config.customize(&proc)
261
+ assert_equal [proc], @config.proc_stack
262
+ end
234
263
  end
235
264
 
236
- should "add the customize proc to the proc stack" do
237
- proc = Proc.new {}
238
- @config.customize(&proc)
239
- assert_equal [proc], @config.proc_stack
265
+ context "syncd folders" do
266
+ should "set the syncpath to nil by default" do
267
+ share_with_opts
268
+ assert !@config.shared_folders['foo'][:syncpath]
269
+ end
270
+
271
+ should "append sync to directory name when boolean" do
272
+ share_with_opts(:sync => true)
273
+ assert_equal @config.shared_folders['foo'][:syncpath], 'foo-dir'
274
+ assert_equal @config.shared_folders['foo'][:guestpath], 'foo-dir-sync'
275
+ end
276
+
277
+ should "use the specified sync directory" do
278
+ share_with_opts(:sync => 'bar-baz')
279
+ assert_equal @config.shared_folders['foo'][:syncpath], 'bar-baz'
280
+ end
281
+
282
+ should "raise an exception an exception if the guestpath and syncpath are the same" do
283
+ assert_raise Exception do
284
+ share_with_opts(:sync => 'foo-dir-sync')
285
+ end
286
+ end
287
+
288
+ should "set the sync required flag to true" do
289
+ share_with_opts(:sync => true)
290
+ assert @config.sync_required
291
+ end
292
+
293
+ def share_with_opts(opts={})
294
+ @config.share_folder('foo', 'foo-dir', '', opts)
295
+ end
240
296
  end
241
297
 
242
298
  context "uid/gid" do
@@ -7,7 +7,8 @@ class BaseDownloaderTest < Test::Unit::TestCase
7
7
 
8
8
  context "base instance" do
9
9
  setup do
10
- @base = Vagrant::Downloaders::Base.new
10
+ @env = mock_environment
11
+ @base = Vagrant::Downloaders::Base.new(@env)
11
12
  end
12
13
 
13
14
  should "implement prepare which does nothing" do
@@ -5,28 +5,38 @@ class HttpDownloaderTest < Test::Unit::TestCase
5
5
  @downloader, @tempfile = mock_downloader(Vagrant::Downloaders::HTTP)
6
6
  @downloader.stubs(:report_progress)
7
7
  @downloader.stubs(:complete_progress)
8
- @uri = "foo.box"
8
+ @uri = "http://google.com/"
9
9
  end
10
10
 
11
11
  context "downloading" do
12
12
  setup do
13
- @parsed_uri = mock("parsed")
14
- URI.stubs(:parse).with(@uri).returns(@parsed_uri)
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)
15
17
  end
16
18
 
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
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
20
28
  @downloader.download!(@uri, @tempfile)
21
29
  end
22
30
 
23
31
  should "read the body of the response and place each segment into the file" do
32
+ h = mock("http")
24
33
  response = mock("response")
25
34
  response.stubs(:content_length)
26
35
  segment = mock("segment")
27
36
  segment.stubs(:length).returns(7)
28
37
 
29
- Net::HTTP.stubs(:get_response).yields(response)
38
+ @http.stubs(:start).yields(h)
39
+ h.expects(:request_get).with(@parsed_uri.request_uri).once.yields(response)
30
40
  response.expects(:read_body).once.yields(segment)
31
41
  @tempfile.expects(:write).with(segment).once
32
42
 
@@ -43,7 +53,7 @@ class HttpDownloaderTest < Test::Unit::TestCase
43
53
  should "return false if there are no extract results" do
44
54
  URI.expects(:extract).returns([])
45
55
  assert !Vagrant::Downloaders::HTTP.match?('foo')
46
- end
56
+ end
47
57
  end
48
58
 
49
59
  context "reporting progress" do
@@ -42,13 +42,13 @@ class EnvironmentTest < Test::Unit::TestCase
42
42
  end
43
43
 
44
44
  should "create the environment with given cwd, load it, and return it" do
45
- Vagrant::Environment.expects(:new).with(@cwd).once.returns(@env)
45
+ Vagrant::Environment.expects(:new).with(:cwd => @cwd).once.returns(@env)
46
46
  @env.expects(:load!).returns(@env)
47
47
  assert_equal @env, Vagrant::Environment.load!(@cwd)
48
48
  end
49
49
 
50
50
  should "work without a given cwd" do
51
- Vagrant::Environment.expects(:new).with(nil).returns(@env)
51
+ Vagrant::Environment.expects(:new).with(:cwd => nil).returns(@env)
52
52
 
53
53
  assert_nothing_raised {
54
54
  env = Vagrant::Environment.load!
@@ -60,7 +60,7 @@ class EnvironmentTest < Test::Unit::TestCase
60
60
  context "initialization" do
61
61
  should "set the cwd if given" do
62
62
  cwd = "foobarbaz"
63
- env = Vagrant::Environment.new(cwd)
63
+ env = Vagrant::Environment.new(:cwd => cwd)
64
64
  assert_equal cwd, env.cwd
65
65
  end
66
66
 
@@ -124,6 +124,90 @@ class EnvironmentTest < Test::Unit::TestCase
124
124
  end
125
125
  end
126
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
+
127
211
  context "loading" do
128
212
  setup do
129
213
  @env = mock_environment
@@ -132,6 +216,7 @@ class EnvironmentTest < Test::Unit::TestCase
132
216
  context "overall load method" do
133
217
  should "load! should call proper sequence and return itself" do
134
218
  call_seq = sequence("call_sequence")
219
+ @env.expects(:load_logger!).once.in_sequence(call_seq)
135
220
  @env.expects(:load_root_path!).once.in_sequence(call_seq)
136
221
  @env.expects(:load_config!).once.in_sequence(call_seq)
137
222
  @env.expects(:load_home_directory!).once.in_sequence(call_seq)
@@ -139,7 +224,6 @@ class EnvironmentTest < Test::Unit::TestCase
139
224
  @env.expects(:load_config!).once.in_sequence(call_seq)
140
225
  Vagrant::Environment.expects(:check_virtualbox!).once.in_sequence(call_seq)
141
226
  @env.expects(:load_vm!).once.in_sequence(call_seq)
142
- @env.expects(:load_ssh!).once.in_sequence(call_seq)
143
227
  @env.expects(:load_active_list!).once.in_sequence(call_seq)
144
228
  @env.expects(:load_commands!).once.in_sequence(call_seq)
145
229
  assert_equal @env, @env.load!
@@ -215,10 +299,11 @@ class EnvironmentTest < Test::Unit::TestCase
215
299
  @home_path = "/bar"
216
300
  @env.stubs(:root_path).returns(@root_path)
217
301
  @env.stubs(:home_path).returns(@home_path)
302
+ @env.stubs(:load_logger!)
303
+
304
+ @parent_env = mock_environment
218
305
 
219
306
  File.stubs(:exist?).returns(false)
220
- Vagrant::Config.stubs(:execute!)
221
- Vagrant::Config.stubs(:reset!)
222
307
  end
223
308
 
224
309
  should "reset the configuration object" do
@@ -268,6 +353,25 @@ class EnvironmentTest < Test::Unit::TestCase
268
353
  @env.load_config!
269
354
  end
270
355
 
356
+ should "load a sub-VM configuration if specified" do
357
+ vm_name = :foo
358
+ sub_box = :YO
359
+ @parent_env.config.vm.box = :NO
360
+ @parent_env.config.vm.define(vm_name) do |config|
361
+ config.vm.box = sub_box
362
+ end
363
+
364
+ # Sanity
365
+ assert_equal :NO, @parent_env.config.vm.box
366
+
367
+ @env.stubs(:vm_name).returns(vm_name)
368
+ @env.stubs(:parent).returns(@parent_env)
369
+
370
+ @env.load_config!
371
+
372
+ assert_equal sub_box, @env.config.vm.box
373
+ end
374
+
271
375
  should "load the files only if exist? returns true" do
272
376
  File.expects(:exist?).once.returns(true)
273
377
  @env.expects(:load).once
@@ -287,6 +391,33 @@ class EnvironmentTest < Test::Unit::TestCase
287
391
  @env.load_config!
288
392
  assert_equal result, @env.config
289
393
  end
394
+
395
+ should "reload the logger after executing" do
396
+ load_seq = sequence("load_seq")
397
+ Vagrant::Config.expects(:execute!).once.returns(nil).in_sequence(load_seq)
398
+ @env.expects(:load_logger!).once.in_sequence(load_seq)
399
+ @env.load_config!
400
+ end
401
+ end
402
+
403
+ context "loading logger" do
404
+ setup do
405
+ @env = mock_environment
406
+ @env.stubs(:vm_name).returns(nil)
407
+ end
408
+
409
+ should "use 'vagrant' by default" do
410
+ assert @env.vm_name.nil? # sanity
411
+ @env.load_logger!
412
+ assert_equal "vagrant", @env.logger.resource
413
+ end
414
+
415
+ should "use the vm name if available" do
416
+ name = "foo"
417
+ @env.stubs(:vm_name).returns(name)
418
+ @env.load_logger!
419
+ assert_equal name, @env.logger.resource
420
+ end
290
421
  end
291
422
 
292
423
  context "loading home directory" do
@@ -354,37 +485,64 @@ class EnvironmentTest < Test::Unit::TestCase
354
485
  File.stubs(:file?).returns(true)
355
486
  end
356
487
 
357
- should "loading of the uuid from the dotfile" do
488
+ should "blank the VMs" do
489
+ load_seq = sequence("load_seq")
490
+ @env.stubs(:root_path).returns("foo")
491
+ @env.expects(:load_blank_vms!).in_sequence(load_seq)
492
+ File.expects(:open).in_sequence(load_seq)
493
+ @env.load_vm!
494
+ end
495
+
496
+ should "load the UUID if the JSON parsing fails" do
358
497
  vm = mock("vm")
359
- vm.expects(:env=).with(@env)
360
498
 
361
499
  filemock = mock("filemock")
362
500
  filemock.expects(:read).returns("foo")
363
- Vagrant::VM.expects(:find).with("foo").returns(vm)
501
+ Vagrant::VM.expects(:find).with("foo", @env, Vagrant::Environment::DEFAULT_VM).returns(vm)
364
502
  File.expects(:open).with(@env.dotfile_path).once.yields(filemock)
365
503
  File.expects(:file?).with(@env.dotfile_path).once.returns(true)
366
504
  @env.load_vm!
367
505
 
368
- assert_equal vm, @env.vm
506
+ assert_equal vm, @env.vms.values.first
369
507
  end
370
508
 
371
- should "not set the environment if the VM is nil" do
509
+ should "load all the VMs from the dotfile" do
510
+ vms = { :foo => "bar", :bar => "baz" }
511
+ results = {}
512
+
372
513
  filemock = mock("filemock")
373
- filemock.expects(:read).returns("foo")
374
- Vagrant::VM.expects(:find).with("foo").returns(nil)
514
+ filemock.expects(:read).returns(vms.to_json)
375
515
  File.expects(:open).with(@env.dotfile_path).once.yields(filemock)
376
516
  File.expects(:file?).with(@env.dotfile_path).once.returns(true)
377
517
 
378
- assert_nothing_raised { @env.load_vm! }
379
- assert_nil @env.vm
518
+ vms.each do |key, value|
519
+ vm = mock("vm#{key}")
520
+ Vagrant::VM.expects(:find).with(value, @env, key.to_sym).returns(vm)
521
+ results[key] = vm
522
+ end
523
+
524
+ @env.load_vm!
525
+
526
+ results.each do |key, value|
527
+ assert_equal value, @env.vms[key]
528
+ end
380
529
  end
381
530
 
382
- should "do nothing if the root path is nil" do
531
+ should "do nothing if the vm_name is set" do
532
+ @env.stubs(:vm_name).returns(:foo)
383
533
  File.expects(:open).never
384
- @env.stubs(:root_path).returns(nil)
385
534
  @env.load_vm!
386
535
  end
387
536
 
537
+ should "do nothing if the dotfile is nil" do
538
+ @env.stubs(:dotfile_path).returns(nil)
539
+ File.expects(:open).never
540
+
541
+ assert_nothing_raised {
542
+ @env.load_vm!
543
+ }
544
+ end
545
+
388
546
  should "do nothing if dotfile is not a file" do
389
547
  File.expects(:file?).returns(false)
390
548
  File.expects(:open).never
@@ -398,16 +556,36 @@ class EnvironmentTest < Test::Unit::TestCase
398
556
  end
399
557
  end
400
558
 
401
- context "loading SSH" do
559
+ context "loading blank VMs" do
402
560
  setup do
403
561
  @env = mock_environment
404
562
  end
405
563
 
406
- should "initialize the SSH object with the given environment" do
407
- ssh = mock("ssh")
408
- Vagrant::SSH.expects(:new).with(@env).returns(ssh)
409
- @env.load_ssh!
410
- assert_equal ssh, @env.ssh
564
+ should "blank the VMs" do
565
+ @env = mock_environment do |config|
566
+ config.vm.define :foo do |config|
567
+ end
568
+
569
+ config.vm.define :bar do |config|
570
+ end
571
+ end
572
+
573
+ @env.load_blank_vms!
574
+
575
+ assert_equal 2, @env.vms.length
576
+ assert(@env.vms.all? { |name, vm| !vm.created? })
577
+
578
+ sorted_vms = @env.vms.keys.sort { |a,b| a.to_s <=> b.to_s }
579
+ assert_equal [:bar, :foo], sorted_vms
580
+ end
581
+
582
+ should "load the default VM blank if no multi-VMs are specified" do
583
+ assert @env.config.vm.defined_vms.empty? # sanity
584
+
585
+ @env.load_blank_vms!
586
+
587
+ assert_equal 1, @env.vms.length
588
+ assert !@env.vms.values.first.created?
411
589
  end
412
590
  end
413
591
 
@@ -530,73 +708,63 @@ class EnvironmentTest < Test::Unit::TestCase
530
708
  @vm.stubs(:uuid).returns("foo")
531
709
  @env.stubs(:vm).returns(@vm)
532
710
  end
711
+ end
533
712
 
534
- context "creating a new VM" do
535
- should "create a new VM" do
536
- assert_nil @env.vm
537
- @env.create_vm
538
- assert !@env.vm.nil?
539
- assert @env.vm.is_a?(Vagrant::VM)
540
- end
541
-
542
- should "set the new VM's environment to the env" do
543
- @env.create_vm
544
- assert_equal @env, @env.vm.env
545
- end
546
-
547
- should "return the new VM" do
548
- result = @env.create_vm
549
- assert result.is_a?(Vagrant::VM)
550
- end
713
+ context "updating the dotfile" do
714
+ setup do
715
+ @env = mock_environment
716
+ @env.stubs(:parent).returns(nil)
717
+ @env.stubs(:dotfile_path).returns("foo")
718
+ File.stubs(:open)
551
719
  end
552
720
 
553
- context "persisting the VM into a file" do
554
- setup do
555
- mock_vm
556
-
557
- File.stubs(:open)
558
- @env.active_list.stubs(:add)
559
- end
721
+ def create_vm(created)
722
+ vm = mock("vm")
723
+ vm.stubs(:created?).returns(created)
724
+ vm.stubs(:uuid).returns("foo")
725
+ vm
726
+ end
560
727
 
561
- should "should save it to the dotfile path" do
562
- filemock = mock("filemock")
563
- filemock.expects(:write).with(@vm.uuid)
564
- File.expects(:open).with(@env.dotfile_path, 'w+').once.yields(filemock)
565
- @env.persist_vm
566
- end
728
+ should "call parent if exists" do
729
+ parent = mock("parent")
730
+ @env.stubs(:parent).returns(parent)
731
+ parent.expects(:update_dotfile).once
567
732
 
568
- should "add the VM to the activelist" do
569
- @env.active_list.expects(:add).with(@vm)
570
- @env.persist_vm
571
- end
733
+ @env.update_dotfile
572
734
  end
573
735
 
574
- context "depersisting the VM" do
575
- setup do
576
- mock_vm
736
+ should "remove the dotfile if the data is empty" do
737
+ vms = {
738
+ :foo => create_vm(false)
739
+ }
577
740
 
578
- File.stubs(:exist?).returns(false)
579
- File.stubs(:delete)
741
+ @env.stubs(:vms).returns(vms)
742
+ File.expects(:delete).with(@env.dotfile_path).once
743
+ @env.update_dotfile
744
+ end
580
745
 
581
- @env.active_list.stubs(:remove)
582
- end
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
+ }
583
752
 
584
- should "remove the dotfile if it exists" do
585
- File.expects(:exist?).with(@env.dotfile_path).returns(true)
586
- File.expects(:delete).with(@env.dotfile_path).once
587
- @env.depersist_vm
588
- end
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
+ }
589
763
 
590
- should "not remove the dotfile if it doesn't exist" do
591
- File.expects(:exist?).returns(false)
592
- File.expects(:delete).never
593
- @env.depersist_vm
764
+ true
594
765
  end
595
766
 
596
- should "remove from the active list" do
597
- @env.active_list.expects(:remove).with(@vm)
598
- @env.depersist_vm
599
- end
767
+ @env.update_dotfile
600
768
  end
601
769
  end
602
770
  end