nixme-vagrant 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (240) hide show
  1. data/.gitignore +15 -0
  2. data/.yardopts +1 -0
  3. data/CHANGELOG.md +261 -0
  4. data/Gemfile +21 -0
  5. data/LICENSE +21 -0
  6. data/README.md +61 -0
  7. data/Rakefile +22 -0
  8. data/bin/vagrant +22 -0
  9. data/config/default.rb +39 -0
  10. data/contrib/README.md +12 -0
  11. data/contrib/emacs/vagrant.el +8 -0
  12. data/contrib/vim/vagrantfile.vim +9 -0
  13. data/keys/README.md +17 -0
  14. data/keys/vagrant +27 -0
  15. data/keys/vagrant.ppk +26 -0
  16. data/keys/vagrant.pub +1 -0
  17. data/lib/vagrant.rb +42 -0
  18. data/lib/vagrant/action.rb +134 -0
  19. data/lib/vagrant/action/box.rb +11 -0
  20. data/lib/vagrant/action/box/destroy.rb +21 -0
  21. data/lib/vagrant/action/box/download.rb +72 -0
  22. data/lib/vagrant/action/box/package.rb +19 -0
  23. data/lib/vagrant/action/box/unpackage.rb +55 -0
  24. data/lib/vagrant/action/box/verify.rb +23 -0
  25. data/lib/vagrant/action/builder.rb +124 -0
  26. data/lib/vagrant/action/builtin.rb +104 -0
  27. data/lib/vagrant/action/env.rb +7 -0
  28. data/lib/vagrant/action/env/set.rb +18 -0
  29. data/lib/vagrant/action/environment.rb +50 -0
  30. data/lib/vagrant/action/general.rb +8 -0
  31. data/lib/vagrant/action/general/package.rb +109 -0
  32. data/lib/vagrant/action/general/validate.rb +19 -0
  33. data/lib/vagrant/action/vm.rb +31 -0
  34. data/lib/vagrant/action/vm/boot.rb +48 -0
  35. data/lib/vagrant/action/vm/check_box.rb +28 -0
  36. data/lib/vagrant/action/vm/check_guest_additions.rb +30 -0
  37. data/lib/vagrant/action/vm/clean_machine_folder.rb +43 -0
  38. data/lib/vagrant/action/vm/clear_forwarded_ports.rb +39 -0
  39. data/lib/vagrant/action/vm/clear_nfs_exports.rb +20 -0
  40. data/lib/vagrant/action/vm/clear_shared_folders.rb +32 -0
  41. data/lib/vagrant/action/vm/customize.rb +21 -0
  42. data/lib/vagrant/action/vm/destroy.rb +19 -0
  43. data/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb +30 -0
  44. data/lib/vagrant/action/vm/discard_state.rb +22 -0
  45. data/lib/vagrant/action/vm/export.rb +52 -0
  46. data/lib/vagrant/action/vm/forward_ports.rb +133 -0
  47. data/lib/vagrant/action/vm/forward_ports_helpers.rb +28 -0
  48. data/lib/vagrant/action/vm/halt.rb +29 -0
  49. data/lib/vagrant/action/vm/host_name.rb +21 -0
  50. data/lib/vagrant/action/vm/import.rb +35 -0
  51. data/lib/vagrant/action/vm/match_mac_address.rb +21 -0
  52. data/lib/vagrant/action/vm/network.rb +139 -0
  53. data/lib/vagrant/action/vm/nfs.rb +159 -0
  54. data/lib/vagrant/action/vm/nfs_helpers.rb +11 -0
  55. data/lib/vagrant/action/vm/package.rb +23 -0
  56. data/lib/vagrant/action/vm/package_vagrantfile.rb +33 -0
  57. data/lib/vagrant/action/vm/provision.rb +41 -0
  58. data/lib/vagrant/action/vm/resume.rb +20 -0
  59. data/lib/vagrant/action/vm/share_folders.rb +70 -0
  60. data/lib/vagrant/action/vm/suspend.rb +20 -0
  61. data/lib/vagrant/action/warden.rb +79 -0
  62. data/lib/vagrant/box.rb +90 -0
  63. data/lib/vagrant/box_collection.rb +53 -0
  64. data/lib/vagrant/cli.rb +55 -0
  65. data/lib/vagrant/command.rb +25 -0
  66. data/lib/vagrant/command/base.rb +106 -0
  67. data/lib/vagrant/command/box.rb +33 -0
  68. data/lib/vagrant/command/destroy.rb +17 -0
  69. data/lib/vagrant/command/group_base.rb +107 -0
  70. data/lib/vagrant/command/halt.rb +18 -0
  71. data/lib/vagrant/command/helpers.rb +33 -0
  72. data/lib/vagrant/command/init.rb +14 -0
  73. data/lib/vagrant/command/named_base.rb +14 -0
  74. data/lib/vagrant/command/package.rb +41 -0
  75. data/lib/vagrant/command/provision.rb +17 -0
  76. data/lib/vagrant/command/reload.rb +17 -0
  77. data/lib/vagrant/command/resume.rb +17 -0
  78. data/lib/vagrant/command/ssh.rb +42 -0
  79. data/lib/vagrant/command/ssh_config.rb +26 -0
  80. data/lib/vagrant/command/status.rb +22 -0
  81. data/lib/vagrant/command/suspend.rb +17 -0
  82. data/lib/vagrant/command/up.rb +19 -0
  83. data/lib/vagrant/command/upgrade_to_060.rb +45 -0
  84. data/lib/vagrant/command/version.rb +13 -0
  85. data/lib/vagrant/config.rb +123 -0
  86. data/lib/vagrant/config/base.rb +85 -0
  87. data/lib/vagrant/config/error_recorder.rb +19 -0
  88. data/lib/vagrant/config/nfs.rb +10 -0
  89. data/lib/vagrant/config/package.rb +9 -0
  90. data/lib/vagrant/config/ssh.rb +33 -0
  91. data/lib/vagrant/config/top.rb +61 -0
  92. data/lib/vagrant/config/vagrant.rb +16 -0
  93. data/lib/vagrant/config/vm.rb +137 -0
  94. data/lib/vagrant/config/vm/provisioner.rb +56 -0
  95. data/lib/vagrant/config/vm/sub_vm.rb +17 -0
  96. data/lib/vagrant/data_store.rb +70 -0
  97. data/lib/vagrant/downloaders.rb +7 -0
  98. data/lib/vagrant/downloaders/base.rb +23 -0
  99. data/lib/vagrant/downloaders/file.rb +22 -0
  100. data/lib/vagrant/downloaders/http.rb +64 -0
  101. data/lib/vagrant/environment.rb +392 -0
  102. data/lib/vagrant/errors.rb +332 -0
  103. data/lib/vagrant/hosts.rb +7 -0
  104. data/lib/vagrant/hosts/base.rb +77 -0
  105. data/lib/vagrant/hosts/bsd.rb +52 -0
  106. data/lib/vagrant/hosts/linux.rb +47 -0
  107. data/lib/vagrant/plugin.rb +57 -0
  108. data/lib/vagrant/provisioners.rb +9 -0
  109. data/lib/vagrant/provisioners/base.rb +63 -0
  110. data/lib/vagrant/provisioners/chef.rb +130 -0
  111. data/lib/vagrant/provisioners/chef_server.rb +97 -0
  112. data/lib/vagrant/provisioners/chef_solo.rb +139 -0
  113. data/lib/vagrant/provisioners/puppet.rb +137 -0
  114. data/lib/vagrant/provisioners/puppet_server.rb +55 -0
  115. data/lib/vagrant/provisioners/shell.rb +52 -0
  116. data/lib/vagrant/ssh.rb +173 -0
  117. data/lib/vagrant/ssh/session.rb +119 -0
  118. data/lib/vagrant/systems.rb +10 -0
  119. data/lib/vagrant/systems/base.rb +87 -0
  120. data/lib/vagrant/systems/debian.rb +36 -0
  121. data/lib/vagrant/systems/freebsd.rb +84 -0
  122. data/lib/vagrant/systems/gentoo.rb +27 -0
  123. data/lib/vagrant/systems/linux.rb +79 -0
  124. data/lib/vagrant/systems/linux/config.rb +21 -0
  125. data/lib/vagrant/systems/linux/error.rb +9 -0
  126. data/lib/vagrant/systems/redhat.rb +39 -0
  127. data/lib/vagrant/systems/solaris.rb +62 -0
  128. data/lib/vagrant/test_helpers.rb +128 -0
  129. data/lib/vagrant/ui.rb +69 -0
  130. data/lib/vagrant/util.rb +13 -0
  131. data/lib/vagrant/util/busy.rb +59 -0
  132. data/lib/vagrant/util/hash_with_indifferent_access.rb +63 -0
  133. data/lib/vagrant/util/plain_logger.rb +25 -0
  134. data/lib/vagrant/util/platform.rb +58 -0
  135. data/lib/vagrant/util/resource_logger.rb +63 -0
  136. data/lib/vagrant/util/retryable.rb +25 -0
  137. data/lib/vagrant/util/stacked_proc_runner.rb +35 -0
  138. data/lib/vagrant/util/template_renderer.rb +83 -0
  139. data/lib/vagrant/version.rb +6 -0
  140. data/lib/vagrant/vm.rb +176 -0
  141. data/templates/chef_server_client.erb +18 -0
  142. data/templates/chef_solo_solo.erb +19 -0
  143. data/templates/commands/init/Vagrantfile.erb +63 -0
  144. data/templates/config/validation_failed.erb +7 -0
  145. data/templates/locales/en.yml +531 -0
  146. data/templates/network_entry_debian.erb +8 -0
  147. data/templates/network_entry_gentoo.erb +7 -0
  148. data/templates/network_entry_redhat.erb +8 -0
  149. data/templates/nfs/exports.erb +5 -0
  150. data/templates/nfs/exports_linux.erb +5 -0
  151. data/templates/package_Vagrantfile.erb +11 -0
  152. data/templates/ssh_config.erb +9 -0
  153. data/test/locales/en.yml +8 -0
  154. data/test/test_helper.rb +26 -0
  155. data/test/vagrant/action/box/destroy_test.rb +18 -0
  156. data/test/vagrant/action/box/download_test.rb +125 -0
  157. data/test/vagrant/action/box/package_test.rb +25 -0
  158. data/test/vagrant/action/box/unpackage_test.rb +84 -0
  159. data/test/vagrant/action/box/verify_test.rb +30 -0
  160. data/test/vagrant/action/builder_test.rb +203 -0
  161. data/test/vagrant/action/env/set_test.rb +24 -0
  162. data/test/vagrant/action/environment_test.rb +27 -0
  163. data/test/vagrant/action/general/package_test.rb +268 -0
  164. data/test/vagrant/action/general/validate_test.rb +31 -0
  165. data/test/vagrant/action/vm/boot_test.rb +66 -0
  166. data/test/vagrant/action/vm/check_box_test.rb +56 -0
  167. data/test/vagrant/action/vm/check_guest_additions_test.rb +9 -0
  168. data/test/vagrant/action/vm/clean_machine_folder_test.rb +84 -0
  169. data/test/vagrant/action/vm/clear_forwarded_ports_test.rb +72 -0
  170. data/test/vagrant/action/vm/clear_nfs_exports_test.rb +22 -0
  171. data/test/vagrant/action/vm/clear_shared_folders_test.rb +49 -0
  172. data/test/vagrant/action/vm/customize_test.rb +30 -0
  173. data/test/vagrant/action/vm/destroy_test.rb +25 -0
  174. data/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb +49 -0
  175. data/test/vagrant/action/vm/discard_state_test.rb +45 -0
  176. data/test/vagrant/action/vm/export_test.rb +107 -0
  177. data/test/vagrant/action/vm/forward_ports_helpers_test.rb +70 -0
  178. data/test/vagrant/action/vm/forward_ports_test.rb +194 -0
  179. data/test/vagrant/action/vm/halt_test.rb +79 -0
  180. data/test/vagrant/action/vm/host_name_test.rb +36 -0
  181. data/test/vagrant/action/vm/import_test.rb +66 -0
  182. data/test/vagrant/action/vm/match_mac_address_test.rb +36 -0
  183. data/test/vagrant/action/vm/network_test.rb +286 -0
  184. data/test/vagrant/action/vm/nfs_helpers_test.rb +26 -0
  185. data/test/vagrant/action/vm/nfs_test.rb +260 -0
  186. data/test/vagrant/action/vm/package_test.rb +25 -0
  187. data/test/vagrant/action/vm/package_vagrantfile_test.rb +46 -0
  188. data/test/vagrant/action/vm/provision_test.rb +90 -0
  189. data/test/vagrant/action/vm/resume_test.rb +35 -0
  190. data/test/vagrant/action/vm/share_folders_test.rb +139 -0
  191. data/test/vagrant/action/vm/suspend_test.rb +35 -0
  192. data/test/vagrant/action/warden_test.rb +119 -0
  193. data/test/vagrant/action_test.rb +89 -0
  194. data/test/vagrant/box_collection_test.rb +45 -0
  195. data/test/vagrant/box_test.rb +74 -0
  196. data/test/vagrant/cli_test.rb +35 -0
  197. data/test/vagrant/command/base_test.rb +23 -0
  198. data/test/vagrant/command/group_base_test.rb +15 -0
  199. data/test/vagrant/command/helpers_test.rb +88 -0
  200. data/test/vagrant/command/package_test.rb +27 -0
  201. data/test/vagrant/config/base_test.rb +52 -0
  202. data/test/vagrant/config/error_recorder_test.rb +18 -0
  203. data/test/vagrant/config/ssh_test.rb +12 -0
  204. data/test/vagrant/config/vagrant_test.rb +35 -0
  205. data/test/vagrant/config/vm/provisioner_test.rb +92 -0
  206. data/test/vagrant/config/vm_test.rb +86 -0
  207. data/test/vagrant/config_test.rb +162 -0
  208. data/test/vagrant/data_store_test.rb +77 -0
  209. data/test/vagrant/downloaders/base_test.rb +28 -0
  210. data/test/vagrant/downloaders/file_test.rb +48 -0
  211. data/test/vagrant/downloaders/http_test.rb +80 -0
  212. data/test/vagrant/environment_test.rb +508 -0
  213. data/test/vagrant/errors_test.rb +42 -0
  214. data/test/vagrant/hosts/base_test.rb +46 -0
  215. data/test/vagrant/hosts/bsd_test.rb +53 -0
  216. data/test/vagrant/hosts/linux_test.rb +54 -0
  217. data/test/vagrant/plugin_test.rb +9 -0
  218. data/test/vagrant/provisioners/base_test.rb +63 -0
  219. data/test/vagrant/provisioners/chef_server_test.rb +186 -0
  220. data/test/vagrant/provisioners/chef_solo_test.rb +219 -0
  221. data/test/vagrant/provisioners/chef_test.rb +179 -0
  222. data/test/vagrant/provisioners/puppet_server_test.rb +68 -0
  223. data/test/vagrant/provisioners/puppet_test.rb +182 -0
  224. data/test/vagrant/provisioners/shell_test.rb +68 -0
  225. data/test/vagrant/ssh/session_test.rb +40 -0
  226. data/test/vagrant/ssh_test.rb +342 -0
  227. data/test/vagrant/systems/base_test.rb +18 -0
  228. data/test/vagrant/systems/linux_test.rb +114 -0
  229. data/test/vagrant/ui_test.rb +29 -0
  230. data/test/vagrant/util/busy_test.rb +106 -0
  231. data/test/vagrant/util/hash_with_indifferent_access_test.rb +39 -0
  232. data/test/vagrant/util/plain_logger_test.rb +17 -0
  233. data/test/vagrant/util/platform_test.rb +18 -0
  234. data/test/vagrant/util/resource_logger_test.rb +78 -0
  235. data/test/vagrant/util/retryable_test.rb +38 -0
  236. data/test/vagrant/util/stacked_proc_runner_test.rb +43 -0
  237. data/test/vagrant/util/template_renderer_test.rb +145 -0
  238. data/test/vagrant/vm_test.rb +291 -0
  239. data/vagrant.gemspec +36 -0
  240. metadata +442 -0
@@ -0,0 +1,28 @@
1
+ require "test_helper"
2
+
3
+ class BaseDownloaderTest < Test::Unit::TestCase
4
+ should "include the util class so subclasses have access to it" do
5
+ assert Vagrant::Downloaders::Base.include?(Vagrant::Util)
6
+ end
7
+
8
+ context "base instance" do
9
+ setup do
10
+ @env = vagrant_env
11
+ @base = Vagrant::Downloaders::Base.new(@env)
12
+ end
13
+
14
+ should "implement prepare which does nothing" do
15
+ assert_nothing_raised do
16
+ assert @base.respond_to?(:prepare)
17
+ @base.prepare("source")
18
+ end
19
+ end
20
+
21
+ should "implement download! which does nothing" do
22
+ assert_nothing_raised do
23
+ assert @base.respond_to?(:download!)
24
+ @base.download!("source", "destination")
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,48 @@
1
+ require "test_helper"
2
+
3
+ class FileDownloaderTest < Test::Unit::TestCase
4
+ setup do
5
+ @downloader, @tempfile = vagrant_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
+
13
+ assert_raises(Vagrant::Errors::DownloaderFileDoesntExist) {
14
+ @downloader.prepare(@uri)
15
+ }
16
+ end
17
+ end
18
+
19
+ context "downloading" do
20
+ setup do
21
+ clean_paths
22
+ end
23
+
24
+ should "cp the file" do
25
+ uri = tmp_path.join("foo_source")
26
+ dest = tmp_path.join("foo_dest")
27
+
28
+ # Create the source file, then "download" it
29
+ File.open(uri, "w+") { |f| f.write("FOO") }
30
+ File.open(dest, "w+") do |dest_file|
31
+ @downloader.download!(uri, dest_file)
32
+ end
33
+
34
+ # Finally, verify the destination file was properly created
35
+ assert File.file?(dest)
36
+ File.open(dest) do |f|
37
+ assert_equal "FOO", f.read
38
+ end
39
+ end
40
+ end
41
+
42
+ context "matching a uri" do
43
+ should "return true if the File exists on the file system" do
44
+ File.expects(:exists?).with('foo').returns(true)
45
+ assert Vagrant::Downloaders::File.match?('foo')
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,80 @@
1
+ require "test_helper"
2
+
3
+ class HttpDownloaderTest < Test::Unit::TestCase
4
+ setup do
5
+ @downloader, @tempfile = vagrant_mock_downloader(Vagrant::Downloaders::HTTP)
6
+ @downloader.stubs(:report_progress)
7
+ @downloader.stubs(:complete_progress)
8
+ @uri = "http://google.com/"
9
+ end
10
+
11
+ context "downloading" do
12
+ setup do
13
+ ENV["http_proxy"] = nil
14
+
15
+ @parsed_uri = URI.parse(@uri)
16
+ @http = Net::HTTP.new(@parsed_uri.host, @parsed_uri.port)
17
+ Net::HTTP.stubs(:new).returns(@http)
18
+ @http.stubs(:start)
19
+ end
20
+
21
+ should "create a proper net/http object" do
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)
31
+ @http.expects(:start)
32
+ @downloader.download!(@uri, @tempfile)
33
+ end
34
+
35
+ should "enable SSL if scheme is https" do
36
+ @uri = "https://google.com/"
37
+ @http.expects(:use_ssl=).with(true).once
38
+ @downloader.download!(@uri, @tempfile)
39
+ end
40
+
41
+ should "read the body of the response and place each segment into the file" do
42
+ h = mock("http")
43
+ response = mock("response")
44
+ response.stubs(:content_length)
45
+ segment = mock("segment")
46
+ segment.stubs(:length).returns(7)
47
+
48
+ @http.stubs(:start).yields(h)
49
+ h.expects(:request_get).with(@parsed_uri.request_uri).once.yields(response)
50
+ response.expects(:read_body).once.yields(segment)
51
+ @tempfile.expects(:write).with(segment).once
52
+
53
+ @downloader.download!(@uri, @tempfile)
54
+ end
55
+
56
+ should "error environment if invalid URL given" do
57
+ Net::HTTP.expects(:new).raises(SocketError.new)
58
+
59
+ assert_raises(Vagrant::Errors::DownloaderHTTPSocketError) {
60
+ @downloader.download!(@uri, @tempfile)
61
+ }
62
+ end
63
+ end
64
+
65
+ context "matching the uri" do
66
+ should "use extract to verify that the string is in fact a uri" do
67
+ URI.expects(:extract).returns(['foo'])
68
+ assert Vagrant::Downloaders::HTTP.match?('foo')
69
+ end
70
+
71
+ should "return false if there are no extract results" do
72
+ URI.expects(:extract).returns([])
73
+ assert !Vagrant::Downloaders::HTTP.match?('foo')
74
+ end
75
+ end
76
+
77
+ context "reporting progress" do
78
+ # TODO: Testing for this, probably
79
+ end
80
+ end
@@ -0,0 +1,508 @@
1
+ require "test_helper"
2
+ require "pathname"
3
+
4
+ class EnvironmentTest < Test::Unit::TestCase
5
+ setup do
6
+ @klass = Vagrant::Environment
7
+
8
+ clean_paths
9
+ end
10
+
11
+ context "class method check virtualbox version" do
12
+ setup do
13
+ VirtualBox.stubs(:version).returns("4.0.0")
14
+ end
15
+
16
+ should "not error and exit if everything is good" do
17
+ VirtualBox.expects(:version).returns("4.0.0")
18
+ assert_nothing_raised { @klass.check_virtualbox! }
19
+ end
20
+
21
+ should "error and exit if VirtualBox is not installed or detected" do
22
+ VirtualBox.expects(:version).returns(nil)
23
+ assert_raises(Vagrant::Errors::VirtualBoxNotDetected) { @klass.check_virtualbox! }
24
+ end
25
+
26
+ should "error and exit if VirtualBox is lower than version 4.0" do
27
+ version = "3.2.12r1041"
28
+ VirtualBox.expects(:version).returns(version)
29
+ assert_raises(Vagrant::Errors::VirtualBoxInvalidVersion) { @klass.check_virtualbox! }
30
+ end
31
+ end
32
+
33
+ context "initialization" do
34
+ should "set the cwd if given" do
35
+ cwd = "foobarbaz"
36
+ env = @klass.new(:cwd => cwd)
37
+ assert_equal Pathname.new(cwd), env.cwd
38
+ end
39
+
40
+ should "default to pwd if cwd is nil" do
41
+ env = @klass.new
42
+ assert_equal Pathname.new(Dir.pwd), env.cwd
43
+ end
44
+ end
45
+
46
+ context "paths" do
47
+ setup do
48
+ @env = vagrant_env
49
+ end
50
+
51
+ context "dotfile path" do
52
+ should "build up the dotfile out of the root path and the dotfile name" do
53
+ assert_equal @env.root_path.join(@env.config.vagrant.dotfile_name), @env.dotfile_path
54
+ end
55
+ end
56
+
57
+ context "home path" do
58
+ setup do
59
+ @env = @klass.new
60
+ end
61
+
62
+ should "return the home path if it loaded" do
63
+ ENV["VAGRANT_HOME"] = nil
64
+
65
+ expected = Pathname.new(File.expand_path(@klass::DEFAULT_HOME))
66
+ assert_equal expected, @env.home_path
67
+ end
68
+
69
+ should "return the home path set by the environmental variable" do
70
+ ENV["VAGRANT_HOME"] = "foo"
71
+
72
+ expected = Pathname.new(File.expand_path(ENV["VAGRANT_HOME"]))
73
+ assert_equal expected, @env.home_path
74
+ end
75
+ end
76
+
77
+ context "temp path" do
78
+ should "return the home path joined with 'tmp'" do
79
+ assert_equal @env.home_path.join("tmp"), @env.tmp_path
80
+ end
81
+ end
82
+
83
+ context "boxes path" do
84
+ should "return the home path joined with 'tmp'" do
85
+ assert_equal @env.home_path.join("boxes"), @env.boxes_path
86
+ end
87
+ end
88
+ end
89
+
90
+ context "resource" do
91
+ setup do
92
+ @env = vagrant_env
93
+ end
94
+
95
+ should "return 'vagrant' as a default" do
96
+ assert_equal 'vagrant', @env.resource
97
+ end
98
+
99
+ should "return the VM name if it is specified" do
100
+ @env.stubs(:vm).returns(mock("vm", :name => "foo"))
101
+ assert_equal "foo", @env.resource
102
+ end
103
+ end
104
+
105
+ context "primary VM helper" do
106
+ should "return the first VM if not multivm" do
107
+ env = vagrant_env
108
+ assert_equal env.vms[@klass::DEFAULT_VM], env.primary_vm
109
+ end
110
+
111
+ should "call and return the primary VM from the parent if has one" do
112
+ env = vagrant_env(vagrantfile(<<-vf))
113
+ config.vm.define(:web, :primary => true) do; end
114
+ config.vm.define :db do; end
115
+ vf
116
+
117
+ assert_equal :web, env.primary_vm.name
118
+ end
119
+
120
+ should "return nil if no VM is marked as primary" do
121
+ env = vagrant_env(vagrantfile(<<-vf))
122
+ config.vm.define :web
123
+ config.vm.define :db
124
+ config.vm.define :utility
125
+ vf
126
+
127
+ assert env.primary_vm.nil?
128
+ end
129
+ end
130
+
131
+ context "multivm? helper" do
132
+ should "return true if VM length greater than 1" do
133
+ env = vagrant_env(vagrantfile(<<-vf))
134
+ config.vm.define :web
135
+ config.vm.define :db
136
+ vf
137
+
138
+ assert env.multivm?
139
+ end
140
+
141
+ should "return true if VM length is 1 and a sub-VM is defined" do
142
+ env = vagrant_env(vagrantfile(<<-vf))
143
+ config.vm.define :web
144
+ vf
145
+
146
+ assert env.multivm?
147
+ end
148
+
149
+ should "return false if only default VM exists" do
150
+ assert !vagrant_env.multivm?
151
+ end
152
+ end
153
+
154
+ context "local data" do
155
+ should "lazy load the data store only once" do
156
+ result = { :foo => :bar }
157
+ Vagrant::DataStore.expects(:new).returns(result).once
158
+ env = vagrant_env
159
+ assert_equal result, env.local_data
160
+ assert_equal result, env.local_data
161
+ assert_equal result, env.local_data
162
+ end
163
+
164
+ should "return the parent's local data if a parent exists" do
165
+ env = vagrant_env(vagrantfile(<<-vf))
166
+ config.vm.define :web
167
+ config.vm.define :db
168
+ vf
169
+
170
+ env.local_data[:foo] = :bar
171
+
172
+ Vagrant::DataStore.expects(:new).never
173
+ assert_equal :bar, env.vms[:web].env.local_data[:foo]
174
+ end
175
+ end
176
+
177
+ context "accessing host" do
178
+ should "load the host once" do
179
+ env = @klass.new(:cwd => vagrantfile)
180
+ result = mock("result")
181
+ Vagrant::Hosts::Base.expects(:load).with(env, env.config.vagrant.host).once.returns(result)
182
+ assert_equal result, env.host
183
+ assert_equal result, env.host
184
+ assert_equal result, env.host
185
+ end
186
+ end
187
+
188
+ context "accessing actions" do
189
+ should "initialize the Action object with the given environment" do
190
+ env = @klass.new(:cwd => vagrant_app)
191
+ result = mock("result")
192
+ Vagrant::Action.expects(:new).with(env).returns(result).once
193
+ assert_equal result, env.actions
194
+ assert_equal result, env.actions
195
+ assert_equal result, env.actions
196
+ end
197
+ end
198
+
199
+ context "global data" do
200
+ should "lazy load the data store only once" do
201
+ env = vagrant_env
202
+ store = env.global_data
203
+
204
+ assert env.global_data.equal?(store)
205
+ assert env.global_data.equal?(store)
206
+ assert env.global_data.equal?(store)
207
+ end
208
+
209
+ should "return the parent's global data if a parent exists" do
210
+ env = vagrant_env(vagrantfile(<<-vf))
211
+ config.vm.define :web
212
+ config.vm.define :db
213
+ vf
214
+
215
+ result = env.global_data
216
+ assert env.vms[:web].env.global_data.equal?(result)
217
+ end
218
+ end
219
+
220
+ context "loading logger" do
221
+ should "lazy load the logger only once" do
222
+ result = Vagrant::Util::ResourceLogger.new("vagrant", vagrant_env)
223
+ Vagrant::Util::ResourceLogger.expects(:new).returns(result).once
224
+ env = vagrant_env
225
+ assert_equal result, env.logger
226
+ assert_equal result, env.logger
227
+ assert_equal result, env.logger
228
+ end
229
+
230
+ should "return the parent's logger if a parent exists" do
231
+ env = vagrant_env(vagrantfile(<<-vf))
232
+ config.vm.define :web
233
+ config.vm.define :db
234
+ vf
235
+
236
+ result = env.logger
237
+
238
+ Vagrant::Util::ResourceLogger.expects(:new).never
239
+ assert env.vms[:web].env.logger.equal?(result)
240
+ end
241
+ end
242
+
243
+ context "loading the root path" do
244
+ should "should walk the parent directories looking for rootfile" do
245
+ paths = [Pathname.new("/foo/bar/baz"),
246
+ Pathname.new("/foo/bar"),
247
+ Pathname.new("/foo"),
248
+ Pathname.new("/")
249
+ ]
250
+
251
+ search_seq = sequence("search_seq")
252
+ paths.each do |path|
253
+ File.expects(:exist?).with(path.join(@klass::ROOTFILE_NAME).to_s).returns(false).in_sequence(search_seq)
254
+ File.expects(:exist?).with(path).returns(true).in_sequence(search_seq) if !path.root?
255
+ end
256
+
257
+ assert !@klass.new(:cwd => paths.first).root_path
258
+ end
259
+
260
+ should "should set the path for the rootfile" do
261
+ path = Pathname.new(File.expand_path("/foo"))
262
+ File.expects(:exist?).with(path.join(@klass::ROOTFILE_NAME).to_s).returns(true)
263
+
264
+ assert_equal path, @klass.new(:cwd => path).root_path
265
+ end
266
+
267
+ should "not infinite loop on relative paths" do
268
+ assert @klass.new(:cwd => "../test").root_path.nil?
269
+ end
270
+
271
+ should "only load the root path once" do
272
+ env = @klass.new
273
+ File.expects(:exist?).with(env.cwd.join(@klass::ROOTFILE_NAME).to_s).returns(true).once
274
+
275
+ assert_equal env.cwd, env.root_path
276
+ assert_equal env.cwd, env.root_path
277
+ assert_equal env.cwd, env.root_path
278
+ end
279
+
280
+ should "only load the root path once even if nil" do
281
+ File.stubs(:exist?).returns(false)
282
+
283
+ env = @klass.new
284
+ assert env.root_path.nil?
285
+ assert env.root_path.nil?
286
+ assert env.root_path.nil?
287
+ end
288
+ end
289
+
290
+ context "accessing the configuration" do
291
+ should "load the environment if its not already loaded" do
292
+ env = @klass.new(:cwd => vagrantfile)
293
+ env.expects(:load!).once
294
+ env.config
295
+ end
296
+
297
+ should "not load the environment if its already loaded" do
298
+ env = vagrant_env
299
+ env.expects(:load!).never
300
+ env.config
301
+ end
302
+ end
303
+
304
+ context "accessing the box collection" do
305
+ should "create a box collection representing the environment" do
306
+ env = vagrant_env
307
+ assert env.boxes.is_a?(Vagrant::BoxCollection)
308
+ assert_equal env, env.boxes.env
309
+ end
310
+
311
+ should "not load the environment if its already loaded" do
312
+ env = vagrant_env
313
+ env.expects(:load!).never
314
+ env.boxes
315
+ end
316
+
317
+ should "return the parent's box collection if it has one" do
318
+ env = vagrant_env(vagrantfile(<<-vf))
319
+ config.vm.define :web
320
+ config.vm.define :db
321
+ vf
322
+
323
+ assert env.vms[:web].env.boxes.equal?(env.boxes)
324
+ end
325
+ end
326
+
327
+ context "accessing the current box" do
328
+ should "return the box that is specified in the config" do
329
+ vagrant_box("foo")
330
+ env = vagrant_env(vagrantfile(<<-vf))
331
+ config.vm.box = "foo"
332
+ vf
333
+
334
+ assert env.box
335
+ assert_equal "foo", env.box.name
336
+ end
337
+ end
338
+
339
+ context "accessing the VMs hash" do
340
+ should "load the environment if its not already loaded" do
341
+ env = @klass.new(:cwd => vagrantfile)
342
+ assert !env.loaded?
343
+ env.vms
344
+ assert env.loaded?
345
+ end
346
+
347
+ should "not load the environment if its already loaded" do
348
+ env = vagrant_env
349
+ env.expects(:load!).never
350
+ env.vms
351
+ end
352
+
353
+ should "return the parent's VMs hash if it has one" do
354
+ env = vagrant_env(vagrantfile(<<-vf))
355
+ config.vm.define :web
356
+ config.vm.define :db
357
+ vf
358
+
359
+ assert env.vms[:web].env.vms.equal?(env.vms)
360
+ end
361
+ end
362
+
363
+ context "loading" do
364
+ context "overall load method" do
365
+ should "load! should call proper sequence and return itself" do
366
+ env = @klass.new(:cwd => vagrantfile)
367
+ call_seq = sequence("call_sequence")
368
+ @klass.expects(:check_virtualbox!).once.in_sequence(call_seq)
369
+ env.expects(:load_config!).once.in_sequence(call_seq)
370
+ assert_equal env, env.load!
371
+ end
372
+ end
373
+
374
+ context "loading config" do
375
+ setup do
376
+ clean_paths
377
+ @env = @klass.new(:cwd => vagrantfile)
378
+ end
379
+
380
+ def create_box_vagrantfile
381
+ vagrantfile(vagrant_box("box"), <<-FILE)
382
+ config.package.name = "box.box"
383
+ config.vm.base_mac = "set"
384
+ FILE
385
+ end
386
+
387
+ def create_home_vagrantfile
388
+ vagrantfile(home_path, 'config.package.name = "home.box"')
389
+ end
390
+
391
+ def create_root_vagrantfile
392
+ vagrantfile(@env.root_path, 'config.package.name = "root.box"')
393
+ end
394
+
395
+ should "load from the project root" do
396
+ assert_equal "package.box", @env.config.package.name
397
+ end
398
+
399
+ should "load from box if specified" do
400
+ create_box_vagrantfile
401
+ vagrantfile(@env.root_path, "config.vm.box = 'box'")
402
+
403
+ assert_equal "box.box", @env.primary_vm.env.config.package.name
404
+ end
405
+
406
+ should "load from home path if exists" do
407
+ create_home_vagrantfile
408
+ assert_equal "home.box", @env.config.package.name
409
+ end
410
+
411
+ should "load from root path" do
412
+ create_home_vagrantfile
413
+ create_root_vagrantfile
414
+ assert_equal "root.box", @env.config.package.name
415
+ end
416
+
417
+ should "load from a sub-vm configuration if environment represents a VM" do
418
+ create_home_vagrantfile
419
+ create_box_vagrantfile
420
+ vagrantfile(@env.root_path, <<-vf)
421
+ config.package.name = "root.box"
422
+ config.vm.define :web do |web|
423
+ web.vm.box = "box"
424
+ web.package.name = "web.box"
425
+ end
426
+ vf
427
+
428
+ assert_equal "root.box", @env.config.package.name
429
+ assert_equal "web.box", @env.vms[:web].env.config.package.name
430
+ assert_equal "set", @env.vms[:web].env.config.vm.base_mac
431
+ end
432
+
433
+ should "reload the logger after executing" do
434
+ @env.load_config!
435
+ assert @env.instance_variable_get(:@logger).nil?
436
+ end
437
+
438
+ should "be able to reload config" do
439
+ vagrantfile(@env.root_path, "config.vm.box = 'box'")
440
+
441
+ # First load the config normally
442
+ @env.load_config!
443
+ assert_equal "box", @env.config.vm.box
444
+ assert_not_equal "set", @env.config.vm.base_mac
445
+
446
+ # Modify the Vagrantfile and reload it, then verify new results
447
+ # are available
448
+ vagrantfile(@env.root_path, "config.vm.base_mac = 'set'")
449
+ @env.reload_config!
450
+ assert_equal "set", @env.config.vm.base_mac
451
+ end
452
+ end
453
+
454
+ context "loading home directory" do
455
+ setup do
456
+ @env = vagrant_env
457
+
458
+ File.stubs(:directory?).returns(true)
459
+ FileUtils.stubs(:mkdir_p)
460
+ end
461
+
462
+ should "create each directory if it doesn't exist" do
463
+ create_seq = sequence("create_seq")
464
+ File.stubs(:directory?).returns(false)
465
+ @klass::HOME_SUBDIRS.each do |subdir|
466
+ FileUtils.expects(:mkdir_p).with(@env.home_path.join(subdir)).in_sequence(create_seq)
467
+ end
468
+
469
+ @env.load_home_directory!
470
+ end
471
+
472
+ should "not create directories if they exist" do
473
+ File.stubs(:directory?).returns(true)
474
+ FileUtils.expects(:mkdir_p).never
475
+ @env.load_home_directory!
476
+ end
477
+ end
478
+
479
+ context "loading the UUID out from the persisted dotfile" do
480
+ setup do
481
+ @env = vagrant_env
482
+ end
483
+
484
+ should "load all the VMs from the dotfile" do
485
+ @env.local_data[:active] = { "foo" => "bar", "bar" => "baz" }
486
+
487
+ results = {}
488
+ @env.local_data[:active].each do |key, value|
489
+ vm = mock("vm#{key}")
490
+ Vagrant::VM.expects(:find).with(value, @env, key.to_sym).returns(vm)
491
+ results[key.to_sym] = vm
492
+ end
493
+
494
+ returned = @env.load_vms!
495
+
496
+ results.each do |key, value|
497
+ assert_equal value, returned[key]
498
+ end
499
+ end
500
+
501
+ should "uuid should be nil if local data contains nothing" do
502
+ assert @env.local_data.empty? # sanity
503
+ @env.load_vms!
504
+ assert_nil @env.vm
505
+ end
506
+ end
507
+ end
508
+ end