fixed-vagrant 0.7.4.dev

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. data/.gitignore +15 -0
  2. data/.yardopts +1 -0
  3. data/CHANGELOG.md +272 -0
  4. data/Gemfile +21 -0
  5. data/LICENSE +21 -0
  6. data/README.md +75 -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 +134 -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 +8 -0
  104. data/lib/vagrant/hosts/arch.rb +27 -0
  105. data/lib/vagrant/hosts/base.rb +78 -0
  106. data/lib/vagrant/hosts/bsd.rb +52 -0
  107. data/lib/vagrant/hosts/linux.rb +47 -0
  108. data/lib/vagrant/plugin.rb +57 -0
  109. data/lib/vagrant/provisioners.rb +9 -0
  110. data/lib/vagrant/provisioners/base.rb +63 -0
  111. data/lib/vagrant/provisioners/chef.rb +130 -0
  112. data/lib/vagrant/provisioners/chef_server.rb +103 -0
  113. data/lib/vagrant/provisioners/chef_solo.rb +142 -0
  114. data/lib/vagrant/provisioners/puppet.rb +137 -0
  115. data/lib/vagrant/provisioners/puppet_server.rb +55 -0
  116. data/lib/vagrant/provisioners/shell.rb +52 -0
  117. data/lib/vagrant/ssh.rb +173 -0
  118. data/lib/vagrant/ssh/session.rb +125 -0
  119. data/lib/vagrant/systems.rb +11 -0
  120. data/lib/vagrant/systems/base.rb +87 -0
  121. data/lib/vagrant/systems/debian.rb +36 -0
  122. data/lib/vagrant/systems/freebsd.rb +84 -0
  123. data/lib/vagrant/systems/gentoo.rb +27 -0
  124. data/lib/vagrant/systems/linux.rb +83 -0
  125. data/lib/vagrant/systems/linux/config.rb +21 -0
  126. data/lib/vagrant/systems/linux/error.rb +9 -0
  127. data/lib/vagrant/systems/redhat.rb +39 -0
  128. data/lib/vagrant/systems/solaris.rb +62 -0
  129. data/lib/vagrant/systems/ubuntu.rb +17 -0
  130. data/lib/vagrant/test_helpers.rb +128 -0
  131. data/lib/vagrant/ui.rb +69 -0
  132. data/lib/vagrant/util.rb +13 -0
  133. data/lib/vagrant/util/busy.rb +59 -0
  134. data/lib/vagrant/util/hash_with_indifferent_access.rb +63 -0
  135. data/lib/vagrant/util/plain_logger.rb +25 -0
  136. data/lib/vagrant/util/platform.rb +65 -0
  137. data/lib/vagrant/util/resource_logger.rb +63 -0
  138. data/lib/vagrant/util/retryable.rb +25 -0
  139. data/lib/vagrant/util/stacked_proc_runner.rb +35 -0
  140. data/lib/vagrant/util/template_renderer.rb +83 -0
  141. data/lib/vagrant/version.rb +6 -0
  142. data/lib/vagrant/vm.rb +177 -0
  143. data/templates/chef_server_client.erb +18 -0
  144. data/templates/chef_solo_solo.erb +19 -0
  145. data/templates/commands/init/Vagrantfile.erb +82 -0
  146. data/templates/config/validation_failed.erb +7 -0
  147. data/templates/locales/en.yml +535 -0
  148. data/templates/network_entry_debian.erb +8 -0
  149. data/templates/network_entry_gentoo.erb +5 -0
  150. data/templates/network_entry_redhat.erb +8 -0
  151. data/templates/nfs/exports.erb +5 -0
  152. data/templates/nfs/exports_linux.erb +5 -0
  153. data/templates/package_Vagrantfile.erb +11 -0
  154. data/templates/ssh_config.erb +9 -0
  155. data/test/locales/en.yml +8 -0
  156. data/test/test_helper.rb +26 -0
  157. data/test/vagrant/action/box/destroy_test.rb +18 -0
  158. data/test/vagrant/action/box/download_test.rb +125 -0
  159. data/test/vagrant/action/box/package_test.rb +25 -0
  160. data/test/vagrant/action/box/unpackage_test.rb +84 -0
  161. data/test/vagrant/action/box/verify_test.rb +30 -0
  162. data/test/vagrant/action/builder_test.rb +203 -0
  163. data/test/vagrant/action/env/set_test.rb +24 -0
  164. data/test/vagrant/action/environment_test.rb +27 -0
  165. data/test/vagrant/action/general/package_test.rb +268 -0
  166. data/test/vagrant/action/general/validate_test.rb +31 -0
  167. data/test/vagrant/action/vm/boot_test.rb +66 -0
  168. data/test/vagrant/action/vm/check_box_test.rb +56 -0
  169. data/test/vagrant/action/vm/check_guest_additions_test.rb +9 -0
  170. data/test/vagrant/action/vm/clean_machine_folder_test.rb +84 -0
  171. data/test/vagrant/action/vm/clear_forwarded_ports_test.rb +72 -0
  172. data/test/vagrant/action/vm/clear_nfs_exports_test.rb +22 -0
  173. data/test/vagrant/action/vm/clear_shared_folders_test.rb +49 -0
  174. data/test/vagrant/action/vm/customize_test.rb +30 -0
  175. data/test/vagrant/action/vm/destroy_test.rb +25 -0
  176. data/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb +49 -0
  177. data/test/vagrant/action/vm/discard_state_test.rb +45 -0
  178. data/test/vagrant/action/vm/export_test.rb +107 -0
  179. data/test/vagrant/action/vm/forward_ports_helpers_test.rb +70 -0
  180. data/test/vagrant/action/vm/forward_ports_test.rb +194 -0
  181. data/test/vagrant/action/vm/halt_test.rb +79 -0
  182. data/test/vagrant/action/vm/host_name_test.rb +36 -0
  183. data/test/vagrant/action/vm/import_test.rb +66 -0
  184. data/test/vagrant/action/vm/match_mac_address_test.rb +36 -0
  185. data/test/vagrant/action/vm/network_test.rb +286 -0
  186. data/test/vagrant/action/vm/nfs_helpers_test.rb +26 -0
  187. data/test/vagrant/action/vm/nfs_test.rb +260 -0
  188. data/test/vagrant/action/vm/package_test.rb +25 -0
  189. data/test/vagrant/action/vm/package_vagrantfile_test.rb +46 -0
  190. data/test/vagrant/action/vm/provision_test.rb +90 -0
  191. data/test/vagrant/action/vm/resume_test.rb +35 -0
  192. data/test/vagrant/action/vm/share_folders_test.rb +139 -0
  193. data/test/vagrant/action/vm/suspend_test.rb +35 -0
  194. data/test/vagrant/action/warden_test.rb +119 -0
  195. data/test/vagrant/action_test.rb +89 -0
  196. data/test/vagrant/box_collection_test.rb +45 -0
  197. data/test/vagrant/box_test.rb +74 -0
  198. data/test/vagrant/cli_test.rb +35 -0
  199. data/test/vagrant/command/base_test.rb +23 -0
  200. data/test/vagrant/command/group_base_test.rb +15 -0
  201. data/test/vagrant/command/helpers_test.rb +88 -0
  202. data/test/vagrant/command/package_test.rb +27 -0
  203. data/test/vagrant/config/base_test.rb +52 -0
  204. data/test/vagrant/config/error_recorder_test.rb +18 -0
  205. data/test/vagrant/config/ssh_test.rb +12 -0
  206. data/test/vagrant/config/vagrant_test.rb +35 -0
  207. data/test/vagrant/config/vm/provisioner_test.rb +92 -0
  208. data/test/vagrant/config/vm_test.rb +86 -0
  209. data/test/vagrant/config_test.rb +162 -0
  210. data/test/vagrant/data_store_test.rb +77 -0
  211. data/test/vagrant/downloaders/base_test.rb +28 -0
  212. data/test/vagrant/downloaders/file_test.rb +48 -0
  213. data/test/vagrant/downloaders/http_test.rb +80 -0
  214. data/test/vagrant/environment_test.rb +508 -0
  215. data/test/vagrant/errors_test.rb +42 -0
  216. data/test/vagrant/hosts/base_test.rb +46 -0
  217. data/test/vagrant/hosts/bsd_test.rb +53 -0
  218. data/test/vagrant/hosts/linux_test.rb +54 -0
  219. data/test/vagrant/plugin_test.rb +9 -0
  220. data/test/vagrant/provisioners/base_test.rb +63 -0
  221. data/test/vagrant/provisioners/chef_server_test.rb +188 -0
  222. data/test/vagrant/provisioners/chef_solo_test.rb +219 -0
  223. data/test/vagrant/provisioners/chef_test.rb +179 -0
  224. data/test/vagrant/provisioners/puppet_server_test.rb +68 -0
  225. data/test/vagrant/provisioners/puppet_test.rb +182 -0
  226. data/test/vagrant/provisioners/shell_test.rb +68 -0
  227. data/test/vagrant/ssh/session_test.rb +40 -0
  228. data/test/vagrant/ssh_test.rb +342 -0
  229. data/test/vagrant/systems/base_test.rb +18 -0
  230. data/test/vagrant/systems/linux_test.rb +114 -0
  231. data/test/vagrant/ui_test.rb +29 -0
  232. data/test/vagrant/util/busy_test.rb +106 -0
  233. data/test/vagrant/util/hash_with_indifferent_access_test.rb +39 -0
  234. data/test/vagrant/util/plain_logger_test.rb +17 -0
  235. data/test/vagrant/util/platform_test.rb +18 -0
  236. data/test/vagrant/util/resource_logger_test.rb +78 -0
  237. data/test/vagrant/util/retryable_test.rb +50 -0
  238. data/test/vagrant/util/stacked_proc_runner_test.rb +43 -0
  239. data/test/vagrant/util/template_renderer_test.rb +145 -0
  240. data/test/vagrant/vm_test.rb +291 -0
  241. data/vagrant.gemspec +36 -0
  242. metadata +514 -0
@@ -0,0 +1,182 @@
1
+ require "test_helper"
2
+
3
+ class PuppetProvisionerTest < Test::Unit::TestCase
4
+ setup do
5
+ clean_paths
6
+
7
+ @klass = Vagrant::Provisioners::Puppet
8
+
9
+ @action_env = Vagrant::Action::Environment.new(vagrant_env.vms[:default].env)
10
+
11
+ @config = @klass::Config.new
12
+ @config.top = Vagrant::Config::Top.new(@action_env.env)
13
+ @config.top.vm.box = "foo"
14
+ @action = @klass.new(@action_env, @config)
15
+ @env = @action.env
16
+ @vm = @action.vm
17
+ end
18
+
19
+ context "config" do
20
+ setup do
21
+ @errors = Vagrant::Config::ErrorRecorder.new
22
+
23
+ # Set a box
24
+ @config.top.vm.box = "foo"
25
+
26
+ # Start in a valid state (verified by the first test)
27
+ @config.expanded_manifests_path.mkdir
28
+ File.open(@config.expanded_manifests_path.join(@config.computed_manifest_file), "w") { |f| f.puts "HELLO" }
29
+ end
30
+
31
+ should "expand the manifest path relative to the root path" do
32
+ assert_equal File.expand_path(@config.manifests_path, @env.root_path), @config.expanded_manifests_path.to_s
33
+ end
34
+
35
+ should "default the manifest file to the box name" do
36
+ assert_equal "#{@config.top.vm.box}.pp", @config.computed_manifest_file
37
+ end
38
+
39
+ should "use the custom manifest file if set" do
40
+ @config.manifest_file = "woot.pp"
41
+ assert_equal "woot.pp", @config.computed_manifest_file
42
+ end
43
+
44
+ should "return an empty array if no module path is set" do
45
+ @config.module_path = nil
46
+ assert_equal [], @config.expanded_module_paths
47
+ end
48
+
49
+ should "return array of module paths expanded relative to root path" do
50
+ @config.module_path = "foo"
51
+
52
+ result = @config.expanded_module_paths
53
+ assert result.is_a?(Array)
54
+ assert_equal 1, result.length
55
+ assert_equal File.expand_path(@config.module_path, @env.root_path), result[0].to_s
56
+ end
57
+
58
+ should "be valid" do
59
+ @config.validate(@errors)
60
+ assert @errors.errors.empty?
61
+ end
62
+
63
+ should "be invalid if the manifests path doesn't exist" do
64
+ @config.expanded_manifests_path.rmtree
65
+ @config.validate(@errors)
66
+ assert !@errors.errors.empty?
67
+ end
68
+
69
+ should "be invalid if a custom manifests path doesn't exist" do
70
+ @config.manifests_path = "dont_exist"
71
+ @config.validate(@errors)
72
+ assert !@errors.errors.empty?
73
+ end
74
+
75
+ should "be invalid if the manifest file doesn't exist" do
76
+ @config.expanded_manifests_path.join(@config.computed_manifest_file).unlink
77
+ @config.validate(@errors)
78
+ assert !@errors.errors.empty?
79
+ end
80
+
81
+ should "be invalid if a specified module path doesn't exist" do
82
+ @config.module_path = "foo"
83
+ @config.validate(@errors)
84
+ assert !@errors.errors.empty?
85
+ end
86
+
87
+ should "be valid if all module paths exist" do
88
+ @config.module_path = "foo"
89
+ @config.expanded_module_paths.first.mkdir
90
+ @config.validate(@errors)
91
+ assert @errors.errors.empty?
92
+ end
93
+ end
94
+
95
+ context "preparing" do
96
+ should "share manifests" do
97
+ pre_seq = sequence("prepare")
98
+ @action.expects(:set_module_paths).once.in_sequence(pre_seq)
99
+ @action.expects(:share_manifests).once.in_sequence(pre_seq)
100
+ @action.expects(:share_module_paths).once.in_sequence(pre_seq)
101
+ @action.prepare
102
+ end
103
+ end
104
+
105
+ context "provisioning" do
106
+ should "run the proper sequence of methods in order" do
107
+ prov_seq = sequence("prov_seq")
108
+ @action.expects(:verify_binary).with("puppet").once.in_sequence(prov_seq)
109
+ @action.expects(:run_puppet_client).once.in_sequence(prov_seq)
110
+ @action.provision!
111
+ end
112
+ end
113
+
114
+ context "share manifests folder" do
115
+ should "share manifest folder" do
116
+ @env.config.vm.expects(:share_folder).with("manifests", @action.manifests_guest_path, @config.expanded_manifests_path)
117
+ @action.share_manifests
118
+ end
119
+ end
120
+
121
+ context "sharing module paths" do
122
+ should "share all the module paths" do
123
+ @config.module_path = ["foo", "bar"]
124
+ @config.expanded_module_paths.each_with_index do |path, i|
125
+ @env.config.vm.expects(:share_folder).with("v-pp-m#{i}", File.join(@config.pp_path, "modules-#{i}"), path)
126
+ end
127
+
128
+ @action.set_module_paths
129
+ @action.share_module_paths
130
+ end
131
+ end
132
+
133
+ context "verifying binary" do
134
+ setup do
135
+ @ssh = mock("ssh")
136
+ @vm.ssh.stubs(:execute).yields(@ssh)
137
+ end
138
+
139
+ should "verify binary exists" do
140
+ binary = "foo"
141
+ @ssh.expects(:sudo!).with("which #{binary}", anything)
142
+ @action.verify_binary(binary)
143
+ end
144
+ end
145
+
146
+ context "running puppet client" do
147
+ setup do
148
+ @ssh = mock("ssh")
149
+ @vm.ssh.stubs(:execute).yields(@ssh)
150
+ @action.set_module_paths
151
+ end
152
+
153
+ def expect_puppet_command(command)
154
+ @ssh.expects(:sudo!).with(["cd #{@action.manifests_guest_path}", command])
155
+ end
156
+
157
+ should "cd into the pp_path directory and run puppet" do
158
+ expect_puppet_command("puppet #{@config.computed_manifest_file}")
159
+ @action.run_puppet_client
160
+ end
161
+
162
+ should "cd into the pp_path directory and run puppet with given options when given as an array" do
163
+ @config.options = ["--modulepath", "modules", "--verbose"]
164
+ expect_puppet_command("puppet --modulepath modules --verbose #{@config.computed_manifest_file}")
165
+ @action.run_puppet_client
166
+ end
167
+
168
+ should "cd into the pp_path directory and run puppet with the options when given as a string" do
169
+ @config.options = "--modulepath modules --verbose"
170
+ expect_puppet_command("puppet --modulepath modules --verbose #{@config.computed_manifest_file}")
171
+ @action.run_puppet_client
172
+ end
173
+
174
+ should "cd into the pp_path and run puppet with module paths if set" do
175
+ @config.module_path = "foo"
176
+ expect_puppet_command("puppet --modulepath '#{File.join(@config.pp_path, 'modules-0')}' #{@config.computed_manifest_file}")
177
+
178
+ @action.set_module_paths
179
+ @action.run_puppet_client
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,68 @@
1
+ require "test_helper"
2
+
3
+ class ShellProvisionerTest < Test::Unit::TestCase
4
+ setup do
5
+ clean_paths
6
+
7
+ @klass = Vagrant::Provisioners::Shell
8
+ @action_env = Vagrant::Action::Environment.new(vagrant_env.vms[:default].env)
9
+ @config = @klass::Config.new
10
+ @config.top = Vagrant::Config::Top.new(@action_env.env)
11
+ @action = @klass.new(@action_env, @config)
12
+
13
+ @config.path = "foo"
14
+ end
15
+
16
+ context "config" do
17
+ setup do
18
+ @errors = Vagrant::Config::ErrorRecorder.new
19
+
20
+ # Start in a valid state (verified by a test below)
21
+ @config.path = "foo"
22
+ File.open(@config.expanded_path, "w") { |f| f.puts "HELLO" }
23
+ end
24
+
25
+ should "be valid" do
26
+ @config.validate(@errors)
27
+ assert @errors.errors.empty?
28
+ end
29
+
30
+ should "be invalid if the path is not set" do
31
+ @config.path = nil
32
+
33
+ @config.validate(@errors)
34
+ assert !@errors.errors.empty?
35
+ end
36
+
37
+ should "be invalid if the path does not exist" do
38
+ @config.path = "bar"
39
+
40
+ @config.validate(@errors)
41
+ assert !@errors.errors.empty?
42
+ end
43
+
44
+ should "be invalid if the upload path is not set" do
45
+ @config.upload_path = nil
46
+
47
+ @config.validate(@errors)
48
+ assert !@errors.errors.empty?
49
+ end
50
+ end
51
+
52
+ context "provisioning" do
53
+ setup do
54
+ @ssh = mock("ssh")
55
+ @action.vm.ssh.stubs(:execute).yields(@ssh)
56
+ end
57
+
58
+ should "upload the file, chmod, then execute it" do
59
+ commands = ["chmod +x #{@config.upload_path}", @config.upload_path]
60
+
61
+ p_seq = sequence("provisioning")
62
+ @action.vm.ssh.expects(:upload!).with(@config.expanded_path.to_s, @config.upload_path).in_sequence(p_seq)
63
+ @ssh.expects(:sudo!).with(commands).in_sequence(p_seq)
64
+
65
+ @action.provision!
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,40 @@
1
+ require "test_helper"
2
+
3
+ class SshSessionTest < Test::Unit::TestCase
4
+ setup do
5
+ @session = mock("session")
6
+ @env = vagrant_env
7
+
8
+ @klass = Vagrant::SSH::Session
9
+ @instance = @klass.new(@session, @env)
10
+ end
11
+
12
+ context "exec!" do
13
+ should "retry 5 times" do
14
+ @session.expects(:open_channel).times(5).raises(IOError)
15
+ assert_raises(IOError) {
16
+ @instance.exec!("foo")
17
+ }
18
+ end
19
+ end
20
+
21
+ context "checking exit status" do
22
+ should "raise an ActionException if its non-zero" do
23
+ assert_raises(Vagrant::Errors::VagrantError) {
24
+ @instance.check_exit_status(1, "foo")
25
+ }
26
+ end
27
+
28
+ should "raise the given exception if specified" do
29
+ assert_raises(Vagrant::Errors::BaseVMNotFound) {
30
+ @instance.check_exit_status(1, "foo", :_error_class => Vagrant::Errors::BaseVMNotFound)
31
+ }
32
+ end
33
+
34
+ should "raise nothing if its zero" do
35
+ assert_nothing_raised {
36
+ @instance.check_exit_status(0, "foo")
37
+ }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,342 @@
1
+ require "test_helper"
2
+
3
+ class SshTest < Test::Unit::TestCase
4
+ def mock_ssh
5
+ @env = vagrant_env.vms[:default].env
6
+ @network_adapters = []
7
+ @vm = mock("vm")
8
+ @vm.stubs(:network_adapters).returns(@network_adapters)
9
+ @env.vm.stubs(:vm).returns(@vm)
10
+
11
+ @ssh = Vagrant::SSH.new(@env)
12
+ end
13
+
14
+ setup do
15
+ VirtualBox.stubs(:version).returns("4.0.0")
16
+ end
17
+
18
+ context "connecting to external SSH" do
19
+ setup do
20
+ mock_ssh
21
+ @ssh.stubs(:check_key_permissions)
22
+ @ssh.stubs(:port).returns(2222)
23
+ Kernel.stubs(:exec)
24
+ Kernel.stubs(:system).returns(true)
25
+
26
+ Vagrant::Util::Platform.stubs(:leopard?).returns(false)
27
+ end
28
+
29
+ should "raise an exception if SSH is not found" do
30
+ Kernel.stubs(:system).returns(false)
31
+ Kernel.expects(:system).returns(false).with() do |command|
32
+ assert command =~ /^which ssh/
33
+ true
34
+ end
35
+
36
+ assert_raises(Vagrant::Errors::SSHUnavailable) {
37
+ @ssh.connect
38
+ }
39
+ end
40
+
41
+ should "check key permissions prior to exec" do
42
+ exec_seq = sequence("exec_seq")
43
+ @ssh.expects(:check_key_permissions).with(@env.config.ssh.private_key_path).once.in_sequence(exec_seq)
44
+ Kernel.expects(:exec).in_sequence(exec_seq)
45
+ @ssh.connect
46
+ end
47
+
48
+ should "call exec with defaults when no options are supplied" do
49
+ ssh_exec_expect(@ssh.port,
50
+ @env.config.ssh.private_key_path,
51
+ @env.config.ssh.username,
52
+ @env.config.ssh.host)
53
+ @ssh.connect
54
+ end
55
+
56
+ should "call exec with supplied params" do
57
+ args = {:username => 'bar', :private_key_path => 'baz', :host => 'bak'}
58
+ ssh_exec_expect(@ssh.port, args[:private_key_path], args[:username], args[:host])
59
+ @ssh.connect(args)
60
+ end
61
+
62
+ should "add forward agent option if enabled" do
63
+ @env.config.ssh.forward_agent = true
64
+ ssh_exec_expect(@ssh.port,
65
+ @env.config.ssh.private_key_path,
66
+ @env.config.ssh.username,
67
+ @env.config.ssh.host) do |args|
68
+ assert args =~ /-o ForwardAgent=yes/
69
+ end
70
+ @ssh.connect
71
+ end
72
+
73
+ should "add forward X11 option if enabled" do
74
+ @env.config.ssh.forward_x11 = true
75
+ ssh_exec_expect(@ssh.port,
76
+ @env.config.ssh.private_key_path,
77
+ @env.config.ssh.username,
78
+ @env.config.ssh.host) do |args|
79
+ assert args =~ /-o ForwardX11=yes/
80
+ end
81
+ @ssh.connect
82
+ end
83
+
84
+ context "on leopard" do
85
+ setup do
86
+ Vagrant::Util::Platform.stubs(:leopard?).returns(true)
87
+ end
88
+
89
+ teardown do
90
+ Vagrant::Util::Platform.stubs(:leopard?).returns(false)
91
+ end
92
+
93
+ should "fork, exec, and wait" do
94
+ pid = mock("pid")
95
+ @ssh.expects(:fork).once.returns(pid)
96
+ Process.expects(:wait).with(pid)
97
+
98
+ @ssh.connect
99
+ end
100
+ end
101
+
102
+ context "checking windows" do
103
+ teardown do
104
+ Mario::Platform.forced = Mario::Platform::Linux
105
+ end
106
+
107
+ should "error and exit if the platform is windows" do
108
+ Mario::Platform.forced = Mario::Platform::Windows7
109
+ assert_raises(Vagrant::Errors::SSHUnavailableWindows) { @ssh.connect }
110
+ end
111
+
112
+ should "not error and exit if the platform is anything other that windows" do
113
+ Mario::Platform.forced = Mario::Platform::Linux
114
+ assert_nothing_raised { @ssh.connect }
115
+ end
116
+ end
117
+
118
+ def ssh_exec_expect(port, key_path, uname, host)
119
+ Kernel.expects(:exec).with() do |arg|
120
+ assert arg =~ /^ssh/, "ssh command expected"
121
+ assert arg =~ /-p #{port}/, "-p #{port} expected"
122
+ assert arg =~ /-i #{key_path}/, "-i #{key_path} expected"
123
+ assert arg =~ /#{uname}@#{host}/, "#{uname}@{host} expected"
124
+ yield arg if block_given?
125
+ true
126
+ end
127
+ end
128
+ end
129
+
130
+ context "executing ssh commands" do
131
+ setup do
132
+ mock_ssh
133
+ @ssh.stubs(:check_key_permissions)
134
+ @ssh.stubs(:port).returns(80)
135
+ end
136
+
137
+ should "check key permissions then attempt to start connection" do
138
+ seq = sequence("seq")
139
+ @ssh.expects(:check_key_permissions).with(@env.config.ssh.private_key_path).once.in_sequence(seq)
140
+ Net::SSH.expects(:start).once.in_sequence(seq)
141
+ @ssh.execute
142
+ end
143
+
144
+ should "call net::ssh.start with the proper names" do
145
+ Net::SSH.expects(:start).once.with() do |host, username, opts|
146
+ assert_equal @env.config.ssh.host, host
147
+ assert_equal @env.config.ssh.username, username
148
+ assert_equal @ssh.port, opts[:port]
149
+ assert_equal [@env.config.ssh.private_key_path], opts[:keys]
150
+ assert opts[:keys_only]
151
+ true
152
+ end
153
+ @ssh.execute
154
+ end
155
+
156
+ should "forward agent if configured" do
157
+ @env.config.ssh.forward_agent = true
158
+ Net::SSH.expects(:start).once.with() do |host, username, opts|
159
+ assert opts[:forward_agent]
160
+ true
161
+ end
162
+
163
+ @ssh.execute
164
+ end
165
+
166
+ should "use custom host if set" do
167
+ @env.config.ssh.host = "foo"
168
+ Net::SSH.expects(:start).with(@env.config.ssh.host, @env.config.ssh.username, anything).once
169
+ @ssh.execute
170
+ end
171
+
172
+ should "yield an SSH session object" do
173
+ raw = mock("raw")
174
+ Net::SSH.expects(:start).yields(raw)
175
+ @ssh.execute do |ssh|
176
+ assert ssh.is_a?(Vagrant::SSH::Session)
177
+ assert_equal raw, ssh.session
178
+ end
179
+ end
180
+ end
181
+
182
+ context "SCPing files to the remote host" do
183
+ setup do
184
+ mock_ssh
185
+ end
186
+
187
+ should "use Vagrant::SSH execute to setup an SCP connection and upload" do
188
+ scp = mock("scp")
189
+ ssh = mock("ssh")
190
+ sess = mock("session")
191
+ ssh.stubs(:session).returns(sess)
192
+ scp.expects(:upload!).with("foo", "bar").once
193
+ Net::SCP.expects(:new).with(ssh.session).returns(scp).once
194
+ @ssh.expects(:execute).yields(ssh).once
195
+ @ssh.upload!("foo", "bar")
196
+ end
197
+ end
198
+
199
+ context "checking if host is up" do
200
+ setup do
201
+ mock_ssh
202
+ @ssh.stubs(:check_key_permissions)
203
+ @ssh.stubs(:port).returns(2222)
204
+ end
205
+
206
+ should "return true if SSH connection works" do
207
+ Net::SSH.expects(:start).yields("success")
208
+ assert @ssh.up?
209
+ end
210
+
211
+ should "return false if SSH connection times out" do
212
+ @env.config.ssh.timeout = 0.5
213
+
214
+ Net::SSH.stubs(:start).with() do
215
+ # Sleep here to artificially fake timeout
216
+ sleep 1
217
+ true
218
+ end
219
+
220
+ assert !@ssh.up?
221
+ end
222
+
223
+ should "return false if the connection is refused" do
224
+ Net::SSH.expects(:start).times(5).raises(Errno::ECONNREFUSED)
225
+ assert_nothing_raised {
226
+ assert !@ssh.up?
227
+ }
228
+ end
229
+
230
+ should "return false if the connection is dropped" do
231
+ Net::SSH.expects(:start).raises(Net::SSH::Disconnect)
232
+ assert_nothing_raised {
233
+ assert !@ssh.up?
234
+ }
235
+ end
236
+
237
+ should "specifity the timeout as an option to execute" do
238
+ @ssh.expects(:execute).yields(true).with() do |opts|
239
+ assert_equal @env.config.ssh.timeout, opts[:timeout]
240
+ true
241
+ end
242
+
243
+ assert @ssh.up?
244
+ end
245
+
246
+ should "error and exit if a Net::SSH::AuthenticationFailed is raised" do
247
+ @ssh.expects(:execute).raises(Net::SSH::AuthenticationFailed)
248
+ assert_raises(Vagrant::Errors::SSHAuthenticationFailed) { @ssh.up? }
249
+ end
250
+
251
+ should "only get the port once (in the main thread)" do
252
+ @ssh.expects(:port).once.returns(2222)
253
+ @ssh.up?
254
+ end
255
+ end
256
+
257
+ context "getting the ssh port" do
258
+ setup do
259
+ mock_ssh
260
+ end
261
+
262
+ should "return the port given in options if it exists" do
263
+ assert_equal "47", @ssh.port({ :port => "47" })
264
+ end
265
+ end
266
+
267
+ context "checking key permissions" do
268
+ setup do
269
+ mock_ssh
270
+ @ssh.stubs(:file_perms)
271
+
272
+ @key_path = "foo"
273
+
274
+
275
+ @stat = mock("stat")
276
+ @stat.stubs(:owned?).returns(true)
277
+ File.stubs(:stat).returns(@stat)
278
+
279
+ Mario::Platform.forced = Mario::Platform::Linux
280
+ end
281
+
282
+ teardown do
283
+ Mario::Platform.forced = Mario::Platform::Linux
284
+ end
285
+
286
+ should "do nothing if on windows" do
287
+ Mario::Platform.forced = Mario::Platform::Windows7
288
+ File.expects(:stat).never
289
+ @ssh.check_key_permissions(@key_path)
290
+ end
291
+
292
+ should "do nothing if the user is not the owner" do
293
+ @stat.expects(:owned?).returns(false)
294
+ File.expects(:chmod).never
295
+ @ssh.check_key_permissions(@key_path)
296
+ end
297
+
298
+ should "do nothing if the file perms equal 600" do
299
+ @ssh.expects(:file_perms).with(@key_path).returns("600")
300
+ File.expects(:chmod).never
301
+ @ssh.check_key_permissions(@key_path)
302
+ end
303
+
304
+ should "chmod the file if the file perms aren't 600" do
305
+ perm_sequence = sequence("perm_seq")
306
+ @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence)
307
+ File.expects(:chmod).with(0600, @key_path).once.in_sequence(perm_sequence)
308
+ @ssh.expects(:file_perms).returns("600").in_sequence(perm_sequence)
309
+ assert_nothing_raised { @ssh.check_key_permissions(@key_path) }
310
+ end
311
+
312
+ should "error and exit if the resulting chmod doesn't work" do
313
+ perm_sequence = sequence("perm_seq")
314
+ @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence)
315
+ File.expects(:chmod).with(0600, @key_path).once.in_sequence(perm_sequence)
316
+ @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence)
317
+ assert_raises(Vagrant::Errors::SSHKeyBadPermissions) { @ssh.check_key_permissions(@key_path) }
318
+ end
319
+
320
+ should "error and exit if a bad file perm is raised" do
321
+ @ssh.expects(:file_perms).with(@key_path).returns("900")
322
+ File.expects(:chmod).raises(Errno::EPERM)
323
+ assert_raises(Vagrant::Errors::SSHKeyBadPermissions) { @ssh.check_key_permissions(@key_path) }
324
+ end
325
+ end
326
+
327
+ context "getting file permissions" do
328
+ setup do
329
+ mock_ssh
330
+ end
331
+
332
+ should "return the last 3 characters of the file mode" do
333
+ path = "foo"
334
+ mode = "10000foo"
335
+ stat = mock("stat")
336
+ File.expects(:stat).with(path).returns(stat)
337
+ stat.expects(:mode).returns(mode)
338
+ @ssh.expects(:sprintf).with("%o", mode).returns(mode)
339
+ assert_equal path, @ssh.file_perms(path)
340
+ end
341
+ end
342
+ end