Merge lp:~billy-olsen/charm-helpers/1662591-add_owner into lp:charm-helpers

Proposed by Billy Olsen
Status: Merged
Merged at revision: 686
Proposed branch: lp:~billy-olsen/charm-helpers/1662591-add_owner
Merge into: lp:charm-helpers
Diff against target: 49 lines (+28/-0)
2 files modified
charmhelpers/core/host.py (+14/-0)
tests/core/test_host.py (+14/-0)
To merge this branch: bzr merge lp:~billy-olsen/charm-helpers/1662591-add_owner
Reviewer Review Type Date Requested Status
Jorge Niedbalski (community) Approve
Review via email: mp+316908@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Jorge Niedbalski (niedbalski) wrote :

LGTM, thanks for your contribution Billy!

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 2017-02-07 02:37:22 +0000
3+++ charmhelpers/core/host.py 2017-02-09 22:20:55 +0000
4@@ -849,6 +849,20 @@
5 chownr(path, owner, group, follow_links=False)
6
7
8+def owner(path):
9+ """Returns a tuple containing the username & groupname owning the path.
10+
11+ :param str path: the string path to retrieve the ownership
12+ :return tuple(str, str): A (username, groupname) tuple containing the
13+ name of the user and group owning the path.
14+ :raises OSError: if the specified path does not exist
15+ """
16+ stat = os.stat(path)
17+ username = pwd.getpwuid(stat.st_uid)[0]
18+ groupname = grp.getgrgid(stat.st_gid)[0]
19+ return username, groupname
20+
21+
22 def get_total_ram():
23 """The total amount of system RAM in bytes.
24
25
26=== modified file 'tests/core/test_host.py'
27--- tests/core/test_host.py 2017-02-07 20:44:55 +0000
28+++ tests/core/test_host.py 2017-02-09 22:20:55 +0000
29@@ -1704,6 +1704,20 @@
30 self.assertEqual(host.cmp_pkgrevno('python', '2.4'), 0)
31 self.assertEqual(host.cmp_pkgrevno('python', '2.5'), -1)
32
33+ @patch.object(host.os, 'stat')
34+ @patch.object(host.pwd, 'getpwuid')
35+ @patch.object(host.grp, 'getgrgid')
36+ @patch('posix.stat_result')
37+ def test_owner(self, stat_result_, getgrgid_, getpwuid_, stat_):
38+ getgrgid_.return_value = ['testgrp']
39+ getpwuid_.return_value = ['testuser']
40+ stat_.return_value = stat_result_()
41+
42+ user, group = host.owner('/some/path')
43+ stat_.assert_called_once_with('/some/path')
44+ self.assertEqual('testuser', user)
45+ self.assertEqual('testgrp', group)
46+
47 def test_get_total_ram(self):
48 raw = dedent('''\
49 MemFree: 183868 kB

Subscribers

People subscribed via source and target branches