Merge lp:~corey.bryant/charm-helpers/adduser-home-dir into lp:charm-helpers

Proposed by Corey Bryant
Status: Merged
Merged at revision: 601
Proposed branch: lp:~corey.bryant/charm-helpers/adduser-home-dir
Merge into: lp:charm-helpers
Diff against target: 63 lines (+27/-1)
2 files modified
charmhelpers/core/host.py (+4/-1)
tests/core/test_host.py (+23/-0)
To merge this branch: bzr merge lp:~corey.bryant/charm-helpers/adduser-home-dir
Reviewer Review Type Date Requested Status
James Page Needs Fixing
Review via email: mp+299436@code.launchpad.net
To post a comment you must log in.
Revision history for this message
James Page (james-page) wrote :

Change to module looks good, but please add a unit test to cover this as well.

review: Needs Fixing
601. By Corey Bryant

Add test for adduser with --home

Revision history for this message
Corey Bryant (corey.bryant) wrote :

Thanks for the review. I've added a unit test for adduser with --home.

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 2016-07-06 14:41:05 +0000
3+++ charmhelpers/core/host.py 2016-07-08 12:59:26 +0000
4@@ -174,7 +174,7 @@
5
6
7 def adduser(username, password=None, shell='/bin/bash', system_user=False,
8- primary_group=None, secondary_groups=None, uid=None):
9+ primary_group=None, secondary_groups=None, uid=None, home_dir=None):
10 """Add a user to the system.
11
12 Will log but otherwise succeed if the user already exists.
13@@ -186,6 +186,7 @@
14 :param str primary_group: Primary group for user; defaults to username
15 :param list secondary_groups: Optional list of additional groups
16 :param int uid: UID for user being created
17+ :param str home_dir: Home directory for user
18
19 :returns: The password database entry struct, as returned by `pwd.getpwnam`
20 """
21@@ -200,6 +201,8 @@
22 cmd = ['useradd']
23 if uid:
24 cmd.extend(['--uid', str(uid)])
25+ if home_dir:
26+ cmd.extend(['--home', str(home_dir)])
27 if system_user or password is None:
28 cmd.append('--system')
29 else:
30
31=== modified file 'tests/core/test_host.py'
32--- tests/core/test_host.py 2016-05-31 20:23:32 +0000
33+++ tests/core/test_host.py 2016-07-08 12:59:26 +0000
34@@ -642,6 +642,29 @@
35 getpwnam.assert_called_with(username)
36
37 @patch('pwd.getpwnam')
38+ @patch('subprocess.check_call')
39+ @patch.object(host, 'log')
40+ def test_adds_a_systemuser_with_home_dir(self, log, check_call, getpwnam):
41+ username = 'johndoe'
42+ existing_user_pwnam = KeyError('user not found')
43+ new_user_pwnam = 'some user pwnam'
44+
45+ getpwnam.side_effect = [existing_user_pwnam, new_user_pwnam]
46+
47+ result = host.adduser(username, system_user=True,
48+ home_dir='/var/lib/johndoe')
49+
50+ self.assertEqual(result, new_user_pwnam)
51+ check_call.assert_called_with([
52+ 'useradd',
53+ '--home',
54+ '/var/lib/johndoe',
55+ '--system',
56+ username
57+ ])
58+ getpwnam.assert_called_with(username)
59+
60+ @patch('pwd.getpwnam')
61 @patch('pwd.getpwuid')
62 @patch('grp.getgrnam')
63 @patch('subprocess.check_call')

Subscribers

People subscribed via source and target branches