summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2019-08-29 12:54:25 -0700
committerGitHub <noreply@github.com>2019-08-29 12:54:25 -0700
commit94a5f9b5aae78a3b7932e041dee101a03c7e9626 (patch)
tree7c5a1d97af9f9374a2a45a9acf916d8bc2e431bb
parent4022ff3be63a7e2fe4ba00d8d7356335ba06187c (diff)
parentd3b8707a698b47572481ea074b2edcf0e926ee29 (diff)
downloadchef-94a5f9b5aae78a3b7932e041dee101a03c7e9626.tar.gz
Merge pull request #8815 from MsysTechnologiesllc/VSingh/MSYS-1063-fix-bootstrap-requiring-sudo
Bootstrap: Only use sudo when changing ownership if --sudo is passed
-rw-r--r--lib/chef/knife/bootstrap/train_connector.rb6
-rw-r--r--spec/unit/knife/bootstrap/train_connector_spec.rb27
2 files changed, 23 insertions, 10 deletions
diff --git a/lib/chef/knife/bootstrap/train_connector.rb b/lib/chef/knife/bootstrap/train_connector.rb
index a3611470d1..66cde6f6b9 100644
--- a/lib/chef/knife/bootstrap/train_connector.rb
+++ b/lib/chef/knife/bootstrap/train_connector.rb
@@ -123,13 +123,13 @@ class Chef
# eg. /tmp/chef_XXXXXX.
# Use mkdir to create TEMP dir to get rid of mktemp
dir = "#{DEFAULT_REMOTE_TEMP}/chef_#{SecureRandom.alphanumeric(6)}"
- cmd = "mkdir -p %s" % dir
+ run_command!("mkdir -p '#{dir}'")
# Ensure that dir has the correct owner. We are possibly
# running with sudo right now - so this directory would be owned by root.
# File upload is performed over SCP as the current logged-in user,
# so we'll set ownership to ensure that works.
- cmd += " && sudo chown #{config[:user]} '#{dir}'"
- run_command!(cmd)
+ run_command!("chown #{config[:user]} '#{dir}'") if config[:sudo]
+
dir
end
end
diff --git a/spec/unit/knife/bootstrap/train_connector_spec.rb b/spec/unit/knife/bootstrap/train_connector_spec.rb
index a2b7a571b0..dd000f7300 100644
--- a/spec/unit/knife/bootstrap/train_connector_spec.rb
+++ b/spec/unit/knife/bootstrap/train_connector_spec.rb
@@ -162,16 +162,29 @@ describe Chef::Knife::Bootstrap::TrainConnector do
allow(SecureRandom).to receive(:alphanumeric).with(6).and_return(random)
end
- it "uses the *nix command to create the temp dir and sets ownership to logged-in user" do
- expected_command = "mkdir -p #{dir} && sudo chown user1 '#{dir}'"
- expect(subject).to receive(:run_command!).with(expected_command)
- .and_return double("result", stdout: "\r\n")
- expect(subject.temp_dir).to eq(dir)
+ context "uses the *nix command to create the temp dir and sets ownership to logged-in" do
+ it "with sudo privilege" do
+ subject.config[:sudo] = true
+ expected_command1 = "mkdir -p '#{dir}'"
+ expected_command2 = "chown user1 '#{dir}'"
+ expect(subject).to receive(:run_command!).with(expected_command1)
+ .and_return double("result", stdout: "\r\n")
+ expect(subject).to receive(:run_command!).with(expected_command2)
+ .and_return double("result", stdout: "\r\n")
+ expect(subject.temp_dir).to eq(dir)
+ end
+
+ it "without sudo privilege" do
+ expected_command = "mkdir -p '#{dir}'"
+ expect(subject).to receive(:run_command!).with(expected_command)
+ .and_return double("result", stdout: "\r\n")
+ expect(subject.temp_dir).to eq(dir)
+ end
end
context "with noise in stderr" do
- it "uses the *nix command to create the temp dir and sets ownership to logged-in user" do
- expected_command = "mkdir -p #{dir} && sudo chown user1 '#{dir}'"
+ it "uses the *nix command to create the temp dir" do
+ expected_command = "mkdir -p '#{dir}'"
expect(subject).to receive(:run_command!).with(expected_command)
.and_return double("result", stdout: "sudo: unable to resolve host hostname.localhost\r\n" + "#{dir}\r\n")
expect(subject.temp_dir).to eq(dir)