diff options
-rw-r--r-- | lib/chef/provider/user/useradd.rb | 7 | ||||
-rw-r--r-- | spec/support/shared/unit/provider/useradd_based_user_provider.rb | 18 |
2 files changed, 25 insertions, 0 deletions
diff --git a/lib/chef/provider/user/useradd.rb b/lib/chef/provider/user/useradd.rb index 1789a4e5ff..409da10e53 100644 --- a/lib/chef/provider/user/useradd.rb +++ b/lib/chef/provider/user/useradd.rb @@ -57,6 +57,13 @@ class Chef # we can get an exit code of 1 even when it's successful on # rhel/centos (redhat bug 578534). See additional error checks below. passwd_s = shell_out!("passwd", "-S", new_resource.username, :returns => [0,1]) + if whyrun_mode? && passwd_s.stdout.empty? && passwd_s.stderr.match(/does not exist/) + # if we're in whyrun mode and the user is not yet created we assume it would be + return false + end + + raise Chef::Exceptions::User, "Cannot determine if #{@new_resource} is locked!" if passwd_s.stdout.empty? + status_line = passwd_s.stdout.split(' ') case status_line[1] when /^P/ diff --git a/spec/support/shared/unit/provider/useradd_based_user_provider.rb b/spec/support/shared/unit/provider/useradd_based_user_provider.rb index 3b8f8678bf..e9fe06dfd7 100644 --- a/spec/support/shared/unit/provider/useradd_based_user_provider.rb +++ b/spec/support/shared/unit/provider/useradd_based_user_provider.rb @@ -335,6 +335,24 @@ shared_examples_for "a useradd-based user provider" do |supported_useradd_option lambda { provider.check_lock }.should raise_error(Mixlib::ShellOut::ShellCommandFailed) end end + + context "when in why run mode" do + before do + passwd_status = mock("Mixlib::ShellOut command", :exitstatus => 0, :stdout => "", :stderr => "passwd: user 'chef-test' does not exist\n") + provider.should_receive(:shell_out!). + with("passwd", "-S", @new_resource.username, {:returns=>[0, 1]}). + and_return(passwd_status) + Chef::Config[:why_run] = true + end + + it "should return false if the user does not exist" do + provider.check_lock.should eql(false) + end + + it "should not raise an error if the user does not exist" do + lambda { provider.check_lock }.should_not raise_error + end + end end describe "when locking the user" do |