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 ::Useradd , "set_options" 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 => "abracadabra" ,
33+ :updated => nil
34+ )
35+ @current_resource = mock ( "Chef::Resource::User" ,
36+ :null_object => true ,
37+ :username => "adam" ,
38+ :comment => "Adam Jacob" ,
39+ :uid => 1000 ,
40+ :gid => 1000 ,
41+ :home => "/home/adam" ,
42+ :shell => "/usr/bin/zsh" ,
43+ :password => "abracadabra" ,
44+ :updated => nil
45+ )
46+ @provider = Chef ::Provider ::User ::Useradd . new ( @node , @new_resource )
47+ @provider . current_resource = @current_resource
48+ end
49+
50+ field_list = {
51+ 'comment' => "-c" ,
52+ 'home' => "-d" ,
53+ 'gid' => "-g" ,
54+ 'uid' => "-u" ,
55+ 'shell' => "-s" ,
56+ 'password' => "-p"
57+ }
58+ field_list . each do |attribute , option |
59+ it "should check for differences in #{ attribute } between the new and current resources" do
60+ @current_resource . should_receive ( attribute )
61+ @new_resource . should_receive ( attribute )
62+ @provider . set_options
63+ end
64+
65+ it "should set the option for #{ attribute } if the new resources #{ attribute } is not null" do
66+ @new_resource . stub! ( attribute ) . and_return ( "hola" )
67+ @provider . set_options . should eql ( " #{ option } '#{ @new_resource . send ( attribute ) } ' -m #{ @new_resource . username } " )
68+ end
69+
70+ it "should set the option for #{ attribute } if the new resources #{ attribute } is not null, without homedir management" do
71+ @new_resource . stub! ( :supports ) . and_return ( { :manage_home => false } )
72+ @new_resource . stub! ( attribute ) . and_return ( "hola" )
73+ @provider . set_options . should eql ( " #{ option } '#{ @new_resource . send ( attribute ) } ' #{ @new_resource . username } " )
74+ end
75+ end
76+
77+ it "should combine all the possible options" do
78+ match_string = ""
79+ field_list . sort { |a , b | a [ 0 ] <=> b [ 0 ] } . each do |attribute , option |
80+ @new_resource . stub! ( attribute ) . and_return ( "hola" )
81+ match_string << " #{ option } 'hola'"
82+ end
83+ match_string << " -m adam"
84+ @provider . set_options . should eql ( match_string )
85+ end
86+
87+ it "should use -m unless the operating system is a redhat descendent" do
88+ @node . stub! ( :[] ) . with ( :operatingsystem ) . and_return ( "Debian" )
89+ @provider . set_options . should eql ( " -m adam" )
90+ end
91+
92+ it "should use -M if the operating system is a redhat descendent" do
93+ @node . stub! ( :[] ) . with ( :operatingsystem ) . and_return ( "RedHat" )
94+ @provider . set_options . should eql ( " -M adam" )
95+ end
96+ end
97+
98+ describe Chef ::Provider ::User ::Useradd , "create_user" do
99+ before ( :each ) do
100+ @node = mock ( "Chef::Node" , :null_object => true )
101+ @new_resource = mock ( "Chef::Resource::User" , :null_object => true )
102+ @provider = Chef ::Provider ::User ::Useradd . new ( @node , @new_resource )
103+ @provider . stub! ( :set_options ) . and_return ( " monkey" )
104+ end
105+
106+ it "should run useradd with the return of set_options" do
107+ @provider . should_receive ( :run_command ) . with ( { :command => "useradd monkey" } ) . and_return ( true )
108+ @provider . create_user
109+ end
110+ end
111+
112+ describe Chef ::Provider ::User ::Useradd , "manage_user" do
113+ before ( :each ) do
114+ @node = mock ( "Chef::Node" , :null_object => true )
115+ @new_resource = mock ( "Chef::Resource::User" , :null_object => true )
116+ @provider = Chef ::Provider ::User ::Useradd . new ( @node , @new_resource )
117+ @provider . stub! ( :set_options ) . and_return ( " monkey" )
118+ end
119+
120+ it "should run usermod with the return of set_options" do
121+ @provider . should_receive ( :run_command ) . with ( { :command => "usermod monkey" } ) . and_return ( true )
122+ @provider . manage_user
123+ end
124+ end
125+
126+ describe Chef ::Provider ::User ::Useradd , "remove_user" do
127+ before ( :each ) do
128+ @node = mock ( "Chef::Node" , :null_object => true )
129+ @new_resource = mock ( "Chef::Resource::User" ,
130+ :null_object => true ,
131+ :username => "adam" ,
132+ :supports => { :manage_home => false }
133+ )
134+ @provider = Chef ::Provider ::User ::Useradd . new ( @node , @new_resource )
135+ end
136+
137+ it "should run userdel with the new resources user name" do
138+ @provider . should_receive ( :run_command ) . with ( { :command => "userdel #{ @new_resource . username } " } ) . and_return ( true )
139+ @provider . remove_user
140+ end
141+
142+ it "should run userdel with the new resources user name and -r if manage_home is true" do
143+ @new_resource . stub! ( :supports ) . and_return ( { :manage_home => true } )
144+ @provider . should_receive ( :run_command ) . with ( { :command => "userdel -r #{ @new_resource . username } " } ) . and_return ( true )
145+ @provider . remove_user
146+ end
147+ end
148+
149+ describe Chef ::Provider ::User ::Useradd , "lock_user" do
150+ before ( :each ) do
151+ @node = mock ( "Chef::Node" , :null_object => true )
152+ @new_resource = mock ( "Chef::Resource::User" ,
153+ :null_object => true ,
154+ :username => "adam"
155+ )
156+ @provider = Chef ::Provider ::User ::Useradd . new ( @node , @new_resource )
157+ end
158+
159+ it "should run usermod -L with the new resources username" do
160+ @provider . should_receive ( :run_command ) . with ( { :command => "usermod -L #{ @new_resource . username } " } )
161+ @provider . lock_user
162+ end
163+ end
164+
165+ describe Chef ::Provider ::User ::Useradd , "unlock_user" do
166+ before ( :each ) do
167+ @node = mock ( "Chef::Node" , :null_object => true )
168+ @new_resource = mock ( "Chef::Resource::User" ,
169+ :null_object => true ,
170+ :username => "adam"
171+ )
172+ @provider = Chef ::Provider ::User ::Useradd . new ( @node , @new_resource )
173+ end
174+
175+ it "should run usermod -L with the new resources username" do
176+ @provider . should_receive ( :run_command ) . with ( { :command => "usermod -U #{ @new_resource . username } " } )
177+ @provider . unlock_user
178+ end
179+ end
0 commit comments