communicators/winrm: more tests
This commit is contained in:
parent
bb9f0aef32
commit
21ac0810fd
|
@ -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,
|
||||
|
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue