Merge lp:~zulcss/charm-helpers/is_container into lp:charm-helpers

Proposed by Chuck Short
Status: Merged
Merged at revision: 656
Proposed branch: lp:~zulcss/charm-helpers/is_container
Merge into: lp:charm-helpers
Diff against target: 39 lines (+24/-0)
2 files modified
charmhelpers/core/host.py (+8/-0)
tests/core/test_host.py (+16/-0)
To merge this branch: bzr merge lp:~zulcss/charm-helpers/is_container
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+309112@code.launchpad.net

Description of the change

Determine whether we are running inside a container or not.

To post a comment you must log in.

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-08-11 15:47:44 +0000
3+++ charmhelpers/core/host.py 2016-10-24 13:03:43 +0000
4@@ -732,3 +732,11 @@
5 assert unit == 'kB', 'Unknown unit'
6 return int(value) * 1024 # Classic, not KiB.
7 raise NotImplementedError()
8+
9+def is_container():
10+ """Return True if system is running inside a container"""
11+ try:
12+ return subprocess.check_output(
13+ ['systemd-virt-check', '-c'])
14+ except subprocess.CalledProcessError:
15+ return False
16
17=== modified file 'tests/core/test_host.py'
18--- tests/core/test_host.py 2016-08-11 15:47:44 +0000
19+++ tests/core/test_host.py 2016-10-24 13:03:43 +0000
20@@ -1682,3 +1682,19 @@
21 mock_file.readlines.return_value = raw.splitlines()
22 self.assertEqual(host.get_total_ram(), 7266414592) # 7GB
23 mock_open.assert_called_once_with('/proc/meminfo', 'r')
24+
25+ @patch('subprocess.check_output')
26+ def test_is_container_with_container(self, check_output):
27+ check_output.return_value = 1
28+ self.assertTrue(host.is_container())
29+
30+ @patch('subprocess.check_output')
31+ def test_is_container_with_vm(self, check_output):
32+ check_output.return_value = 0
33+ self.assertFalse(host.is_container())
34+
35+ @patch('subprocess.check_output')
36+ def test_is_container_with_exception(self, check_output):
37+ error = subprocess.CalledProcessError(1, 'oops', 'systemd-detect-virt')
38+ check_output.side_effect = error
39+ self.assertFalse(host.is_container())

Subscribers

People subscribed via source and target branches