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"
|
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
let(:connection) { double("winrm_connection") }
|
|
|
|
let(:shell) { double("winrm_shell") }
|
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'
|
2015-01-28 18:40:22 +00:00
|
|
|
c.max_tries = 3
|
|
|
|
c.retry_delay = 0
|
2016-01-24 18:38:22 +00:00
|
|
|
c.basic_auth_only = false
|
2016-01-25 07:33:16 +00:00
|
|
|
c.retry_delay = 1
|
|
|
|
c.max_tries = 2
|
2014-07-22 23:30:29 +00:00
|
|
|
c.finalize!
|
|
|
|
end
|
|
|
|
}
|
2016-12-11 08:52:00 +00:00
|
|
|
let(:output) { WinRM::Output.new.tap { |out| out.exitcode = 0 } }
|
2014-04-22 18:03:37 +00:00
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
before { allow(connection).to receive(:shell).and_yield(shell) }
|
2016-08-13 14:49:08 +00:00
|
|
|
|
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|
|
2016-12-11 08:52:00 +00:00
|
|
|
allow(comm).to receive(:new_connection).and_return(connection)
|
2014-04-23 23:12:27 +00:00
|
|
|
end
|
2014-04-22 18:03:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".powershell" do
|
|
|
|
it "should call winrm powershell" do
|
2016-12-11 08:52:00 +00:00
|
|
|
expect(shell).to receive(:run).with("dir").and_return(output)
|
|
|
|
expect(subject.powershell("dir").exitcode).to eq(0)
|
2014-04-22 18:03:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an execution error when an exception occurs" do
|
2016-12-11 08:52:00 +00:00
|
|
|
expect(shell).to receive(:run).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
|
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
describe ".elevated" do
|
|
|
|
it "should call winrm elevated" do
|
|
|
|
expect(shell).to receive(:run).with("dir").and_return(output)
|
|
|
|
expect(shell).to receive(:interactive_logon=).with(false)
|
|
|
|
expect(subject.elevated("dir").exitcode).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should set interactive_logon when interactive is true" do
|
|
|
|
expect(shell).to receive(:run).with("dir").and_return(output)
|
|
|
|
expect(shell).to receive(:interactive_logon=).with(true)
|
|
|
|
expect(subject.elevated("dir", { interactive: true }).exitcode).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an execution error when an exception occurs" do
|
|
|
|
expect(shell).to receive(:run).with("dir").and_raise(
|
|
|
|
StandardError.new("Oh no! a 500 SOAP error!"))
|
|
|
|
expect { subject.powershell("dir") }.to raise_error(
|
|
|
|
VagrantPlugins::CommunicatorWinRM::Errors::ExecutionError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".cmd" do
|
2014-04-22 18:03:37 +00:00
|
|
|
it "should call winrm cmd" do
|
2016-12-11 08:52:00 +00:00
|
|
|
expect(connection).to receive(:shell).with(:cmd, { })
|
|
|
|
expect(shell).to receive(:run).with("dir").and_return(output)
|
|
|
|
expect(subject.cmd("dir").exitcode).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when codepage is given" do
|
|
|
|
let(:config) {
|
|
|
|
VagrantPlugins::CommunicatorWinRM::Config.new.tap do |c|
|
|
|
|
c.codepage = 800
|
|
|
|
c.finalize!
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
it "creates shell with the given codepage when set" do
|
|
|
|
expect(connection).to receive(:shell).with(:cmd, { codepage: 800 })
|
|
|
|
expect(shell).to receive(:run).with("dir").and_return(output)
|
|
|
|
expect(subject.cmd("dir").exitcode).to eq(0)
|
|
|
|
end
|
2014-04-22 18:03:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-25 07:33:16 +00:00
|
|
|
describe ".wql" do
|
|
|
|
it "should call winrm wql" do
|
2016-12-11 08:52:00 +00:00
|
|
|
expect(connection).to receive(:run_wql).with("select * from Win32_OperatingSystem")
|
|
|
|
subject.wql("select * from Win32_OperatingSystem")
|
2016-01-25 07:33:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should retry when a WinRMAuthorizationError is received" do
|
2016-12-11 08:52:00 +00:00
|
|
|
expect(connection).to receive(:run_wql).with("select * from Win32_OperatingSystem").exactly(2).times.and_raise(
|
2016-01-25 07:33:16 +00:00
|
|
|
# Note: The initialize for WinRMAuthorizationError may require a status_code as
|
|
|
|
# the second argument in a future WinRM release. Currently it doesn't track the
|
|
|
|
# status code.
|
|
|
|
WinRM::WinRMAuthorizationError.new("Oh no!! Unauthrorized")
|
|
|
|
)
|
|
|
|
expect { subject.wql("select * from Win32_OperatingSystem") }.to raise_error(
|
|
|
|
VagrantPlugins::CommunicatorWinRM::Errors::AuthenticationFailed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-22 18:03:37 +00:00
|
|
|
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
|
|
|
|
|
2016-01-24 18:38:22 +00:00
|
|
|
context "when transport is :negotiate" do
|
|
|
|
it "should create winrm endpoint address using http" do
|
|
|
|
expect(subject.send(:endpoint)).to eq("http://localhost:5985/wsman")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-22 23:58:27 +00:00
|
|
|
context "when transport is :plaintext" do
|
2016-01-24 18:38:22 +00:00
|
|
|
let(:config) {
|
|
|
|
VagrantPlugins::CommunicatorWinRM::Config.new.tap do |c|
|
|
|
|
c.transport = :plaintext
|
|
|
|
c.finalize!
|
|
|
|
end
|
|
|
|
}
|
2014-07-22 23:58:27 +00:00
|
|
|
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(
|
2016-12-11 08:52:00 +00:00
|
|
|
{ endpoint: "http://localhost:5985/wsman", operation_timeout: 1800,
|
|
|
|
user: "username", password: "password", host: "localhost", port: 5985,
|
2016-01-25 07:33:16 +00:00
|
|
|
basic_auth_only: false, no_ssl_peer_verification: false,
|
2016-12-11 08:52:00 +00:00
|
|
|
retry_delay: 1, retry_limit: 2, transport: :negotiate })
|
2014-04-22 18:03:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|