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
=== modified file 'charmhelpers/core/host.py'
--- charmhelpers/core/host.py 2013-05-30 11:02:07 +0000
+++ charmhelpers/core/host.py 2013-06-10 16:02:28 +0000
@@ -33,7 +33,7 @@
33 return False33 return False
3434
3535
36def adduser(username, password, shell='/bin/bash'):36def adduser(username, password=None, shell='/bin/bash', system_user=False):
37 """Add a user"""37 """Add a user"""
38 # TODO: generate a password if none is given38 # TODO: generate a password if none is given
39 try:39 try:
@@ -41,13 +41,16 @@
41 log('user {0} already exists!'.format(username))41 log('user {0} already exists!'.format(username))
42 except KeyError:42 except KeyError:
43 log('creating user {0}'.format(username))43 log('creating user {0}'.format(username))
44 cmd = [44 cmd = ['useradd']
45 'useradd',45 if system_user or password is None:
46 '--create-home',46 cmd.append('--system')
47 '--shell', shell,47 else:
48 '--password', password,48 cmd.extend([
49 username49 '--create-home',
50 ]50 '--shell', shell,
51 '--password', password,
52 ])
53 cmd.append(username)
51 subprocess.check_call(cmd)54 subprocess.check_call(cmd)
52 user_info = pwd.getpwnam(username)55 user_info = pwd.getpwnam(username)
53 return user_info56 return user_info
5457
=== modified file 'tests/core/test_host.py'
--- tests/core/test_host.py 2013-05-31 10:16:36 +0000
+++ tests/core/test_host.py 2013-06-10 16:02:28 +0000
@@ -183,6 +183,26 @@
183 ])183 ])
184 getpwnam.assert_called_with(username)184 getpwnam.assert_called_with(username)
185185
186 @patch('pwd.getpwnam')
187 @patch('subprocess.check_call')
188 @patch.object(host, 'log')
189 def test_adds_a_systemuser(self, log, check_call, getpwnam):
190 username = 'johndoe'
191 existing_user_pwnam = KeyError('user not found')
192 new_user_pwnam = 'some user pwnam'
193
194 getpwnam.side_effect = [existing_user_pwnam, new_user_pwnam]
195
196 result = host.adduser(username, system_user=True)
197
198 self.assertEqual(result, new_user_pwnam)
199 check_call.assert_called_with([
200 'useradd',
201 '--system',
202 username
203 ])
204 getpwnam.assert_called_with(username)
205
186 @patch('subprocess.check_call')206 @patch('subprocess.check_call')
187 @patch.object(host, 'log')207 @patch.object(host, 'log')
188 def test_adds_a_user_to_a_group(self, log, check_call):208 def test_adds_a_user_to_a_group(self, log, check_call):

Subscribers

People subscribed via source and target branches