From c04fa5e54e306fb36a79cfe7d4d7ca9bb9716d3c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 3 Dec 2013 18:30:59 -0800 Subject: [PATCH] core: clean up tests for synced folder built-ins --- .../action/builtin/mixin_synced_folders.rb | 16 +--- .../action/builtin/synced_folder_cleanup.rb | 2 +- lib/vagrant/action/builtin/synced_folders.rb | 14 +++ test/unit/base.rb | 1 + .../virtualbox/driver/version_4_3_test.rb | 2 + .../shared/action_synced_folders_context.rb | 14 +++ .../builtin/mixin_synced_folders_test.rb | 87 +++++++++++++++++++ .../builtin/synced_folder_cleanup_test.rb | 44 ++++------ .../action/builtin/synced_folders_test.rb | 75 +--------------- 9 files changed, 142 insertions(+), 113 deletions(-) create mode 100644 test/unit/support/shared/action_synced_folders_context.rb create mode 100644 test/unit/vagrant/action/builtin/mixin_synced_folders_test.rb diff --git a/lib/vagrant/action/builtin/mixin_synced_folders.rb b/lib/vagrant/action/builtin/mixin_synced_folders.rb index eb71e9dff..74def17a7 100644 --- a/lib/vagrant/action/builtin/mixin_synced_folders.rb +++ b/lib/vagrant/action/builtin/mixin_synced_folders.rb @@ -25,20 +25,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. @@ -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>] def synced_folders(machine) folders = {} diff --git a/lib/vagrant/action/builtin/synced_folder_cleanup.rb b/lib/vagrant/action/builtin/synced_folder_cleanup.rb index 33217e854..66e6a2783 100644 --- a/lib/vagrant/action/builtin/synced_folder_cleanup.rb +++ b/lib/vagrant/action/builtin/synced_folder_cleanup.rb @@ -9,7 +9,7 @@ module Vagrant # the appropriate synced folder plugin. class SyncedFolderCleanup include MixinSyncedFolders - + def initialize(app, env) @app = app @logger = Log4r::Logger.new("vagrant::action::builtin::synced_folder_cleanup") diff --git a/lib/vagrant/action/builtin/synced_folders.rb b/lib/vagrant/action/builtin/synced_folders.rb index 7c7802ca2..a241c289a 100644 --- a/lib/vagrant/action/builtin/synced_folders.rb +++ b/lib/vagrant/action/builtin/synced_folders.rb @@ -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 diff --git a/test/unit/base.rb b/test/unit/base.rb index d2545b060..4218abcbf 100644 --- a/test/unit/base.rb +++ b/test/unit/base.rb @@ -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 diff --git a/test/unit/plugins/providers/virtualbox/driver/version_4_3_test.rb b/test/unit/plugins/providers/virtualbox/driver/version_4_3_test.rb index 3bb7b6c2e..b13235e8e 100644 --- a/test/unit/plugins/providers/virtualbox/driver/version_4_3_test.rb +++ b/test/unit/plugins/providers/virtualbox/driver/version_4_3_test.rb @@ -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" diff --git a/test/unit/support/shared/action_synced_folders_context.rb b/test/unit/support/shared/action_synced_folders_context.rb new file mode 100644 index 000000000..d63a0a038 --- /dev/null +++ b/test/unit/support/shared/action_synced_folders_context.rb @@ -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 diff --git a/test/unit/vagrant/action/builtin/mixin_synced_folders_test.rb b/test/unit/vagrant/action/builtin/mixin_synced_folders_test.rb new file mode 100644 index 000000000..2c766e200 --- /dev/null +++ b/test/unit/vagrant/action/builtin/mixin_synced_folders_test.rb @@ -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 diff --git a/test/unit/vagrant/action/builtin/synced_folder_cleanup_test.rb b/test/unit/vagrant/action/builtin/synced_folder_cleanup_test.rb index 46d28972c..9ac78bdd3 100644 --- a/test/unit/vagrant/action/builtin/synced_folder_cleanup_test.rb +++ b/test/unit/vagrant/action/builtin/synced_folder_cleanup_test.rb @@ -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,21 +74,15 @@ 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 = [] + 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] plugins[:tracker_1] = [trackers[1], 15] plugins[:tracker_2] = [trackers[2], 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 diff --git a/test/unit/vagrant/action/builtin/synced_folders_test.rb b/test/unit/vagrant/action/builtin/synced_folders_test.rb index 94d199df5..d18ac15e2 100644 --- a/test/unit/vagrant/action/builtin/synced_folders_test.rb +++ b/test/unit/vagrant/action/builtin/synced_folders_test.rb @@ -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