core: SyncedFolders middleware passes inoptions

This commit is contained in:
Mitchell Hashimoto 2013-11-22 17:13:14 -08:00
parent 079ac12f5d
commit 125584aaf4
2 changed files with 58 additions and 26 deletions

View File

@ -50,18 +50,18 @@ module Vagrant
end end
# Go through each folder and prepare the folders # Go through each folder and prepare the folders
folders.each do |impl, fs| folders.each do |impl_name, fs|
@logger.info("Invoking synced folder prepare for: #{impl}") @logger.info("Invoking synced folder prepare for: #{impl_name}")
impl.new.prepare(env[:machine], fs) plugins[impl_name.to_sym][0].new.prepare(env[:machine], fs, impl_opts(impl_name, env))
end end
# Continue, we need the VM to be booted. # Continue, we need the VM to be booted.
@app.call(env) @app.call(env)
# Once booted, setup the folder contents # Once booted, setup the folder contents
folders.each do |impl, fs| folders.each do |impl_name, fs|
@logger.info("Invoking synced folder enable: #{impl}") @logger.info("Invoking synced folder enable: #{impl_name}")
impl.new.enable(env[:machine], fs) plugins[impl_name.to_sym][0].new.enable(env[:machine], fs, impl_opts(impl_name, env))
end end
end end
@ -75,20 +75,34 @@ module Vagrant
impl = data[0] impl = data[0]
priority = data[1] priority = data[1]
ordered << [priority, impl] ordered << [priority, key, impl]
end end
# Order the plugins by priority # Order the plugins by priority
ordered = ordered.sort { |a, b| b[0] <=> a[0] }.map { |p| p[1] } ordered = ordered.sort { |a, b| b[0] <=> a[0] }
# Find the proper implementation # Find the proper implementation
ordered.each do |impl| ordered.each do |_, key, impl|
return impl if impl.new.usable?(machine) return key if impl.new.usable?(machine)
end end
return nil return nil
end end
# This finds the options in the env that are set for a given
# synced folder type.
def impl_opts(name, env)
{}.tap do |result|
env.each do |k, v|
if k.to_s.start_with?("#{name}_")
k = k.dup if !k.is_a?(Symbol)
v = v.dup if !v.is_a?(Symbol)
result[k] = v
end
end
end
end
# This returns the available synced folder implementations. This # This returns the available synced folder implementations. This
# is a separate method so that it can be easily stubbed by tests. # is a separate method so that it can be easily stubbed by tests.
def plugins def plugins
@ -107,19 +121,15 @@ module Vagrant
# Ignore disabled synced folders # Ignore disabled synced folders
next if data[:disabled] next if data[:disabled]
impl = ["", 0] impl = ""
impl = plugins[data[:type].to_sym] if data[:type] impl = data[:type].to_sym if data[:type]
if impl == nil if impl != "" && !plugins[impl]
# This should never happen because configuration validation # This should never happen because configuration validation
# should catch this case. But we put this here as an assert # should catch this case. But we put this here as an assert
raise "Internal error. Report this as a bug. Invalid: #{data[:type]}" raise "Internal error. Report this as a bug. Invalid: #{data[:type]}"
end end
# The implementation class rather than the priority, since the
# array is [class, priority].
impl = impl[0]
# Keep track of this shared folder by the implementation. # Keep track of this shared folder by the implementation.
folders[impl] ||= {} folders[impl] ||= {}
folders[impl][id] = data.dup folders[impl][id] = data.dup

View File

@ -43,14 +43,19 @@ describe Vagrant::Action::Builtin::SyncedFolders do
describe "call" do describe "call" do
let(:synced_folders) { {} } let(:synced_folders) { {} }
let(:plugins) { {} }
before do before do
plugins[:default] = [impl(true, "default"), 10]
plugins[:nfs] = [impl(true, "nfs"), 5]
env[:root_path] = Pathname.new(Dir.mktmpdir) env[:root_path] = Pathname.new(Dir.mktmpdir)
subject.stub(:plugins => plugins)
subject.stub(:synced_folders => synced_folders) subject.stub(:synced_folders => synced_folders)
end end
it "should create on the host if specified" do it "should create on the host if specified" do
synced_folders[impl(true, "good")] = { synced_folders["default"] = {
"root" => { "root" => {
hostpath: "foo", hostpath: "foo",
}, },
@ -69,17 +74,19 @@ describe Vagrant::Action::Builtin::SyncedFolders do
it "should invoke prepare then enable" do it "should invoke prepare then enable" do
order = [] order = []
sf = Class.new(impl(true, "good")) do tracker = Class.new(impl(true, "good")) do
define_method(:prepare) do |machine, folders| define_method(:prepare) do |machine, folders, opts|
order << :prepare order << :prepare
end end
define_method(:enable) do |machine, folders| define_method(:enable) do |machine, folders, opts|
order << :enable order << :enable
end end
end end
synced_folders[sf] = { plugins[:tracker] = [tracker, 15]
synced_folders["tracker"] = {
"root" => { "root" => {
hostpath: "foo", hostpath: "foo",
}, },
@ -105,7 +112,22 @@ describe Vagrant::Action::Builtin::SyncedFolders do
} }
result = subject.default_synced_folder_type(machine, plugins) result = subject.default_synced_folder_type(machine, plugins)
result.new.name.should == "good" result.should == "good"
end
end
describe "impl_opts" do
it "should return only relevant keys" do
env = {
:foo_bar => "baz",
:bar_bar => "nope",
:foo_baz => "bar",
}
result = subject.impl_opts("foo", env)
result.length.should == 2
result[:foo_bar].should == "baz"
result[:foo_baz].should == "bar"
end end
end end
@ -134,8 +156,8 @@ describe Vagrant::Action::Builtin::SyncedFolders do
result = subject.synced_folders(machine) result = subject.synced_folders(machine)
result.length.should == 2 result.length.should == 2
result[plugins[:default][0]].should == { "root" => folders["root"] } result[:default].should == { "root" => folders["root"] }
result[plugins[:nfs][0]].should == { "nfs" => folders["nfs"] } result[:nfs].should == { "nfs" => folders["nfs"] }
end end
it "should ignore disabled folders" do it "should ignore disabled folders" do
@ -144,7 +166,7 @@ describe Vagrant::Action::Builtin::SyncedFolders do
result = subject.synced_folders(machine) result = subject.synced_folders(machine)
result.length.should == 1 result.length.should == 1
result[plugins[:default][0]].length.should == 1 result[:default].length.should == 1
end end
end end
end end