summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2019-02-08 16:08:08 -0800
committerTim Smith <tsmith@chef.io>2019-02-19 10:06:11 -0800
commite2f1323d57ad9366ec1c0328713b618c2e6e6418 (patch)
tree46543019ecca6e5f1871882ddd7ae44f5268bf6c
parentdd3094a5ec35d3130086b4f7d4e102b92e1ae9ed (diff)
downloadchef-e2f1323d57ad9366ec1c0328713b618c2e6e6418.tar.gz
Convert the user resource to use the resource DSL
This is one of the last ones that needed to be updated for the resource DSL. This gets us quite a bit of resource inspector goodness. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/user.rb171
-rw-r--r--spec/unit/provider/user_spec.rb48
-rw-r--r--spec/unit/resource/user_spec.rb43
3 files changed, 91 insertions, 171 deletions
diff --git a/lib/chef/resource/user.rb b/lib/chef/resource/user.rb
index df52ebb083..8e8f6b61a1 100644
--- a/lib/chef/resource/user.rb
+++ b/lib/chef/resource/user.rb
@@ -23,137 +23,58 @@ class Chef
# Use the user resource to add users, update existing users, remove users, and to lock/unlock user passwords.
class User < Chef::Resource
resource_name :user_resource_abstract_base_class # this prevents magickal class name DSL wiring
- identity_attr :username
-
- state_attrs :uid, :gid, :home
default_action :create
allowed_actions :create, :remove, :modify, :manage, :lock, :unlock
- def initialize(name, run_context = nil)
- super
- @username = name
- @comment = nil
- @uid = nil
- @gid = nil
- @home = nil
- @shell = nil
- @password = nil
- @system = false
- @manage_home = false
- @force = false
- @non_unique = false
- @iterations = 27855
- @salt = nil
- end
-
- def username(arg = nil)
- set_or_return(
- :username,
- arg,
- kind_of: [ String ]
- )
- end
-
- def comment(arg = nil)
- set_or_return(
- :comment,
- arg,
- kind_of: [ String ]
- )
- end
-
- def uid(arg = Chef::NOT_PASSED)
- set_or_return(
- :uid,
- arg,
- kind_of: [ String, Integer, NilClass ],
- coerce: proc { |x| x || nil }
- )
- end
-
- def gid(arg = Chef::NOT_PASSED)
- set_or_return(
- :gid,
- arg,
- kind_of: [ String, Integer, NilClass ],
- coerce: proc { |x| x || nil }
- )
- end
+ property :username, String,
+ description: "",
+ name_property: true, identity: true
- alias_method :group, :gid
+ property :comment, String,
+ description: ""
+
+ property :home, String,
+ description: ""
+
+ property :salt, String,
+ description: "",
+ desired_state: false
+
+ property :shell, String,
+ description: ""
+
+ property :password, String,
+ description: "",
+ desired_state: false
+
+ property :iterations, Integer,
+ description: "",
+ default: 27855, desired_state: false
- def home(arg = nil)
- set_or_return(
- :home,
- arg,
- kind_of: [ String ]
- )
- end
-
- def shell(arg = nil)
- set_or_return(
- :shell,
- arg,
- kind_of: [ String ]
- )
- end
-
- def password(arg = nil)
- set_or_return(
- :password,
- arg,
- kind_of: [ String ]
- )
- end
-
- def salt(arg = nil)
- set_or_return(
- :salt,
- arg,
- kind_of: [ String ]
- )
- end
-
- def iterations(arg = nil)
- set_or_return(
- :iterations,
- arg,
- kind_of: [ Integer ]
- )
- end
-
- def system(arg = nil)
- set_or_return(
- :system,
- arg,
- kind_of: [ TrueClass, FalseClass ]
- )
- end
-
- def manage_home(arg = nil)
- set_or_return(
- :manage_home,
- arg,
- kind_of: [ TrueClass, FalseClass ]
- )
- end
-
- def force(arg = nil)
- set_or_return(
- :force,
- arg,
- kind_of: [ TrueClass, FalseClass ]
- )
- end
-
- def non_unique(arg = nil)
- set_or_return(
- :non_unique,
- arg,
- kind_of: [ TrueClass, FalseClass ]
- )
- end
+ property :non_unique, [ TrueClass, FalseClass ],
+ description: "",
+ default: false, desired_state: false
+
+ property :manage_home, [ TrueClass, FalseClass ],
+ description: "",
+ default: false, desired_state: false
+
+ property :force, [ TrueClass, FalseClass ],
+ description: "",
+ default: false, desired_state: false
+
+ property :system, [ TrueClass, FalseClass ],
+ description: "",
+ default: false
+
+ property :uid, [ String, Integer, NilClass ], # nil for backwards compat
+ description: ""
+
+ property :gid, [ String, Integer, NilClass ], # nil for backwards compat
+ description: ""
+
+ alias_method :group, :gid
end
end
end
diff --git a/spec/unit/provider/user_spec.rb b/spec/unit/provider/user_spec.rb
index 7a07527834..85312f2319 100644
--- a/spec/unit/provider/user_spec.rb
+++ b/spec/unit/provider/user_spec.rb
@@ -1,6 +1,6 @@
#
# Author:: Adam Jacob (<adam@chef.io>)
-# Copyright:: Copyright 2008-2016, Chef Software Inc.
+# Copyright:: Copyright 2008-2019, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,18 +27,18 @@ describe Chef::Provider::User do
@events = Chef::EventDispatch::Dispatcher.new
@run_context = Chef::RunContext.new(@node, {}, @events)
- @new_resource = Chef::Resource::User.new("adam")
- @new_resource.comment "Adam Jacob"
+ @new_resource = Chef::Resource::User.new("notarealuser")
+ @new_resource.comment "Nota Realuser"
@new_resource.uid 1000
@new_resource.gid 1000
- @new_resource.home "/home/adam"
+ @new_resource.home "/home/notarealuser"
@new_resource.shell "/usr/bin/zsh"
- @current_resource = Chef::Resource::User.new("adam")
- @current_resource.comment "Adam Jacob"
+ @current_resource = Chef::Resource::User.new("notarealuser")
+ @current_resource.comment "Nota Realuser"
@current_resource.uid 1000
@current_resource.gid 1000
- @current_resource.home "/home/adam"
+ @current_resource.home "/home/notarealuser"
@current_resource.shell "/usr/bin/zsh"
@provider = Chef::Provider::User.new(@new_resource, @run_context)
@@ -60,22 +60,22 @@ describe Chef::Provider::User do
@node = Chef::Node.new
# @new_resource = double("Chef::Resource::User",
# :null_object => true,
- # :username => "adam",
- # :comment => "Adam Jacob",
+ # :username => "notarealuser",
+ # :comment => "Nota Realuser",
# :uid => 1000,
# :gid => 1000,
- # :home => "/home/adam",
+ # :home => "/home/notarealuser",
# :shell => "/usr/bin/zsh",
# :password => nil,
# :updated => nil
# )
allow(Chef::Resource::User).to receive(:new).and_return(@current_resource)
@pw_user = EtcPwnamIsh.new
- @pw_user.name = "adam"
+ @pw_user.name = "notarealuser"
@pw_user.gid = 1000
@pw_user.uid = 1000
- @pw_user.gecos = "Adam Jacob"
- @pw_user.dir = "/home/adam"
+ @pw_user.gecos = "Nota Realuser"
+ @pw_user.dir = "/home/notarealuser"
@pw_user.shell = "/usr/bin/zsh"
@pw_user.passwd = "*"
allow(Etc).to receive(:getpwnam).and_return(@pw_user)
@@ -83,7 +83,7 @@ describe Chef::Provider::User do
it "should create a current resource with the same name as the new resource" do
@provider.load_current_resource
- expect(@provider.current_resource.name).to eq("adam")
+ expect(@provider.current_resource.name).to eq("notarealuser")
end
it "should set the username of the current resource to the username of the new resource" do
@@ -129,7 +129,7 @@ describe Chef::Provider::User do
end
it "shouldn't try and convert the group gid if none has been supplied" do
- @new_resource.gid(false)
+ @new_resource.gid(nil)
expect(@provider).not_to receive(:convert_group_name)
@provider.load_current_resource
end
@@ -168,7 +168,7 @@ describe Chef::Provider::User do
expect(@provider).to receive(:require) { |*args| original_method.call(*args) }
passwd_info = Struct::PasswdEntry.new(sp_namp: "adm ", sp_pwdp: "$1$T0N0Q.lc$nyG6pFI3Dpqa5cxUz/57j0", sp_lstchg: 14861, sp_min: 0, sp_max: 99999,
sp_warn: 7, sp_inact: -1, sp_expire: -1, sp_flag: -1)
- expect(Shadow::Passwd).to receive(:getspnam).with("adam").and_return(passwd_info)
+ expect(Shadow::Passwd).to receive(:getspnam).with("notarealuser").and_return(passwd_info)
@provider.load_current_resource
@provider.define_resource_requirements
@provider.process_resource_requirements
@@ -190,11 +190,11 @@ describe Chef::Provider::User do
describe "compare_user" do
let(:mapping) do
{
- "username" => %w{adam Adam},
- "comment" => ["Adam Jacob", "adam jacob"],
+ "username" => %w{notarealuser notarealuser},
+ "comment" => ["Nota Realuser", "Nota Realuser"],
"uid" => [1000, 1001],
"gid" => [1000, 1001],
- "home" => ["/home/adam", "/Users/adam"],
+ "home" => ["/home/notarealuser", "/Users/notarealuser"],
"shell" => ["/usr/bin/zsh", "/bin/bash"],
"password" => %w{abcd 12345},
}
@@ -221,8 +221,8 @@ describe Chef::Provider::User do
end
it "should ignore differences in trailing slash in home paths" do
- @new_resource.home "/home/adam"
- @current_resource.home "/home/adam/"
+ @new_resource.home "/home/notarealuser"
+ @current_resource.home "/home/notarealuser/"
expect(@provider.compare_user).to eql(false)
end
end
@@ -232,11 +232,11 @@ describe Chef::Provider::User do
allow(@provider).to receive(:load_current_resource)
# @current_resource = double("Chef::Resource::User",
# :null_object => true,
- # :username => "adam",
- # :comment => "Adam Jacob",
+ # :username => "notarealuser",
+ # :comment => "Nota Realuser",
# :uid => 1000,
# :gid => 1000,
- # :home => "/home/adam",
+ # :home => "/home/notarealuser",
# :shell => "/usr/bin/zsh",
# :password => nil,
# :updated => nil
diff --git a/spec/unit/resource/user_spec.rb b/spec/unit/resource/user_spec.rb
index 2a96c5328d..08d1636643 100644
--- a/spec/unit/resource/user_spec.rb
+++ b/spec/unit/resource/user_spec.rb
@@ -1,6 +1,6 @@
#
# Author:: Adam Jacob (<adam@chef.io>)
-# Copyright:: Copyright 2008-2017, Chef Software Inc.
+# Copyright:: Copyright 2008-2019, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,19 +19,19 @@
require "spec_helper"
describe Chef::Resource::User, "initialize" do
- let(:resource) { Chef::Resource::User.new("adam") }
+ let(:resource) { Chef::Resource::User.new("notarealuser") }
- it "sets the resource_name to :user" do
+ it "sets the resource_name to :user_resource_abstract_base_class" do
expect(resource.resource_name).to eql(:user_resource_abstract_base_class)
end
- it "sets the username equal to the argument to initialize" do
- expect(resource.username).to eql("adam")
+ it "username property is the name property" do
+ expect(resource.username).to eql("notarealuser")
end
- %w{comment uid gid home shell password}.each do |attrib|
- it "sets #{attrib} to nil" do
- expect(resource.send(attrib)).to eql(nil)
+ %w{comment uid gid home shell password}.each do |prop|
+ it "sets #{prop} to nil" do
+ expect(resource.send(prop)).to eql(nil)
end
end
@@ -39,16 +39,10 @@ describe Chef::Resource::User, "initialize" do
expect(resource.action).to eql([:create])
end
- it "sets manage_home to false" do
- expect(resource.manage_home).to eql(false)
- end
-
- it "sets non_unique to false" do
- expect(resource.non_unique).to eql(false)
- end
-
- it "sets force to false" do
- expect(resource.force).to eql(false)
+ %w{manage_home non_unique force system}.each do |prop|
+ it "sets #{prop} to false" do
+ expect(resource.send(prop)).to eql(false)
+ end
end
%w{create remove modify manage lock unlock}.each do |action|
@@ -57,6 +51,11 @@ describe Chef::Resource::User, "initialize" do
end
end
+ it "group is an alias for the gid property" do
+ resource.group(1234)
+ expect(resource.gid).to eql(1234)
+ end
+
it "accepts domain users (@ or \ separator) on non-windows" do
expect { resource.username "domain\@user" }.not_to raise_error
expect(resource.username).to eq("domain\@user")
@@ -67,11 +66,11 @@ end
%w{username comment home shell password}.each do |attrib|
describe Chef::Resource::User, attrib do
- let(:resource) { Chef::Resource::User.new("adam") }
+ let(:resource) { Chef::Resource::User.new("notarealuser") }
it "allows a string" do
- resource.send(attrib, "adam")
- expect(resource.send(attrib)).to eql("adam")
+ resource.send(attrib, "something")
+ expect(resource.send(attrib)).to eql("something")
end
it "does not allow a hash" do
@@ -82,7 +81,7 @@ end
%w{uid gid}.each do |attrib|
describe Chef::Resource::User, attrib do
- let(:resource) { Chef::Resource::User.new("adam") }
+ let(:resource) { Chef::Resource::User.new("notarealuser") }
it "allows a string" do
resource.send(attrib, "100")