2013-12-04 15:33:27 +00:00
|
|
|
require File.expand_path("../../../../base", __FILE__)
|
|
|
|
|
2014-01-11 16:59:13 +00:00
|
|
|
require Vagrant.source_root.join("plugins/commands/ssh_config/command")
|
|
|
|
|
|
|
|
describe VagrantPlugins::CommandSSHConfig::Command do
|
2013-12-04 15:33:27 +00:00
|
|
|
include_context "unit"
|
|
|
|
include_context "virtualbox"
|
|
|
|
|
2014-01-11 16:59:13 +00:00
|
|
|
let(:iso_env) do
|
|
|
|
# We have to create a Vagrantfile so there is a root path
|
|
|
|
env = isolated_environment
|
|
|
|
env.vagrantfile("")
|
|
|
|
env.create_vagrant_env
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:guest) { double("guest") }
|
|
|
|
let(:host) { double("host") }
|
|
|
|
let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }
|
2013-12-04 15:33:27 +00:00
|
|
|
|
|
|
|
let(:argv) { [] }
|
|
|
|
let(:ssh_info) {{
|
2014-05-22 16:35:12 +00:00
|
|
|
host: "testhost.vagrant.dev",
|
|
|
|
port: 1234,
|
|
|
|
username: "testuser",
|
2016-07-22 01:15:21 +00:00
|
|
|
keys_only: true,
|
|
|
|
paranoid: false,
|
2017-08-22 23:43:20 +00:00
|
|
|
private_key_path: ["/home/vagrant/.private/keys.key"],
|
2014-05-22 16:35:12 +00:00
|
|
|
forward_agent: false,
|
|
|
|
forward_x11: false
|
2013-12-04 15:33:27 +00:00
|
|
|
}}
|
|
|
|
|
2014-01-11 16:59:13 +00:00
|
|
|
subject { described_class.new(argv, iso_env) }
|
2013-12-04 15:33:27 +00:00
|
|
|
|
|
|
|
before do
|
2017-08-04 17:10:58 +00:00
|
|
|
allow(machine).to receive(:ssh_info).and_return(ssh_info)
|
2014-03-14 15:02:07 +00:00
|
|
|
allow(subject).to receive(:with_target_vms) { |&block| block.call machine }
|
2013-12-04 15:33:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "execute" do
|
|
|
|
it "prints out the ssh config for the given machine" do
|
2014-04-09 23:00:17 +00:00
|
|
|
output = ""
|
|
|
|
allow(subject).to receive(:safe_puts) do |data|
|
|
|
|
output += data if data
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
expect(output).to eq(<<-SSHCONFIG)
|
2014-01-11 16:59:13 +00:00
|
|
|
Host #{machine.name}
|
2013-12-04 15:33:27 +00:00
|
|
|
HostName testhost.vagrant.dev
|
|
|
|
User testuser
|
|
|
|
Port 1234
|
|
|
|
UserKnownHostsFile /dev/null
|
|
|
|
StrictHostKeyChecking no
|
|
|
|
PasswordAuthentication no
|
2017-08-22 23:43:20 +00:00
|
|
|
IdentityFile /home/vagrant/.private/keys.key
|
2013-12-04 15:33:27 +00:00
|
|
|
IdentitiesOnly yes
|
|
|
|
LogLevel FATAL
|
|
|
|
SSHCONFIG
|
|
|
|
end
|
|
|
|
|
|
|
|
it "turns on agent forwarding when it is configured" do
|
2014-05-22 16:35:12 +00:00
|
|
|
allow(machine).to receive(:ssh_info) { ssh_info.merge(forward_agent: true) }
|
2014-04-09 23:00:17 +00:00
|
|
|
|
|
|
|
output = ""
|
|
|
|
allow(subject).to receive(:safe_puts) do |data|
|
|
|
|
output += data if data
|
|
|
|
end
|
|
|
|
|
2013-12-04 15:33:27 +00:00
|
|
|
subject.execute
|
2014-04-09 23:00:17 +00:00
|
|
|
|
|
|
|
expect(output).to include("ForwardAgent yes")
|
2013-12-04 15:33:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "turns on x11 forwarding when it is configured" do
|
2014-05-22 16:35:12 +00:00
|
|
|
allow(machine).to receive(:ssh_info) { ssh_info.merge(forward_x11: true) }
|
2014-04-09 23:00:17 +00:00
|
|
|
|
|
|
|
output = ""
|
|
|
|
allow(subject).to receive(:safe_puts) do |data|
|
|
|
|
output += data if data
|
|
|
|
end
|
|
|
|
|
2013-12-04 15:33:27 +00:00
|
|
|
subject.execute
|
2014-04-09 23:00:17 +00:00
|
|
|
|
|
|
|
expect(output).to include("ForwardX11 yes")
|
2013-12-04 15:33:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "handles multiple private key paths" do
|
2014-05-22 16:35:12 +00:00
|
|
|
allow(machine).to receive(:ssh_info) { ssh_info.merge(private_key_path: ["foo", "bar"]) }
|
2014-04-09 23:00:17 +00:00
|
|
|
|
|
|
|
output = ""
|
|
|
|
allow(subject).to receive(:safe_puts) do |data|
|
|
|
|
output += data if data
|
|
|
|
end
|
|
|
|
|
2013-12-04 15:33:27 +00:00
|
|
|
subject.execute
|
2014-04-09 23:00:17 +00:00
|
|
|
|
2016-02-02 14:38:53 +00:00
|
|
|
expect(output).to include("IdentityFile foo")
|
|
|
|
expect(output).to include("IdentityFile bar")
|
2013-12-04 15:33:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "puts quotes around an identityfile path if it has a space" do
|
2014-05-22 16:35:12 +00:00
|
|
|
allow(machine).to receive(:ssh_info) { ssh_info.merge(private_key_path: ["with a space"]) }
|
2014-04-09 23:00:17 +00:00
|
|
|
output = ""
|
|
|
|
allow(subject).to receive(:safe_puts) do |data|
|
|
|
|
output += data if data
|
|
|
|
end
|
|
|
|
|
2013-12-04 15:33:27 +00:00
|
|
|
subject.execute
|
2014-04-09 23:00:17 +00:00
|
|
|
|
|
|
|
expect(output).to include('IdentityFile "with a space"')
|
2013-12-04 15:33:27 +00:00
|
|
|
end
|
2016-07-22 01:15:21 +00:00
|
|
|
|
|
|
|
it "omits IdentitiesOnly when keys_only is false" do
|
|
|
|
allow(machine).to receive(:ssh_info) { ssh_info.merge(keys_only: false) }
|
|
|
|
|
|
|
|
output = ""
|
|
|
|
allow(subject).to receive(:safe_puts) do |data|
|
|
|
|
output += data if data
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
expect(output).not_to include('IdentitiesOnly')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "omits StrictHostKeyChecking and UserKnownHostsFile when paranoid is true" do
|
|
|
|
allow(machine).to receive(:ssh_info) { ssh_info.merge(paranoid: true) }
|
|
|
|
|
|
|
|
output = ""
|
|
|
|
allow(subject).to receive(:safe_puts) do |data|
|
|
|
|
output += data if data
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
expect(output).not_to include('StrictHostKeyChecking ')
|
|
|
|
expect(output).not_to include('UserKnownHostsFile ')
|
|
|
|
end
|
2017-08-22 23:43:20 +00:00
|
|
|
|
|
|
|
it "formats windows paths if windows" do
|
|
|
|
allow(machine).to receive(:ssh_info) { ssh_info.merge(private_key_path: ["C:\\path\\to\\vagrant\\home.key"]) }
|
|
|
|
allow(Vagrant::Util::Platform).to receive(:format_windows_path).and_return("/home/vagrant/home.key")
|
|
|
|
allow(Vagrant::Util::Platform).to receive(:windows?).and_return(true)
|
|
|
|
|
|
|
|
output = ""
|
|
|
|
allow(subject).to receive(:safe_puts) do |data|
|
|
|
|
output += data if data
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
expect(output).to include('IdentityFile /home/vagrant/home.key')
|
|
|
|
end
|
2013-12-04 15:33:27 +00:00
|
|
|
end
|
|
|
|
end
|