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,55 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
2
+
3
+ class BootActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Boot)
6
+ @runner.stubs(:invoke_callback)
7
+ mock_config
8
+ end
9
+
10
+ context "preparing" do
11
+ should "add the root shared folder" do
12
+ @runner.env.config.vm.expects(:share_folder).with("vagrant-root", @runner.env.config.vm.project_directory, @runner.env.root_path).once
13
+ @action.prepare
14
+ end
15
+ end
16
+
17
+ context "execution" do
18
+ should "invoke the 'boot' around callback" do
19
+ boot_seq = sequence("boot_seq")
20
+ @runner.expects(:invoke_around_callback).with(:boot).once.in_sequence(boot_seq).yields
21
+ @action.expects(:boot).in_sequence(boot_seq)
22
+ @action.expects(:wait_for_boot).returns(true).in_sequence(boot_seq)
23
+ @action.execute!
24
+ end
25
+
26
+ should "error and exit if the bootup failed" do
27
+ fail_boot_seq = sequence("fail_boot_seq")
28
+ @action.expects(:boot).once.in_sequence(fail_boot_seq)
29
+ @action.expects(:wait_for_boot).returns(false).in_sequence(fail_boot_seq)
30
+ @action.expects(:error_and_exit).with(:vm_failed_to_boot).once.in_sequence(fail_boot_seq)
31
+ @action.execute!
32
+ end
33
+ end
34
+
35
+ context "booting" do
36
+ should "start the VM in headless mode" do
37
+ @vm.expects(:start).with(:headless, true).once
38
+ @action.boot
39
+ end
40
+ end
41
+
42
+ context "waiting for boot" do
43
+ should "repeatedly ping the SSH port and return false with no response" do
44
+ seq = sequence('pings')
45
+ @runner.env.ssh.expects(:up?).times(@runner.env.config.ssh.max_tries.to_i - 1).returns(false).in_sequence(seq)
46
+ @runner.env.ssh.expects(:up?).once.returns(true).in_sequence(seq)
47
+ assert @action.wait_for_boot(0)
48
+ end
49
+
50
+ should "ping the max number of times then just return" do
51
+ @runner.env.ssh.expects(:up?).times(Vagrant.config.ssh.max_tries.to_i).returns(false)
52
+ assert !@action.wait_for_boot(0)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,16 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
2
+
3
+ class CustomizeActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Customize)
6
+ mock_config
7
+ end
8
+
9
+ context "executing" do
10
+ should "run the VM customization procs then save the VM" do
11
+ @runner.env.config.vm.expects(:run_procs!).with(@vm)
12
+ @vm.expects(:save).with(true).once
13
+ @action.execute!
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,36 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
2
+
3
+ class DestroyActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Destroy)
6
+ end
7
+
8
+ context "executing" do
9
+ should "invoke an around callback around the destroy" do
10
+ @runner.expects(:invoke_around_callback).with(:destroy).once
11
+ @action.execute!
12
+ end
13
+
14
+ should "destroy VM and clear persist" do
15
+ @runner.stubs(:invoke_around_callback).yields
16
+ clear_seq = sequence("clear")
17
+ @action.expects(:destroy_vm).in_sequence(clear_seq)
18
+ @action.expects(:depersist).in_sequence(clear_seq)
19
+ @action.execute!
20
+ end
21
+ end
22
+
23
+ context "destroying the VM" do
24
+ should "destroy VM and attached images" do
25
+ @vm.expects(:destroy).with(:destroy_image => true).once
26
+ @action.destroy_vm
27
+ end
28
+ end
29
+
30
+ context "depersisting" do
31
+ should "call depersist_vm on Env" do
32
+ @runner.env.expects(:depersist_vm).once
33
+ @action.depersist
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
2
+
3
+ class DownActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @mock_vm, @vm, @action = mock_action(Vagrant::Actions::VM::Down)
6
+ mock_config
7
+ end
8
+
9
+ context "preparing" do
10
+ setup do
11
+ @vm.stubs(:running?).returns(false)
12
+ end
13
+
14
+ def setup_action_expectations(order)
15
+ default_seq = sequence("default_seq")
16
+ order.each do |action|
17
+ @mock_vm.expects(:add_action).with(action).once.in_sequence(default_seq)
18
+ end
19
+ end
20
+
21
+ should "add the destroy action alone if VM is not running" do
22
+ setup_action_expectations([Vagrant::Actions::VM::Destroy])
23
+ @action.prepare
24
+ end
25
+
26
+ should "add the halt action if the VM is running" do
27
+ @vm.expects(:running?).returns(true)
28
+ setup_action_expectations([Vagrant::Actions::VM::Halt, Vagrant::Actions::VM::Destroy])
29
+ @action.prepare
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,88 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
2
+
3
+ class ExportActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Export)
6
+ mock_config
7
+ end
8
+
9
+ context "executing" do
10
+ should "setup the temp dir then export" do
11
+ exec_seq = sequence('execute')
12
+ @action.expects(:setup_temp_dir).once.in_sequence(exec_seq)
13
+ @action.expects(:export).once.in_sequence(exec_seq)
14
+ @action.execute!
15
+ end
16
+ end
17
+
18
+ context "setting up the temporary directory" do
19
+ setup do
20
+ @time_now = Time.now.to_i.to_s
21
+ Time.stubs(:now).returns(@time_now)
22
+
23
+ @tmp_path = "foo"
24
+ @runner.env.stubs(:tmp_path).returns(@tmp_path)
25
+
26
+ @temp_dir = File.join(@runner.env.tmp_path, @time_now)
27
+ FileUtils.stubs(:mkpath)
28
+ end
29
+
30
+ should "create the temporary directory using the current time" do
31
+ FileUtils.expects(:mkpath).with(@temp_dir).once
32
+ @action.setup_temp_dir
33
+ end
34
+
35
+ should "set the temporary directory to the temp_dir variable" do
36
+ @action.setup_temp_dir
37
+ assert_equal @temp_dir, @action.temp_dir
38
+ end
39
+ end
40
+
41
+ context "path to OVF file" do
42
+ setup do
43
+ @temp_dir = "foo"
44
+ @action.stubs(:temp_dir).returns(@temp_dir)
45
+ end
46
+
47
+ should "be the temporary directory joined with the OVF filename" do
48
+ assert_equal File.join(@temp_dir, @runner.env.config.vm.box_ovf), @action.ovf_path
49
+ end
50
+ end
51
+
52
+ context "exporting" do
53
+ setup do
54
+ @ovf_path = mock("ovf_path")
55
+ @action.stubs(:ovf_path).returns(@ovf_path)
56
+ end
57
+
58
+ should "call export on the runner with the ovf path" do
59
+ @vm.expects(:export).with(@ovf_path, {}, true).once
60
+ @action.export
61
+ end
62
+ end
63
+
64
+ context "cleanup" do
65
+ setup do
66
+ @temp_dir = "foo"
67
+ @action.stubs(:temp_dir).returns(@temp_dir)
68
+ end
69
+
70
+ should "remove the temporary directory" do
71
+ FileUtils.expects(:rm_r).with(@temp_dir).once
72
+ @action.cleanup
73
+ end
74
+
75
+ should "not remove a directory if temp_dir is nil" do
76
+ FileUtils.expects(:rm_r).never
77
+ @action.stubs(:temp_dir).returns(nil)
78
+ @action.cleanup
79
+ end
80
+ end
81
+
82
+ context "rescue" do
83
+ should "call cleanup method" do
84
+ @action.expects(:cleanup).once
85
+ @action.rescue(nil)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,104 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
2
+
3
+ class ForwardPortsActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @mock_vm, @vm, @action = mock_action(Vagrant::Actions::VM::ForwardPorts)
6
+ mock_config
7
+ end
8
+
9
+ context "checking for colliding ports" do
10
+ setup do
11
+ @forwarded_port = mock("forwarded_port")
12
+ @forwarded_port.stubs(:hostport)
13
+ @forwarded_ports = [@forwarded_port]
14
+
15
+ @vm = mock("vm")
16
+ @vm.stubs(:forwarded_ports).returns(@forwarded_ports)
17
+ @vm.stubs(:running?).returns(true)
18
+ @vm.stubs(:uuid).returns("foo")
19
+ @mock_vm.stubs(:uuid).returns("bar")
20
+ vms = [@vm]
21
+ VirtualBox::VM.stubs(:all).returns(vms)
22
+
23
+ @env = mock_environment do |config|
24
+ config.vm.forwarded_ports.clear
25
+ config.vm.forward_port("ssh", 22, 2222)
26
+ end
27
+
28
+ @mock_vm.stubs(:env).returns(@env)
29
+ end
30
+
31
+ should "ignore vms which aren't running" do
32
+ @vm.expects(:running?).returns(false)
33
+ @vm.expects(:forwarded_ports).never
34
+ @action.prepare
35
+ end
36
+
37
+ should "ignore vms which are equivalent to ours" do
38
+ @mock_vm.expects(:uuid).returns(@vm.uuid)
39
+ @vm.expects(:forwarded_ports).never
40
+ @action.prepare
41
+ end
42
+
43
+ should "not raise any errors if no forwarded ports collide" do
44
+ @forwarded_port.expects(:hostport).returns(80)
45
+ assert_nothing_raised { @action.prepare }
46
+ end
47
+
48
+ should "raise an ActionException if a port collides" do
49
+ @forwarded_port.expects(:hostport).returns(2222)
50
+ assert_raises(Vagrant::Actions::ActionException) {
51
+ @action.prepare
52
+ }
53
+ end
54
+
55
+ should "convert ports to strings prior to checking" do
56
+ @forwarded_port.expects(:hostport).returns("2222")
57
+ assert_raises(Vagrant::Actions::ActionException) {
58
+ @action.prepare
59
+ }
60
+ end
61
+ end
62
+
63
+ context "execution" do
64
+ should "clear all previous ports and forward new ports" do
65
+ exec_seq = sequence("exec_seq")
66
+ @action.expects(:clear).once.in_sequence(exec_seq)
67
+ @action.expects(:forward_ports).once.in_sequence(exec_seq)
68
+ @action.execute!
69
+ end
70
+ end
71
+
72
+ context "forwarding ports" do
73
+ should "create a port forwarding for the VM" do
74
+ forwarded_ports = mock("forwarded_ports")
75
+
76
+ @mock_vm.env.config.vm.forwarded_ports.each do |name, opts|
77
+ forwarded_ports.expects(:<<).with do |port|
78
+ assert_equal name, port.name
79
+ assert_equal opts[:hostport], port.hostport
80
+ assert_equal opts[:guestport], port.guestport
81
+ true
82
+ end
83
+ end
84
+
85
+ @vm.expects(:forwarded_ports).returns(forwarded_ports)
86
+ @vm.expects(:save).with(true).once
87
+ @action.forward_ports
88
+ end
89
+ end
90
+
91
+ context "clearing forwarded ports" do
92
+ should "call destroy on all forwarded ports" do
93
+ forwarded_ports = []
94
+ 5.times do |i|
95
+ port = mock("port#{i}")
96
+ port.expects(:destroy).with(true).once
97
+ forwarded_ports << port
98
+ end
99
+
100
+ @vm.expects(:forwarded_ports).returns(forwarded_ports)
101
+ @action.clear
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
2
+
3
+ class HaltActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Halt)
6
+ mock_config
7
+ end
8
+
9
+ context "executing" do
10
+ setup do
11
+ @vm.stubs(:running?).returns(true)
12
+ end
13
+
14
+ should "force the VM to stop" do
15
+ @vm.expects(:stop).with(true).once
16
+ @action.execute!
17
+ end
18
+
19
+ should "raise an ActionException if VM is not running" do
20
+ @vm.stubs(:running?).returns(false)
21
+ @vm.expects(:stop).never
22
+ assert_raises(Vagrant::Actions::ActionException) {
23
+ @action.execute!
24
+ }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
2
+
3
+ class ImportActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @runner, @vm, @import = mock_action(Vagrant::Actions::VM::Import)
6
+
7
+ @ovf_file = "foo"
8
+ @box = mock("box")
9
+ @box.stubs(:ovf_file).returns(@ovf_file)
10
+ @runner.env.stubs(:box).returns(@box)
11
+
12
+ VirtualBox::VM.stubs(:import)
13
+ end
14
+
15
+ should "run in a busy block" do
16
+ Vagrant::Busy.expects(:busy).once
17
+ @import.execute!
18
+ end
19
+
20
+ should "invoke an around callback around the import" do
21
+ @runner.expects(:invoke_around_callback).with(:import).once
22
+ @import.execute!
23
+ end
24
+
25
+ should "call import on VirtualBox::VM with the proper base" do
26
+ VirtualBox::VM.expects(:import).once.with(@ovf_file).returns("foo")
27
+ assert_nothing_raised { @import.execute! }
28
+ end
29
+
30
+ should "raise an exception if import is nil" do
31
+ @runner.expects(:vm).returns(nil)
32
+ assert_raises(Vagrant::Actions::ActionException) {
33
+ @import.execute!
34
+ }
35
+ end
36
+
37
+ should "set the resulting VM as the VM of the Vagrant VM object" do
38
+ new_vm = mock("new_vm")
39
+ @runner.expects(:vm=).with(new_vm).once
40
+ VirtualBox::VM.expects(:import).returns(new_vm).returns("foo")
41
+ @import.execute!
42
+ end
43
+ end
@@ -0,0 +1,108 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
2
+
3
+ class MoveHardDriveActionTest < Test::Unit::TestCase
4
+ setup do
5
+ @mock_vm, @vm, @action = mock_action(Vagrant::Actions::VM::MoveHardDrive)
6
+ @hd_location = "/foo"
7
+ mock_config do |config|
8
+ File.expects(:directory?).with(@hd_location).returns(true)
9
+ config.vm.hd_location = @hd_location
10
+ end
11
+ end
12
+
13
+
14
+ should "be able to identifiy a hard drive within the storage controllers" do
15
+ hd = mock('hd')
16
+ hd_image = mock('hd_image')
17
+ hd_image.expects(:is_a?).returns(true)
18
+ hd.expects(:image).returns(hd_image)
19
+
20
+ dvd = mock('dvd')
21
+ controller = mock('controller')
22
+ controller.expects(:devices).returns([hd, dvd])
23
+
24
+ @vm.expects(:storage_controllers).once.returns([controller])
25
+ assert_equal @action.find_hard_drive, hd
26
+ end
27
+
28
+ context "execution" do
29
+ should "error and exit if the vm is not powered off" do
30
+ @mock_vm.expects(:powered_off?).returns(false)
31
+ @action.expects(:error_and_exit).with(:vm_power_off_to_move_hd).once
32
+ @action.execute!
33
+ end
34
+
35
+ should "move the hard drive if vm is powered off" do
36
+ @mock_vm.expects(:powered_off?).returns(true)
37
+ @action.expects(:error_and_exit).never
38
+ @action.expects(:destroy_drive_after).once
39
+ @action.execute!
40
+ end
41
+ end
42
+
43
+ context "new image path" do
44
+ setup do
45
+ @hd = mock("hd")
46
+ @image = mock("image")
47
+ @filename = "foo"
48
+ @hd.stubs(:image).returns(@image)
49
+ @image.stubs(:filename).returns(@filename)
50
+ @action.stubs(:hard_drive).returns(@hd)
51
+ end
52
+
53
+ should "be the configured hd location and the existing hard drive filename" do
54
+ joined = File.join(Vagrant.config.vm.hd_location, @filename)
55
+ assert_equal joined, @action.new_image_path
56
+ end
57
+ end
58
+
59
+ context "cloning and attaching new image" do
60
+ setup do
61
+ @hd = mock("hd")
62
+ @image = mock("image")
63
+ @hd.stubs(:image).returns(@image)
64
+ @action.stubs(:hard_drive).returns(@hd)
65
+ @new_image_path = "foo"
66
+ @action.stubs(:new_image_path).returns(@new_image_path)
67
+ end
68
+
69
+ should "clone to the new path" do
70
+ new_image = mock("new_image")
71
+ @image.expects(:clone).with(@new_image_path, Vagrant.config.vm.disk_image_format, true).returns(new_image).once
72
+ @hd.expects(:image=).with(new_image).once
73
+ @vm.expects(:save).once
74
+ @action.clone_and_attach
75
+ end
76
+ end
77
+
78
+ context "destroying the old image" do
79
+ setup do
80
+ @hd = mock("hd")
81
+ @action.stubs(:hard_drive).returns(@hd)
82
+ end
83
+
84
+ should "yield the block, and destroy the old image after" do
85
+ image = mock("image")
86
+ image.stubs(:filename).returns("foo")
87
+ destroy_seq = sequence("destroy_seq")
88
+ @hd.expects(:image).returns(image).in_sequence(destroy_seq)
89
+ @hd.expects(:foo).once.in_sequence(destroy_seq)
90
+ image.expects(:destroy).with(true).once.in_sequence(destroy_seq)
91
+
92
+ @action.destroy_drive_after { @hd.foo }
93
+ end
94
+
95
+ # Ensures that the image is not destroyed in an "ensure" block
96
+ should "not destroy the image if an exception is raised" do
97
+ image = mock("image")
98
+ image.expects(:destroy).never
99
+ @hd.expects(:image).returns(image)
100
+
101
+ assert_raises(Exception) do
102
+ @action.destroy_drive_after do
103
+ raise Exception.new("FOO")
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end