Package uses a reference to the Export action to get the temp path rather than the wonky callback. Implemented Runner#find_action for this.

This commit is contained in:
Mitchell Hashimoto 2010-02-28 20:23:32 -08:00
parent 5dd3e51788
commit 7c19419ef2
7 changed files with 91 additions and 46 deletions

View File

@ -25,6 +25,11 @@ module Vagrant
@actions ||= []
end
# Returns the first action instance which matches the given class.
def find_action(action_klass)
actions.find { |a| a.is_a?(action_klass) }
end
# Add an action to the list of queued actions to execute. This method
# appends the given action class to the end of the queue.
def add_action(action_klass, *args)

View File

@ -25,8 +25,6 @@ module Vagrant
logger.info "Creating temporary directory for export..."
FileUtils.mkpath(temp_dir)
@runner.invoke_callback(:set_export_temp_path, @temp_dir)
end
def ovf_path

View File

@ -4,7 +4,7 @@ module Vagrant
class Package < Base
attr_accessor :out_path
attr_accessor :include_files
attr_accessor :temp_path
attr_reader :export_action
def initialize(vm, out_path = nil, include_files = nil, *args)
super
@ -14,9 +14,14 @@ module Vagrant
end
def prepare
# Verify the existance of all the additional files, if any
@include_files.each do |file|
raise ActionException.new("#{file} does not exist") unless File.exists?(file)
end
# Get the export action and store a reference to it
@export_action = @runner.find_action(Export)
raise ActionException.new("Package must be used in conjunction with export.") unless @export_action
end
def execute!
@ -27,6 +32,10 @@ module Vagrant
File.join(FileUtils.pwd, "#{out_path}#{Vagrant.config.package.extension}")
end
def temp_path
export_action.temp_dir
end
def compress
logger.info "Packaging VM into #{tar_path} ..."
Tar.open(tar_path, File::CREAT | File::WRONLY, 0644, Tar::GNU) do |tar|
@ -46,11 +55,6 @@ module Vagrant
end
end
end
# This is a callback by Actions::VM::Export
def set_export_temp_path(temp_path)
@temp_path = temp_path
end
end
end
end

View File

@ -68,15 +68,16 @@ class Test::Unit::TestCase
# Sets up the mocks and instantiates an action for testing
def mock_action(action_klass, *args)
@vm = mock("vboxvm")
@mock_vm = mock("vm")
@mock_vm.stubs(:vm).returns(@vm)
@mock_vm.stubs(:vm=)
@mock_vm.stubs(:invoke_callback)
@mock_vm.stubs(:invoke_around_callback).yields
vm = mock("vboxvm")
mock_vm = mock("vm")
action = action_klass.new(mock_vm, *args)
@action = action_klass.new(@mock_vm, *args)
mock_vm.stubs(:vm).returns(vm)
mock_vm.stubs(:vm=)
mock_vm.stubs(:invoke_callback)
mock_vm.stubs(:invoke_around_callback).yields
mock_vm.stubs(:actions).returns([action])
[@mock_vm, @vm, @action]
[mock_vm, vm, action]
end
end

View File

@ -67,6 +67,23 @@ class ActionRunnerTest < Test::Unit::TestCase
end
end
context "finding actions" do
setup do
@runner = Vagrant::Actions::Runner.new
end
should "return nil if the action could not be found" do
assert_nil @runner.find_action(Vagrant::Actions::VM::Export)
end
should "return the first instance of the action found" do
@runner.add_action(Vagrant::Actions::VM::Export)
@runner.add_action(Vagrant::Actions::VM::Export)
assert @runner.actions[0].equal?(@runner.find_action(Vagrant::Actions::VM::Export))
end
end
context "adding actions" do
setup do
@runner = Vagrant::Actions::Runner.new

View File

@ -36,11 +36,6 @@ class ExportActionTest < Test::Unit::TestCase
@action.setup_temp_dir
assert_equal @temp_dir, @action.temp_dir
end
should "call the set_export_temp_path callback" do
@runner.expects(:invoke_callback).with(:set_export_temp_path, @temp_dir).once
@action.setup_temp_dir
end
end
context "path to OVF file" do

View File

@ -2,21 +2,20 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
class PackageActionTest < Test::Unit::TestCase
setup do
@wrapper_vm, @vm, @action = mock_action(Vagrant::Actions::VM::Package, "bing", [])
@runner, @vm, @action = mock_action(Vagrant::Actions::VM::Package, "bing", [])
mock_config
@temp_path = "temp_path"
@action.temp_path = @temp_path
end
context "initialization" do
def get_action(*args)
wrapper_vm, vm, action = mock_action(Vagrant::Actions::VM::Package, *args)
runner, vm, action = mock_action(Vagrant::Actions::VM::Package, *args)
return action
end
should "make out_path 'package' by default if nil is given" do
action = get_action(nil, [])
assert_equal "package", @action.out_path
assert_equal "package", action.out_path
end
should "make include files an empty array by default" do
@ -46,11 +45,27 @@ class PackageActionTest < Test::Unit::TestCase
end
end
context "temp path" do
setup do
@export = mock("export")
@action.expects(:export_action).returns(@export)
end
should "use the export action's temp dir" do
path = mock("path")
@export.expects(:temp_dir).returns(path)
@action.temp_path
end
end
context "compression" do
setup do
@tar_path = "foo"
@action.stubs(:tar_path).returns(@tar_path)
@temp_path = "foo"
@action.stubs(:temp_path).returns(@temp_path)
@pwd = "bar"
FileUtils.stubs(:pwd).returns(@pwd)
FileUtils.stubs(:cd)
@ -89,6 +104,7 @@ class PackageActionTest < Test::Unit::TestCase
should "add included files when passed" do
include_files = ['foo', 'bar']
action = mock_action(Vagrant::Actions::VM::Package, "bing", include_files).last
action.stubs(:temp_path).returns("foo")
@tar.expects(:append_tree).with(".")
include_files.each { |f| @tar.expects(:append_file).with(f) }
action.compress
@ -101,30 +117,39 @@ class PackageActionTest < Test::Unit::TestCase
end
end
context "export callback to set temp path" do
should "save to the temp_path directory" do
foo = mock("foo")
@action.set_export_temp_path(foo)
assert foo.equal?(@action.temp_path)
end
end
context "preparing the action" do
setup do
@include_files = ['fooiest', 'booiest']
@action = mock_action(Vagrant::Actions::VM::Package, "bing", @include_files).last
end
should "check that all the include files exist" do
@include_files.each do |file|
File.expects(:exists?).with(file).returns(true)
context "checking include files" do
setup do
@include_files = ['fooiest', 'booiest']
@runner, @vm, @action = mock_action(Vagrant::Actions::VM::Package, "bing", @include_files)
@runner.stubs(:find_action).returns("foo")
end
should "check that all the include files exist" do
@include_files.each do |file|
File.expects(:exists?).with(file).returns(true)
end
@action.prepare
end
should "raise an exception when an include file does not exist" do
File.expects(:exists?).once.returns(false)
assert_raises(Vagrant::Actions::ActionException) { @action.prepare }
end
@action.prepare
end
should "raise an exception when an include file does not exist" do
File.expects(:exists?).once.returns(false)
assert_raises(Vagrant::Actions::ActionException) { @action.prepare }
context "loading export reference" do
should "find and store a reference to the export action" do
@export = mock("export")
@runner.expects(:find_action).with(Vagrant::Actions::VM::Export).once.returns(@export)
@action.prepare
assert @export.equal?(@action.export_action)
end
should "raise an exception if the export action couldn't be found" do
@runner.expects(:find_action).with(Vagrant::Actions::VM::Export).once.returns(nil)
assert_raises(Vagrant::Actions::ActionException) { @action.prepare }
end
end
end
end