1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../lib/puppettest'
require 'puppettest'
require 'etc'
class TestUser < Test::Unit::TestCase
include PuppetTest
p = Puppet::Type.type(:user).provide :fake, :parent => PuppetTest::FakeProvider do
@name = :fake
apimethods
def create
@ensure = :present
@resource.send(:properties).each do |property|
next if property.name == :ensure
property.sync
end
end
def delete
@ensure = :absent
@resource.send(:properties).each do |property|
send(property.name.to_s + "=", :absent)
end
end
def exists?
if defined? @ensure and @ensure == :present
true
else
false
end
end
end
FakeUserProvider = p
@@fakeproviders[:group] = p
def findshell(old = nil)
%w{/bin/sh /bin/bash /sbin/sh /bin/ksh /bin/zsh /bin/csh /bin/tcsh
/usr/bin/sh /usr/bin/bash /usr/bin/ksh /usr/bin/zsh /usr/bin/csh
/usr/bin/tcsh}.find { |shell|
if old
FileTest.exists?(shell) and shell != old
else
FileTest.exists?(shell)
end
}
end
def setup
super
Puppet::Type.type(:user).defaultprovider = FakeUserProvider
end
def teardown
Puppet::Type.type(:user).defaultprovider = nil
super
end
def mkuser(name)
user = nil
assert_nothing_raised {
user = Puppet::Type.type(:user).new(
:name => name,
:comment => "Puppet Testing User",
:gid => Puppet::Util::SUIDManager.gid,
:shell => findshell(),
:home => "/home/%s" % name
)
}
assert(user, "Did not create user")
return user
end
def attrtest_ensure(user)
old = user.provider.ensure
user[:ensure] = :absent
comp = mk_catalog("ensuretest", user)
assert_apply(user)
assert(!user.provider.exists?, "User is still present")
user[:ensure] = :present
assert_events([:user_created], comp)
assert(user.provider.exists?, "User is absent")
user[:ensure] = :absent
trans = assert_events([:user_removed], comp)
assert_rollback_events(trans, [:user_created], "user")
user[:ensure] = old
assert_apply(user)
end
def attrtest_comment(user)
user.retrieve
old = user.provider.comment
user[:comment] = "A different comment"
comp = mk_catalog("commenttest", user)
trans = assert_events([:user_changed], comp, "user")
assert_equal("A different comment", user.provider.comment,
"Comment was not changed")
assert_rollback_events(trans, [:user_changed], "user")
assert_equal(old, user.provider.comment,
"Comment was not reverted")
end
def attrtest_home(user)
obj = nil
comp = mk_catalog("hometest", user)
old = user.provider.home
user[:home] = old
trans = assert_events([], comp, "user")
user[:home] = "/tmp"
trans = assert_events([:user_changed], comp, "user")
assert_equal("/tmp", user.provider.home, "Home was not changed")
assert_rollback_events(trans, [:user_changed], "user")
assert_equal(old, user.provider.home, "Home was not reverted")
end
def attrtest_shell(user)
old = user.provider.shell
comp = mk_catalog("shelltest", user)
user[:shell] = old
trans = assert_events([], comp, "user")
newshell = findshell(old)
unless newshell
$stderr.puts "Cannot find alternate shell; skipping shell test"
return
end
user[:shell] = newshell
trans = assert_events([:user_changed], comp, "user")
user.retrieve
assert_equal(newshell, user.provider.shell,
"Shell was not changed")
assert_rollback_events(trans, [:user_changed], "user")
user.retrieve
assert_equal(old, user.provider.shell, "Shell was not reverted")
end
def attrtest_uid(user)
obj = nil
comp = mk_catalog("uidtest", user)
user.provider.uid = 1
old = 1
newuid = 1
while true
newuid += 1
if newuid - old > 1000
$stderr.puts "Could not find extra test UID"
return
end
begin
newuser = Etc.getpwuid(newuid)
rescue ArgumentError => detail
break
end
end
assert_nothing_raised("Failed to change user id") {
user[:uid] = newuid
}
trans = assert_events([:user_changed], comp, "user")
assert_equal(newuid, user.provider.uid, "UID was not changed")
assert_rollback_events(trans, [:user_changed], "user")
assert_equal(old, user.provider.uid, "UID was not reverted")
end
def attrtest_groups(user)
Etc.setgrent
max = 0
while group = Etc.getgrent
if group.gid > max and group.gid < 5000
max = group.gid
end
end
groups = []
main = []
extra = []
5.times do |i|
i += 1
name = "pptstgr%s" % i
groups << name
if i < 3
main << name
else
extra << name
end
end
assert(user[:membership] == :minimum, "Membership did not default correctly")
assert_nothing_raised {
user.retrieve
}
# Now add some of them to our user
assert_nothing_raised {
user[:groups] = extra
}
assert_nothing_raised {
user.retrieve
}
assert_instance_of(String, user.property(:groups).should)
# Some tests to verify that groups work correctly startig from nothing
# Remove our user
user[:ensure] = :absent
assert_apply(user)
assert_nothing_raised do
user.retrieve
end
# And add it again
user[:ensure] = :present
assert_apply(user)
# Make sure that the groups are a string, not an array
assert(user.provider.groups.is_a?(String),
"Incorrectly passed an array to groups")
currentvalue = user.retrieve
assert(currentvalue[user.property(:groups)], "Did not retrieve group list")
list = currentvalue[user.property(:groups)]
assert_equal(extra.sort, list.sort, "Group list is not equal")
# Now set to our main list of groups
assert_nothing_raised {
user[:groups] = main
}
assert_equal((main + extra).sort, user.property(:groups).should.split(",").sort)
currentvalue = nil
assert_nothing_raised {
currentvalue = user.retrieve
}
assert(!user.insync?(currentvalue), "User is incorrectly in sync")
assert_apply(user)
assert_nothing_raised {
currentvalue = user.retrieve
}
# We're not managing inclusively, so it should keep the old group
# memberships and add the new ones
list = currentvalue[user.property(:groups)]
assert_equal((main + extra).sort, list.sort, "Group list is not equal")
assert_nothing_raised {
user[:membership] = :inclusive
}
assert_nothing_raised {
currentvalue = user.retrieve
}
assert(!user.insync?(currentvalue), "User is incorrectly in sync")
assert_events([:user_changed], user)
assert_nothing_raised {
currentvalue = user.retrieve
}
list = currentvalue[user.property(:groups)]
assert_equal(main.sort, list.sort, "Group list is not equal")
# Set the values a bit differently.
user.property(:groups).should = list.sort { |a,b| b <=> a }
assert(user.property(:groups).insync?(list.sort), "Groups property did not sort groups")
user.delete(:groups)
end
def test_groups_list_must_not_contain_commas
assert_raise(Puppet::Error) do
Puppet::Type.type(:user).new :name => "luke", :groups => "root,adm"
end
end
def test_autorequire
file = tempfile()
comp = nil
user = nil
group =nil
home = nil
ogroup = nil
assert_nothing_raised {
user = Puppet::Type.type(:user).new(
:name => "pptestu",
:home => file,
:gid => "pptestg",
:groups => "yayness"
)
home = Puppet::Type.type(:file).new(
:path => file,
:owner => "pptestu",
:ensure => "directory"
)
group = Puppet::Type.type(:group).new(
:name => "pptestg"
)
ogroup = Puppet::Type.type(:group).new(
:name => "yayness"
)
comp = mk_catalog(user, group, home, ogroup)
}
rels = nil
assert_nothing_raised() { rels = user.autorequire }
assert(rels.detect { |r| r.source == group }, "User did not require group")
assert(rels.detect { |r| r.source == ogroup }, "User did not require other groups")
assert_nothing_raised() { rels = home.autorequire }
assert(rels.detect { |r| r.source == user }, "Homedir did not require user")
end
def test_simpleuser
name = "pptest"
user = mkuser(name)
comp = mk_catalog("usercomp", user)
trans = assert_events([:user_created], comp, "user")
assert_equal(user.should(:comment), user.provider.comment,
"Comment was not set correctly")
assert_rollback_events(trans, [:user_removed], "user")
assert(! user.provider.exists?, "User did not get deleted")
end
def test_allusermodelproperties
user = nil
name = "pptest"
user = mkuser(name)
assert(! user.provider.exists?, "User %s is present" % name)
comp = mk_catalog("usercomp", user)
trans = assert_events([:user_created], comp, "user")
user.retrieve
assert_equal("Puppet Testing User", user.provider.comment,
"Comment was not set")
tests = Puppet::Type.type(:user).validproperties
tests.each { |test|
if self.respond_to?("attrtest_%s" % test)
self.send("attrtest_%s" % test, user)
else
Puppet.err "Not testing attr %s of user" % test
end
}
user[:ensure] = :absent
assert_apply(user)
end
# Testing #455
def test_autorequire_with_no_group_should
user = Puppet::Type.type(:user).new(:name => "yaytest", :check => :all)
catalog = mk_catalog(user)
assert_nothing_raised do
user.autorequire
end
user[:ensure] = :absent
assert_nothing_raised do
user.evaluate
end
assert(user.send(:property, :groups).insync?(nil),
"Groups state considered out of sync with no :should value")
end
# Make sure the 'managehome' param can only be set when the provider
# has that feature. Uses a patch from #432.
def test_managehome
user = Puppet::Type.type(:user).new(:name => "yaytest", :check => :all)
prov = user.provider
home = false
prov.class.meta_def(:manages_homedir?) { home }
assert_nothing_raised("failed on false managehome") do
user[:managehome] = false
end
assert_raise(Puppet::Error, "did not fail when managehome? is false") do
user[:managehome] = true
end
home = true
assert(prov.class.manages_homedir?, "provider did not enable homedir")
assert_nothing_raised("failed when managehome is true") do
user[:managehome] = true
end
end
end
|