Merge lp:~james-page/charm-helpers/is-container into lp:charm-helpers

Proposed by James Page
Status: Merged
Merged at revision: 656
Proposed branch: lp:~james-page/charm-helpers/is-container
Merge into: lp:charm-helpers
Diff against target: 80 lines (+65/-0)
2 files modified
charmhelpers/core/host.py (+17/-0)
tests/core/test_host.py (+48/-0)
To merge this branch: bzr merge lp:~james-page/charm-helpers/is-container
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+310417@code.launchpad.net
To post a comment you must log in.
654. By James Page

Remove ambiguity in container detection

Revision history for this message
Liam Young (gnuoy) wrote :

LGTM, merging

review: Approve

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-11-09 11:15:35 +0000
@@ -732,3 +732,20 @@
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
736
737UPSTART_CONTAINER_TYPE = '/run/container_type'
738
739
740def is_container():
741 """Determine whether unit is running in a container
742
743 @return: boolean indicating if unit is in a container
744 """
745 if init_is_systemd():
746 # Detect using systemd-detect-virt
747 return subprocess.call(['systemd-detect-virt',
748 '--container']) == 0
749 else:
750 # Detect using upstart container file marker
751 return os.path.exists(UPSTART_CONTAINER_TYPE)
735752
=== 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-11-09 11:15:35 +0000
@@ -1682,3 +1682,51 @@
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.object(host, 'os')
1687 @patch.object(host, 'init_is_systemd')
1688 @patch('subprocess.call')
1689 def test_is_container_with_systemd_container(self,
1690 call,
1691 init_is_systemd,
1692 mock_os):
1693 init_is_systemd.return_value = True
1694 call.return_value = 0
1695 self.assertTrue(host.is_container())
1696 call.assert_called_with(['systemd-detect-virt', '--container'])
1697
1698 @patch.object(host, 'os')
1699 @patch.object(host, 'init_is_systemd')
1700 @patch('subprocess.call')
1701 def test_is_container_with_systemd_non_container(self,
1702 call,
1703 init_is_systemd,
1704 mock_os):
1705 init_is_systemd.return_value = True
1706 call.return_value = 1
1707 self.assertFalse(host.is_container())
1708 call.assert_called_with(['systemd-detect-virt', '--container'])
1709
1710 @patch.object(host, 'os')
1711 @patch.object(host, 'init_is_systemd')
1712 @patch('subprocess.call')
1713 def test_is_container_with_upstart_container(self,
1714 call,
1715 init_is_systemd,
1716 mock_os):
1717 init_is_systemd.return_value = False
1718 mock_os.path.exists.return_value = True
1719 self.assertTrue(host.is_container())
1720 mock_os.path.exists.assert_called_with('/run/container_type')
1721
1722 @patch.object(host, 'os')
1723 @patch.object(host, 'init_is_systemd')
1724 @patch('subprocess.call')
1725 def test_is_container_with_upstart_not_container(self,
1726 call,
1727 init_is_systemd,
1728 mock_os):
1729 init_is_systemd.return_value = False
1730 mock_os.path.exists.return_value = False
1731 self.assertFalse(host.is_container())
1732 mock_os.path.exists.assert_called_with('/run/container_type')

Subscribers

People subscribed via source and target branches