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
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-11-09 11:15:35 +0000
4@@ -732,3 +732,20 @@
5 assert unit == 'kB', 'Unknown unit'
6 return int(value) * 1024 # Classic, not KiB.
7 raise NotImplementedError()
8+
9+
10+UPSTART_CONTAINER_TYPE = '/run/container_type'
11+
12+
13+def is_container():
14+ """Determine whether unit is running in a container
15+
16+ @return: boolean indicating if unit is in a container
17+ """
18+ if init_is_systemd():
19+ # Detect using systemd-detect-virt
20+ return subprocess.call(['systemd-detect-virt',
21+ '--container']) == 0
22+ else:
23+ # Detect using upstart container file marker
24+ return os.path.exists(UPSTART_CONTAINER_TYPE)
25
26=== modified file 'tests/core/test_host.py'
27--- tests/core/test_host.py 2016-08-11 15:47:44 +0000
28+++ tests/core/test_host.py 2016-11-09 11:15:35 +0000
29@@ -1682,3 +1682,51 @@
30 mock_file.readlines.return_value = raw.splitlines()
31 self.assertEqual(host.get_total_ram(), 7266414592) # 7GB
32 mock_open.assert_called_once_with('/proc/meminfo', 'r')
33+
34+ @patch.object(host, 'os')
35+ @patch.object(host, 'init_is_systemd')
36+ @patch('subprocess.call')
37+ def test_is_container_with_systemd_container(self,
38+ call,
39+ init_is_systemd,
40+ mock_os):
41+ init_is_systemd.return_value = True
42+ call.return_value = 0
43+ self.assertTrue(host.is_container())
44+ call.assert_called_with(['systemd-detect-virt', '--container'])
45+
46+ @patch.object(host, 'os')
47+ @patch.object(host, 'init_is_systemd')
48+ @patch('subprocess.call')
49+ def test_is_container_with_systemd_non_container(self,
50+ call,
51+ init_is_systemd,
52+ mock_os):
53+ init_is_systemd.return_value = True
54+ call.return_value = 1
55+ self.assertFalse(host.is_container())
56+ call.assert_called_with(['systemd-detect-virt', '--container'])
57+
58+ @patch.object(host, 'os')
59+ @patch.object(host, 'init_is_systemd')
60+ @patch('subprocess.call')
61+ def test_is_container_with_upstart_container(self,
62+ call,
63+ init_is_systemd,
64+ mock_os):
65+ init_is_systemd.return_value = False
66+ mock_os.path.exists.return_value = True
67+ self.assertTrue(host.is_container())
68+ mock_os.path.exists.assert_called_with('/run/container_type')
69+
70+ @patch.object(host, 'os')
71+ @patch.object(host, 'init_is_systemd')
72+ @patch('subprocess.call')
73+ def test_is_container_with_upstart_not_container(self,
74+ call,
75+ init_is_systemd,
76+ mock_os):
77+ init_is_systemd.return_value = False
78+ mock_os.path.exists.return_value = False
79+ self.assertFalse(host.is_container())
80+ mock_os.path.exists.assert_called_with('/run/container_type')

Subscribers

People subscribed via source and target branches