2014-03-14 16:55:27 +00:00
|
|
|
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
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(machine).to receive(:ssh_info).and_return({ host: "bar" })
|
2014-03-14 16:55:27 +00:00
|
|
|
expect(subject.winrm_address(machine)).to eq("bar")
|
|
|
|
end
|
|
|
|
|
2014-04-22 18:03:37 +00:00
|
|
|
it "raise an exception if it can't detect a host" do
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(machine).to receive(:ssh_info).and_return(nil)
|
2014-03-14 16:55:27 +00:00
|
|
|
expect { subject.winrm_address(machine) }.
|
2016-12-01 19:03:38 +00:00
|
|
|
to raise_error(VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raise an exception if it detects an empty host ip" do
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(machine).to receive(:ssh_info).and_return({ host: "" })
|
2016-12-01 19:03:38 +00:00
|
|
|
expect { subject.winrm_address(machine) }.
|
2017-03-06 17:14:04 +00:00
|
|
|
to raise_error(VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raise a WinRMNotReady exception if it detects an unset host ip" do
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(machine).to receive(:ssh_info).and_return({ host: nil })
|
2017-03-06 17:14:04 +00:00
|
|
|
expect { subject.winrm_address(machine) }.
|
2014-03-14 16:55:27 +00:00
|
|
|
to raise_error(VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady)
|
|
|
|
end
|
2017-09-25 21:31:37 +00:00
|
|
|
|
|
|
|
it "raise an exception if it detects an APIPA" do
|
|
|
|
machine.stub(ssh_info: { host: "169.254.123.123" })
|
|
|
|
expect { subject.winrm_address(machine) }.
|
|
|
|
to raise_error(VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady)
|
|
|
|
end
|
2014-03-14 16:55:27 +00:00
|
|
|
end
|
|
|
|
|
2014-05-21 03:12:50 +00:00
|
|
|
describe ".winrm_info" do
|
|
|
|
before do
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(machine.provider).to receive(:capability?)
|
|
|
|
.with(:winrm_info).and_return(false)
|
|
|
|
allow(subject).to receive(:winrm_address).and_return(nil)
|
|
|
|
allow(subject).to receive(:winrm_port).and_return(nil)
|
2014-05-21 03:12:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns default info if no capability" do
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(subject).to receive(:winrm_address).and_return("bar")
|
|
|
|
allow(subject).to receive(:winrm_port).and_return(45)
|
2014-05-21 03:12:50 +00:00
|
|
|
|
|
|
|
result = subject.winrm_info(machine)
|
|
|
|
expect(result[:host]).to eq("bar")
|
|
|
|
expect(result[:port]).to eq(45)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception if capability returns nil" do
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(machine.provider).to receive(:capability?)
|
|
|
|
.with(:winrm_info).and_return(true)
|
|
|
|
allow(machine.provider).to receive(:capability)
|
|
|
|
.with(:winrm_info).and_return(nil)
|
2014-05-21 03:12:50 +00:00
|
|
|
|
|
|
|
expect { subject.winrm_info(machine) }.
|
|
|
|
to raise_error(VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the proper information if set" do
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(machine.provider).to receive(:capability?)
|
|
|
|
.with(:winrm_info).and_return(true)
|
|
|
|
allow(machine.provider).to receive(:capability).with(:winrm_info).and_return({
|
2014-05-21 03:12:50 +00:00
|
|
|
host: "foo",
|
|
|
|
port: 12,
|
|
|
|
})
|
|
|
|
|
|
|
|
result = subject.winrm_info(machine)
|
|
|
|
expect(result[:host]).to eq("foo")
|
|
|
|
expect(result[:port]).to eq(12)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "defaults information if capability doesn't set it" do
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(machine.provider).to receive(:capability?)
|
|
|
|
.with(:winrm_info).and_return(true)
|
|
|
|
allow(machine.provider).to receive(:capability).with(:winrm_info).and_return({})
|
2014-05-21 03:12:50 +00:00
|
|
|
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(subject).to receive(:winrm_address).and_return("bar")
|
|
|
|
allow(subject).to receive(:winrm_port).and_return(45)
|
2014-05-21 03:12:50 +00:00
|
|
|
|
|
|
|
result = subject.winrm_info(machine)
|
|
|
|
expect(result[:host]).to eq("bar")
|
|
|
|
expect(result[:port]).to eq(45)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-14 16:55:27 +00:00
|
|
|
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
|
|
|
|
|
2014-05-21 03:12:50 +00:00
|
|
|
it "returns the configured guest port if non local" 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, false)).to eq(2222)
|
|
|
|
end
|
|
|
|
|
2014-03-14 16:55:27 +00:00
|
|
|
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
|
|
|
|
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(machine.provider).to receive(:capability?).with(:forwarded_ports).and_return(true)
|
|
|
|
allow(machine.provider).to receive(:capability).with(:forwarded_ports).and_return({
|
2014-03-14 16:55:27 +00:00
|
|
|
1234 => 4567,
|
|
|
|
2456 => 2222,
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(subject.winrm_port(machine)).to eq(2456)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|