From 3fd9f44921528fbe279824c26ffae42e2f338a0c Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Thu, 21 Dec 2017 09:54:24 -0800 Subject: [PATCH] Add test coverage for windows host smb capability --- .../plugins/hosts/windows/cap/smb_test.rb | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 test/unit/plugins/hosts/windows/cap/smb_test.rb diff --git a/test/unit/plugins/hosts/windows/cap/smb_test.rb b/test/unit/plugins/hosts/windows/cap/smb_test.rb new file mode 100644 index 000000000..20c09d1ef --- /dev/null +++ b/test/unit/plugins/hosts/windows/cap/smb_test.rb @@ -0,0 +1,108 @@ +require_relative "../../../../base" + +require_relative "../../../../../../plugins/hosts/windows/cap/smb" + +describe VagrantPlugins::HostWindows::Cap::SMB do + let(:subject){ VagrantPlugins::HostWindows::Cap::SMB } + let(:machine){ double(:machine, env: double(:machine_env, ui: double(:ui))) } + let(:env){ double(:env) } + let(:options){ {} } + let(:result){ Vagrant::Util::Subprocess::Result } + let(:powershell_version){ "3" } + + before do + allow(subject).to receive(:machine_id).and_return("CUSTOM_ID") + allow(Vagrant::Util::PowerShell).to receive(:version).and_return(powershell_version) + allow(machine.env.ui).to receive(:warn) + allow(subject).to receive(:sleep) + end + + describe ".smb_installed" do + context "when powershell version is greater than 2" do + it "is valid installation" do + expect(subject.smb_installed(nil)).to eq(true) + end + end + + context "when powershell version is less than 3" do + let(:powershell_version){ "2" } + + it "is not a valid installation" do + expect(subject.smb_installed(nil)).to eq(false) + end + end + end + + describe ".smb_cleanup" do + before do + allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).with("net share"). + and_return("vgt-CUSTOM_ID-1\nvgt-CUSTOM_ID-2\n") + allow(Vagrant::Util::PowerShell).to receive(:execute).and_return(result.new(0, "", "")) + end + after{ subject.smb_cleanup(env, machine, options) } + + it "should pause after warning user" do + expect(machine.env.ui).to receive(:warn) + expect(subject).to receive(:sleep) + end + + it "should remove all shares in single call" do + expect(Vagrant::Util::PowerShell).to receive(:execute).with(any_args, sudo: true).once + end + + context "when no shares are defined" do + before do + expect(Vagrant::Util::PowerShell).to receive(:execute_cmd).with("net share"). + and_return("") + end + + it "should not attempt to remove shares" do + expect(Vagrant::Util::PowerShell).not_to receive(:execute).with(any_args, sudo: true) + end + + it "should not warn user" do + expect(machine.env.ui).not_to receive(:warn) + end + end + end + + describe ".smb_prepare" do + let(:folders){ {"/first/path" => {hostpath: "/host/1"}, "/second/path" => {hostpath: "/host/2", smb_id: "ID1"}} } + let(:options){ {} } + + before{ allow(Vagrant::Util::PowerShell).to receive(:execute).and_return(result.new(0, "", "")) } + + it "should add ID when not defined" do + subject.smb_prepare(env, machine, folders, options) + expect(folders["/first/path"][:smb_id]).to start_with("vgt-") + end + + it "should not modify ID when defined" do + subject.smb_prepare(env, machine, folders, options) + expect(folders["/second/path"][:smb_id]).to eq("ID1") + end + + it "should pause after warning user" do + expect(machine.env.ui).to receive(:warn) + expect(subject).to receive(:sleep) + subject.smb_prepare(env, machine, folders, options) + end + + it "should add all shares in single call" do + expect(Vagrant::Util::PowerShell).to receive(:execute).with(any_args, sudo: true).once + subject.smb_prepare(env, machine, folders, options) + end + + context "when no shared are defined" do + after{ subject.smb_prepare(env, machine, {}, options) } + + it "should not attempt to add shares" do + expect(Vagrant::Util::PowerShell).not_to receive(:execute).with(any_args, sudo: true) + end + + it "should not warn user" do + expect(machine.env.ui).not_to receive(:warn) + end + end + end +end