From 351522ea504aee9e635dc7de6e668c9346f89763 Mon Sep 17 00:00:00 2001 From: Pete Higgins Date: Wed, 30 Dec 2020 15:57:04 -0800 Subject: Coerce uid to integer in Windows user resource. Signed-off-by: Pete Higgins --- .../cookbooks/end_to_end/recipes/windows.rb | 10 +++++- lib/chef/resource/user/windows_user.rb | 5 +++ spec/unit/resource/user/windows_user_spec.rb | 36 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 spec/unit/resource/user/windows_user_spec.rb diff --git a/kitchen-tests/cookbooks/end_to_end/recipes/windows.rb b/kitchen-tests/cookbooks/end_to_end/recipes/windows.rb index 58ccec9b26..ea5f0f7421 100644 --- a/kitchen-tests/cookbooks/end_to_end/recipes/windows.rb +++ b/kitchen-tests/cookbooks/end_to_end/recipes/windows.rb @@ -105,4 +105,12 @@ include_recipe "::_ohai_hint" hostname "new-hostname" do windows_reboot false -end \ No newline at end of file +end + +user "phil" do + uid "8019" +end + +user "phil" do + action :remove +end diff --git a/lib/chef/resource/user/windows_user.rb b/lib/chef/resource/user/windows_user.rb index d504158edf..d738ba1636 100644 --- a/lib/chef/resource/user/windows_user.rb +++ b/lib/chef/resource/user/windows_user.rb @@ -29,6 +29,11 @@ class Chef property :full_name, String, description: "The full name of the user.", introduced: "14.6" + + # Override the property from the parent class to coerce to integer. + property :uid, [ String, Integer, NilClass ], # nil for backwards compat + description: "The numeric user identifier.", + coerce: proc { |n| n && Integer(n) rescue n } end end end diff --git a/spec/unit/resource/user/windows_user_spec.rb b/spec/unit/resource/user/windows_user_spec.rb new file mode 100644 index 0000000000..32f8e69d99 --- /dev/null +++ b/spec/unit/resource/user/windows_user_spec.rb @@ -0,0 +1,36 @@ +# +# Copyright:: Copyright (c) Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "spec_helper" + +describe Chef::Resource::User::WindowsUser, "#uid" do + let(:resource) { Chef::Resource::User::WindowsUser.new("notarealuser") } + + it "allows a string" do + resource.uid "100" + expect(resource.uid).to eql(100) + end + + it "allows an integer" do + resource.uid 100 + expect(resource.uid).to eql(100) + end + + it "does not allow a hash" do + expect { resource.uid({ woot: "i found it" }) }.to raise_error(ArgumentError) + end +end -- cgit v1.2.1