summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVivek Singh <vivek.singh@msystechnologies.com>2019-08-14 09:02:56 +0530
committerVivek Singh <vivek.singh@msystechnologies.com>2019-08-14 09:02:56 +0530
commit75234876e5dada09707f2b8d04d5c968e905b364 (patch)
tree97bbc6ea06acbd9d8d6121c2beaec0e738fb500a
parent9cdf03ee25d0349459474e33f42c64d68bd900e1 (diff)
downloadchef-75234876e5dada09707f2b8d04d5c968e905b364.tar.gz
Fix always require sudo to change ownership
- Add check to append sudo only if require sudo privilege. - Work fine with Ubuntu, CentOS, AIX nodes. Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
-rw-r--r--lib/chef/knife/bootstrap/train_connector.rb4
-rw-r--r--spec/unit/knife/bootstrap/train_connector_spec.rb22
2 files changed, 19 insertions, 7 deletions
diff --git a/lib/chef/knife/bootstrap/train_connector.rb b/lib/chef/knife/bootstrap/train_connector.rb
index a3611470d1..3f5ea9b94a 100644
--- a/lib/chef/knife/bootstrap/train_connector.rb
+++ b/lib/chef/knife/bootstrap/train_connector.rb
@@ -128,7 +128,9 @@ class Chef
# 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}'"
+ ownership = config[:sudo] ? "sudo chown" : "chown"
+ cmd += " && #{ownership} #{config[:user]} '#{dir}'"
+
run_command!(cmd)
dir
end
diff --git a/spec/unit/knife/bootstrap/train_connector_spec.rb b/spec/unit/knife/bootstrap/train_connector_spec.rb
index a2b7a571b0..83a18cb6ca 100644
--- a/spec/unit/knife/bootstrap/train_connector_spec.rb
+++ b/spec/unit/knife/bootstrap/train_connector_spec.rb
@@ -162,16 +162,26 @@ 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_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)
+ end
+
+ it "without sudo privilege" do
+ expected_command = "mkdir -p #{dir} && 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)
+ 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}'"
+ expected_command = "mkdir -p #{dir} && chown user1 '#{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)