Skip to content

Commit 0c99f89

Browse files
committed
Adding some user specs
1 parent 16c9364 commit 0c99f89

2 files changed

Lines changed: 346 additions & 1 deletion

File tree

chef/lib/chef/provider/user.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class User < Chef::Provider
2626

2727
include Chef::Mixin::Command
2828

29+
attr_accessor :user_exists, :locked
30+
2931
def initialize(node, new_resource)
3032
super(node, new_resource)
3133
@user_exists = true
@@ -65,7 +67,12 @@ def load_current_resource
6567

6668
@current_resource
6769
end
68-
70+
71+
# Check to see if the user needs any changes
72+
#
73+
# === Returns
74+
# <true>:: If a change is required
75+
# <false>:: If the users are identical
6976
def compare_user
7077
change_required = false
7178
change_required = true if @new_resource.uid != @current_resource.uid
Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
#
2+
# Author:: Adam Jacob (<adam@hjksolutions.com>)
3+
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
20+
21+
describe Chef::Provider::User, "initialize" do
22+
before(:each) do
23+
@node = mock("Chef::Node", :null_object => true)
24+
@new_resource = mock("Chef::Resource::User",
25+
:null_object => true,
26+
:username => "adam",
27+
:comment => "Adam Jacob",
28+
:uid => 1000,
29+
:gid => 1000,
30+
:home => "/home/adam",
31+
:shell => "/usr/bin/zsh",
32+
:password => nil,
33+
:updated => nil
34+
)
35+
36+
@provider = Chef::Provider::User.new(@node, @new_resource)
37+
end
38+
39+
it "should return a Chef::Provider::User" do
40+
@provider.should be_a_kind_of(Chef::Provider::User)
41+
end
42+
43+
it "should assume the user exists by default" do
44+
@provider.user_exists.should eql(true)
45+
end
46+
47+
it "should assume we do not know the locked state" do
48+
@provider.locked.should eql(nil)
49+
end
50+
end
51+
52+
describe Chef::Provider::User, "load_current_resource" do
53+
before(:each) do
54+
@node = mock("Chef::Node", :null_object => true)
55+
@new_resource = mock("Chef::Resource::User",
56+
:null_object => true,
57+
:username => "adam",
58+
:comment => "Adam Jacob",
59+
:uid => 1000,
60+
:gid => 1000,
61+
:home => "/home/adam",
62+
:shell => "/usr/bin/zsh",
63+
:password => nil,
64+
:updated => nil
65+
)
66+
@current_resource = mock("Chef::Resource::User",
67+
:null_object => true,
68+
:username => "adam",
69+
:comment => "Adam Jacob",
70+
:uid => 1000,
71+
:gid => 1000,
72+
:home => "/home/adam",
73+
:shell => "/usr/bin/zsh",
74+
:password => nil,
75+
:updated => nil
76+
)
77+
Chef::Resource::User.stub!(:new).and_return(@current_resource)
78+
@pw_user = mock("Etc::User",
79+
:null_object => true,
80+
:uid => "adam",
81+
:gid => 1000,
82+
:uid => 1000,
83+
:comment => "Adam Jacob",
84+
:home => "/home/adam",
85+
:shell => "/usr/bin/zsh"
86+
)
87+
Etc.stub!(:getpwnam).and_return(@pw_user)
88+
89+
@provider = Chef::Provider::User.new(@node, @new_resource)
90+
end
91+
92+
it "should create a current resource with the same name as the new resource" do
93+
Chef::Resource::User.should_receive(:new).with(@new_resource.name).and_return(@current_resource)
94+
@provider.load_current_resource
95+
end
96+
97+
it "should set the username of the current resource to the username of the new resource" do
98+
@current_resource.should_receive(:username).with(@new_resource.username)
99+
@provider.load_current_resource
100+
end
101+
102+
it "should look up the user in /etc/passwd with getpwnam" do
103+
Etc.should_receive(:getpwnam).with(@new_resource.username).and_return(@pw_user)
104+
@provider.load_current_resource
105+
end
106+
107+
it "should set user_exists to false if the user is not found with getpwnam" do
108+
Etc.should_receive(:getpwnam).and_raise(ArgumentError)
109+
@provider.load_current_resource
110+
@provider.user_exists.should eql(false)
111+
end
112+
113+
# The mapping between the Chef::Resource::User and Getpwnam struct
114+
user_attrib_map = {
115+
:uid => :uid,
116+
:gid => :gid,
117+
:comment => :gecos,
118+
:home => :dir,
119+
:shell => :shell
120+
}
121+
user_attrib_map.each do |user_attrib, getpwnam_attrib|
122+
it "should set the current resources #{user_attrib} based on getpwnam #{getpwnam_attrib}" do
123+
@current_resource.should_receive(user_attrib).with(@pw_user.send(getpwnam_attrib))
124+
@provider.load_current_resource
125+
end
126+
end
127+
128+
it "should return the current resource" do
129+
@provider.load_current_resource.should eql(@current_resource)
130+
end
131+
end
132+
133+
describe Chef::Provider::User, "compare_user" do
134+
before(:each) do
135+
@node = mock("Chef::Node", :null_object => true)
136+
@new_resource = mock("Chef::Resource::User",
137+
:null_object => true,
138+
:username => "adam",
139+
:comment => "Adam Jacob",
140+
:uid => 1000,
141+
:gid => 1000,
142+
:home => "/home/adam",
143+
:shell => "/usr/bin/zsh",
144+
:password => nil,
145+
:updated => nil
146+
)
147+
@current_resource = mock("Chef::Resource::User",
148+
:null_object => true,
149+
:username => "adam",
150+
:comment => "Adam Jacob",
151+
:uid => 1000,
152+
:gid => 1000,
153+
:home => "/home/adam",
154+
:shell => "/usr/bin/zsh",
155+
:password => nil,
156+
:updated => nil
157+
)
158+
@provider = Chef::Provider::User.new(@node, @new_resource)
159+
@provider.current_resource = @current_resource
160+
end
161+
162+
%w{uid gid comment home shell password}.each do |attribute|
163+
it "should return true if #{attribute} doesn't match" do
164+
@new_resource.should_receive(attribute).and_return(true)
165+
@current_resource.should_receive(attribute).and_return(false)
166+
@provider.compare_user.should eql(true)
167+
end
168+
end
169+
170+
it "should return false if the objects are identical" do
171+
@provider.compare_user.should eql(false)
172+
end
173+
174+
end
175+
176+
describe Chef::Provider::User, "action_create" do
177+
before(:each) do
178+
@node = mock("Chef::Node", :null_object => true)
179+
@new_resource = mock("Chef::Resource::User",
180+
:null_object => true,
181+
:username => "adam",
182+
:comment => "Adam Jacob",
183+
:uid => 1000,
184+
:gid => 1000,
185+
:home => "/home/adam",
186+
:shell => "/usr/bin/zsh",
187+
:password => nil,
188+
:updated => nil
189+
)
190+
@current_resource = mock("Chef::Resource::User",
191+
:null_object => true,
192+
:username => "adam",
193+
:comment => "Adam Jacob",
194+
:uid => 1000,
195+
:gid => 1000,
196+
:home => "/home/adam",
197+
:shell => "/usr/bin/zsh",
198+
:password => nil,
199+
:updated => nil
200+
)
201+
@provider = Chef::Provider::User.new(@node, @new_resource)
202+
@provider.current_resource = @current_resource
203+
@provider.user_exists = false
204+
@provider.stub!(:create_user).and_return(true)
205+
@provider.stub!(:manage_user).and_return(true)
206+
end
207+
208+
it "should call create_user if the user does not exist" do
209+
@provider.should_receive(:create_user).and_return(true)
210+
@provider.action_create
211+
end
212+
213+
it "should set the the new_resources updated flag when it creates the user" do
214+
@new_resource.should_receive(:updated=).with(true).and_return(true)
215+
@provider.action_create
216+
end
217+
218+
it "should check to see if the user has mismatched attributes if the user exists" do
219+
@provider.user_exists = true
220+
@provider.should_receive(:compare_user).and_return(false)
221+
@provider.action_create
222+
end
223+
224+
it "should call manage_user if the user exists and has mismatched attributes" do
225+
@provider.user_exists = true
226+
@provider.stub!(:compare_user).and_return(true)
227+
@provider.should_receive(:manage_user).and_return(true)
228+
@provider.action_create
229+
end
230+
231+
it "should set the the new_resources updated flag when it creates the user if we call manage_user" do
232+
@provider.user_exists = true
233+
@provider.stub!(:compare_user).and_return(true)
234+
@provider.stub!(:manage_user).and_return(true)
235+
@new_resource.should_receive(:updated=).with(true).and_return(true)
236+
@provider.action_create
237+
end
238+
239+
end
240+
241+
describe Chef::Provider::User, "action_remove" do
242+
before(:each) do
243+
@node = mock("Chef::Node", :null_object => true)
244+
@new_resource = mock("Chef::Resource::User",
245+
:null_object => true,
246+
:username => "adam",
247+
:comment => "Adam Jacob",
248+
:uid => 1000,
249+
:gid => 1000,
250+
:home => "/home/adam",
251+
:shell => "/usr/bin/zsh",
252+
:password => nil,
253+
:updated => nil
254+
)
255+
@current_resource = mock("Chef::Resource::User",
256+
:null_object => true,
257+
:username => "adam",
258+
:comment => "Adam Jacob",
259+
:uid => 1000,
260+
:gid => 1000,
261+
:home => "/home/adam",
262+
:shell => "/usr/bin/zsh",
263+
:password => nil,
264+
:updated => nil
265+
)
266+
@provider = Chef::Provider::User.new(@node, @new_resource)
267+
@provider.current_resource = @current_resource
268+
@provider.user_exists = false
269+
@provider.stub!(:remove_user).and_return(true)
270+
end
271+
272+
it "should not call remove_user if the user does not exist" do
273+
@provider.should_not_receive(:remove_user)
274+
@provider.action_remove
275+
end
276+
277+
it "should call remove_user if the user exists" do
278+
@provider.user_exists = true
279+
@provider.should_receive(:remove_user)
280+
@provider.action_remove
281+
end
282+
283+
it "should set the new_resources updated flag to true if the user is removed" do
284+
@provider.user_exists = true
285+
@new_resource.should_receive(:updated=).with(true).and_return(true)
286+
@provider.action_remove
287+
end
288+
end
289+
290+
describe Chef::Provider::User, "action_manage" do
291+
before(:each) do
292+
@node = mock("Chef::Node", :null_object => true)
293+
@new_resource = mock("Chef::Resource::User",
294+
:null_object => true,
295+
:username => "adam",
296+
:comment => "Adam Jacob",
297+
:uid => 1000,
298+
:gid => 1000,
299+
:home => "/home/adam",
300+
:shell => "/usr/bin/zsh",
301+
:password => nil,
302+
:updated => nil
303+
)
304+
@current_resource = mock("Chef::Resource::User",
305+
:null_object => true,
306+
:username => "adam",
307+
:comment => "Adam Jacob",
308+
:uid => 1000,
309+
:gid => 1000,
310+
:home => "/home/adam",
311+
:shell => "/usr/bin/zsh",
312+
:password => nil,
313+
:updated => nil
314+
)
315+
@provider = Chef::Provider::User.new(@node, @new_resource)
316+
@provider.current_resource = @current_resource
317+
@provider.user_exists = false
318+
@provider.stub!(:remove_user).and_return(true)
319+
end
320+
321+
it "should not call remove_user if the user does not exist" do
322+
@provider.should_not_receive(:remove_user)
323+
@provider.action_remove
324+
end
325+
326+
it "should call remove_user if the user exists" do
327+
@provider.user_exists = true
328+
@provider.should_receive(:remove_user)
329+
@provider.action_remove
330+
end
331+
332+
it "should set the new_resources updated flag to true if the user is removed" do
333+
@provider.user_exists = true
334+
@new_resource.should_receive(:updated=).with(true).and_return(true)
335+
@provider.action_remove
336+
end
337+
end
338+

0 commit comments

Comments
 (0)