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
=== modified file 'charmhelpers/core/host.py'
--- charmhelpers/core/host.py 2016-08-11 15:47:44 +0000
+++ charmhelpers/core/host.py 2016-10-24 13:03:43 +0000
@@ -732,3 +732,11 @@
732 assert unit == 'kB', 'Unknown unit'732 assert unit == 'kB', 'Unknown unit'
733 return int(value) * 1024 # Classic, not KiB.733 return int(value) * 1024 # Classic, not KiB.
734 raise NotImplementedError()734 raise NotImplementedError()
735
736def is_container():
737 """Return True if system is running inside a container"""
738 try:
739 return subprocess.check_output(
740 ['systemd-virt-check', '-c'])
741 except subprocess.CalledProcessError:
742 return False
735743
=== modified file 'tests/core/test_host.py'
--- tests/core/test_host.py 2016-08-11 15:47:44 +0000
+++ tests/core/test_host.py 2016-10-24 13:03:43 +0000
@@ -1682,3 +1682,19 @@
1682 mock_file.readlines.return_value = raw.splitlines()1682 mock_file.readlines.return_value = raw.splitlines()
1683 self.assertEqual(host.get_total_ram(), 7266414592) # 7GB1683 self.assertEqual(host.get_total_ram(), 7266414592) # 7GB
1684 mock_open.assert_called_once_with('/proc/meminfo', 'r')1684 mock_open.assert_called_once_with('/proc/meminfo', 'r')
1685
1686 @patch('subprocess.check_output')
1687 def test_is_container_with_container(self, check_output):
1688 check_output.return_value = 1
1689 self.assertTrue(host.is_container())
1690
1691 @patch('subprocess.check_output')
1692 def test_is_container_with_vm(self, check_output):
1693 check_output.return_value = 0
1694 self.assertFalse(host.is_container())
1695
1696 @patch('subprocess.check_output')
1697 def test_is_container_with_exception(self, check_output):
1698 error = subprocess.CalledProcessError(1, 'oops', 'systemd-detect-virt')
1699 check_output.side_effect = error
1700 self.assertFalse(host.is_container())

Subscribers

People subscribed via source and target branches