vagrantup 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/Gemfile +17 -0
  4. data/README.md +45 -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 +35 -0
  10. data/bin/vagrant-down +28 -0
  11. data/bin/vagrant-halt +29 -0
  12. data/bin/vagrant-init +28 -0
  13. data/bin/vagrant-package +30 -0
  14. data/bin/vagrant-reload +30 -0
  15. data/bin/vagrant-resume +28 -0
  16. data/bin/vagrant-ssh +28 -0
  17. data/bin/vagrant-suspend +28 -0
  18. data/bin/vagrant-up +30 -0
  19. data/config/default.rb +29 -0
  20. data/lib/vagrant.rb +14 -0
  21. data/lib/vagrant/actions/base.rb +93 -0
  22. data/lib/vagrant/actions/box/add.rb +22 -0
  23. data/lib/vagrant/actions/box/destroy.rb +14 -0
  24. data/lib/vagrant/actions/box/download.rb +63 -0
  25. data/lib/vagrant/actions/box/unpackage.rb +49 -0
  26. data/lib/vagrant/actions/runner.rb +128 -0
  27. data/lib/vagrant/actions/vm/destroy.rb +14 -0
  28. data/lib/vagrant/actions/vm/down.rb +12 -0
  29. data/lib/vagrant/actions/vm/export.rb +41 -0
  30. data/lib/vagrant/actions/vm/forward_ports.rb +32 -0
  31. data/lib/vagrant/actions/vm/halt.rb +14 -0
  32. data/lib/vagrant/actions/vm/import.rb +17 -0
  33. data/lib/vagrant/actions/vm/move_hard_drive.rb +53 -0
  34. data/lib/vagrant/actions/vm/package.rb +61 -0
  35. data/lib/vagrant/actions/vm/provision.rb +71 -0
  36. data/lib/vagrant/actions/vm/reload.rb +17 -0
  37. data/lib/vagrant/actions/vm/resume.rb +16 -0
  38. data/lib/vagrant/actions/vm/shared_folders.rb +69 -0
  39. data/lib/vagrant/actions/vm/start.rb +50 -0
  40. data/lib/vagrant/actions/vm/suspend.rb +16 -0
  41. data/lib/vagrant/actions/vm/up.rb +35 -0
  42. data/lib/vagrant/box.rb +129 -0
  43. data/lib/vagrant/busy.rb +73 -0
  44. data/lib/vagrant/commands.rb +174 -0
  45. data/lib/vagrant/config.rb +156 -0
  46. data/lib/vagrant/downloaders/base.rb +13 -0
  47. data/lib/vagrant/downloaders/file.rb +21 -0
  48. data/lib/vagrant/downloaders/http.rb +47 -0
  49. data/lib/vagrant/env.rb +140 -0
  50. data/lib/vagrant/ssh.rb +43 -0
  51. data/lib/vagrant/util.rb +45 -0
  52. data/lib/vagrant/vm.rb +57 -0
  53. data/script/vagrant-ssh-expect.sh +22 -0
  54. data/templates/Vagrantfile +8 -0
  55. data/test/test_helper.rb +91 -0
  56. data/test/vagrant/actions/base_test.rb +32 -0
  57. data/test/vagrant/actions/box/add_test.rb +37 -0
  58. data/test/vagrant/actions/box/destroy_test.rb +18 -0
  59. data/test/vagrant/actions/box/download_test.rb +118 -0
  60. data/test/vagrant/actions/box/unpackage_test.rb +101 -0
  61. data/test/vagrant/actions/runner_test.rb +236 -0
  62. data/test/vagrant/actions/vm/destroy_test.rb +24 -0
  63. data/test/vagrant/actions/vm/down_test.rb +32 -0
  64. data/test/vagrant/actions/vm/export_test.rb +88 -0
  65. data/test/vagrant/actions/vm/forward_ports_test.rb +50 -0
  66. data/test/vagrant/actions/vm/halt_test.rb +27 -0
  67. data/test/vagrant/actions/vm/import_test.rb +36 -0
  68. data/test/vagrant/actions/vm/move_hard_drive_test.rb +108 -0
  69. data/test/vagrant/actions/vm/package_test.rb +155 -0
  70. data/test/vagrant/actions/vm/provision_test.rb +103 -0
  71. data/test/vagrant/actions/vm/reload_test.rb +44 -0
  72. data/test/vagrant/actions/vm/resume_test.rb +27 -0
  73. data/test/vagrant/actions/vm/shared_folders_test.rb +117 -0
  74. data/test/vagrant/actions/vm/start_test.rb +55 -0
  75. data/test/vagrant/actions/vm/suspend_test.rb +27 -0
  76. data/test/vagrant/actions/vm/up_test.rb +76 -0
  77. data/test/vagrant/box_test.rb +92 -0
  78. data/test/vagrant/busy_test.rb +81 -0
  79. data/test/vagrant/commands_test.rb +252 -0
  80. data/test/vagrant/config_test.rb +123 -0
  81. data/test/vagrant/downloaders/base_test.rb +20 -0
  82. data/test/vagrant/downloaders/file_test.rb +32 -0
  83. data/test/vagrant/downloaders/http_test.rb +40 -0
  84. data/test/vagrant/env_test.rb +293 -0
  85. data/test/vagrant/ssh_test.rb +95 -0
  86. data/test/vagrant/util_test.rb +64 -0
  87. data/test/vagrant/vm_test.rb +96 -0
  88. metadata +263 -0
@@ -0,0 +1,20 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '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
+ @base = Vagrant::Downloaders::Base.new
11
+ end
12
+
13
+ should "implement prepare which does nothing" do
14
+ assert_nothing_raised do
15
+ assert @base.respond_to?(:download!)
16
+ @base.download!("source", "destination")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class FileDownloaderTest < Test::Unit::TestCase
4
+ setup do
5
+ @downloader, @tempfile = mock_downloader(Vagrant::Downloaders::File)
6
+ @uri = "foo.box"
7
+ end
8
+
9
+ context "downloading" do
10
+ setup do
11
+ @file = mock("file")
12
+ @file.stubs(:read)
13
+ @file.stubs(:eof?).returns(false)
14
+ @downloader.stubs(:open).yields(@file)
15
+ end
16
+
17
+ should "open with the given uri" do
18
+ @downloader.expects(:open).with(@uri).once
19
+ @downloader.download!(@uri, @tempfile)
20
+ end
21
+
22
+ should "buffer the read from the file and write to the tempfile" do
23
+ data = mock("data")
24
+ write_seq = sequence("write_seq")
25
+ @file.stubs(:eof?).returns(false).in_sequence(write_seq)
26
+ @file.expects(:read).returns(data).in_sequence(write_seq)
27
+ @tempfile.expects(:write).with(data).in_sequence(write_seq)
28
+ @file.stubs(:eof?).returns(true).in_sequence(write_seq)
29
+ @downloader.download!(@uri, @tempfile)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class HttpDownloaderTest < Test::Unit::TestCase
4
+ setup do
5
+ @downloader, @tempfile = mock_downloader(Vagrant::Downloaders::HTTP)
6
+ @downloader.stubs(:report_progress)
7
+ @downloader.stubs(:complete_progress)
8
+ @uri = "foo.box"
9
+ end
10
+
11
+ context "downloading" do
12
+ setup do
13
+ @parsed_uri = mock("parsed")
14
+ URI.stubs(:parse).with(@uri).returns(@parsed_uri)
15
+ end
16
+
17
+ should "parse the URI and use that parsed URI for Net::HTTP" do
18
+ URI.expects(:parse).with(@uri).returns(@parsed_uri).once
19
+ Net::HTTP.expects(:get_response).with(@parsed_uri).once
20
+ @downloader.download!(@uri, @tempfile)
21
+ end
22
+
23
+ should "read the body of the response and place each segment into the file" do
24
+ response = mock("response")
25
+ response.stubs(:content_length)
26
+ segment = mock("segment")
27
+ segment.stubs(:length).returns(7)
28
+
29
+ Net::HTTP.stubs(:get_response).yields(response)
30
+ response.expects(:read_body).once.yields(segment)
31
+ @tempfile.expects(:write).with(segment).once
32
+
33
+ @downloader.download!(@uri, @tempfile)
34
+ end
35
+ end
36
+
37
+ context "reporting progress" do
38
+ # TODO: Testing for this, probably
39
+ end
40
+ end
@@ -0,0 +1,293 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class EnvTest < Test::Unit::TestCase
4
+ def mock_persisted_vm(returnvalue="foovm")
5
+ filemock = mock("filemock")
6
+ filemock.expects(:read).returns("foo")
7
+ Vagrant::VM.expects(:find).with("foo").returns(returnvalue)
8
+ File.expects(:open).with(Vagrant::Env.dotfile_path).once.yields(filemock)
9
+ Vagrant::Env.load_vm!
10
+ end
11
+
12
+ setup do
13
+ mock_config
14
+ Vagrant::Box.stubs(:find).returns("foo")
15
+ end
16
+
17
+ context "requiring a VM" do
18
+ should "error and exit if no persisted VM was found" do
19
+ assert_nil Vagrant::Env.persisted_vm
20
+ Vagrant::Env.expects(:error_and_exit).once
21
+ Vagrant::Env.require_persisted_vm
22
+ end
23
+
24
+ should "return and continue if persisted VM is found" do
25
+ mock_persisted_vm
26
+ Vagrant::Env.expects(:error_and_exit).never
27
+ Vagrant::Env.require_persisted_vm
28
+ end
29
+ end
30
+
31
+ context "loading home directory" do
32
+ setup do
33
+ @home_dir = File.expand_path(Vagrant.config.vagrant.home)
34
+
35
+ File.stubs(:directory?).returns(true)
36
+ FileUtils.stubs(:mkdir_p)
37
+ end
38
+
39
+ should "create each directory if it doesn't exist" do
40
+ create_seq = sequence("create_seq")
41
+ File.stubs(:directory?).returns(false)
42
+ Vagrant::Env::HOME_SUBDIRS.each do |subdir|
43
+ FileUtils.expects(:mkdir_p).with(File.join(@home_dir, subdir)).in_sequence(create_seq)
44
+ end
45
+
46
+ Vagrant::Env.load_home_directory!
47
+ end
48
+
49
+ should "not create directories if they exist" do
50
+ File.stubs(:directory?).returns(true)
51
+ FileUtils.expects(:mkdir_p).never
52
+ Vagrant::Env.load_home_directory!
53
+ end
54
+ end
55
+
56
+ context "loading config" do
57
+ setup do
58
+ @root_path = "/foo"
59
+ Vagrant::Env.stubs(:root_path).returns(@root_path)
60
+ Vagrant::Env.stubs(:box).returns(nil)
61
+ File.stubs(:exist?).returns(false)
62
+ Vagrant::Config.stubs(:execute!)
63
+ Vagrant::Config.stubs(:reset!)
64
+ end
65
+
66
+ should "reset the configuration object" do
67
+ Vagrant::Config.expects(:reset!).once
68
+ Vagrant::Env.load_config!
69
+ end
70
+
71
+ should "load from the project root" do
72
+ File.expects(:exist?).with(File.join(PROJECT_ROOT, "config", "default.rb")).once
73
+ Vagrant::Env.load_config!
74
+ end
75
+
76
+ should "load from the root path" do
77
+ File.expects(:exist?).with(File.join(@root_path, Vagrant::Env::ROOTFILE_NAME)).once
78
+ Vagrant::Env.load_config!
79
+ end
80
+
81
+ should "not load from the root path if nil" do
82
+ Vagrant::Env.stubs(:root_path).returns(nil)
83
+ File.expects(:exist?).with(File.join(@root_path, Vagrant::Env::ROOTFILE_NAME)).never
84
+ Vagrant::Env.load_config!
85
+ end
86
+
87
+ should "not load from the box directory if it is nil" do
88
+ Vagrant::Env.expects(:box).once.returns(nil)
89
+ Vagrant::Env.load_config!
90
+ end
91
+
92
+ should "load from the box directory if it is not nil" do
93
+ dir = "foo"
94
+ box = mock("box")
95
+ box.stubs(:directory).returns(dir)
96
+ Vagrant::Env.expects(:box).twice.returns(box)
97
+ File.expects(:exist?).with(File.join(dir, Vagrant::Env::ROOTFILE_NAME)).once
98
+ Vagrant::Env.load_config!
99
+ end
100
+
101
+ should "load the files only if exist? returns true" do
102
+ File.expects(:exist?).once.returns(true)
103
+ Vagrant::Env.expects(:load).once
104
+ Vagrant::Env.load_config!
105
+ end
106
+
107
+ should "not load the files if exist? returns false" do
108
+ Vagrant::Env.expects(:load).never
109
+ Vagrant::Env.load_config!
110
+ end
111
+
112
+ should "execute after loading" do
113
+ File.expects(:exist?).once.returns(true)
114
+ Vagrant::Env.expects(:load).once
115
+ Vagrant::Config.expects(:execute!).once
116
+ Vagrant::Env.load_config!
117
+ end
118
+ end
119
+
120
+ context "initial load" do
121
+ test "load! should load the config and set the persisted_uid" do
122
+ Vagrant::Env.expects(:load_config!).once
123
+ Vagrant::Env.expects(:load_vm!).once
124
+ Vagrant::Env.expects(:load_root_path!).with(Pathname.new(Dir.pwd), {}).once
125
+ Vagrant::Env.expects(:load_home_directory!).once
126
+ Vagrant::Env.expects(:load_box!).once
127
+ Vagrant::Env.load!
128
+ end
129
+ end
130
+
131
+ context "persisting the VM into a file" do
132
+ setup do
133
+ mock_config
134
+ end
135
+
136
+ test "should save it to the dotfile path" do
137
+ vm = mock("vm")
138
+ vm.stubs(:uuid).returns("foo")
139
+
140
+ filemock = mock("filemock")
141
+ filemock.expects(:write).with(vm.uuid)
142
+ File.expects(:open).with(Vagrant::Env.dotfile_path, 'w+').once.yields(filemock)
143
+ Vagrant::Env.persist_vm(vm)
144
+ end
145
+ end
146
+
147
+ context "loading the UUID out from the persisted file" do
148
+ test "loading of the uuid from the dotfile" do
149
+ mock_persisted_vm
150
+ assert_equal 'foovm', Vagrant::Env.persisted_vm
151
+ end
152
+
153
+ should "do nothing if the root path is nil" do
154
+ File.expects(:open).never
155
+ Vagrant::Env.stubs(:root_path).returns(nil)
156
+ Vagrant::Env.load_vm!
157
+ end
158
+
159
+ test "uuid should be nil if dotfile didn't exist" do
160
+ File.expects(:open).raises(Errno::ENOENT)
161
+ Vagrant::Env.load_vm!
162
+ assert_nil Vagrant::Env.persisted_vm
163
+ end
164
+
165
+ test "should build up the dotfile out of the root path and the dotfile name" do
166
+ assert_equal File.join(Vagrant::Env.root_path, Vagrant.config.vagrant.dotfile_name), Vagrant::Env.dotfile_path
167
+ end
168
+ end
169
+
170
+ context "loading the root path" do
171
+ test "should walk the parent directories looking for rootfile" do
172
+ Vagrant::Env.expects(:error_and_exit).once
173
+
174
+ paths = [
175
+ Pathname.new("/foo/bar/baz"),
176
+ Pathname.new("/foo/bar"),
177
+ Pathname.new("/foo")
178
+ ]
179
+
180
+ search_seq = sequence("search_seq")
181
+ paths.each do |path|
182
+ File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(false).in_sequence(search_seq)
183
+ end
184
+
185
+ assert_nil Vagrant::Env.load_root_path!(paths.first)
186
+ end
187
+
188
+ test "should print out an error and exit if not found" do
189
+ path = Pathname.new("/")
190
+
191
+ Vagrant::Env.expects(:error_and_exit).once
192
+ Vagrant::Env.load_root_path!(path)
193
+ end
194
+
195
+ should "return false if suppress errors is set and no root path is found" do
196
+ path = Pathname.new("/")
197
+
198
+ Vagrant::Env.expects(:error_and_exit).never
199
+ assert !Vagrant::Env.load_root_path!(path, :suppress_errors => true)
200
+ end
201
+
202
+ should "pipe suppress errors flag through recursion" do
203
+ path = Pathname.new("/foo/bar/baz")
204
+ File.expects(:exist?).times(3).returns(false)
205
+
206
+ Vagrant::Env.expects(:error_and_exit).never
207
+ assert !Vagrant::Env.load_root_path!(path, :suppress_errors => true)
208
+ end
209
+
210
+ test "should set the path for the rootfile" do
211
+ path = "/foo"
212
+ File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(true)
213
+
214
+ assert Vagrant::Env.load_root_path!(Pathname.new(path))
215
+ assert_equal path, Vagrant::Env.root_path
216
+ end
217
+ end
218
+
219
+ context "home directory paths" do
220
+ should "return the expanded config for `home_path`" do
221
+ assert_equal File.expand_path(Vagrant.config.vagrant.home), Vagrant::Env.home_path
222
+ end
223
+
224
+ should "return the home_path joined with tmp for a tmp path" do
225
+ @home_path = "foo"
226
+ Vagrant::Env.stubs(:home_path).returns(@home_path)
227
+ assert_equal File.join(@home_path, "tmp"), Vagrant::Env.tmp_path
228
+ end
229
+
230
+ should "return the boxes path" do
231
+ @home_path = "foo"
232
+ Vagrant::Env.stubs(:home_path).returns(@home_path)
233
+ assert_equal File.join(@home_path, "boxes"), Vagrant::Env.boxes_path
234
+ end
235
+ end
236
+
237
+ context "loading box" do
238
+ setup do
239
+ @box = mock("box")
240
+
241
+ Vagrant::Env.stubs(:load_config!)
242
+ end
243
+
244
+ should "not load the box if its not set" do
245
+ mock_config do |config|
246
+ config.vm.box = nil
247
+ end
248
+
249
+ Vagrant::Box.expects(:find).never
250
+ Vagrant::Env.load_box!
251
+ end
252
+
253
+ should "set the box to what is found by the Box class" do
254
+ Vagrant::Box.expects(:find).with(Vagrant.config.vm.box).once.returns(@box)
255
+ Vagrant::Env.load_box!
256
+ assert @box.equal?(Vagrant::Env.box)
257
+ end
258
+
259
+ should "load the config if a box is loaded" do
260
+ Vagrant::Env.expects(:load_config!).once
261
+ Vagrant::Box.expects(:find).returns(@box)
262
+ Vagrant::Env.load_box!
263
+ end
264
+ end
265
+
266
+ context "requiring boxes" do
267
+ should "error and exit if no box is found" do
268
+ mock_config do |config|
269
+ config.vm.box = nil
270
+ end
271
+
272
+ Vagrant::Env.expects(:box).returns(nil)
273
+ Vagrant::Env.expects(:error_and_exit).once.with() do |msg|
274
+ assert msg =~ /no base box was specified/i
275
+ true
276
+ end
277
+ Vagrant::Env.require_box
278
+ end
279
+
280
+ should "error and exit if box is specified but doesn't exist" do
281
+ mock_config do |config|
282
+ config.vm.box = "foo"
283
+ end
284
+
285
+ Vagrant::Env.expects(:box).returns(nil)
286
+ Vagrant::Env.expects(:error_and_exit).once.with() do |msg|
287
+ assert msg =~ /does not exist/i
288
+ true
289
+ end
290
+ Vagrant::Env.require_box
291
+ end
292
+ end
293
+ end
@@ -0,0 +1,95 @@
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
+ context "connecting to SSH" do
9
+ setup do
10
+ @script = Vagrant::SSH::SCRIPT
11
+ end
12
+
13
+ test "should call exec with defaults when no options are supplied" do
14
+ ssh = Vagrant.config.ssh
15
+ Kernel.expects(:exec).with("#{@script} #{ssh[:username]} #{ssh[:password]} #{ssh[:host]} #{Vagrant::SSH.port}")
16
+ Vagrant::SSH.connect
17
+ end
18
+
19
+ test "should call exec with supplied params" do
20
+ args = {:username => 'bar', :password => 'baz', :host => 'bak', :port => 'bag'}
21
+ Kernel.expects(:exec).with("#{@script} #{args[:username]} #{args[:password]} #{args[:host]} #{args[:port]}")
22
+ Vagrant::SSH.connect(args)
23
+ end
24
+ end
25
+
26
+ context "executing ssh commands" do
27
+ should "call net::ssh.start with the proper names" do
28
+ Net::SSH.expects(:start).once.with() do |host, username, opts|
29
+ assert_equal Vagrant.config.ssh.host, host
30
+ assert_equal Vagrant.config.ssh.username, username
31
+ assert_equal Vagrant::SSH.port, opts[:port]
32
+ assert_equal Vagrant.config.ssh.password, opts[:password]
33
+ true
34
+ end
35
+ Vagrant::SSH.execute
36
+ end
37
+
38
+ should "use custom host if set" do
39
+ Vagrant.config.ssh.host = "foo"
40
+ Net::SSH.expects(:start).with(Vagrant.config.ssh.host, Vagrant.config.ssh.username, anything).once
41
+ Vagrant::SSH.execute
42
+ end
43
+ end
44
+
45
+ context "SCPing files to the remote host" do
46
+ should "use Vagrant::SSH execute to setup an SCP connection and upload" do
47
+ scp = mock("scp")
48
+ ssh = mock("ssh")
49
+ scp.expects(:upload!).with("foo", "bar").once
50
+ Net::SCP.expects(:new).with(ssh).returns(scp).once
51
+ Vagrant::SSH.expects(:execute).yields(ssh).once
52
+ Vagrant::SSH.upload!("foo", "bar")
53
+ end
54
+ end
55
+
56
+ context "checking if host is up" do
57
+ setup do
58
+ mock_config
59
+ end
60
+
61
+ should "return true if SSH connection works" do
62
+ Net::SSH.expects(:start).yields("success")
63
+ assert Vagrant::SSH.up?
64
+ end
65
+
66
+ should "return false if SSH connection times out" do
67
+ Net::SSH.expects(:start)
68
+ assert !Vagrant::SSH.up?
69
+ end
70
+
71
+ should "return false if the connection is refused" do
72
+ Net::SSH.expects(:start).raises(Errno::ECONNREFUSED)
73
+ assert_nothing_raised {
74
+ assert !Vagrant::SSH.up?
75
+ }
76
+ end
77
+
78
+ should "return false if the connection is dropped" do
79
+ Net::SSH.expects(:start).raises(Net::SSH::Disconnect)
80
+ assert_nothing_raised {
81
+ assert !Vagrant::SSH.up?
82
+ }
83
+ end
84
+ end
85
+
86
+ context "getting the ssh port" do
87
+ should "return the configured port by default" do
88
+ assert_equal Vagrant.config.vm.forwarded_ports[Vagrant.config.ssh.forwarded_port_key][:hostport], Vagrant::SSH.port
89
+ end
90
+
91
+ should "return the port given in options if it exists" do
92
+ assert_equal "47", Vagrant::SSH.port({ :port => "47" })
93
+ end
94
+ end
95
+ end