summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVivek Singh <vivek.singh@msystechnologies.com>2020-09-11 09:25:49 +0530
committerVivek Singh <vivek.singh@msystechnologies.com>2020-09-17 09:21:43 +0530
commitf8abc8de96da8f98a7c37b6ab236b62c3a9e67b2 (patch)
tree1828c0160937054bd82a34d6acc49bc21837eb78
parent507b0c9fc8c9a1211f7de494bc04d3ea05fa42fd (diff)
downloadchef-f8abc8de96da8f98a7c37b6ab236b62c3a9e67b2.tar.gz
Add test cases
Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
-rw-r--r--spec/unit/knife/bootstrap_spec.rb46
1 files changed, 44 insertions, 2 deletions
diff --git a/spec/unit/knife/bootstrap_spec.rb b/spec/unit/knife/bootstrap_spec.rb
index 53c18e3b77..85f506d924 100644
--- a/spec/unit/knife/bootstrap_spec.rb
+++ b/spec/unit/knife/bootstrap_spec.rb
@@ -1738,7 +1738,8 @@ describe Chef::Knife::Bootstrap do
describe "#perform_bootstrap" do
let(:exit_status) { 0 }
- let(:result_mock) { double("result", exit_status: exit_status, stderr: "A message", stdout: "") }
+ let(:stdout) { "" }
+ let(:result_mock) { double("result", exit_status: exit_status, stderr: "A message", stdout: stdout) }
before do
allow(connection).to receive(:hostname).and_return "testhost"
@@ -1757,6 +1758,7 @@ describe Chef::Knife::Bootstrap do
expect(knife.ui).to receive(:msg).with(/testhost/)
knife.perform_bootstrap("/path.sh")
end
+
context "when the remote command fails" do
let(:exit_status) { 1 }
it "shows an error and exits" do
@@ -1768,6 +1770,28 @@ describe Chef::Knife::Bootstrap do
expect { knife.perform_bootstrap("/path.sh") }.to raise_error(SystemExit)
end
end
+
+ context "when the remote command failed due to su auth error" do
+ let(:exit_status) { 1 }
+ let(:stdout) { "su: Authentication failure" }
+ let(:connection_obj) { double("connection", transport_options: {}) }
+ let(:result_mock2) { double("result", exit_status: 1, stderr: "A message", stdout: "") }
+ it "shows an error and exits" do
+ allow(connection).to receive(:connection).and_return(connection_obj)
+ expect(knife.ui).to receive(:info).with(/Bootstrapping.*/)
+ expect(knife).to receive(:bootstrap_command)
+ .with("/path.sh")
+ .and_return("su - USER -c 'sh /path.sh'")
+ expect(connection)
+ .to receive(:run_command)
+ .with("su - USER -c 'sh /path.sh'")
+ .and_yield("output here")
+ .and_return result_mock
+ expect(knife.ui).to receive(:ask).and_return("password").twice
+ expect(connection).to receive(:run_command).with("su - USER -c 'sh /path.sh'").and_return(result_mock, result_mock2)
+ expect { knife.perform_bootstrap("/path.sh") }.to raise_error(SystemExit)
+ end
+ end
end
describe "#connect!" do
@@ -1976,7 +2000,25 @@ describe Chef::Knife::Bootstrap do
context "under Linux" do
let(:linux_test) { true }
it "prefixes the command to run under sh" do
- expect(knife.bootstrap_command("bootstrap")).to eq "sh bootstrap"
+ expect(knife.bootstrap_command("bootstrap.sh")).to eq "sh bootstrap.sh"
+ end
+
+ context "with --su-user option" do
+ let(:connection_obj) { double("connection", transport_options: {}) }
+ before do
+ knife.config[:su_user] = "root"
+ allow(connection).to receive(:connection).and_return(connection_obj)
+ end
+ it "prefixes the command to run using su -USER -c" do
+ expect(knife.bootstrap_command("bootstrap.sh")).to eq "su - #{knife.config[:su_user]} -c 'sh bootstrap.sh'"
+ expect(connection_obj.transport_options.key?(:pty)).to eq true
+ end
+
+ it "sudo appended if --sudo option enabled" do
+ knife.config[:use_sudo] = true
+ expect(knife.bootstrap_command("bootstrap.sh")).to eq "sudo su - #{knife.config[:su_user]} -c 'sh bootstrap.sh'"
+ expect(connection_obj.transport_options.key?(:pty)).to eq true
+ end
end
end
end