Merge lp:~gnuoy/charm-helpers/user-exists-fstab-mount into lp:charm-helpers

Proposed by Liam Young
Status: Merged
Merged at revision: 431
Proposed branch: lp:~gnuoy/charm-helpers/user-exists-fstab-mount
Merge into: lp:charm-helpers
Diff against target: 80 lines (+45/-0)
2 files modified
charmhelpers/core/host.py (+21/-0)
tests/core/test_host.py (+24/-0)
To merge this branch: bzr merge lp:~gnuoy/charm-helpers/user-exists-fstab-mount
Reviewer Review Type Date Requested Status
Chris Glass (community) Approve
Review via email: mp+268212@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Chris Glass (tribaal) wrote :

Looks good! +1

review: Approve

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 2015-08-04 15:01:57 +0000
3+++ charmhelpers/core/host.py 2015-08-17 11:40:14 +0000
4@@ -148,6 +148,16 @@
5 return user_info
6
7
8+def user_exists(username):
9+ """Check if a user exists"""
10+ try:
11+ pwd.getpwnam(username)
12+ user_exists = True
13+ except KeyError:
14+ user_exists = False
15+ return user_exists
16+
17+
18 def add_group(group_name, system_group=False):
19 """Add a group to the system"""
20 try:
21@@ -280,6 +290,17 @@
22 return system_mounts
23
24
25+def fstab_mount(mountpoint):
26+ """Mount filesystem using fstab"""
27+ cmd_args = ['mount', mountpoint]
28+ try:
29+ subprocess.check_output(cmd_args)
30+ except subprocess.CalledProcessError as e:
31+ log('Error unmounting {}\n{}'.format(mountpoint, e.output))
32+ return False
33+ return True
34+
35+
36 def file_hash(path, hash_type='md5'):
37 """
38 Generate a hash checksum of the contents of 'path' or None if not found.
39
40=== modified file 'tests/core/test_host.py'
41--- tests/core/test_host.py 2015-08-04 15:01:57 +0000
42+++ tests/core/test_host.py 2015-08-17 11:40:14 +0000
43@@ -303,6 +303,16 @@
44 ])
45 getpwnam.assert_called_with(username)
46
47+ @patch('pwd.getpwnam')
48+ def test_user_exists_true(self, getpwnam):
49+ getpwnam.side_effect = 'pw info'
50+ self.assertTrue(host.user_exists('bob'))
51+
52+ @patch('pwd.getpwnam')
53+ def test_user_exists_false(self, getpwnam):
54+ getpwnam.side_effect = KeyError('user not found')
55+ self.assertFalse(host.user_exists('bob'))
56+
57 @patch('subprocess.check_call')
58 @patch.object(host, 'log')
59 def test_adds_a_user_to_a_group(self, log, check_call):
60@@ -666,6 +676,20 @@
61 '/etc/missing.conf': None
62 }
63
64+ @patch('subprocess.check_output')
65+ @patch.object(host, 'log')
66+ def test_fstab_mount(self, log, check_output):
67+ self.assertTrue(host.fstab_mount('/mnt/mymntpnt'))
68+ check_output.assert_called_with(['mount', '/mnt/mymntpnt'])
69+
70+ @patch('subprocess.check_output')
71+ @patch.object(host, 'log')
72+ def test_fstab_mount_fail(self, log, check_output):
73+ error = subprocess.CalledProcessError(123, 'mount it', 'Oops...')
74+ check_output.side_effect = error
75+ self.assertFalse(host.fstab_mount('/mnt/mymntpnt'))
76+ check_output.assert_called_with(['mount', '/mnt/mymntpnt'])
77+
78 @patch('hashlib.md5')
79 @patch('os.path.exists')
80 def test_file_hash_exists(self, exists, md5):

Subscribers

People subscribed via source and target branches