Add test coverage on ssh capabilities
This commit is contained in:
parent
98c6903e9c
commit
42cbae1e90
|
@ -34,9 +34,12 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do
|
|||
double("machine",
|
||||
config: config,
|
||||
provider: provider,
|
||||
ui: ui
|
||||
ui: ui,
|
||||
env: env
|
||||
)
|
||||
end
|
||||
let(:env){ double("env", host: host) }
|
||||
let(:host){ double("host") }
|
||||
# SSH information of the machine
|
||||
let(:machine_ssh_info){ {host: '10.1.2.3', port: 22} }
|
||||
# Subject instance to test
|
||||
|
@ -89,6 +92,10 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do
|
|||
allow(communicator).to receive(:retryable).and_return(connection)
|
||||
end
|
||||
|
||||
before do
|
||||
allow(host).to receive(:has_capability?).and_return(false)
|
||||
end
|
||||
|
||||
describe ".wait_for_ready" do
|
||||
before(&connection_setup)
|
||||
context "with no static config (default scenario)" do
|
||||
|
@ -208,41 +215,14 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do
|
|||
expect(private_key_file).to receive(:write).with(new_private_key)
|
||||
end
|
||||
|
||||
it "should set private key file as user readable only" do
|
||||
expect(private_key_file).to receive(:chmod).with(0600)
|
||||
it "should call the set_ssh_key_permissions host capability" do
|
||||
expect(host).to receive(:has_capability?).with(:set_ssh_key_permissions).and_return(true)
|
||||
expect(host).to receive(:capability).with(:set_ssh_key_permissions, private_key_file)
|
||||
end
|
||||
|
||||
it "should remove the default public key" do
|
||||
expect(guest).to receive(:capability).with(:remove_public_key, any_args)
|
||||
end
|
||||
|
||||
context "on windows platform" do
|
||||
let(:owner){ "owner" }
|
||||
|
||||
before do
|
||||
allow(private_key_file).to receive(:to_s).and_return("PRIVATE_KEY_PATH")
|
||||
allow(File).to receive(:set_permissions)
|
||||
allow(Vagrant::Util::Platform).to receive(:windows?).and_return(true)
|
||||
allow(Etc).to receive(:getlogin).and_return(owner)
|
||||
stub_const('File::FULL', :full)
|
||||
end
|
||||
|
||||
it "should get set new permissions on private key file" do
|
||||
expect(File).to receive(:set_permissions).with("PRIVATE_KEY_PATH", any_args)
|
||||
end
|
||||
|
||||
it "should proceed when error is encountered" do
|
||||
expect(File).to receive(:set_permissions).and_raise(StandardError)
|
||||
end
|
||||
|
||||
context "with multiple permissions on file" do
|
||||
|
||||
it "should delete all non-owner permissions" do
|
||||
expect(File).to receive(:set_permissions).with("PRIVATE_KEY_PATH",
|
||||
owner => :full)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
require_relative "../../../../base"
|
||||
|
||||
require_relative "../../../../../../plugins/hosts/bsd/cap/ssh"
|
||||
|
||||
describe VagrantPlugins::HostBSD::Cap::SSH do
|
||||
let(:subject){ VagrantPlugins::HostBSD::Cap::SSH }
|
||||
|
||||
let(:env){ double("env") }
|
||||
let(:key_path){ double("key_path") }
|
||||
|
||||
it "should set file as user only read/write" do
|
||||
expect(key_path).to receive(:chmod).with(0600)
|
||||
subject.set_ssh_key_permissions(env, key_path)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,15 @@
|
|||
require_relative "../../../../base"
|
||||
|
||||
require_relative "../../../../../../plugins/hosts/linux/cap/ssh"
|
||||
|
||||
describe VagrantPlugins::HostLinux::Cap::SSH do
|
||||
let(:subject){ VagrantPlugins::HostLinux::Cap::SSH }
|
||||
|
||||
let(:env){ double("env") }
|
||||
let(:key_path){ double("key_path") }
|
||||
|
||||
it "should set file as user only read/write" do
|
||||
expect(key_path).to receive(:chmod).with(0600)
|
||||
subject.set_ssh_key_permissions(env, key_path)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,38 @@
|
|||
require_relative "../../../../base"
|
||||
|
||||
require_relative "../../../../../../plugins/hosts/windows/cap/ssh"
|
||||
|
||||
describe VagrantPlugins::HostWindows::Cap::SSH do
|
||||
let(:subject){ VagrantPlugins::HostWindows::Cap::SSH }
|
||||
let(:result){ Vagrant::Util::Subprocess::Result.new(exit_code, stdout, stderr) }
|
||||
let(:exit_code){ 0 }
|
||||
let(:stdout){ "" }
|
||||
let(:stderr){ "" }
|
||||
|
||||
let(:key_path){ double("keypath", to_s: "keypath") }
|
||||
let(:env){ double("env") }
|
||||
|
||||
before do
|
||||
allow(Vagrant::Util::PowerShell).to receive(:execute).and_return(result)
|
||||
end
|
||||
|
||||
it "should execute PowerShell script" do
|
||||
expect(Vagrant::Util::PowerShell).to receive(:execute).with(
|
||||
/set_ssh_key_permissions.ps1/, key_path.to_s, any_args
|
||||
).and_return(result)
|
||||
subject.set_ssh_key_permissions(env, key_path)
|
||||
end
|
||||
|
||||
it "should return the result" do
|
||||
|
||||
expect(subject.set_ssh_key_permissions(env, key_path)).to eq(result)
|
||||
end
|
||||
|
||||
context "when command fails" do
|
||||
let(:exit_code){ 1 }
|
||||
|
||||
it "should raise an error" do
|
||||
expect{ subject.set_ssh_key_permissions(env, key_path) }.to raise_error(Vagrant::Errors::PowerShellError)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue