From 21ac0810fdb17254a488b2493942ba7169a328fa Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 14 Mar 2014 09:55:27 -0700 Subject: [PATCH] communicators/winrm: more tests --- plugins/communicators/winrm/communicator.rb | 45 +----------- plugins/communicators/winrm/helper.rb | 60 ++++++++++++++++ .../communicators/winrm/helper_test.rb | 71 +++++++++++++++++++ 3 files changed, 134 insertions(+), 42 deletions(-) create mode 100644 plugins/communicators/winrm/helper.rb create mode 100644 test/unit/plugins/communicators/winrm/helper_test.rb diff --git a/plugins/communicators/winrm/communicator.rb b/plugins/communicators/winrm/communicator.rb index 92d9da428..2782bcfd3 100644 --- a/plugins/communicators/winrm/communicator.rb +++ b/plugins/communicators/winrm/communicator.rb @@ -2,6 +2,7 @@ require "timeout" require "log4r" +require_relative "helper" require_relative "shell" module VagrantPlugins @@ -88,48 +89,8 @@ module VagrantPlugins # This creates anew WinRMShell based on the information we know # about this machine. def create_shell - host_address = @machine.config.winrm.host - if !host_address - ssh_info = @machine.ssh_info - raise Errors::WinRMNotReady if !ssh_info - host_address = ssh_info[:host] - end - - host_port = @machine.config.winrm.port - if @machine.config.winrm.guest_port - @logger.debug("Searching for WinRM host port to match: " + - @machine.config.winrm.guest_port.to_s) - - # Search by guest port if we can. We use a provider capability - # if we have it. Otherwise, we just scan the Vagrantfile defined - # ports. - port = nil - if @machine.provider.capability?(:forwarded_ports) - @machine.provider.capability(:forwarded_ports).each do |host, guest| - if guest == @machine.config.winrm.guest_port - port = host - break - end - end - end - - if !port - machine.config.vm.networks.each do |type, netopts| - next if type != :forwarded_port - next if !netopts[:host] - if netopts[:guest] == @machine.config.winrm.guest_port - port = netopts[:host] - break - end - end - end - - # Set it if we found it - if port - @logger.debug("Found forwarded port: #{host_port}") - host_port = port - end - end + host_address = Helper.winrm_address(@machine) + host_port = Helper.winrm_port(@machine) WinRMShell.new( host_address, diff --git a/plugins/communicators/winrm/helper.rb b/plugins/communicators/winrm/helper.rb new file mode 100644 index 000000000..4ce47ab2b --- /dev/null +++ b/plugins/communicators/winrm/helper.rb @@ -0,0 +1,60 @@ +module VagrantPlugins + module CommunicatorWinRM + # This is a helper module that provides some functions to the + # communicator. This is extracted into a module so that we can + # easily unit test these methods. + module Helper + # Returns the address to access WinRM. This does not contain + # the port. + # + # @param [Vagrant::Machine] machine + # @return [String] + def self.winrm_address(machine) + addr = machine.config.winrm.host + return addr if addr + + ssh_info = machine.ssh_info + raise Errors::WinRMNotReady if !ssh_info + return ssh_info[:host] + end + + # Returns the port to access WinRM. + # + # @param [Vagrant::Machine] machine + # @return [Integer] + def self.winrm_port(machine) + host_port = machine.config.winrm.port + if machine.config.winrm.guest_port + # Search by guest port if we can. We use a provider capability + # if we have it. Otherwise, we just scan the Vagrantfile defined + # ports. + port = nil + if machine.provider.capability?(:forwarded_ports) + machine.provider.capability(:forwarded_ports).each do |host, guest| + if guest == machine.config.winrm.guest_port + port = host + break + end + end + end + + if !port + machine.config.vm.networks.each do |type, netopts| + next if type != :forwarded_port + next if !netopts[:host] + if netopts[:guest] == machine.config.winrm.guest_port + port = netopts[:host] + break + end + end + end + + # Set it if we found it + host_port = port if port + end + + host_port + end + end + end +end diff --git a/test/unit/plugins/communicators/winrm/helper_test.rb b/test/unit/plugins/communicators/winrm/helper_test.rb new file mode 100644 index 000000000..deb7917e5 --- /dev/null +++ b/test/unit/plugins/communicators/winrm/helper_test.rb @@ -0,0 +1,71 @@ +require File.expand_path("../../../../base", __FILE__) + +require Vagrant.source_root.join("plugins/communicators/winrm/helper") + +describe VagrantPlugins::CommunicatorWinRM::Helper do + include_context "unit" + + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + test_iso_env.vagrantfile("") + test_iso_env.create_vagrant_env + end + let(:test_iso_env) { isolated_environment } + + let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) } + + subject { described_class } + + describe ".winrm_address" do + before do + machine.config.winrm.host = nil + end + + it "returns the configured host if set" do + machine.config.winrm.host = "foo" + expect(subject.winrm_address(machine)).to eq("foo") + end + + it "returns the SSH info host if available" do + machine.stub(ssh_info: { host: "bar" }) + expect(subject.winrm_address(machine)).to eq("bar") + end + + it "raisee an exception if it can't detect a host" do + machine.stub(ssh_info: nil) + expect { subject.winrm_address(machine) }. + to raise_error(VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady) + end + end + + describe ".winrm_port" do + it "returns the configured port if no guest port set" do + machine.config.winrm.port = 22 + machine.config.winrm.guest_port = nil + + expect(subject.winrm_port(machine)).to eq(22) + end + + it "returns a forwarded port that matches the guest port" do + machine.config.winrm.port = 22 + machine.config.winrm.guest_port = 2222 + machine.config.vm.network "forwarded_port", host: 1234, guest: 2222 + + expect(subject.winrm_port(machine)).to eq(1234) + end + + it "uses the provider capability if it exists" do + machine.config.winrm.port = 22 + machine.config.winrm.guest_port = 2222 + machine.config.vm.network "forwarded_port", host: 1234, guest: 2222 + + machine.provider.stub(:capability?).with(:forwarded_ports).and_return(true) + machine.provider.stub(:capability).with(:forwarded_ports).and_return({ + 1234 => 4567, + 2456 => 2222, + }) + + expect(subject.winrm_port(machine)).to eq(2456) + end + end +end