summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsnehaldwivedi <sdwivedi@msystechnologies.com>2021-03-31 06:48:10 -0700
committersnehaldwivedi <sdwivedi@msystechnologies.com>2021-04-04 23:00:07 -0700
commit944aee756a2542dbe06e3fd65f63e341eb655f85 (patch)
treed06ac2d72e931ab2c981c8e7b969ba7b78712316
parent8cfbf115fb2f0ebcd015f9c96aa810c5057312c8 (diff)
downloadchef-snehal/Knife_client_create_should_check_file_permissions.tar.gz
Updated writable condition for check file permissionssnehal/Knife_client_create_should_check_file_permissions
Signed-off-by: snehaldwivedi <sdwivedi@msystechnologies.com>
-rw-r--r--knife/lib/chef/knife/client_create.rb10
-rw-r--r--spec/unit/knife/client_create_spec.rb14
2 files changed, 22 insertions, 2 deletions
diff --git a/knife/lib/chef/knife/client_create.rb b/knife/lib/chef/knife/client_create.rb
index 825cdac1c6..0df8c2c666 100644
--- a/knife/lib/chef/knife/client_create.rb
+++ b/knife/lib/chef/knife/client_create.rb
@@ -84,8 +84,14 @@ class Chef
# Check the file before creating the client so the api is more transactional.
if config[:file]
file = config[:file]
- unless File.exist?(file) ? File.writable?(file) : File.writable?(File.dirname(file))
- ui.fatal "File #{config[:file]} is not writable. Check permissions."
+
+ unless File.writable?(File.dirname(file))
+ ui.fatal "Dir #{File.dirname(file)} is not writable. Check permissions."
+ exit 1
+ end
+
+ unless File.writable?(file)
+ ui.fatal "File #{config[:file]} is not writable. Check permissions."
exit 1
end
end
diff --git a/spec/unit/knife/client_create_spec.rb b/spec/unit/knife/client_create_spec.rb
index d7d108658e..043dad71e7 100644
--- a/spec/unit/knife/client_create_spec.rb
+++ b/spec/unit/knife/client_create_spec.rb
@@ -166,6 +166,20 @@ describe Chef::Knife::ClientCreate do
expect(client.validator).to be_truthy
end
end
+
+ describe "with -f or --file when dir or file is not writable" do
+ it "when the directory is not writable" do
+ knife.config[:file] = "example/client1.pem"
+ expect(knife.ui).to receive(:fatal).with("Dir example is not writable. Check permissions.")
+ expect { knife.run }.to raise_error(SystemExit)
+ end
+
+ it "when the file is not writable" do
+ knife.config[:file] = "test/client1.pem"
+ expect(knife.ui).to receive(:fatal).with("File test/client1.pem is not writable. Check permissions.")
+ expect { knife.run }.to raise_error(SystemExit)
+ end
+ end
end
end
end