2014-04-22 18:03:37 +00:00
|
|
|
require File.expand_path("../../../../base", __FILE__)
|
|
|
|
|
|
|
|
require Vagrant.source_root.join("plugins/communicators/winrm/shell")
|
2014-07-22 23:30:29 +00:00
|
|
|
require Vagrant.source_root.join("plugins/communicators/winrm/config")
|
2014-04-22 18:03:37 +00:00
|
|
|
|
|
|
|
describe VagrantPlugins::CommunicatorWinRM::WinRMShell do
|
|
|
|
include_context "unit"
|
|
|
|
|
|
|
|
let(:session) { double("winrm_session") }
|
2014-07-22 23:58:27 +00:00
|
|
|
let(:port) { config.transport == :ssl ? 5986 : 5985 }
|
2014-07-22 23:30:29 +00:00
|
|
|
let(:config) {
|
|
|
|
VagrantPlugins::CommunicatorWinRM::Config.new.tap do |c|
|
|
|
|
c.username = 'username'
|
|
|
|
c.password = 'password'
|
|
|
|
c.finalize!
|
|
|
|
end
|
|
|
|
}
|
2014-04-22 18:03:37 +00:00
|
|
|
|
|
|
|
subject do
|
2014-07-22 23:58:27 +00:00
|
|
|
described_class.new('localhost', port, config).tap do |comm|
|
2014-04-23 23:12:27 +00:00
|
|
|
allow(comm).to receive(:new_session).and_return(session)
|
|
|
|
end
|
2014-04-22 18:03:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".powershell" do
|
|
|
|
it "should call winrm powershell" do
|
2014-04-24 01:05:53 +00:00
|
|
|
expect(session).to receive(:powershell).with(/^dir.+/).and_return({ exitcode: 0 })
|
2014-04-22 18:03:37 +00:00
|
|
|
expect(subject.powershell("dir")[:exitcode]).to eq(0)
|
|
|
|
end
|
|
|
|
|
2014-12-10 20:15:07 +00:00
|
|
|
it "should raise auth error when WinRM exception has a response code of 401" do
|
|
|
|
# The default settings might an account lockout - 20 auth failures!
|
|
|
|
expect(session).to receive(:powershell).with(/^dir.+/).exactly(20).times.and_raise(
|
2015-01-28 15:47:03 +00:00
|
|
|
WinRM::WinRMAuthorizationError.new("Oh no!!", 401))
|
2014-04-22 18:03:37 +00:00
|
|
|
expect { subject.powershell("dir") }.to raise_error(
|
2014-12-10 20:15:07 +00:00
|
|
|
VagrantPlugins::CommunicatorWinRM::Errors::AuthenticationFailed)
|
2014-04-22 18:03:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an execution error when an exception occurs" do
|
2014-04-24 01:05:53 +00:00
|
|
|
expect(session).to receive(:powershell).with(/^dir.+/).and_raise(
|
2014-04-22 18:03:37 +00:00
|
|
|
StandardError.new("Oh no! a 500 SOAP error!"))
|
|
|
|
expect { subject.powershell("dir") }.to raise_error(
|
|
|
|
VagrantPlugins::CommunicatorWinRM::Errors::ExecutionError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".cmd" do
|
|
|
|
it "should call winrm cmd" do
|
2014-04-23 23:16:08 +00:00
|
|
|
expect(session).to receive(:cmd).with("dir").and_return({ exitcode: 0 })
|
2014-04-22 18:03:37 +00:00
|
|
|
expect(subject.cmd("dir")[:exitcode]).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".endpoint" do
|
2014-07-22 23:58:27 +00:00
|
|
|
context 'when transport is :ssl' do
|
2014-07-23 19:04:33 +00:00
|
|
|
let(:config) {
|
|
|
|
VagrantPlugins::CommunicatorWinRM::Config.new.tap do |c|
|
|
|
|
c.transport = :ssl
|
|
|
|
c.finalize!
|
|
|
|
end
|
|
|
|
}
|
2014-07-22 23:58:27 +00:00
|
|
|
it "should create winrm endpoint address using https" do
|
|
|
|
expect(subject.send(:endpoint)).to eq("https://localhost:5986/wsman")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when transport is :plaintext" do
|
|
|
|
it "should create winrm endpoint address using http" do
|
|
|
|
expect(subject.send(:endpoint)).to eq("http://localhost:5985/wsman")
|
|
|
|
end
|
2014-04-22 18:03:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".endpoint_options" do
|
|
|
|
it "should create endpoint options" do
|
|
|
|
expect(subject.send(:endpoint_options)).to eq(
|
2014-07-23 19:04:33 +00:00
|
|
|
{ user: "username", pass: "password", host: "localhost", port: 5985,
|
2014-07-22 23:30:29 +00:00
|
|
|
basic_auth_only: true, no_ssl_peer_verification: false })
|
2014-04-22 18:03:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|