diff options
author | Kapil Chouhan <kapil.chouhan@msystechnologies.com> | 2020-04-15 18:08:00 +0530 |
---|---|---|
committer | Kapil Chouhan <kapil.chouhan@msystechnologies.com> | 2020-04-17 17:07:34 +0530 |
commit | adcc1c6d0fe1b4cb34dc981b1819177545b30463 (patch) | |
tree | 40801fc5df64692c13d7ba73af24bc340f1e6ecc | |
parent | cb4383bb80656be29db6194308d6a913894ad218 (diff) | |
download | chef-Kapil/GitHub-2860_exception_raised_in_define_resource_requirements.tar.gz |
Fix for exception raised in define resource requirementsKapil/GitHub-2860_exception_raised_in_define_resource_requirements
Signed-off-by: Kapil Chouhan <kapil.chouhan@msystechnologies.com>
-rw-r--r-- | lib/chef/provider/git.rb | 41 | ||||
-rw-r--r-- | spec/unit/provider/git_spec.rb | 37 |
2 files changed, 66 insertions, 12 deletions
diff --git a/lib/chef/provider/git.rb b/lib/chef/provider/git.rb index 1ef1481c9e..431c19449b 100644 --- a/lib/chef/provider/git.rb +++ b/lib/chef/provider/git.rb @@ -41,6 +41,20 @@ class Chef end def define_resource_requirements + unless new_resource.user.nil? + requirements.assert(:all_actions) do |a| + a.assertion do + begin + get_homedir(new_resource.user) + rescue ArgumentError + false + end + end + a.whyrun("User #{new_resource.user} does not exist, this run will fail unless it has been previously created. Assuming it would have been created.") + a.failure_message(Chef::Exceptions::User, "#{new_resource.user} required by resource #{new_resource.name} does not exist") + end + end + # Parent directory of the target must exist. requirements.assert(:checkout, :sync) do |a| dirname = ::File.dirname(cwd) @@ -313,18 +327,7 @@ class Chef # Certain versions of `git` misbehave if git configuration is # inaccessible in $HOME. We need to ensure $HOME matches the # user who is executing `git` not the user running Chef. - env["HOME"] = - begin - require "etc" unless defined?(Etc) - case new_resource.user - when Integer - Etc.getpwuid(new_resource.user).dir - else - Etc.getpwnam(new_resource.user.to_s).dir - end - rescue ArgumentError # user not found - raise Chef::Exceptions::User, "Could not determine HOME for specified user '#{new_resource.user}' for resource '#{new_resource.name}'" - end + env["HOME"] = get_homedir(new_resource.user) end run_opts[:group] = new_resource.group if new_resource.group env["GIT_SSH"] = new_resource.ssh_wrapper if new_resource.ssh_wrapper @@ -355,6 +358,20 @@ class Chef new_resource.repository end end + + # Returns the home directory of the user + # @param [String] user must be a string. + # @return [String] the home directory of the user. + # + def get_homedir(user) + require "etc" unless defined?(Etc) + case user + when Integer + Etc.getpwuid(user).dir + else + Etc.getpwnam(user.to_s).dir + end + end end end end diff --git a/spec/unit/provider/git_spec.rb b/spec/unit/provider/git_spec.rb index 9a6ae31397..953c3c1e98 100644 --- a/spec/unit/provider/git_spec.rb +++ b/spec/unit/provider/git_spec.rb @@ -590,6 +590,43 @@ describe Chef::Provider::Git do end end + context "with why-run mode" do + before do + Chef::Config[:why_run] = true + @resource.user "test" + end + + after do + Chef::Config[:why_run] = false + end + + it "does not raise an error if user does not exist" do + allow(@provider).to receive(:get_homedir).with(@resource.user).and_return(nil) + expect { @provider.run_action(:sync) }.not_to raise_error + end + + it "does not raise an error if user exists" do + allow(@provider).to receive(:get_homedir).with(@resource.user).and_return("/home/test") + expect { @provider.run_action(:sync) }.not_to raise_error(ArgumentError) + end + end + + context "without why-run mode" do + before do + @resource.user "test" + end + + it "raises an error if user does not exist" do + allow(@provider).to receive(:get_homedir).with(@resource.user).and_return(nil) + expect { @provider.run_action(:sync) }.to raise_error(Chef::Exceptions::User) + end + + it "does not raise an error if user exists" do + allow(@provider).to receive(:get_homedir).with(@resource.user).and_return("/home/test") + expect { @provider.run_action(:sync) }.not_to raise_error(Chef::Exceptions::User) + end + end + it "raises an error if the git clone command would fail because the enclosing directory doesn't exist" do allow(@provider).to receive(:shell_out!) expect { @provider.run_action(:sync) }.to raise_error(Chef::Exceptions::MissingParentDirectory) |