bmabey-vagrant 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. data/.gitignore +12 -0
  2. data/Gemfile +17 -0
  3. data/LICENSE +21 -0
  4. data/README.md +53 -0
  5. data/Rakefile +41 -0
  6. data/VERSION +1 -0
  7. data/bin/.gitignore +0 -0
  8. data/bin/vagrant +15 -0
  9. data/bin/vagrant-box +34 -0
  10. data/bin/vagrant-down +27 -0
  11. data/bin/vagrant-halt +28 -0
  12. data/bin/vagrant-init +27 -0
  13. data/bin/vagrant-package +29 -0
  14. data/bin/vagrant-reload +29 -0
  15. data/bin/vagrant-resume +27 -0
  16. data/bin/vagrant-ssh +27 -0
  17. data/bin/vagrant-status +29 -0
  18. data/bin/vagrant-suspend +27 -0
  19. data/bin/vagrant-up +29 -0
  20. data/config/default.rb +26 -0
  21. data/keys/README.md +10 -0
  22. data/keys/vagrant +27 -0
  23. data/keys/vagrant.pub +1 -0
  24. data/lib/vagrant.rb +19 -0
  25. data/lib/vagrant/actions/base.rb +118 -0
  26. data/lib/vagrant/actions/box/add.rb +22 -0
  27. data/lib/vagrant/actions/box/destroy.rb +14 -0
  28. data/lib/vagrant/actions/box/download.rb +72 -0
  29. data/lib/vagrant/actions/box/unpackage.rb +43 -0
  30. data/lib/vagrant/actions/collection.rb +36 -0
  31. data/lib/vagrant/actions/runner.rb +132 -0
  32. data/lib/vagrant/actions/vm/boot.rb +47 -0
  33. data/lib/vagrant/actions/vm/customize.rb +17 -0
  34. data/lib/vagrant/actions/vm/destroy.rb +23 -0
  35. data/lib/vagrant/actions/vm/down.rb +12 -0
  36. data/lib/vagrant/actions/vm/export.rb +41 -0
  37. data/lib/vagrant/actions/vm/forward_ports.rb +46 -0
  38. data/lib/vagrant/actions/vm/halt.rb +14 -0
  39. data/lib/vagrant/actions/vm/import.rb +18 -0
  40. data/lib/vagrant/actions/vm/move_hard_drive.rb +51 -0
  41. data/lib/vagrant/actions/vm/package.rb +65 -0
  42. data/lib/vagrant/actions/vm/provision.rb +49 -0
  43. data/lib/vagrant/actions/vm/reload.rb +17 -0
  44. data/lib/vagrant/actions/vm/resume.rb +16 -0
  45. data/lib/vagrant/actions/vm/shared_folders.rb +81 -0
  46. data/lib/vagrant/actions/vm/start.rb +18 -0
  47. data/lib/vagrant/actions/vm/suspend.rb +16 -0
  48. data/lib/vagrant/actions/vm/up.rb +40 -0
  49. data/lib/vagrant/active_list.rb +73 -0
  50. data/lib/vagrant/box.rb +152 -0
  51. data/lib/vagrant/busy.rb +73 -0
  52. data/lib/vagrant/commands.rb +219 -0
  53. data/lib/vagrant/config.rb +183 -0
  54. data/lib/vagrant/downloaders/base.rb +16 -0
  55. data/lib/vagrant/downloaders/file.rb +17 -0
  56. data/lib/vagrant/downloaders/http.rb +47 -0
  57. data/lib/vagrant/environment.rb +263 -0
  58. data/lib/vagrant/provisioners/base.rb +29 -0
  59. data/lib/vagrant/provisioners/chef.rb +103 -0
  60. data/lib/vagrant/provisioners/chef_server.rb +84 -0
  61. data/lib/vagrant/provisioners/chef_solo.rb +97 -0
  62. data/lib/vagrant/ssh.rb +104 -0
  63. data/lib/vagrant/util.rb +51 -0
  64. data/lib/vagrant/util/errors.rb +36 -0
  65. data/lib/vagrant/util/stacked_proc_runner.rb +35 -0
  66. data/lib/vagrant/util/template_renderer.rb +83 -0
  67. data/lib/vagrant/vm.rb +61 -0
  68. data/templates/Vagrantfile.erb +8 -0
  69. data/templates/errors.yml +117 -0
  70. data/test/test_helper.rb +163 -0
  71. data/test/vagrant/actions/base_test.rb +32 -0
  72. data/test/vagrant/actions/box/add_test.rb +37 -0
  73. data/test/vagrant/actions/box/destroy_test.rb +18 -0
  74. data/test/vagrant/actions/box/download_test.rb +131 -0
  75. data/test/vagrant/actions/box/unpackage_test.rb +100 -0
  76. data/test/vagrant/actions/collection_test.rb +110 -0
  77. data/test/vagrant/actions/runner_test.rb +265 -0
  78. data/test/vagrant/actions/vm/boot_test.rb +55 -0
  79. data/test/vagrant/actions/vm/customize_test.rb +16 -0
  80. data/test/vagrant/actions/vm/destroy_test.rb +36 -0
  81. data/test/vagrant/actions/vm/down_test.rb +32 -0
  82. data/test/vagrant/actions/vm/export_test.rb +88 -0
  83. data/test/vagrant/actions/vm/forward_ports_test.rb +104 -0
  84. data/test/vagrant/actions/vm/halt_test.rb +27 -0
  85. data/test/vagrant/actions/vm/import_test.rb +43 -0
  86. data/test/vagrant/actions/vm/move_hard_drive_test.rb +108 -0
  87. data/test/vagrant/actions/vm/package_test.rb +181 -0
  88. data/test/vagrant/actions/vm/provision_test.rb +108 -0
  89. data/test/vagrant/actions/vm/reload_test.rb +47 -0
  90. data/test/vagrant/actions/vm/resume_test.rb +27 -0
  91. data/test/vagrant/actions/vm/shared_folders_test.rb +176 -0
  92. data/test/vagrant/actions/vm/start_test.rb +36 -0
  93. data/test/vagrant/actions/vm/suspend_test.rb +27 -0
  94. data/test/vagrant/actions/vm/up_test.rb +107 -0
  95. data/test/vagrant/active_list_test.rb +190 -0
  96. data/test/vagrant/box_test.rb +151 -0
  97. data/test/vagrant/busy_test.rb +83 -0
  98. data/test/vagrant/commands_test.rb +307 -0
  99. data/test/vagrant/config_test.rb +256 -0
  100. data/test/vagrant/downloaders/base_test.rb +27 -0
  101. data/test/vagrant/downloaders/file_test.rb +26 -0
  102. data/test/vagrant/downloaders/http_test.rb +40 -0
  103. data/test/vagrant/environment_test.rb +607 -0
  104. data/test/vagrant/provisioners/base_test.rb +33 -0
  105. data/test/vagrant/provisioners/chef_server_test.rb +187 -0
  106. data/test/vagrant/provisioners/chef_solo_test.rb +149 -0
  107. data/test/vagrant/provisioners/chef_test.rb +117 -0
  108. data/test/vagrant/ssh_test.rb +222 -0
  109. data/test/vagrant/util/errors_test.rb +57 -0
  110. data/test/vagrant/util/stacked_proc_runner_test.rb +43 -0
  111. data/test/vagrant/util/template_renderer_test.rb +138 -0
  112. data/test/vagrant/util_test.rb +64 -0
  113. data/test/vagrant/vm_test.rb +114 -0
  114. data/vagrant.gemspec +216 -0
  115. metadata +285 -0
@@ -0,0 +1,222 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class SshTest < Test::Unit::TestCase
4
+ setup do
5
+ mock_config
6
+ end
7
+
8
+ def mock_ssh
9
+ @env = mock_environment do |config|
10
+ yield config if block_given?
11
+ end
12
+
13
+ @ssh = Vagrant::SSH.new(@env)
14
+ end
15
+
16
+ context "connecting to external SSH" do
17
+ setup do
18
+ mock_ssh
19
+ @ssh.stubs(:check_key_permissions)
20
+ end
21
+
22
+ should "check key permissions prior to exec" do
23
+ exec_seq = sequence("exec_seq")
24
+ @ssh.expects(:check_key_permissions).with(@env.config.ssh.private_key_path).once.in_sequence(exec_seq)
25
+ Kernel.expects(:exec).in_sequence(exec_seq)
26
+ @ssh.connect
27
+ end
28
+
29
+ should "call exec with defaults when no options are supplied" do
30
+ ssh_exec_expect(@ssh.port,
31
+ @env.config.ssh.private_key_path,
32
+ @env.config.ssh.username,
33
+ @env.config.ssh.host)
34
+ @ssh.connect
35
+ end
36
+
37
+ should "call exec with supplied params" do
38
+ args = {:username => 'bar', :private_key_path => 'baz', :host => 'bak', :port => 'bag'}
39
+ ssh_exec_expect(args[:port], args[:private_key_path], args[:username], args[:host])
40
+ @ssh.connect(args)
41
+ end
42
+
43
+ def ssh_exec_expect(port, key_path, uname, host)
44
+ Kernel.expects(:exec).with() do |arg|
45
+ assert arg =~ /^ssh/
46
+ assert arg =~ /-p #{port}/
47
+ assert arg =~ /-i #{key_path}/
48
+ assert arg =~ /#{uname}@#{host}/
49
+ # TODO options not tested for as they may be removed
50
+ true
51
+ end
52
+ end
53
+ end
54
+
55
+ context "executing ssh commands" do
56
+ setup do
57
+ mock_ssh
58
+ end
59
+
60
+ should "call net::ssh.start with the proper names" do
61
+ Net::SSH.expects(:start).once.with() do |host, username, opts|
62
+ assert_equal @env.config.ssh.host, host
63
+ assert_equal @env.config.ssh.username, username
64
+ assert_equal @ssh.port, opts[:port]
65
+ assert_equal [@env.config.ssh.private_key_path], opts[:keys]
66
+ true
67
+ end
68
+ @ssh.execute
69
+ end
70
+
71
+ should "use custom host if set" do
72
+ @env.config.ssh.host = "foo"
73
+ Net::SSH.expects(:start).with(@env.config.ssh.host, @env.config.ssh.username, anything).once
74
+ @ssh.execute
75
+ end
76
+ end
77
+
78
+ context "SCPing files to the remote host" do
79
+ setup do
80
+ mock_ssh
81
+ end
82
+
83
+ should "use Vagrant::SSH execute to setup an SCP connection and upload" do
84
+ scp = mock("scp")
85
+ ssh = mock("ssh")
86
+ scp.expects(:upload!).with("foo", "bar").once
87
+ Net::SCP.expects(:new).with(ssh).returns(scp).once
88
+ @ssh.expects(:execute).yields(ssh).once
89
+ @ssh.upload!("foo", "bar")
90
+ end
91
+ end
92
+
93
+ context "checking if host is up" do
94
+ setup do
95
+ mock_ssh
96
+ end
97
+
98
+ should "return true if SSH connection works" do
99
+ Net::SSH.expects(:start).yields("success")
100
+ assert @ssh.up?
101
+ end
102
+
103
+ should "return false if SSH connection times out" do
104
+ Net::SSH.expects(:start)
105
+ assert !@ssh.up?
106
+ end
107
+
108
+ should "allow the thread the configured timeout time" do
109
+ @thread = mock("thread")
110
+ @thread.stubs(:[])
111
+ Thread.expects(:new).returns(@thread)
112
+ @thread.expects(:join).with(@env.config.ssh.timeout).once
113
+ @ssh.up?
114
+ end
115
+
116
+ should "return false if the connection is refused" do
117
+ Net::SSH.expects(:start).raises(Errno::ECONNREFUSED)
118
+ assert_nothing_raised {
119
+ assert !@ssh.up?
120
+ }
121
+ end
122
+
123
+ should "return false if the connection is dropped" do
124
+ Net::SSH.expects(:start).raises(Net::SSH::Disconnect)
125
+ assert_nothing_raised {
126
+ assert !@ssh.up?
127
+ }
128
+ end
129
+
130
+ should "specifity the timeout as an option to execute" do
131
+ @ssh.expects(:execute).with(:timeout => @env.config.ssh.timeout).yields(true)
132
+ assert @ssh.up?
133
+ end
134
+
135
+ should "error and exit if a Net::SSH::AuthenticationFailed is raised" do
136
+ @ssh.expects(:execute).raises(Net::SSH::AuthenticationFailed)
137
+ @ssh.expects(:error_and_exit).with(:vm_ssh_auth_failed).once
138
+ @ssh.up?
139
+ end
140
+ end
141
+
142
+ context "getting the ssh port" do
143
+ setup do
144
+ mock_ssh
145
+ end
146
+
147
+ should "return the configured port by default" do
148
+ assert_equal @env.config.vm.forwarded_ports[@env.config.ssh.forwarded_port_key][:hostport], @ssh.port
149
+ end
150
+
151
+ should "return the port given in options if it exists" do
152
+ assert_equal "47", @ssh.port({ :port => "47" })
153
+ end
154
+ end
155
+
156
+ context "checking key permissions" do
157
+ setup do
158
+ mock_ssh
159
+ @ssh.stubs(:file_perms)
160
+
161
+ @key_path = "foo"
162
+
163
+
164
+ @stat = mock("stat")
165
+ @stat.stubs(:owned?).returns(true)
166
+ File.stubs(:stat).returns(@stat)
167
+ end
168
+
169
+ should "do nothing if the user is not the owner" do
170
+ @stat.expects(:owned?).returns(false)
171
+ File.expects(:chmod).never
172
+ @ssh.check_key_permissions(@key_path)
173
+ end
174
+
175
+ should "do nothing if the file perms equal 600" do
176
+ @ssh.expects(:file_perms).with(@key_path).returns("600")
177
+ File.expects(:chmod).never
178
+ @ssh.check_key_permissions(@key_path)
179
+ end
180
+
181
+ should "chmod the file if the file perms aren't 600" do
182
+ perm_sequence = sequence("perm_seq")
183
+ @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence)
184
+ File.expects(:chmod).with(0600, @key_path).once.in_sequence(perm_sequence)
185
+ @ssh.expects(:file_perms).returns("600").in_sequence(perm_sequence)
186
+ @ssh.expects(:error_and_exit).never
187
+ @ssh.check_key_permissions(@key_path)
188
+ end
189
+
190
+ should "error and exit if the resulting chmod doesn't work" do
191
+ perm_sequence = sequence("perm_seq")
192
+ @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence)
193
+ File.expects(:chmod).with(0600, @key_path).once.in_sequence(perm_sequence)
194
+ @ssh.expects(:file_perms).returns("900").in_sequence(perm_sequence)
195
+ @ssh.expects(:error_and_exit).once.with(:ssh_bad_permissions, :key_path => @key_path).in_sequence(perm_sequence)
196
+ @ssh.check_key_permissions(@key_path)
197
+ end
198
+
199
+ should "error and exit if a bad file perm is raised" do
200
+ @ssh.expects(:file_perms).with(@key_path).returns("900")
201
+ File.expects(:chmod).raises(Errno::EPERM)
202
+ @ssh.expects(:error_and_exit).once.with(:ssh_bad_permissions, :key_path => @key_path)
203
+ @ssh.check_key_permissions(@key_path)
204
+ end
205
+ end
206
+
207
+ context "getting file permissions" do
208
+ setup do
209
+ mock_ssh
210
+ end
211
+
212
+ should "return the last 3 characters of the file mode" do
213
+ path = "foo"
214
+ mode = "10000foo"
215
+ stat = mock("stat")
216
+ File.expects(:stat).with(path).returns(stat)
217
+ stat.expects(:mode).returns(mode)
218
+ @ssh.expects(:sprintf).with("%o", mode).returns(mode)
219
+ assert_equal path, @ssh.file_perms(path)
220
+ end
221
+ end
222
+ end
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class ErrorsUtilTest < Test::Unit::TestCase
4
+ include Vagrant::Util
5
+
6
+ context "loading the errors from the YML" do
7
+ setup do
8
+ YAML.stubs(:load_file)
9
+ Errors.reset!
10
+ end
11
+
12
+ should "load the file initially, then never again unless reset" do
13
+ YAML.expects(:load_file).with(File.join(PROJECT_ROOT, "templates", "errors.yml")).once
14
+ Errors.errors
15
+ Errors.errors
16
+ Errors.errors
17
+ Errors.errors
18
+ end
19
+
20
+ should "reload if reset! is called" do
21
+ YAML.expects(:load_file).with(File.join(PROJECT_ROOT, "templates", "errors.yml")).twice
22
+ Errors.errors
23
+ Errors.reset!
24
+ Errors.errors
25
+ end
26
+ end
27
+
28
+ context "getting the error string" do
29
+ setup do
30
+ @errors = {}
31
+ @errors[:foo] = "foo bar baz"
32
+ Errors.stubs(:errors).returns(@errors)
33
+ end
34
+
35
+ should "render the error string" do
36
+ TemplateRenderer.expects(:render_string).with(@errors[:foo], anything).once
37
+ Errors.error_string(:foo)
38
+ end
39
+
40
+ should "pass in any data entries" do
41
+ data = mock("data")
42
+ TemplateRenderer.expects(:render_string).with(@errors[:foo], data).once
43
+ Errors.error_string(:foo, data)
44
+ end
45
+
46
+ should "return the result of the render" do
47
+ result = mock("result")
48
+ TemplateRenderer.expects(:render_string).returns(result)
49
+ assert_equal result, Errors.error_string(:foo)
50
+ end
51
+
52
+ should "return an unknown if the key doesn't exist" do
53
+ result = Errors.error_string(:unknown)
54
+ assert result =~ /Unknown/i
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class StackedProcRunnerUtilTest < Test::Unit::TestCase
4
+ class TestClass
5
+ include Vagrant::Util::StackedProcRunner
6
+ end
7
+
8
+ setup do
9
+ @instance = TestClass.new
10
+ @instance.proc_stack.clear
11
+ end
12
+
13
+ should "not run the procs right away" do
14
+ obj = mock("obj")
15
+ obj.expects(:foo).never
16
+ @instance.push_proc { |config| obj.foo }
17
+ @instance.push_proc { |config| obj.foo }
18
+ @instance.push_proc { |config| obj.foo }
19
+ end
20
+
21
+ should "run the blocks when run_procs! is ran" do
22
+ obj = mock("obj")
23
+ obj.expects(:foo).times(2)
24
+ @instance.push_proc { obj.foo }
25
+ @instance.push_proc { obj.foo }
26
+ @instance.run_procs!
27
+ end
28
+
29
+ should "run the blocks with the same arguments" do
30
+ passed_config = mock("config")
31
+ @instance.push_proc { |config| assert passed_config.equal?(config) }
32
+ @instance.push_proc { |config| assert passed_config.equal?(config) }
33
+ @instance.run_procs!(passed_config)
34
+ end
35
+
36
+ should "not clear the blocks after running" do
37
+ obj = mock("obj")
38
+ obj.expects(:foo).times(2)
39
+ @instance.push_proc { obj.foo }
40
+ @instance.run_procs!
41
+ @instance.run_procs!
42
+ end
43
+ end
@@ -0,0 +1,138 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class TemplateRendererUtilTest < Test::Unit::TestCase
4
+ context "initializing" do
5
+ should "set the template to the given argument" do
6
+ r = Vagrant::Util::TemplateRenderer.new("foo")
7
+ assert_equal "foo", r.template
8
+ end
9
+
10
+ should "set any additional variables" do
11
+ r = Vagrant::Util::TemplateRenderer.new("foo", {:bar => :baz})
12
+ assert_equal :baz, r.bar
13
+ end
14
+ end
15
+
16
+ context "rendering" do
17
+ setup do
18
+ @template = "foo"
19
+ @r = Vagrant::Util::TemplateRenderer.new(@template)
20
+ @r.stubs(:full_template_path).returns(@template + "!")
21
+
22
+ @contents = "bar"
23
+
24
+ @file = mock("file")
25
+ @file.stubs(:read).returns(@contents)
26
+ File.stubs(:open).yields(@file)
27
+ end
28
+
29
+ should "open the template file for reading" do
30
+ File.expects(:open).with(@r.full_template_path, 'r').once
31
+ @r.render
32
+ end
33
+
34
+ should "set the template to the file contents, render, then set it back" do
35
+ result = "bar"
36
+
37
+ template_seq = sequence("template_seq")
38
+ @r.expects(:template=).with(@file.read).in_sequence(template_seq)
39
+ @r.expects(:render_string).returns(result).in_sequence(template_seq)
40
+ @r.expects(:template=).with(@template).in_sequence(template_seq)
41
+ assert_equal result, @r.render
42
+ end
43
+
44
+ should "render the ERB file in the context of the renderer" do
45
+ result = "bar"
46
+ template = "<%= foo %>"
47
+ @r.foo = result
48
+ @file.expects(:read).returns(template)
49
+ assert_equal result, @r.render
50
+ end
51
+ end
52
+
53
+ context "rendering as string" do
54
+ setup do
55
+ @result = "foo"
56
+ @erb = mock("erb")
57
+ @erb.stubs(:result).returns(@result)
58
+
59
+ @r = Vagrant::Util::TemplateRenderer.new("foo")
60
+ end
61
+
62
+ should "simply render the template as a string" do
63
+ ERB.expects(:new).with(@r.template).returns(@erb)
64
+ assert_equal @result, @r.render_string
65
+ end
66
+ end
67
+
68
+ context "the full template path" do
69
+ setup do
70
+ @template = "foo"
71
+ @r = Vagrant::Util::TemplateRenderer.new(@template)
72
+ end
73
+
74
+ should "be the ERB file in the templates directory" do
75
+ result = File.join(PROJECT_ROOT, "templates", "#{@template}.erb")
76
+ assert_equal result, @r.full_template_path
77
+ end
78
+ end
79
+
80
+ context "class methods" do
81
+ context "render_with method" do
82
+ setup do
83
+ @template = "foo"
84
+ @r = Vagrant::Util::TemplateRenderer.new(@template)
85
+ @r.stubs(:render)
86
+
87
+ @method = :rawr
88
+
89
+ Vagrant::Util::TemplateRenderer.stubs(:new).with(@template, {}).returns(@r)
90
+ end
91
+
92
+ should "use the second argument as the template" do
93
+ Vagrant::Util::TemplateRenderer.expects(:new).with(@template, {}).returns(@r)
94
+ Vagrant::Util::TemplateRenderer.render_with(@method, @template)
95
+ end
96
+
97
+ should "send in additional argument to the renderer" do
98
+ data = {:hey => :foo}
99
+ Vagrant::Util::TemplateRenderer.expects(:new).with(@template, data).returns(@r)
100
+ Vagrant::Util::TemplateRenderer.render_with(@method, @template, data)
101
+ end
102
+
103
+ should "yield a block if given with the renderer as the argument" do
104
+ @r.expects(:yielded=).with(true).once
105
+ Vagrant::Util::TemplateRenderer.render_with(@method, @template) do |r|
106
+ r.yielded = true
107
+ end
108
+ end
109
+
110
+ should "render the result using the given method" do
111
+ result = mock('result')
112
+ @r.expects(@method).returns(result)
113
+ assert_equal result, Vagrant::Util::TemplateRenderer.render_with(@method, @template)
114
+ end
115
+
116
+ should "convert the given method to a sym prior to calling" do
117
+ @r.expects(@method.to_sym).returns(nil)
118
+ Vagrant::Util::TemplateRenderer.render_with(@method.to_s, @template)
119
+ end
120
+ end
121
+
122
+ context "render method" do
123
+ should "call render_with the render! method" do
124
+ args = ["foo", "bar", "baz"]
125
+ Vagrant::Util::TemplateRenderer.expects(:render_with).with(:render, *args)
126
+ Vagrant::Util::TemplateRenderer.render(*args)
127
+ end
128
+ end
129
+
130
+ context "render_string method" do
131
+ should "call render_with the render! method" do
132
+ args = ["foo", "bar", "baz"]
133
+ Vagrant::Util::TemplateRenderer.expects(:render_with).with(:render_string, *args)
134
+ Vagrant::Util::TemplateRenderer.render_string(*args)
135
+ end
136
+ end
137
+ end
138
+ end