Merge lp:~gnuoy/charm-helpers/minor-additions into lp:charm-helpers

Proposed by Liam Young
Status: Merged
Merged at revision: 22
Proposed branch: lp:~gnuoy/charm-helpers/minor-additions
Merge into: lp:charm-helpers
Diff against target: 67 lines (+31/-8)
2 files modified
charmhelpers/core/host.py (+11/-8)
tests/core/test_host.py (+20/-0)
To merge this branch: bzr merge lp:~gnuoy/charm-helpers/minor-additions
Reviewer Review Type Date Requested Status
Charm Helper Maintainers Pending
Review via email: mp+168117@code.launchpad.net

Description of the change

Added a system_user option to add_user and a trivial apt_update function

To post a comment you must log in.
23. By Liam Young

Add tests for apt_update and creating a system user

24. By Liam Young

Remove apt_update code as gandelman-a already has a super/faster/stronger mp for this

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/host.py'
2--- charmhelpers/core/host.py 2013-05-30 11:02:07 +0000
3+++ charmhelpers/core/host.py 2013-06-10 16:02:28 +0000
4@@ -33,7 +33,7 @@
5 return False
6
7
8-def adduser(username, password, shell='/bin/bash'):
9+def adduser(username, password=None, shell='/bin/bash', system_user=False):
10 """Add a user"""
11 # TODO: generate a password if none is given
12 try:
13@@ -41,13 +41,16 @@
14 log('user {0} already exists!'.format(username))
15 except KeyError:
16 log('creating user {0}'.format(username))
17- cmd = [
18- 'useradd',
19- '--create-home',
20- '--shell', shell,
21- '--password', password,
22- username
23- ]
24+ cmd = ['useradd']
25+ if system_user or password is None:
26+ cmd.append('--system')
27+ else:
28+ cmd.extend([
29+ '--create-home',
30+ '--shell', shell,
31+ '--password', password,
32+ ])
33+ cmd.append(username)
34 subprocess.check_call(cmd)
35 user_info = pwd.getpwnam(username)
36 return user_info
37
38=== modified file 'tests/core/test_host.py'
39--- tests/core/test_host.py 2013-05-31 10:16:36 +0000
40+++ tests/core/test_host.py 2013-06-10 16:02:28 +0000
41@@ -183,6 +183,26 @@
42 ])
43 getpwnam.assert_called_with(username)
44
45+ @patch('pwd.getpwnam')
46+ @patch('subprocess.check_call')
47+ @patch.object(host, 'log')
48+ def test_adds_a_systemuser(self, log, check_call, getpwnam):
49+ username = 'johndoe'
50+ existing_user_pwnam = KeyError('user not found')
51+ new_user_pwnam = 'some user pwnam'
52+
53+ getpwnam.side_effect = [existing_user_pwnam, new_user_pwnam]
54+
55+ result = host.adduser(username, system_user=True)
56+
57+ self.assertEqual(result, new_user_pwnam)
58+ check_call.assert_called_with([
59+ 'useradd',
60+ '--system',
61+ username
62+ ])
63+ getpwnam.assert_called_with(username)
64+
65 @patch('subprocess.check_call')
66 @patch.object(host, 'log')
67 def test_adds_a_user_to_a_group(self, log, check_call):

Subscribers

People subscribed via source and target branches