core: clean up tests for synced folder built-ins

This commit is contained in:
Mitchell Hashimoto 2013-12-03 18:30:59 -08:00
parent ca521887eb
commit c04fa5e54e
9 changed files with 142 additions and 113 deletions

View File

@ -26,20 +26,6 @@ module Vagrant
return nil
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
# is a separate method so that it can be easily stubbed by tests.
def plugins
@ -49,6 +35,8 @@ module Vagrant
# This returns the set of shared folders that should be done for
# this machine. It returns the folders in a hash keyed by the
# implementation class for the synced folders.
#
# @return [Hash<Symbol, Hash<String, Hash>>]
def synced_folders(machine)
folders = {}

View File

@ -73,6 +73,20 @@ module Vagrant
plugins[impl_name.to_sym][0].new.enable(env[:machine], fs, impl_opts(impl_name, env))
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
end
end
end

View File

@ -13,6 +13,7 @@ require "support/tempdir"
require "unit/support/dummy_communicator"
require "unit/support/dummy_provider"
require "unit/support/shared/base_context"
require "unit/support/shared/action_synced_folders_context"
require "unit/support/shared/virtualbox_context"
# Do not buffer output

View File

@ -2,7 +2,9 @@ require_relative "../base"
describe VagrantPlugins::ProviderVirtualBox::Driver::Version_4_3 do
include_context "virtualbox"
let(:vbox_version) { "4.3.0" }
subject { VagrantPlugins::ProviderVirtualBox::Driver::Meta.new(uuid) }
it_behaves_like "a version 4.x virtualbox driver"

View File

@ -0,0 +1,14 @@
shared_context "synced folder actions" do
# This creates a synced folder implementation.
def impl(usable, name)
Class.new(Vagrant.plugin("2", :synced_folder)) do
define_method(:name) do
name
end
define_method(:usable?) do |machine|
usable
end
end
end
end

View File

@ -0,0 +1,87 @@
require File.expand_path("../../../../base", __FILE__)
require "vagrant/action/builtin/mixin_synced_folders"
describe Vagrant::Action::Builtin::MixinSyncedFolders do
include_context "synced folder actions"
subject do
Class.new do
extend Vagrant::Action::Builtin::MixinSyncedFolders
end
end
let(:machine) do
double("machine").tap do |machine|
machine.stub(:config).and_return(machine_config)
end
end
let(:machine_config) do
double("machine_config").tap do |top_config|
top_config.stub(:vm => vm_config)
end
end
let(:vm_config) { double("machine_vm_config") }
describe "default_synced_folder_type" do
it "returns the usable implementation" do
plugins = {
"bad" => [impl(false, "bad"), 0],
"nope" => [impl(true, "nope"), 1],
"good" => [impl(true, "good"), 5],
}
result = subject.default_synced_folder_type(machine, plugins)
result.should == "good"
end
end
describe "synced_folders" do
let(:folders) { {} }
let(:plugins) { {} }
before do
plugins[:default] = [impl(true, "default"), 10]
plugins[:nfs] = [impl(true, "nfs"), 5]
subject.stub(:plugins => plugins)
vm_config.stub(:synced_folders => folders)
end
it "should raise exception if bad type is given" do
folders["root"] = { type: "bad" }
expect { subject.synced_folders(machine) }.
to raise_error(StandardError)
end
it "should return the proper set of folders" do
folders["root"] = {}
folders["nfs"] = { type: "nfs" }
result = subject.synced_folders(machine)
result.length.should == 2
result[:default].should == { "root" => folders["root"] }
result[:nfs].should == { "nfs" => folders["nfs"] }
end
it "should error if an explicit type is unusable" do
plugins[:unusable] = [impl(false, "bad"), 15]
folders["root"] = { type: "unusable" }
expect { subject.synced_folders(machine) }.
to raise_error
end
it "should ignore disabled folders" do
folders["root"] = {}
folders["foo"] = { disabled: true }
result = subject.synced_folders(machine)
result.length.should == 1
result[:default].length.should == 1
end
end
end

View File

@ -4,6 +4,8 @@ require "tmpdir"
require File.expand_path("../../../../base", __FILE__)
describe Vagrant::Action::Builtin::SyncedFolderCleanup do
include_context "synced folder actions"
let(:app) { lambda { |env| } }
let(:env) { { :machine => machine, :ui => ui } }
let(:machine) do
@ -28,15 +30,16 @@ describe Vagrant::Action::Builtin::SyncedFolderCleanup do
subject { described_class.new(app, env) }
# This creates a synced folder implementation.
def impl(usable, name)
Class.new(Vagrant.plugin("2", :synced_folder)) do
define_method(:name) do
name
def create_cleanup_tracker
Class.new(impl(true, "good")) do
class_variable_set(:@@clean, false)
def self.clean
class_variable_get(:@@clean)
end
define_method(:usable?) do |machine|
usable
def cleanup(machine)
self.class.class_variable_set(:@@clean, true)
end
end
end
@ -50,18 +53,13 @@ describe Vagrant::Action::Builtin::SyncedFolderCleanup do
plugins[:nfs] = [impl(true, "nfs"), 5]
env[:root_path] = Pathname.new(Dir.mktmpdir)
subject.stub(:plugins => plugins)
subject.stub(:synced_folders => synced_folders)
end
it "should invoke cleanup" do
cleaned_up = nil
tracker = Class.new(impl(true, "good")) do
define_method(:cleanup) do |machine|
cleaned_up = true
end
end
tracker = create_cleanup_tracker
plugins[:tracker] = [tracker, 15]
synced_folders["tracker"] = {
@ -76,19 +74,13 @@ describe Vagrant::Action::Builtin::SyncedFolderCleanup do
}
subject.call(env)
cleaned_up.should be_true
tracker.clean.should be_true
end
it "should invoke cleanup once per implementation" do
call_count = 0
trackers = []
(0..2).each do |tracker|
trackers << Class.new(impl(true, "good")) do
define_method(:cleanup) do |machine|
call_count += 1
end
end
trackers << create_cleanup_tracker
end
plugins[:tracker_0] = [trackers[0], 15]
@ -129,7 +121,9 @@ describe Vagrant::Action::Builtin::SyncedFolderCleanup do
subject.call(env)
call_count.should == 3
trackers[0].clean.should be_true
trackers[1].clean.should be_true
trackers[2].clean.should be_true
end
end
end

View File

@ -4,6 +4,8 @@ require "tmpdir"
require File.expand_path("../../../../base", __FILE__)
describe Vagrant::Action::Builtin::SyncedFolders do
include_context "synced folder actions"
let(:app) { lambda { |env| } }
let(:env) { { :machine => machine, :ui => ui } }
let(:machine) do
@ -28,19 +30,6 @@ describe Vagrant::Action::Builtin::SyncedFolders do
subject { described_class.new(app, env) }
# This creates a synced folder implementation.
def impl(usable, name)
Class.new(Vagrant.plugin("2", :synced_folder)) do
define_method(:name) do
name
end
define_method(:usable?) do |machine|
usable
end
end
end
describe "call" do
let(:synced_folders) { {} }
let(:plugins) { {} }
@ -125,19 +114,6 @@ describe Vagrant::Action::Builtin::SyncedFolders do
end
end
describe "default_synced_folder_type" do
it "returns the usable implementation" do
plugins = {
"bad" => [impl(false, "bad"), 0],
"nope" => [impl(true, "nope"), 1],
"good" => [impl(true, "good"), 5],
}
result = subject.default_synced_folder_type(machine, plugins)
result.should == "good"
end
end
describe "impl_opts" do
it "should return only relevant keys" do
env = {
@ -152,51 +128,4 @@ describe Vagrant::Action::Builtin::SyncedFolders do
result[:foo_baz].should == "bar"
end
end
describe "synced_folders" do
let(:folders) { {} }
let(:plugins) { {} }
before do
plugins[:default] = [impl(true, "default"), 10]
plugins[:nfs] = [impl(true, "nfs"), 5]
subject.stub(:plugins => plugins)
vm_config.stub(:synced_folders => folders)
end
it "should raise exception if bad type is given" do
folders["root"] = { type: "bad" }
expect { subject.synced_folders(machine) }.
to raise_error(StandardError)
end
it "should return the proper set of folders" do
folders["root"] = {}
folders["nfs"] = { type: "nfs" }
result = subject.synced_folders(machine)
result.length.should == 2
result[:default].should == { "root" => folders["root"] }
result[:nfs].should == { "nfs" => folders["nfs"] }
end
it "should error if an explicit type is unusable" do
plugins[:unusable] = [impl(false, "bad"), 15]
folders["root"] = { type: "unusable" }
expect { subject.synced_folders(machine) }.
to raise_error
end
it "should ignore disabled folders" do
folders["root"] = {}
folders["foo"] = { disabled: true }
result = subject.synced_folders(machine)
result.length.should == 1
result[:default].length.should == 1
end
end
end