vagrantup 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -2
  3. data/README.md +2 -2
  4. data/Rakefile +1 -1
  5. data/VERSION +1 -1
  6. data/config/default.rb +13 -3
  7. data/lib/vagrant.rb +10 -13
  8. data/lib/vagrant/actions/base.rb +14 -2
  9. data/lib/vagrant/actions/box/download.rb +2 -7
  10. data/lib/vagrant/actions/box/verify.rb +1 -1
  11. data/lib/vagrant/actions/runner.rb +0 -1
  12. data/lib/vagrant/actions/vm/boot.rb +2 -6
  13. data/lib/vagrant/actions/vm/customize.rb +7 -5
  14. data/lib/vagrant/actions/vm/destroy.rb +4 -3
  15. data/lib/vagrant/actions/vm/down.rb +6 -3
  16. data/lib/vagrant/actions/vm/export.rb +2 -4
  17. data/lib/vagrant/actions/vm/forward_ports.rb +77 -16
  18. data/lib/vagrant/actions/vm/halt.rb +10 -2
  19. data/lib/vagrant/actions/vm/import.rb +2 -4
  20. data/lib/vagrant/actions/vm/move_hard_drive.rb +2 -2
  21. data/lib/vagrant/actions/vm/network.rb +120 -0
  22. data/lib/vagrant/actions/vm/package.rb +11 -7
  23. data/lib/vagrant/actions/vm/provision.rb +3 -3
  24. data/lib/vagrant/actions/vm/reload.rb +2 -9
  25. data/lib/vagrant/actions/vm/shared_folders.rb +19 -39
  26. data/lib/vagrant/actions/vm/start.rb +10 -2
  27. data/lib/vagrant/actions/vm/up.rb +5 -6
  28. data/lib/vagrant/active_list.rb +23 -13
  29. data/lib/vagrant/box.rb +2 -2
  30. data/lib/vagrant/busy.rb +3 -3
  31. data/lib/vagrant/command.rb +2 -2
  32. data/lib/vagrant/commands/base.rb +40 -20
  33. data/lib/vagrant/commands/destroy.rb +17 -3
  34. data/lib/vagrant/commands/halt.rb +23 -3
  35. data/lib/vagrant/commands/package.rb +54 -14
  36. data/lib/vagrant/commands/provision.rb +31 -0
  37. data/lib/vagrant/commands/reload.rb +16 -3
  38. data/lib/vagrant/commands/resume.rb +16 -3
  39. data/lib/vagrant/commands/ssh.rb +25 -3
  40. data/lib/vagrant/commands/ssh_config.rb +20 -5
  41. data/lib/vagrant/commands/status.rb +107 -40
  42. data/lib/vagrant/commands/suspend.rb +16 -3
  43. data/lib/vagrant/commands/up.rb +26 -7
  44. data/lib/vagrant/config.rb +82 -12
  45. data/lib/vagrant/downloaders/base.rb +8 -1
  46. data/lib/vagrant/downloaders/http.rb +31 -19
  47. data/lib/vagrant/environment.rb +146 -49
  48. data/lib/vagrant/provisioners/base.rb +19 -5
  49. data/lib/vagrant/provisioners/chef.rb +12 -4
  50. data/lib/vagrant/provisioners/chef_server.rb +13 -6
  51. data/lib/vagrant/provisioners/chef_solo.rb +7 -3
  52. data/lib/vagrant/resource_logger.rb +126 -0
  53. data/lib/vagrant/ssh.rb +109 -8
  54. data/lib/vagrant/systems/base.rb +70 -0
  55. data/lib/vagrant/systems/linux.rb +137 -0
  56. data/lib/vagrant/util.rb +1 -45
  57. data/lib/vagrant/util/error_helper.rb +13 -0
  58. data/lib/vagrant/util/glob_loader.rb +22 -0
  59. data/lib/vagrant/util/output_helper.rb +9 -0
  60. data/lib/vagrant/util/plain_logger.rb +12 -0
  61. data/lib/vagrant/util/platform.rb +7 -2
  62. data/lib/vagrant/util/template_renderer.rb +2 -2
  63. data/lib/vagrant/util/translator.rb +35 -0
  64. data/lib/vagrant/vm.rb +91 -10
  65. data/templates/crontab_entry.erb +1 -0
  66. data/templates/network_entry.erb +8 -0
  67. data/templates/ssh_config.erb +1 -0
  68. data/templates/{errors.yml → strings.yml} +111 -3
  69. data/templates/sync.erb +14 -0
  70. data/test/test_helper.rb +46 -3
  71. data/test/vagrant/actions/box/download_test.rb +0 -17
  72. data/test/vagrant/actions/vm/boot_test.rb +3 -10
  73. data/test/vagrant/actions/vm/customize_test.rb +6 -0
  74. data/test/vagrant/actions/vm/destroy_test.rb +6 -5
  75. data/test/vagrant/actions/vm/down_test.rb +5 -11
  76. data/test/vagrant/actions/vm/export_test.rb +1 -0
  77. data/test/vagrant/actions/vm/forward_ports_test.rb +92 -15
  78. data/test/vagrant/actions/vm/halt_test.rb +36 -4
  79. data/test/vagrant/actions/vm/import_test.rb +2 -0
  80. data/test/vagrant/actions/vm/network_test.rb +237 -0
  81. data/test/vagrant/actions/vm/package_test.rb +35 -5
  82. data/test/vagrant/actions/vm/provision_test.rb +3 -3
  83. data/test/vagrant/actions/vm/reload_test.rb +1 -1
  84. data/test/vagrant/actions/vm/shared_folders_test.rb +41 -74
  85. data/test/vagrant/actions/vm/start_test.rb +41 -3
  86. data/test/vagrant/actions/vm/up_test.rb +10 -21
  87. data/test/vagrant/active_list_test.rb +28 -43
  88. data/test/vagrant/commands/base_test.rb +25 -4
  89. data/test/vagrant/commands/destroy_test.rb +24 -12
  90. data/test/vagrant/commands/halt_test.rb +33 -11
  91. data/test/vagrant/commands/package_test.rb +77 -57
  92. data/test/vagrant/commands/provision_test.rb +50 -0
  93. data/test/vagrant/commands/reload_test.rb +27 -11
  94. data/test/vagrant/commands/resume_test.rb +25 -14
  95. data/test/vagrant/commands/ssh_config_test.rb +40 -17
  96. data/test/vagrant/commands/ssh_test.rb +52 -13
  97. data/test/vagrant/commands/status_test.rb +21 -1
  98. data/test/vagrant/commands/suspend_test.rb +25 -14
  99. data/test/vagrant/commands/up_test.rb +25 -19
  100. data/test/vagrant/config_test.rb +74 -18
  101. data/test/vagrant/downloaders/base_test.rb +2 -1
  102. data/test/vagrant/downloaders/http_test.rb +18 -8
  103. data/test/vagrant/environment_test.rb +245 -77
  104. data/test/vagrant/provisioners/base_test.rb +4 -4
  105. data/test/vagrant/provisioners/chef_server_test.rb +18 -7
  106. data/test/vagrant/provisioners/chef_solo_test.rb +17 -7
  107. data/test/vagrant/provisioners/chef_test.rb +22 -9
  108. data/test/vagrant/resource_logger_test.rb +144 -0
  109. data/test/vagrant/ssh_session_test.rb +46 -0
  110. data/test/vagrant/ssh_test.rb +42 -2
  111. data/test/vagrant/systems/linux_test.rb +174 -0
  112. data/test/vagrant/util/error_helper_test.rb +5 -0
  113. data/test/vagrant/util/output_helper_test.rb +5 -0
  114. data/test/vagrant/util/plain_logger_test.rb +17 -0
  115. data/test/vagrant/util/platform_test.rb +18 -0
  116. data/test/vagrant/util/{errors_test.rb → translator_test.rb} +25 -21
  117. data/test/vagrant/util_test.rb +12 -49
  118. data/test/vagrant/vm_test.rb +133 -11
  119. data/vagrant.gemspec +39 -15
  120. metadata +38 -14
  121. data/lib/vagrant/commands/down.rb +0 -16
  122. data/lib/vagrant/util/errors.rb +0 -36
  123. data/lib/vagrant/util/progress_meter.rb +0 -33
  124. data/test/vagrant/commands/down_test.rb +0 -17
  125. data/test/vagrant/util/progress_meter_test.rb +0 -33
@@ -2,11 +2,14 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
2
2
 
3
3
  class StartActionTest < Test::Unit::TestCase
4
4
  setup do
5
- @mock_vm, @vm, @action = mock_action(Vagrant::Actions::VM::Start)
5
+ @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Start)
6
+
7
+ @action.options[:provision] = true
6
8
  end
7
9
 
8
10
  context "sub-actions" do
9
11
  setup do
12
+ @runner.stubs(:created?).returns(false)
10
13
  @vm.stubs(:saved?).returns(true)
11
14
  File.stubs(:file?).returns(true)
12
15
  File.stubs(:exist?).returns(true)
@@ -16,7 +19,7 @@ class StartActionTest < Test::Unit::TestCase
16
19
  def setup_action_expectations
17
20
  default_seq = sequence("default_seq")
18
21
  @default_order.flatten.each do |action|
19
- @mock_vm.expects(:add_action).with(action).once.in_sequence(default_seq)
22
+ @runner.expects(:add_action).with(action, @action.options).once.in_sequence(default_seq)
20
23
  end
21
24
  end
22
25
 
@@ -27,9 +30,44 @@ class StartActionTest < Test::Unit::TestCase
27
30
 
28
31
  should "add customize to the beginning if its not saved" do
29
32
  @vm.expects(:saved?).returns(false)
30
- @default_order.unshift([Vagrant::Actions::VM::Customize, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders])
33
+ @default_order.unshift([Vagrant::Actions::VM::Customize, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Network])
34
+ setup_action_expectations
35
+ @action.prepare
36
+ end
37
+
38
+ should "add do additional if VM is not created yet" do
39
+ @runner.stubs(:vm).returns(nil)
40
+ @default_order.unshift([Vagrant::Actions::VM::Customize, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Network])
31
41
  setup_action_expectations
32
42
  @action.prepare
33
43
  end
44
+
45
+ should "add provisioning if its enabled and not saved" do
46
+ @vm.env.config.vm.provisioner = :chef_solo
47
+
48
+ @runner.stubs(:vm).returns(nil)
49
+ @default_order.unshift([Vagrant::Actions::VM::Customize, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Network])
50
+ @default_order << Vagrant::Actions::VM::Provision
51
+ setup_action_expectations
52
+ @action.prepare
53
+ end
54
+ end
55
+
56
+ context "provision?" do
57
+ should "return false if no provisioner is set" do
58
+ @vm.env.config.vm.provisioner = nil
59
+ assert !@action.provision?
60
+ end
61
+
62
+ should "return true if a provisioner is set" do
63
+ @vm.env.config.vm.provisioner = :chef_solo
64
+ assert @action.provision?
65
+ end
66
+
67
+ should "return false if provisioning is specifically disabled" do
68
+ @vm.env.config.vm.provisioner = :chef_solo
69
+ @action.options[:provision] = false
70
+ assert !@action.provision?
71
+ end
34
72
  end
35
73
  end
@@ -7,9 +7,11 @@ class UpActionTest < Test::Unit::TestCase
7
7
 
8
8
  context "sub-actions" do
9
9
  setup do
10
+ @runner.stubs(:created?).returns(false)
11
+
10
12
  File.stubs(:file?).returns(true)
11
13
  File.stubs(:exist?).returns(true)
12
- @default_order = [Vagrant::Actions::VM::Import, Vagrant::Actions::VM::Customize, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Boot]
14
+ @default_order = [Vagrant::Actions::VM::Import, Vagrant::Actions::VM::Start]
13
15
 
14
16
  @dotfile_path = "foo"
15
17
  @runner.env.stubs(:dotfile_path).returns(@dotfile_path)
@@ -18,7 +20,7 @@ class UpActionTest < Test::Unit::TestCase
18
20
  def setup_action_expectations
19
21
  default_seq = sequence("default_seq")
20
22
  @default_order.each do |action|
21
- @runner.expects(:add_action).with(action).once.in_sequence(default_seq)
23
+ @runner.expects(:add_action).with(action, @action.options).once.in_sequence(default_seq)
22
24
  end
23
25
  end
24
26
 
@@ -47,19 +49,6 @@ class UpActionTest < Test::Unit::TestCase
47
49
  @action.prepare
48
50
  end
49
51
 
50
- should "add in the provisioning step if enabled" do
51
- env = mock_environment do |config|
52
- config.vm.provisioner = "foo"
53
- end
54
-
55
- @runner.stubs(:env).returns(env)
56
- env.stubs(:dotfile_path).returns(@dotfile_path)
57
-
58
- @default_order.push(Vagrant::Actions::VM::Provision)
59
- setup_action_expectations
60
- @action.prepare
61
- end
62
-
63
52
  should "add in the action to move hard drive if config is set" do
64
53
  env = mock_environment do |config|
65
54
  File.expects(:directory?).with("foo").returns(true)
@@ -76,19 +65,19 @@ class UpActionTest < Test::Unit::TestCase
76
65
  end
77
66
 
78
67
  context "callbacks" do
79
- should "call persist and mac address setup after import" do
68
+ should "call update dotfile and mac address setup after import" do
80
69
  boot_seq = sequence("boot")
81
- @action.expects(:persist).once.in_sequence(boot_seq)
70
+ @action.expects(:update_dotfile).once.in_sequence(boot_seq)
82
71
  @action.expects(:setup_mac_address).once.in_sequence(boot_seq)
83
72
  @action.after_import
84
73
  end
85
74
  end
86
75
 
87
- context "persisting" do
88
- should "persist the VM with Env" do
76
+ context "updating the dotfile" do
77
+ should "call update dotfile on the VM's environment" do
89
78
  @runner.stubs(:uuid)
90
- @runner.env.expects(:persist_vm).once
91
- @action.persist
79
+ @runner.env.expects(:update_dotfile).once
80
+ @action.update_dotfile
92
81
  end
93
82
  end
94
83
 
@@ -36,13 +36,13 @@ class ActiveListTest < Test::Unit::TestCase
36
36
  should "not load if the active json file doesn't exist" do
37
37
  File.expects(:file?).with(@list.path).returns(false)
38
38
  File.expects(:open).never
39
- assert_equal [], @list.list(true)
39
+ assert_equal Hash.new, @list.list(true)
40
40
  end
41
41
 
42
42
  should "parse the JSON by reading the file" do
43
43
  file = mock("file")
44
44
  data = mock("data")
45
- result = mock("result")
45
+ result = { :hey => :yep }
46
46
  File.expects(:file?).returns(true)
47
47
  File.expects(:open).with(@list.path, 'r').once.yields(file)
48
48
  file.expects(:read).returns(data)
@@ -57,52 +57,43 @@ class ActiveListTest < Test::Unit::TestCase
57
57
  assert result.equal?(@list.list)
58
58
  assert result.equal?(@list.list)
59
59
  end
60
- end
61
60
 
62
- context "vms" do
63
- setup do
64
- @the_list = ["foo", "bar"]
65
- @list.stubs(:list).returns(@the_list)
66
- end
61
+ should "be an empty hash if JSON parsing raises an exception" do
62
+ file = mock("file")
63
+ file.stubs(:read)
64
+ File.expects(:file?).returns(true)
65
+ File.expects(:open).with(@list.path, 'r').once.yields(file)
66
+ JSON.expects(:parse).raises(Exception)
67
67
 
68
- should "return the list, but with each value as a VM" do
69
- new_seq = sequence("new")
70
- results = []
71
- @the_list.each do |item|
72
- result = mock("result-#{item}")
73
- Vagrant::VM.expects(:find).with(item).returns(result).in_sequence(new_seq)
74
- results << result
68
+ assert_nothing_raised do
69
+ assert_equal Hash.new, @list.list(true)
75
70
  end
76
-
77
- assert_equal results, @list.vms
78
- end
79
-
80
- should "compact out the nil values" do
81
- Vagrant::VM.stubs(:find).returns(nil)
82
- results = @list.vms
83
- assert results.empty?
84
71
  end
85
72
  end
86
73
 
87
- context "filtered list" do
88
- should "return a list of UUIDs from the VMs" do
89
- vms = []
90
- result = []
74
+ context "filter list" do
75
+ should "remove nonexistent VMs" do
76
+ list = {}
77
+ result = {}
91
78
  5.times do |i|
92
79
  vm = mock("vm#{i}")
93
- vm.expects(:uuid).returns(i)
94
- result << i
95
- vms << vm
80
+ vm.stubs(:uuid).returns(i)
81
+
82
+ list[vm.uuid] = {}
83
+
84
+ found_vm = i % 2 ? nil : vm
85
+ Vagrant::VM.stubs(:find).with(vm.uuid, @env).returns(found_vm)
86
+ results[vm.uuid] = {} if found_vm
96
87
  end
97
88
 
98
- @list.stubs(:vms).returns(vms)
99
- assert_equal result, @list.filtered_list
89
+ @list.stubs(:list).returns(list)
90
+ assert_equal result, @list.filter_list
100
91
  end
101
92
  end
102
93
 
103
94
  context "adding a VM to the list" do
104
95
  setup do
105
- @the_list = []
96
+ @the_list = {}
106
97
  @list.stubs(:list).returns(@the_list)
107
98
  @list.stubs(:save)
108
99
 
@@ -113,19 +104,13 @@ class ActiveListTest < Test::Unit::TestCase
113
104
 
114
105
  should "add the VMs UUID to the list" do
115
106
  @list.add(@vm)
116
- assert_equal [@uuid], @the_list
117
- end
118
-
119
- should "uniq the array so multiples never exist" do
120
- @the_list << @uuid
121
- assert_equal 1, @the_list.length
122
- @list.add(@vm)
123
- assert_equal 1, @the_list.length
107
+ assert @the_list[@uuid]
108
+ assert @the_list[@uuid].is_a?(Hash)
124
109
  end
125
110
 
126
111
  should "save after adding" do
127
112
  save_seq = sequence('save')
128
- @the_list.expects(:<<).in_sequence(save_seq)
113
+ @the_list.expects(:[]=).in_sequence(save_seq)
129
114
  @list.expects(:save).in_sequence(save_seq)
130
115
  @list.add(@vm)
131
116
  end
@@ -165,7 +150,7 @@ class ActiveListTest < Test::Unit::TestCase
165
150
  context "saving" do
166
151
  setup do
167
152
  @filtered = ["zoo"]
168
- @list.stubs(:filtered_list).returns(@filtered)
153
+ @list.stubs(:filter_list).returns(@filtered)
169
154
  end
170
155
 
171
156
  should "open the JSON path and save to it" do
@@ -67,20 +67,40 @@ class CommandsBaseTest < Test::Unit::TestCase
67
67
  context "executing" do
68
68
  should "show version if flag is set" do
69
69
  @instance.expects(:puts_version).once
70
- @instance.expects(:puts_help).never
70
+ @instance.expects(:show_help).never
71
71
  @instance.execute(["--version"])
72
72
  end
73
73
 
74
74
  should "just print the help by default" do
75
75
  @instance.expects(:puts_version).never
76
- @klass.expects(:puts_help)
76
+ @instance.expects(:show_help).once
77
77
  @instance.execute([])
78
78
  end
79
79
  end
80
80
 
81
+ context "all or single methods" do
82
+ should "call the single method if a name is given" do
83
+ name = "bar"
84
+ @instance.expects(:foo_single).with(name).once
85
+ @instance.all_or_single(["bar"], :foo)
86
+ end
87
+
88
+ should "call the single method for each VM if no name is given" do
89
+ vms = { :foo => nil, :bar => nil }
90
+ vms.keys.each do |name|
91
+ @instance.expects(:foo_single).with(name).once
92
+ end
93
+
94
+ @env.stubs(:vms).returns(vms)
95
+ @instance.all_or_single([], :foo)
96
+ end
97
+ end
98
+
81
99
  context "getting the option parser" do
82
100
  should "create it with the options spec if it hasn't been created yet" do
83
101
  opts = mock("opts")
102
+ opts.stubs(:on)
103
+
84
104
  result = mock("result")
85
105
  OptionParser.expects(:new).yields(opts).returns(result)
86
106
  @instance.expects(:options_spec).with(opts)
@@ -110,8 +130,9 @@ class CommandsBaseTest < Test::Unit::TestCase
110
130
  end
111
131
 
112
132
  should "parse the options with the args" do
113
- @option_parser.expects(:parse!).with(@args).once
114
- assert_equal @options, @instance.parse_options(@args)
133
+ result = mock("result")
134
+ @option_parser.expects(:parse!).with(@args).once.returns(result)
135
+ assert_equal result, @instance.parse_options(@args)
115
136
  end
116
137
  end
117
138
  end
@@ -4,29 +4,41 @@ class CommandsDestroyTest < Test::Unit::TestCase
4
4
  setup do
5
5
  @klass = Vagrant::Commands::Destroy
6
6
 
7
- @persisted_vm = mock("persisted_vm")
8
- @persisted_vm.stubs(:execute!)
9
-
10
7
  @env = mock_environment
11
- @env.stubs(:require_persisted_vm)
12
- @env.stubs(:vm).returns(@persisted_vm)
13
8
 
14
9
  @instance = @klass.new(@env)
15
10
  end
16
11
 
17
12
  context "executing" do
13
+ should "call all or single for the method" do
14
+ @instance.expects(:all_or_single).with([], :destroy)
15
+ @instance.execute
16
+ end
17
+ end
18
+
19
+ context "destroying a single VM" do
18
20
  setup do
19
- @persisted_vm.stubs(:destroy)
21
+ @foo_vm = mock("vm")
22
+ @foo_vm.stubs(:env).returns(@env)
23
+ vms = { :foo => @foo_vm }
24
+ @env.stubs(:vms).returns(vms)
25
+ end
26
+ should "error and exit if the VM doesn't exist" do
27
+ @env.stubs(:vms).returns({})
28
+ @instance.expects(:error_and_exit).with(:unknown_vm, :vm => :foo).once
29
+ @instance.destroy_single(:foo)
20
30
  end
21
31
 
22
- should "require a persisted VM" do
23
- @env.expects(:require_persisted_vm).once
24
- @instance.execute
32
+ should "destroy if its created" do
33
+ @foo_vm.stubs(:created?).returns(true)
34
+ @foo_vm.expects(:destroy).once
35
+ @instance.destroy_single(:foo)
25
36
  end
26
37
 
27
- should "destroy the persisted VM and the VM image" do
28
- @persisted_vm.expects(:destroy).once
29
- @instance.execute
38
+ should "do nothing if its not created" do
39
+ @foo_vm.stubs(:created?).returns(false)
40
+ @foo_vm.expects(:destroy).never
41
+ @instance.destroy_single(:foo)
30
42
  end
31
43
  end
32
44
  end
@@ -4,25 +4,47 @@ class CommandsHaltTest < Test::Unit::TestCase
4
4
  setup do
5
5
  @klass = Vagrant::Commands::Halt
6
6
 
7
- @persisted_vm = mock("persisted_vm")
8
- @persisted_vm.stubs(:execute!)
9
-
10
7
  @env = mock_environment
11
- @env.stubs(:require_persisted_vm)
12
- @env.stubs(:vm).returns(@persisted_vm)
13
-
14
8
  @instance = @klass.new(@env)
15
9
  end
16
10
 
17
11
  context "executing" do
18
- should "require a persisted VM" do
19
- @env.expects(:require_persisted_vm).once
12
+ should "call all or single for the method" do
13
+ @instance.expects(:all_or_single).with([], :halt)
20
14
  @instance.execute
21
15
  end
16
+ end
17
+
18
+ context "halting a single VM" do
19
+ setup do
20
+ @foo_vm = mock("vm")
21
+ @foo_vm.stubs(:env).returns(@env)
22
+ vms = { :foo => @foo_vm }
23
+ @env.stubs(:vms).returns(vms)
24
+ end
22
25
 
23
- should "call the `halt` action on the VM" do
24
- @persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Halt).once
25
- @instance.execute
26
+ should "error and exit if the VM doesn't exist" do
27
+ @env.stubs(:vms).returns({})
28
+ @instance.expects(:error_and_exit).with(:unknown_vm, :vm => :foo).once
29
+ @instance.halt_single(:foo)
30
+ end
31
+
32
+ should "halt if its created" do
33
+ @foo_vm.stubs(:created?).returns(true)
34
+ @foo_vm.expects(:halt).with(:force => false).once
35
+ @instance.execute(["foo"])
36
+ end
37
+
38
+ should "halt and force if specified" do
39
+ @foo_vm.stubs(:created?).returns(true)
40
+ @foo_vm.expects(:halt).with(:force => true).once
41
+ @instance.execute(["foo", "--force"])
42
+ end
43
+
44
+ should "do nothing if its not created" do
45
+ @foo_vm.stubs(:created?).returns(false)
46
+ @foo_vm.expects(:halt).never
47
+ @instance.halt_single(:foo)
26
48
  end
27
49
  end
28
50
  end
@@ -4,81 +4,101 @@ class CommandsPackageTest < Test::Unit::TestCase
4
4
  setup do
5
5
  @klass = Vagrant::Commands::Package
6
6
 
7
- @persisted_vm = mock("persisted_vm")
8
- @persisted_vm.stubs(:execute!)
9
-
10
7
  @env = mock_environment
11
- @env.stubs(:require_persisted_vm)
12
- @env.stubs(:vm).returns(@persisted_vm)
13
-
14
8
  @instance = @klass.new(@env)
15
9
  end
16
10
 
17
11
  context "executing" do
12
+ should "package base if a base is given" do
13
+ @instance.expects(:package_base).once
14
+ @instance.execute(["--base","foo"])
15
+ end
16
+
17
+ should "package single if no name is given" do
18
+ @instance.expects(:package_single).with(nil).once
19
+ @instance.execute
20
+ end
21
+
22
+ should "package single if a name is given" do
23
+ @instance.expects(:package_single).with("foo").once
24
+ @instance.execute(["foo"])
25
+ end
26
+ end
27
+
28
+ context "packaging base" do
29
+ should "error and exit if no VM is found" do
30
+ Vagrant::VM.expects(:find).with("foo", @instance.env).returns(nil)
31
+ @instance.expects(:error_and_exit).with(:vm_base_not_found, :name => "foo").once
32
+ @instance.execute(["--base", "foo"])
33
+ end
34
+
35
+ should "package the VM like any other VM" do
36
+ vm = mock("vm")
37
+ Vagrant::VM.expects(:find).with("foo", @instance.env).returns(vm)
38
+ @instance.expects(:package_vm).with(vm).once
39
+ @instance.execute(["--base", "foo"])
40
+ end
41
+ end
42
+
43
+ context "packaging a single VM" do
18
44
  setup do
19
- @persisted_vm.stubs(:package)
20
- @persisted_vm.stubs(:powered_off?).returns(true)
45
+ @vm = mock("vm")
46
+ @vm.stubs(:created?).returns(true)
47
+
48
+ @vms = {:bar => @vm}
49
+ @env.stubs(:vms).returns(@vms)
50
+ @env.stubs(:multivm?).returns(false)
21
51
  end
22
52
 
23
- context "with no base specified" do
24
- should "require a persisted vm" do
25
- @env.expects(:require_persisted_vm).once
26
- @instance.execute
27
- end
53
+ should "error and exit if no name is given in a multi-vm env" do
54
+ @env.stubs(:multivm?).returns(true)
55
+ @instance.expects(:error_and_exit).with(:package_multivm).once
56
+ @instance.package_single(nil)
28
57
  end
29
58
 
30
- context "with base specified" do
31
- setup do
32
- @vm = mock("vm")
59
+ should "error and exit if the VM doesn't exist" do
60
+ @instance.expects(:error_and_exit).with(:unknown_vm, :vm => :foo).once
61
+ @instance.package_single(:foo)
62
+ end
33
63
 
34
- Vagrant::VM.stubs(:find).with(@name).returns(@vm)
35
- @vm.stubs(:env=).with(@env)
36
- @env.stubs(:vm=)
64
+ should "error and exit if the VM is not created" do
65
+ @vm.stubs(:created?).returns(false)
66
+ @instance.expects(:error_and_exit).with(:environment_not_created).once
67
+ @instance.package_single(:bar)
68
+ end
37
69
 
38
- @name = "bar"
39
- end
70
+ should "use the first VM is no name is given in a single VM environment" do
71
+ @instance.expects(:package_vm).with(@vm).once
72
+ @instance.package_single(nil)
73
+ end
40
74
 
41
- should "find the given base and set it on the env" do
42
- Vagrant::VM.expects(:find).with(@name).returns(@vm)
43
- @vm.expects(:env=).with(@env)
44
- @env.expects(:vm=).with(@vm)
75
+ should "package the VM" do
76
+ @instance.expects(:package_vm).with(@vm).once
77
+ @instance.package_single(:bar)
78
+ end
79
+ end
45
80
 
46
- @instance.execute(["foo", "--base", @name])
47
- end
81
+ context "packaging a VM" do
82
+ setup do
83
+ @vm = mock("vm")
84
+ @vm.stubs(:powered_off?).returns(true)
48
85
 
49
- should "error if the VM is not found" do
50
- Vagrant::VM.expects(:find).with(@name).returns(nil)
51
- @instance.expects(:error_and_exit).with(:vm_base_not_found, :name => @name).once
86
+ @options = {}
87
+ @instance.stubs(:options).returns(@options)
88
+ end
52
89
 
53
- @instance.execute(["foo", "--base", @name])
54
- end
90
+ should "error and exit if VM is not powered off" do
91
+ @vm.stubs(:powered_off?).returns(false)
92
+ @instance.expects(:error_and_exit).with(:vm_power_off_to_package).once
93
+ @instance.package_vm(@vm)
55
94
  end
56
95
 
57
- context "shared (with and without base specified)" do
58
- should "error and exit if the VM is not powered off" do
59
- @persisted_vm.stubs(:powered_off?).returns(false)
60
- @instance.expects(:error_and_exit).with(:vm_power_off_to_package).once
61
- @persisted_vm.expects(:package).never
62
- @instance.execute
63
- end
64
-
65
- should "call package on the persisted VM" do
66
- @persisted_vm.expects(:package).once
67
- @instance.execute
68
- end
69
-
70
- should "pass the out path and include_files to the package method" do
71
- out_path = mock("out_path")
72
- include_files = "foo"
73
- @persisted_vm.expects(:package).with(out_path, [include_files]).once
74
- @instance.execute([out_path, "--include", include_files])
75
- end
76
-
77
- should "default to an empty array when not include_files are specified" do
78
- out_path = mock("out_path")
79
- @persisted_vm.expects(:package).with(out_path, []).once
80
- @instance.execute([out_path])
81
- end
96
+ should "package the VM with the proper arguments" do
97
+ @options[:output] = "foo.box"
98
+ @options[:include] = :bar
99
+
100
+ @vm.expects(:package).with(@options).once
101
+ @instance.package_vm(@vm)
82
102
  end
83
103
  end
84
104
  end