Merge lp:~stub/charm-helpers/meminfo into lp:charm-helpers

Proposed by Stuart Bishop
Status: Merged
Merged at revision: 471
Proposed branch: lp:~stub/charm-helpers/meminfo
Merge into: lp:charm-helpers
Diff against target: 50 lines (+28/-0)
2 files modified
charmhelpers/core/host.py (+16/-0)
tests/core/test_host.py (+12/-0)
To merge this branch: bzr merge lp:~stub/charm-helpers/meminfo
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+265939@code.launchpad.net

Description of the change

Helper to get the total system RAM.

This helper keeps getting reimplemented, so lets stick it in charm-helpers and stop wasting time.

To post a comment you must log in.
lp:~stub/charm-helpers/meminfo updated
417. By Stuart Bishop

Test filename and mode too

Revision history for this message
Stuart Bishop (stub) wrote :

I'm going to land this if there are no objections.

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-07-15 16:36:29 +0000
3+++ charmhelpers/core/host.py 2015-07-28 04:07:46 +0000
4@@ -492,3 +492,19 @@
5
6 def lchownr(path, owner, group):
7 chownr(path, owner, group, follow_links=False)
8+
9+
10+def get_total_ram():
11+ '''The total amount of system RAM in bytes.
12+
13+ This is what is reported by the OS, and may be overcommitted when
14+ there are multiple containers hosted on the same machine.
15+ '''
16+ with open('/proc/meminfo', 'r') as f:
17+ for line in f.readlines():
18+ if line:
19+ key, value, unit = line.split()
20+ if key == 'MemTotal:':
21+ assert unit == 'kB', 'Unknown unit'
22+ return int(value) * 1024 # Classic, not KiB.
23+ raise NotImplementedError()
24
25=== modified file 'tests/core/test_host.py'
26--- tests/core/test_host.py 2015-07-15 15:46:23 +0000
27+++ tests/core/test_host.py 2015-07-28 04:07:46 +0000
28@@ -3,6 +3,7 @@
29 import subprocess
30 from tempfile import mkdtemp
31 from shutil import rmtree
32+from textwrap import dedent
33
34 import apt_pkg
35
36@@ -1005,3 +1006,14 @@
37 self.assertEqual(host.cmp_pkgrevno('python', '2.3'), 1)
38 self.assertEqual(host.cmp_pkgrevno('python', '2.4'), 0)
39 self.assertEqual(host.cmp_pkgrevno('python', '2.5'), -1)
40+
41+ def test_get_total_ram(self):
42+ raw = dedent('''\
43+ MemFree: 183868 kB
44+ MemTotal: 7096108 kB
45+ MemAvailable: 5645240 kB
46+ ''').strip()
47+ with patch_open() as (mock_open, mock_file):
48+ mock_file.readlines.return_value = raw.splitlines()
49+ self.assertEqual(host.get_total_ram(), 7266414592) # 7GB
50+ mock_open.assert_called_once_with('/proc/meminfo', 'r')

Subscribers

People subscribed via source and target branches