From 42cbae1e90ee73cd26399195c07a3a90f956e1bc Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 12 Jun 2018 11:57:14 -0700 Subject: [PATCH] Add test coverage on ssh capabilities --- .../communicators/ssh/communicator_test.rb | 42 +++++-------------- test/unit/plugins/hosts/bsd/cap/ssh_test.rb | 15 +++++++ test/unit/plugins/hosts/linux/cap/ssh_test.rb | 15 +++++++ .../plugins/hosts/windows/cap/ssh_test.rb | 38 +++++++++++++++++ 4 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 test/unit/plugins/hosts/bsd/cap/ssh_test.rb create mode 100644 test/unit/plugins/hosts/linux/cap/ssh_test.rb create mode 100644 test/unit/plugins/hosts/windows/cap/ssh_test.rb diff --git a/test/unit/plugins/communicators/ssh/communicator_test.rb b/test/unit/plugins/communicators/ssh/communicator_test.rb index 1b244b8a3..d9ccb6835 100644 --- a/test/unit/plugins/communicators/ssh/communicator_test.rb +++ b/test/unit/plugins/communicators/ssh/communicator_test.rb @@ -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 diff --git a/test/unit/plugins/hosts/bsd/cap/ssh_test.rb b/test/unit/plugins/hosts/bsd/cap/ssh_test.rb new file mode 100644 index 000000000..1b571a907 --- /dev/null +++ b/test/unit/plugins/hosts/bsd/cap/ssh_test.rb @@ -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 diff --git a/test/unit/plugins/hosts/linux/cap/ssh_test.rb b/test/unit/plugins/hosts/linux/cap/ssh_test.rb new file mode 100644 index 000000000..1dd67b5e9 --- /dev/null +++ b/test/unit/plugins/hosts/linux/cap/ssh_test.rb @@ -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 diff --git a/test/unit/plugins/hosts/windows/cap/ssh_test.rb b/test/unit/plugins/hosts/windows/cap/ssh_test.rb new file mode 100644 index 000000000..e1a9f93fc --- /dev/null +++ b/test/unit/plugins/hosts/windows/cap/ssh_test.rb @@ -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