vagrant 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (234) hide show
  1. data/.gitignore +1 -0
  2. data/.yardopts +1 -0
  3. data/CHANGELOG.md +56 -0
  4. data/Gemfile +14 -3
  5. data/Gemfile.lock +38 -11
  6. data/README.md +1 -1
  7. data/bin/vagrant +20 -5
  8. data/config/default.rb +6 -8
  9. data/lib/vagrant.rb +40 -13
  10. data/lib/vagrant/action.rb +56 -16
  11. data/lib/vagrant/action/box/destroy.rb +3 -1
  12. data/lib/vagrant/action/box/download.rb +8 -13
  13. data/lib/vagrant/action/box/unpackage.rb +8 -11
  14. data/lib/vagrant/action/box/verify.rb +3 -3
  15. data/lib/vagrant/action/builder.rb +3 -30
  16. data/lib/vagrant/action/builtin.rb +6 -1
  17. data/lib/vagrant/action/environment.rb +14 -62
  18. data/lib/vagrant/action/general/package.rb +29 -22
  19. data/lib/vagrant/action/vm/boot.rb +5 -12
  20. data/lib/vagrant/action/vm/check_box.rb +4 -4
  21. data/lib/vagrant/action/vm/check_guest_additions.rb +4 -6
  22. data/lib/vagrant/action/vm/clear_forwarded_ports.rb +2 -2
  23. data/lib/vagrant/action/vm/clear_nfs_exports.rb +1 -1
  24. data/lib/vagrant/action/vm/clear_shared_folders.rb +1 -1
  25. data/lib/vagrant/action/vm/customize.rb +1 -1
  26. data/lib/vagrant/action/vm/destroy.rb +1 -2
  27. data/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb +1 -1
  28. data/lib/vagrant/action/vm/disable_networks.rb +11 -9
  29. data/lib/vagrant/action/vm/discard_state.rb +2 -2
  30. data/lib/vagrant/action/vm/export.rb +10 -11
  31. data/lib/vagrant/action/vm/forward_ports.rb +21 -9
  32. data/lib/vagrant/action/vm/halt.rb +3 -8
  33. data/lib/vagrant/action/vm/import.rb +16 -14
  34. data/lib/vagrant/action/vm/match_mac_address.rb +1 -1
  35. data/lib/vagrant/action/vm/network.rb +9 -16
  36. data/lib/vagrant/action/vm/nfs.rb +14 -18
  37. data/lib/vagrant/action/vm/provision.rb +5 -5
  38. data/lib/vagrant/action/vm/resume.rb +1 -1
  39. data/lib/vagrant/action/vm/share_folders.rb +6 -44
  40. data/lib/vagrant/action/vm/suspend.rb +1 -1
  41. data/lib/vagrant/action/warden.rb +74 -0
  42. data/lib/vagrant/box.rb +18 -82
  43. data/lib/vagrant/box_collection.rb +47 -0
  44. data/lib/vagrant/cli.rb +55 -0
  45. data/lib/vagrant/command/base.rb +106 -0
  46. data/lib/vagrant/command/box.rb +33 -0
  47. data/lib/vagrant/command/destroy.rb +17 -0
  48. data/lib/vagrant/command/group_base.rb +99 -0
  49. data/lib/vagrant/command/halt.rb +18 -0
  50. data/lib/vagrant/command/helpers.rb +33 -0
  51. data/lib/vagrant/command/init.rb +14 -0
  52. data/lib/vagrant/command/named_base.rb +14 -0
  53. data/lib/vagrant/command/package.rb +41 -0
  54. data/lib/vagrant/command/provision.rb +17 -0
  55. data/lib/vagrant/command/reload.rb +17 -0
  56. data/lib/vagrant/command/resume.rb +17 -0
  57. data/lib/vagrant/command/ssh.rb +41 -0
  58. data/lib/vagrant/command/ssh_config.rb +21 -0
  59. data/lib/vagrant/command/status.rb +23 -0
  60. data/lib/vagrant/command/suspend.rb +17 -0
  61. data/lib/vagrant/command/up.rb +20 -0
  62. data/lib/vagrant/command/upgrade_to_060.rb +45 -0
  63. data/lib/vagrant/command/version.rb +13 -0
  64. data/lib/vagrant/config.rb +107 -189
  65. data/lib/vagrant/config/base.rb +67 -0
  66. data/lib/vagrant/config/error_recorder.rb +19 -0
  67. data/lib/vagrant/config/nfs.rb +10 -0
  68. data/lib/vagrant/config/package.rb +9 -0
  69. data/lib/vagrant/config/ssh.rb +28 -0
  70. data/lib/vagrant/config/vagrant.rb +21 -0
  71. data/lib/vagrant/config/vm.rb +111 -0
  72. data/lib/vagrant/data_store.rb +68 -0
  73. data/lib/vagrant/downloaders/file.rb +3 -3
  74. data/lib/vagrant/downloaders/http.rb +5 -5
  75. data/lib/vagrant/environment.rb +246 -243
  76. data/lib/vagrant/errors.rb +306 -0
  77. data/lib/vagrant/hosts/base.rb +1 -1
  78. data/lib/vagrant/hosts/bsd.rb +3 -9
  79. data/lib/vagrant/hosts/linux.rb +3 -9
  80. data/lib/vagrant/plugin.rb +50 -0
  81. data/lib/vagrant/provisioners/base.rb +0 -6
  82. data/lib/vagrant/provisioners/chef.rb +63 -58
  83. data/lib/vagrant/provisioners/chef_server.rb +9 -11
  84. data/lib/vagrant/provisioners/chef_solo.rb +2 -2
  85. data/lib/vagrant/ssh.rb +34 -37
  86. data/lib/vagrant/systems/base.rb +0 -13
  87. data/lib/vagrant/systems/linux.rb +10 -33
  88. data/lib/vagrant/systems/solaris.rb +59 -0
  89. data/lib/vagrant/test_helpers.rb +109 -0
  90. data/lib/vagrant/ui.rb +65 -0
  91. data/lib/vagrant/util.rb +9 -19
  92. data/lib/vagrant/util/glob_loader.rb +19 -17
  93. data/lib/vagrant/util/hash_with_indifferent_access.rb +63 -0
  94. data/lib/vagrant/util/plain_logger.rb +2 -0
  95. data/lib/vagrant/util/platform.rb +2 -0
  96. data/lib/vagrant/util/resource_logger.rb +5 -70
  97. data/lib/vagrant/util/retryable.rb +25 -0
  98. data/lib/vagrant/util/template_renderer.rb +1 -1
  99. data/lib/vagrant/version.rb +1 -1
  100. data/lib/vagrant/vm.rb +27 -13
  101. data/templates/commands/init/Vagrantfile.erb +13 -0
  102. data/templates/config/validation_failed.erb +7 -0
  103. data/templates/locales/en.yml +402 -0
  104. data/templates/package_Vagrantfile.erb +1 -1
  105. data/test/locales/en.yml +8 -0
  106. data/test/test_helper.rb +19 -103
  107. data/test/vagrant/action/box/destroy_test.rb +7 -19
  108. data/test/vagrant/action/box/download_test.rb +9 -25
  109. data/test/vagrant/action/box/package_test.rb +2 -2
  110. data/test/vagrant/action/box/unpackage_test.rb +8 -34
  111. data/test/vagrant/action/box/verify_test.rb +10 -19
  112. data/test/vagrant/action/builder_test.rb +0 -15
  113. data/test/vagrant/action/env/set_test.rb +1 -1
  114. data/test/vagrant/action/environment_test.rb +8 -26
  115. data/test/vagrant/action/general/package_test.rb +53 -53
  116. data/test/vagrant/action/vm/boot_test.rb +5 -22
  117. data/test/vagrant/action/vm/check_box_test.rb +35 -25
  118. data/test/vagrant/action/vm/clean_machine_folder_test.rb +1 -1
  119. data/test/vagrant/action/vm/clear_forwarded_ports_test.rb +1 -1
  120. data/test/vagrant/action/vm/clear_nfs_exports_test.rb +1 -1
  121. data/test/vagrant/action/vm/clear_shared_folders_test.rb +1 -1
  122. data/test/vagrant/action/vm/customize_test.rb +2 -1
  123. data/test/vagrant/action/vm/destroy_test.rb +1 -2
  124. data/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb +1 -1
  125. data/test/vagrant/action/vm/disable_networks_test.rb +10 -1
  126. data/test/vagrant/action/vm/discard_state_test.rb +10 -1
  127. data/test/vagrant/action/vm/export_test.rb +9 -37
  128. data/test/vagrant/action/vm/forward_ports_helpers_test.rb +2 -2
  129. data/test/vagrant/action/vm/forward_ports_test.rb +19 -16
  130. data/test/vagrant/action/vm/halt_test.rb +11 -1
  131. data/test/vagrant/action/vm/import_test.rb +37 -21
  132. data/test/vagrant/action/vm/match_mac_address_test.rb +1 -1
  133. data/test/vagrant/action/vm/network_test.rb +8 -7
  134. data/test/vagrant/action/vm/nfs_helpers_test.rb +6 -3
  135. data/test/vagrant/action/vm/nfs_test.rb +20 -45
  136. data/test/vagrant/action/vm/package_test.rb +1 -1
  137. data/test/vagrant/action/vm/package_vagrantfile_test.rb +1 -1
  138. data/test/vagrant/action/vm/provision_test.rb +10 -17
  139. data/test/vagrant/action/vm/resume_test.rb +1 -1
  140. data/test/vagrant/action/vm/share_folders_test.rb +25 -106
  141. data/test/vagrant/action/vm/suspend_test.rb +1 -1
  142. data/test/vagrant/action/warden_test.rb +105 -0
  143. data/test/vagrant/action_test.rb +5 -27
  144. data/test/vagrant/box_collection_test.rb +44 -0
  145. data/test/vagrant/box_test.rb +5 -105
  146. data/test/vagrant/cli_test.rb +35 -0
  147. data/test/vagrant/command/base_test.rb +23 -0
  148. data/test/vagrant/command/group_base_test.rb +15 -0
  149. data/test/vagrant/command/helpers_test.rb +88 -0
  150. data/test/vagrant/config/base_test.rb +52 -0
  151. data/test/vagrant/config/error_recorder_test.rb +18 -0
  152. data/test/vagrant/config/ssh_test.rb +12 -0
  153. data/test/vagrant/config/vagrant_test.rb +11 -0
  154. data/test/vagrant/config/vm_test.rb +70 -0
  155. data/test/vagrant/config_test.rb +113 -206
  156. data/test/vagrant/data_store_test.rb +68 -0
  157. data/test/vagrant/downloaders/base_test.rb +1 -1
  158. data/test/vagrant/downloaders/file_test.rb +4 -3
  159. data/test/vagrant/downloaders/http_test.rb +14 -4
  160. data/test/vagrant/environment_test.rb +290 -590
  161. data/test/vagrant/errors_test.rb +42 -0
  162. data/test/vagrant/hosts/base_test.rb +1 -1
  163. data/test/vagrant/hosts/bsd_test.rb +3 -6
  164. data/test/vagrant/hosts/linux_test.rb +3 -5
  165. data/test/vagrant/plugin_test.rb +9 -0
  166. data/test/vagrant/provisioners/base_test.rb +1 -1
  167. data/test/vagrant/provisioners/chef_server_test.rb +31 -35
  168. data/test/vagrant/provisioners/chef_solo_test.rb +1 -1
  169. data/test/vagrant/provisioners/chef_test.rb +7 -7
  170. data/test/vagrant/ssh_session_test.rb +3 -10
  171. data/test/vagrant/ssh_test.rb +25 -33
  172. data/test/vagrant/systems/linux_test.rb +6 -71
  173. data/test/vagrant/ui_test.rb +29 -0
  174. data/test/vagrant/util/hash_with_indifferent_access_test.rb +39 -0
  175. data/test/vagrant/util/resource_logger_test.rb +14 -81
  176. data/test/vagrant/util/retryable_test.rb +38 -0
  177. data/test/vagrant/util/template_renderer_test.rb +4 -4
  178. data/test/vagrant/vm_test.rb +47 -26
  179. data/vagrant.gemspec +14 -12
  180. metadata +142 -108
  181. data/bin/.gitignore +0 -0
  182. data/lib/vagrant/action/action_exception.rb +0 -16
  183. data/lib/vagrant/action/env/error_halt.rb +0 -16
  184. data/lib/vagrant/action/exception_catcher.rb +0 -14
  185. data/lib/vagrant/action/vm/persist.rb +0 -22
  186. data/lib/vagrant/active_list.rb +0 -83
  187. data/lib/vagrant/command.rb +0 -27
  188. data/lib/vagrant/commands/base.rb +0 -181
  189. data/lib/vagrant/commands/box.rb +0 -16
  190. data/lib/vagrant/commands/box/add.rb +0 -30
  191. data/lib/vagrant/commands/box/list.rb +0 -30
  192. data/lib/vagrant/commands/box/remove.rb +0 -30
  193. data/lib/vagrant/commands/box/repackage.rb +0 -35
  194. data/lib/vagrant/commands/destroy.rb +0 -37
  195. data/lib/vagrant/commands/halt.rb +0 -43
  196. data/lib/vagrant/commands/init.rb +0 -36
  197. data/lib/vagrant/commands/package.rb +0 -81
  198. data/lib/vagrant/commands/provision.rb +0 -31
  199. data/lib/vagrant/commands/reload.rb +0 -36
  200. data/lib/vagrant/commands/resume.rb +0 -35
  201. data/lib/vagrant/commands/ssh.rb +0 -78
  202. data/lib/vagrant/commands/ssh_config.rb +0 -45
  203. data/lib/vagrant/commands/status.rb +0 -125
  204. data/lib/vagrant/commands/suspend.rb +0 -36
  205. data/lib/vagrant/commands/up.rb +0 -44
  206. data/lib/vagrant/exceptions/uncallable_action.rb +0 -17
  207. data/lib/vagrant/util/translator.rb +0 -35
  208. data/templates/strings.yml +0 -341
  209. data/templates/unison/crontab_entry.erb +0 -1
  210. data/templates/unison/script.erb +0 -71
  211. data/test/vagrant/action/env/error_halt_test.rb +0 -21
  212. data/test/vagrant/action/exception_catcher_test.rb +0 -30
  213. data/test/vagrant/action/vm/persist_test.rb +0 -50
  214. data/test/vagrant/active_list_test.rb +0 -173
  215. data/test/vagrant/command_test.rb +0 -53
  216. data/test/vagrant/commands/base_test.rb +0 -139
  217. data/test/vagrant/commands/box/add_test.rb +0 -34
  218. data/test/vagrant/commands/box/list_test.rb +0 -32
  219. data/test/vagrant/commands/box/remove_test.rb +0 -41
  220. data/test/vagrant/commands/box/repackage_test.rb +0 -52
  221. data/test/vagrant/commands/destroy_test.rb +0 -44
  222. data/test/vagrant/commands/halt_test.rb +0 -50
  223. data/test/vagrant/commands/init_test.rb +0 -71
  224. data/test/vagrant/commands/package_test.rb +0 -97
  225. data/test/vagrant/commands/provision_test.rb +0 -60
  226. data/test/vagrant/commands/reload_test.rb +0 -47
  227. data/test/vagrant/commands/resume_test.rb +0 -44
  228. data/test/vagrant/commands/ssh_config_test.rb +0 -77
  229. data/test/vagrant/commands/ssh_test.rb +0 -129
  230. data/test/vagrant/commands/status_test.rb +0 -40
  231. data/test/vagrant/commands/suspend_test.rb +0 -44
  232. data/test/vagrant/commands/up_test.rb +0 -49
  233. data/test/vagrant/util/translator_test.rb +0 -61
  234. data/test/vagrant/util_test.rb +0 -27
@@ -0,0 +1,68 @@
1
+ require "test_helper"
2
+
3
+ class DataStoreTest < Test::Unit::TestCase
4
+ setup do
5
+ @klass = Vagrant::DataStore
6
+ @initial_data = { "foo" => "bar" }
7
+ @db_file = File.join(tmp_path, "data_store_test")
8
+ File.open(@db_file, "w") { |f| f.write(@initial_data.to_json) }
9
+
10
+ @instance = @klass.new(@db_file)
11
+ end
12
+
13
+ teardown do
14
+ File.delete(@db_file) if File.file?(@db_file)
15
+ end
16
+
17
+ should "be a hash with indifferent access" do
18
+ assert @instance.is_a?(Vagrant::Util::HashWithIndifferentAccess)
19
+ end
20
+
21
+ should "just be an empty hash if file doesn't exist" do
22
+ assert @klass.new("NEvERNENVENRNE").empty?
23
+ end
24
+
25
+ should "read the data" do
26
+ assert_equal @initial_data["foo"], @instance[:foo]
27
+ end
28
+
29
+ should "read the data by stringifying keys" do
30
+ @instance[:bar] = { "baz" => "yay" }
31
+ @instance.commit
32
+ @instance = @klass.new(@db_file)
33
+ assert_equal "yay", @instance[:bar]["baz"]
34
+ end
35
+
36
+ should "write the data, but not save it right away" do
37
+ @instance[:foo] = "changed"
38
+ assert_equal "changed", @instance[:foo]
39
+ assert_equal @initial_data["foo"], @klass.new(@db_file)["foo"]
40
+ end
41
+
42
+ should "write the data if commit is called" do
43
+ @instance[:foo] = "changed"
44
+ @instance.commit
45
+
46
+ assert_equal "changed", @klass.new(@db_file)[:foo]
47
+ end
48
+
49
+ should "delete the data store file if the hash is empty" do
50
+ @instance[:foo] = :bar
51
+ @instance.commit
52
+ assert File.exist?(@db_file)
53
+
54
+ @instance.clear
55
+ assert @instance.empty?
56
+ @instance.commit
57
+ assert !File.exist?(@db_file)
58
+ end
59
+
60
+ should "clean nil and empties if commit is called" do
61
+ @instance[:foo] = { :bar => nil }
62
+ @instance[:bar] = {}
63
+ @instance.commit
64
+
65
+ assert !@instance.has_key?(:foo)
66
+ assert !@instance.has_key?(:bar)
67
+ end
68
+ end
@@ -7,7 +7,7 @@ class BaseDownloaderTest < Test::Unit::TestCase
7
7
 
8
8
  context "base instance" do
9
9
  setup do
10
- @env = mock_environment
10
+ @env = vagrant_env
11
11
  @base = Vagrant::Downloaders::Base.new(@env)
12
12
  end
13
13
 
@@ -9,9 +9,10 @@ class FileDownloaderTest < Test::Unit::TestCase
9
9
  context "preparing" do
10
10
  should "raise an exception if the file does not exist" do
11
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
12
+
13
+ assert_raises(Vagrant::Errors::DownloaderFileDoesntExist) {
14
+ @downloader.prepare(@uri)
15
+ }
15
16
  end
16
17
  end
17
18
 
@@ -10,6 +10,8 @@ class HttpDownloaderTest < Test::Unit::TestCase
10
10
 
11
11
  context "downloading" do
12
12
  setup do
13
+ ENV["http_proxy"] = nil
14
+
13
15
  @parsed_uri = URI.parse(@uri)
14
16
  @http = Net::HTTP.new(@parsed_uri.host, @parsed_uri.port)
15
17
  Net::HTTP.stubs(:new).returns(@http)
@@ -17,7 +19,15 @@ class HttpDownloaderTest < Test::Unit::TestCase
17
19
  end
18
20
 
19
21
  should "create a proper net/http object" do
20
- Net::HTTP.expects(:new).with(@parsed_uri.host, @parsed_uri.port).once.returns(@http)
22
+ Net::HTTP.expects(:new).with(@parsed_uri.host, @parsed_uri.port, nil, nil, nil, nil).once.returns(@http)
23
+ @http.expects(:start)
24
+ @downloader.download!(@uri, @tempfile)
25
+ end
26
+
27
+ should "create a proper net/http object with a proxy" do
28
+ ENV["http_proxy"] = "http://user:foo@google.com"
29
+ @proxy = URI.parse(ENV["http_proxy"])
30
+ Net::HTTP.expects(:new).with(@parsed_uri.host, @parsed_uri.port, @proxy.host, @proxy.port, @proxy.user, @proxy.password).once.returns(@http)
21
31
  @http.expects(:start)
22
32
  @downloader.download!(@uri, @tempfile)
23
33
  end
@@ -45,10 +55,10 @@ class HttpDownloaderTest < Test::Unit::TestCase
45
55
 
46
56
  should "error environment if invalid URL given" do
47
57
  Net::HTTP.expects(:new).raises(SocketError.new)
48
- @downloader.download!(@uri, @tempfile)
49
58
 
50
- assert @downloader.env.error?
51
- assert_equal :box_download_http_socket_error, @downloader.env.error.first
59
+ assert_raises(Vagrant::Errors::DownloaderHTTPSocketError) {
60
+ @downloader.download!(@uri, @tempfile)
61
+ }
52
62
  end
53
63
  end
54
64
 
@@ -1,6 +1,11 @@
1
1
  require "test_helper"
2
+ require "pathname"
2
3
 
3
4
  class EnvironmentTest < Test::Unit::TestCase
5
+ setup do
6
+ @klass = Vagrant::Environment
7
+ end
8
+
4
9
  context "class method check virtualbox version" do
5
10
  setup do
6
11
  VirtualBox.stubs(:version).returns("3.1.4")
@@ -8,424 +13,409 @@ class EnvironmentTest < Test::Unit::TestCase
8
13
 
9
14
  should "not error and exit if everything is good" do
10
15
  VirtualBox.expects(:version).returns("3.2.4")
11
- Vagrant::Environment.expects(:error_and_exit).never
12
- Vagrant::Environment.check_virtualbox!
16
+ assert_nothing_raised { @klass.check_virtualbox! }
13
17
  end
14
18
 
15
19
  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
20
  VirtualBox.expects(:version).returns(nil)
18
- Vagrant::Environment.check_virtualbox!
21
+ assert_raises(Vagrant::Errors::VirtualBoxNotDetected) { @klass.check_virtualbox! }
19
22
  end
20
23
 
21
24
  should "error and exit if VirtualBox is lower than version 3.2" do
22
25
  version = "3.1.12r1041"
23
- Vagrant::Environment.expects(:error_and_exit).with(:virtualbox_invalid_version, :version => version.to_s).once
24
26
  VirtualBox.expects(:version).returns(version)
25
- Vagrant::Environment.check_virtualbox!
27
+ assert_raises(Vagrant::Errors::VirtualBoxInvalidVersion) { @klass.check_virtualbox! }
26
28
  end
27
29
 
28
30
  should "error and exit for OSE VirtualBox" do
29
31
  version = "3.2.6_OSE"
30
- Vagrant::Environment.expects(:error_and_exit).with(:virtualbox_invalid_ose, :version => version.to_s).once
31
32
  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
- }
33
+ assert_raises(Vagrant::Errors::VirtualBoxInvalidOSE) { @klass.check_virtualbox! }
57
34
  end
58
35
  end
59
36
 
60
37
  context "initialization" do
61
38
  should "set the cwd if given" do
62
39
  cwd = "foobarbaz"
63
- env = Vagrant::Environment.new(:cwd => cwd)
64
- assert_equal cwd, env.cwd
40
+ env = @klass.new(:cwd => cwd)
41
+ assert_equal Pathname.new(cwd), env.cwd
65
42
  end
66
43
 
67
44
  should "default to pwd if cwd is nil" do
68
- env = Vagrant::Environment.new
69
- assert_equal Dir.pwd, env.cwd
45
+ env = @klass.new
46
+ assert_equal Pathname.new(Dir.pwd), env.cwd
70
47
  end
71
48
  end
72
49
 
73
50
  context "paths" do
74
51
  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
52
+ @env = vagrant_env
87
53
  end
88
54
 
89
55
  context "dotfile path" do
90
- setup do
91
- @env.stubs(:root_path).returns("foo")
92
- end
93
-
94
56
  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
57
+ assert_equal @env.root_path.join(@env.config.vagrant.dotfile_name), @env.dotfile_path
96
58
  end
97
59
  end
98
60
 
99
61
  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
62
  should "return the home path if it loaded" do
106
- assert_equal @env.config.vagrant.home, @env.home_path
63
+ expected = Pathname.new(File.expand_path(@env.config.vagrant.home, @env.root_path))
64
+ assert_equal expected, @env.home_path
107
65
  end
108
66
  end
109
67
 
110
68
  context "temp path" do
111
69
  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
70
+ assert_equal @env.home_path.join("tmp"), @env.tmp_path
115
71
  end
116
72
  end
117
73
 
118
74
  context "boxes path" do
119
75
  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
76
+ assert_equal @env.home_path.join("boxes"), @env.boxes_path
123
77
  end
124
78
  end
125
79
  end
126
80
 
127
- context "primary VM helper" do
81
+ context "resource" do
128
82
  setup do
129
- @env = mock_environment
130
- @env.stubs(:multivm?).returns(true)
83
+ @env = vagrant_env
131
84
  end
132
85
 
133
- should "return the first VM if not multivm" do
134
- result = mock("result")
86
+ should "return 'vagrant' as a default" do
87
+ assert_equal 'vagrant', @env.resource
88
+ end
135
89
 
136
- @env.stubs(:multivm?).returns(false)
137
- @env.stubs(:vms).returns({:default => result})
90
+ should "return the VM name if it is specified" do
91
+ @env.stubs(:vm).returns(mock("vm", :name => "foo"))
92
+ assert_equal "foo", @env.resource
93
+ end
94
+ end
138
95
 
139
- assert_equal result, @env.primary_vm
96
+ context "primary VM helper" do
97
+ should "return the first VM if not multivm" do
98
+ env = vagrant_env
99
+ assert_equal env.vms[@klass::DEFAULT_VM], env.primary_vm
140
100
  end
141
101
 
142
102
  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)
103
+ env = vagrant_env(vagrantfile(<<-vf))
104
+ config.vm.define(:web, :primary => true) do; end
105
+ config.vm.define :db do; end
106
+ vf
146
107
 
147
- @env.stubs(:parent).returns(parent)
148
- assert_equal result, @env.primary_vm
108
+ assert_equal :web, env.primary_vm.name
149
109
  end
150
110
 
151
111
  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)
112
+ env = vagrant_env(vagrantfile(<<-vf))
113
+ config.vm.define :web
114
+ config.vm.define :db
115
+ config.vm.define :utility
116
+ vf
155
117
 
156
- assert @env.primary_vm.nil?
118
+ assert env.primary_vm.nil?
157
119
  end
120
+ end
158
121
 
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)
122
+ context "multivm? helper" do
123
+ should "return true if VM length greater than 1" do
124
+ env = vagrant_env(vagrantfile(<<-vf))
125
+ config.vm.define :web
126
+ config.vm.define :db
127
+ vf
163
128
 
164
- result = mock("result")
165
- vms = {
166
- :foo => :foo,
167
- :bar => result,
168
- :baz => :baz
169
- }
170
- @env.stubs(:vms).returns(vms)
129
+ assert env.multivm?
130
+ end
131
+
132
+ should "return false if VM length is 1" do
133
+ env = vagrant_env(vagrantfile(<<-vf))
134
+ config.vm.define :web
135
+ vf
171
136
 
172
- assert_equal result, @env.primary_vm
137
+ assert !env.multivm?
173
138
  end
174
139
  end
175
140
 
176
- context "multivm? helper" do
177
- setup do
178
- @env = mock_environment
141
+ context "local data" do
142
+ should "lazy load the data store only once" do
143
+ result = { :foo => :bar }
144
+ Vagrant::DataStore.expects(:new).returns(result).once
145
+ env = vagrant_env
146
+ assert_equal result, env.local_data
147
+ assert_equal result, env.local_data
148
+ assert_equal result, env.local_data
179
149
  end
180
150
 
181
- context "with a parent" do
182
- setup do
183
- @parent = mock('parent')
184
- @env.stubs(:parent).returns(@parent)
185
- end
151
+ should "return the parent's local data if a parent exists" do
152
+ env = vagrant_env(vagrantfile(<<-vf))
153
+ config.vm.define :web
154
+ config.vm.define :db
155
+ vf
186
156
 
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
157
+ env.local_data[:foo] = :bar
158
+
159
+ Vagrant::DataStore.expects(:new).never
160
+ assert_equal :bar, env.vms[:web].env.local_data[:foo]
192
161
  end
162
+ end
193
163
 
194
- context "without a parent" do
195
- setup do
196
- @env.stubs(:parent).returns(nil)
197
- end
164
+ context "accessing host" do
165
+ should "load the host once" do
166
+ env = @klass.new(:cwd => vagrantfile)
167
+ result = mock("result")
168
+ Vagrant::Hosts::Base.expects(:load).with(env, env.config.vagrant.host).once.returns(result)
169
+ assert_equal result, env.host
170
+ assert_equal result, env.host
171
+ assert_equal result, env.host
172
+ end
173
+ end
198
174
 
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
175
+ context "accessing actions" do
176
+ should "initialize the Action object with the given environment" do
177
+ env = @klass.new(:cwd => vagrant_app)
178
+ result = mock("result")
179
+ Vagrant::Action.expects(:new).with(env).returns(result).once
180
+ assert_equal result, env.actions
181
+ assert_equal result, env.actions
182
+ assert_equal result, env.actions
183
+ end
184
+ end
203
185
 
204
- should "return false if VM length is 1" do
205
- @env.stubs(:vms).returns([1])
206
- assert !@env.multivm?
207
- end
186
+ context "global data" do
187
+ should "lazy load the data store only once" do
188
+ env = vagrant_env
189
+ store = env.global_data
190
+
191
+ assert env.global_data.equal?(store)
192
+ assert env.global_data.equal?(store)
193
+ assert env.global_data.equal?(store)
194
+ end
195
+
196
+ should "return the parent's global data if a parent exists" do
197
+ env = vagrant_env(vagrantfile(<<-vf))
198
+ config.vm.define :web
199
+ config.vm.define :db
200
+ vf
201
+
202
+ result = env.global_data
203
+ assert env.vms[:web].env.global_data.equal?(result)
208
204
  end
209
205
  end
210
206
 
211
- context "loading" do
212
- setup do
213
- @env = mock_environment
207
+ context "loading logger" do
208
+ should "lazy load the logger only once" do
209
+ result = Vagrant::Util::ResourceLogger.new("vagrant", vagrant_env)
210
+ Vagrant::Util::ResourceLogger.expects(:new).returns(result).once
211
+ env = vagrant_env
212
+ assert_equal result, env.logger
213
+ assert_equal result, env.logger
214
+ assert_equal result, env.logger
214
215
  end
215
216
 
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
217
+ should "return the parent's logger if a parent exists" do
218
+ env = vagrant_env(vagrantfile(<<-vf))
219
+ config.vm.define :web
220
+ config.vm.define :db
221
+ vf
222
+
223
+ result = env.logger
224
+
225
+ Vagrant::Util::ResourceLogger.expects(:new).never
226
+ assert env.vms[:web].env.logger.equal?(result)
233
227
  end
228
+ end
234
229
 
235
- context "loading the root path" do
236
- setup do
237
- @env.cwd = "/foo"
238
- end
230
+ context "loading the root path" do
231
+ should "should walk the parent directories looking for rootfile" do
232
+ paths = [Pathname.new("/foo/bar/baz"),
233
+ Pathname.new("/foo/bar"),
234
+ Pathname.new("/foo"),
235
+ Pathname.new("/")
236
+ ]
239
237
 
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)
238
+ search_seq = sequence("search_seq")
239
+ paths.each do |path|
240
+ File.expects(:exist?).with(path.join(@klass::ROOTFILE_NAME).to_s).returns(false).in_sequence(search_seq)
246
241
  end
247
242
 
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
243
+ assert !@klass.new(:cwd => paths.first).root_path
244
+ end
256
245
 
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
246
+ should "should set the path for the rootfile" do
247
+ path = Pathname.new(File.expand_path("/foo"))
248
+ File.expects(:exist?).with(path.join(@klass::ROOTFILE_NAME).to_s).returns(true)
269
249
 
270
- assert !@env.load_root_path!(paths.first)
271
- end
250
+ assert_equal path, @klass.new(:cwd => path).root_path
251
+ end
272
252
 
273
- should "return false if not found" do
274
- path = Pathname.new("/")
275
- assert !@env.load_root_path!(path)
276
- end
253
+ should "only load the root path once" do
254
+ env = @klass.new
255
+ File.expects(:exist?).with(env.cwd.join(@klass::ROOTFILE_NAME).to_s).returns(true).once
277
256
 
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
257
+ assert_equal env.cwd, env.root_path
258
+ assert_equal env.cwd, env.root_path
259
+ assert_equal env.cwd, env.root_path
260
+ end
287
261
 
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)
262
+ should "only load the root path once even if nil" do
263
+ File.stubs(:exist?).returns(false)
292
264
 
293
- assert @env.load_root_path!(Pathname.new(path))
294
- assert_equal path, @env.root_path
295
- end
265
+ env = @klass.new
266
+ assert env.root_path.nil?
267
+ assert env.root_path.nil?
268
+ assert env.root_path.nil?
296
269
  end
270
+ end
297
271
 
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!)
272
+ context "accessing the configuration" do
273
+ should "load the environment if its not already loaded" do
274
+ env = @klass.new(:cwd => vagrantfile)
275
+ env.expects(:load!).once
276
+ env.config
277
+ end
305
278
 
306
- @parent_env = mock_environment
279
+ should "not load the environment if its already loaded" do
280
+ env = vagrant_env
281
+ env.expects(:load!).never
282
+ env.config
283
+ end
284
+ end
307
285
 
308
- File.stubs(:exist?).returns(false)
309
- end
286
+ context "accessing the box collection" do
287
+ should "create a box collection representing the environment" do
288
+ env = vagrant_env
289
+ assert env.boxes.is_a?(Vagrant::BoxCollection)
290
+ assert_equal env, env.boxes.env
291
+ end
310
292
 
311
- should "reset the configuration object" do
312
- Vagrant::Config.expects(:reset!).with(@env).once
313
- @env.load_config!
314
- end
293
+ should "not load the environment if its already loaded" do
294
+ env = vagrant_env
295
+ env.expects(:load!).never
296
+ env.boxes
297
+ end
315
298
 
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
299
+ should "return the parent's box collection if it has one" do
300
+ env = vagrant_env(vagrantfile(<<-vf))
301
+ config.vm.define :web
302
+ config.vm.define :db
303
+ vf
320
304
 
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
305
+ assert env.vms[:web].env.boxes.equal?(env.boxes)
306
+ end
307
+ end
325
308
 
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
309
+ context "accessing the current box" do
310
+ should "return the box that is specified in the config" do
311
+ vagrant_box("foo")
312
+ env = vagrant_env(vagrantfile(<<-vf))
313
+ config.vm.box = "foo"
314
+ vf
331
315
 
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
316
+ assert env.box
317
+ assert_equal "foo", env.box.name
318
+ end
319
+ end
336
320
 
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
321
+ context "accessing the VMs hash" do
322
+ should "load the environment if its not already loaded" do
323
+ env = @klass.new(:cwd => vagrantfile)
324
+ assert !env.loaded?
325
+ env.vms
326
+ assert env.loaded?
327
+ end
342
328
 
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
329
+ should "not load the environment if its already loaded" do
330
+ env = vagrant_env
331
+ env.expects(:load!).never
332
+ env.vms
333
+ end
348
334
 
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
335
+ should "return the parent's VMs hash if it has one" do
336
+ env = vagrant_env(vagrantfile(<<-vf))
337
+ config.vm.define :web
338
+ config.vm.define :db
339
+ vf
357
340
 
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
341
+ assert env.vms[:web].env.vms.equal?(env.vms)
342
+ end
343
+ end
365
344
 
366
- # Sanity
367
- assert_equal :NO, @parent_env.config.vm.box
345
+ context "loading" do
346
+ context "overall load method" do
347
+ should "load! should call proper sequence and return itself" do
348
+ env = @klass.new(:cwd => vagrantfile)
349
+ call_seq = sequence("call_sequence")
350
+ @klass.expects(:check_virtualbox!).once.in_sequence(call_seq)
351
+ env.expects(:load_config!).once.in_sequence(call_seq)
352
+ env.actions.expects(:run).with(:environment_load).once.in_sequence(call_seq)
353
+ assert_equal env, env.load!
354
+ end
355
+ end
368
356
 
369
- @env.stubs(:vm_name).returns(vm_name)
370
- @env.stubs(:parent).returns(@parent_env)
357
+ context "loading config" do
358
+ setup do
359
+ clean_paths
360
+ @env = @klass.new(:cwd => vagrantfile)
361
+ end
371
362
 
372
- @env.load_config!
363
+ def create_box_vagrantfile
364
+ vagrantfile(vagrant_box("box"), 'config.package.name = "box.box"')
365
+ end
373
366
 
374
- assert_equal sub_box, @env.config.vm.box
367
+ def create_home_vagrantfile
368
+ vagrantfile(home_path, 'config.package.name = "home.box"')
375
369
  end
376
370
 
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!
371
+ def create_root_vagrantfile
372
+ vagrantfile(@env.root_path, 'config.package.name = "root.box"')
381
373
  end
382
374
 
383
- should "not load the files if exist? returns false" do
384
- @env.expects(:load).never
385
- @env.load_config!
375
+ should "load from the project root" do
376
+ assert_equal "package.box", @env.config.package.name
386
377
  end
387
378
 
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
379
+ should "load from box if specified" do
380
+ create_box_vagrantfile
381
+ vagrantfile(@env.root_path, "config.vm.box = 'box'")
382
+
383
+ assert_equal "box.box", @env.config.package.name
395
384
  end
396
385
 
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!
386
+ should "load from home path if exists" do
387
+ create_home_vagrantfile
388
+ assert_equal "home.box", @env.config.package.name
402
389
  end
403
- end
404
390
 
405
- context "loading logger" do
406
- setup do
407
- @env = mock_environment
408
- @env.stubs(:vm_name).returns(nil)
391
+ should "load from root path" do
392
+ create_home_vagrantfile
393
+ create_root_vagrantfile
394
+ assert_equal "root.box", @env.config.package.name
409
395
  end
410
396
 
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
397
+ should "load from a sub-vm configuration if environment represents a VM" do
398
+ create_home_vagrantfile
399
+ vagrantfile(@env.root_path, <<-vf)
400
+ config.package.name = "root.box"
401
+ config.vm.define :web do |web|
402
+ web.package.name = "web.box"
403
+ end
404
+ vf
405
+
406
+ assert_equal "root.box", @env.config.package.name
407
+ assert_equal "web.box", @env.vms[:web].env.config.package.name
415
408
  end
416
409
 
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
410
+ should "reload the logger after executing" do
411
+ @env.load_config!
412
+ assert @env.instance_variable_get(:@logger).nil?
422
413
  end
423
414
  end
424
415
 
425
416
  context "loading home directory" do
426
417
  setup do
427
- @env = mock_environment
428
- @home_dir = File.expand_path(@env.config.vagrant.home)
418
+ @env = vagrant_env
429
419
 
430
420
  File.stubs(:directory?).returns(true)
431
421
  FileUtils.stubs(:mkdir_p)
@@ -434,8 +424,8 @@ class EnvironmentTest < Test::Unit::TestCase
434
424
  should "create each directory if it doesn't exist" do
435
425
  create_seq = sequence("create_seq")
436
426
  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)
427
+ @klass::HOME_SUBDIRS.each do |subdir|
428
+ FileUtils.expects(:mkdir_p).with(@env.home_path.join(subdir)).in_sequence(create_seq)
439
429
  end
440
430
 
441
431
  @env.load_home_directory!
@@ -448,323 +438,33 @@ class EnvironmentTest < Test::Unit::TestCase
448
438
  end
449
439
  end
450
440
 
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
441
  context "loading the UUID out from the persisted dotfile" do
496
442
  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
443
+ @env = vagrant_env
522
444
  end
523
445
 
524
446
  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)
447
+ @env.local_data[:active] = { "foo" => "bar", "bar" => "baz" }
532
448
 
533
- vms.each do |key, value|
449
+ results = {}
450
+ @env.local_data[:active].each do |key, value|
534
451
  vm = mock("vm#{key}")
535
452
  Vagrant::VM.expects(:find).with(value, @env, key.to_sym).returns(vm)
536
- results[key] = vm
453
+ results[key.to_sym] = vm
537
454
  end
538
455
 
539
- @env.load_vm!
456
+ returned = @env.load_vms!
540
457
 
541
458
  results.each do |key, value|
542
- assert_equal value, @env.vms[key]
459
+ assert_equal value, returned[key]
543
460
  end
544
461
  end
545
462
 
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!
463
+ should "uuid should be nil if local data contains nothing" do
464
+ assert @env.local_data.empty? # sanity
465
+ @env.load_vms!
570
466
  assert_nil @env.vm
571
467
  end
572
468
  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
469
  end
770
470
  end