Export middleware

This commit is contained in:
Mitchell Hashimoto 2010-07-07 20:17:32 -07:00
parent 072e71df8a
commit 667cd56139
2 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,40 @@
module Vagrant
class Action
module VM
class Export
def initialize(app, env)
@app = app
@env = env
end
def call(env)
@env = env
setup_temp_dir
export
@app.call(env)
end
def setup_temp_dir
@env.logger.info "Creating temporary directory for export..."
@env["export.temp_dir"] = File.join(@env.env.tmp_path, Time.now.to_i.to_s)
FileUtils.mkpath(@env["export.temp_dir"])
end
def export
@env.logger.info "Exporting VM to #{ovf_path}..."
@env["vm"].vm.export(ovf_path) do |progress|
@env.logger.report_progress(progress.percent, 100, false)
end
ensure
@env.logger.clear_progress
end
def ovf_path
File.join(@env["export.temp_dir"], @env.env.config.vm.box_ovf)
end
end
end
end
end

View File

@ -0,0 +1,73 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
class ExportVMActionTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Action::VM::Export
@app, @env = mock_action_data
@vm = mock("vm")
@env["vm"] = @vm
@internal_vm = mock("internal")
@vm.stubs(:vm).returns(@internal_vm)
@instance = @klass.new(@app, @env)
end
context "calling" do
should "call the proper methods then continue chain" do
seq = sequence("seq")
@instance.expects(:setup_temp_dir).in_sequence(seq)
@instance.expects(:export).in_sequence(seq)
@app.expects(:call).with(@env).in_sequence(seq)
@instance.call(@env)
end
end
context "setting up the temporary directory" do
setup do
@time_now = Time.now.to_i.to_s
Time.stubs(:now).returns(@time_now)
@tmp_path = "foo"
@env.env.stubs(:tmp_path).returns(@tmp_path)
@temp_dir = File.join(@env.env.tmp_path, @time_now)
FileUtils.stubs(:mkpath)
end
should "create the temporary directory using the current time" do
FileUtils.expects(:mkpath).with(@temp_dir).once
@instance.setup_temp_dir
end
should "set to the environment" do
@instance.setup_temp_dir
assert_equal @temp_dir, @env["export.temp_dir"]
end
end
context "exporting" do
setup do
@ovf_path = mock("ovf_path")
@instance.stubs(:ovf_path).returns(@ovf_path)
end
should "call export on the runner with the ovf path" do
@internal_vm.expects(:export).with(@ovf_path).once
@instance.export
end
end
context "path to OVF file" do
setup do
@temp_dir = "foo"
@env["export.temp_dir"] = @temp_dir
end
should "be the temporary directory joined with the OVF filename" do
assert_equal File.join(@temp_dir, @env.env.config.vm.box_ovf), @instance.ovf_path
end
end
end