Merge pull request #4 from terminalmage/pr-44335

Add test for PR 44335
This commit is contained in:
Daniel Wallace 2017-11-27 09:31:38 -07:00 committed by GitHub
commit f02b02032d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View File

@ -696,13 +696,14 @@ def _virtual(osdata):
pass pass
if os.path.isfile('/proc/1/cgroup'): if os.path.isfile('/proc/1/cgroup'):
try: try:
with salt.utils.fopen('/proc/1/cgroup', 'r') as fhr:
if ':/lxc/' in fhr.read():
grains['virtual_subtype'] = 'LXC'
dstrings = (':/system.slice/docker', ':/docker/', ':/docker-ce/')
with salt.utils.fopen('/proc/1/cgroup', 'r') as fhr: with salt.utils.fopen('/proc/1/cgroup', 'r') as fhr:
fhr_contents = fhr.read() fhr_contents = fhr.read()
if any(dstring in fhr_contents for dstring in dstrings): if ':/lxc/' in fhr_contents:
grains['virtual_subtype'] = 'LXC'
else:
if any(x in fhr_contents
for x in (':/system.slice/docker', ':/docker/',
':/docker-ce/')):
grains['virtual_subtype'] = 'Docker' grains['virtual_subtype'] = 'Docker'
except IOError: except IOError:
pass pass

View File

@ -5,6 +5,7 @@
# Import Python libs # Import Python libs
from __future__ import absolute_import from __future__ import absolute_import
import logging
import os import os
import platform import platform
@ -28,6 +29,8 @@ from salt.grains import core
# Globals # Globals
core.__salt__ = {} core.__salt__ = {}
core.__opts__ = {}
log = logging.getLogger(__name__)
@skipIf(NO_MOCK, NO_MOCK_REASON) @skipIf(NO_MOCK, NO_MOCK_REASON)
@ -459,6 +462,27 @@ PATCHLEVEL = 3
self.assertListEqual(list(os_grains.get('osrelease_info')), os_release_map['osrelease_info']) self.assertListEqual(list(os_grains.get('osrelease_info')), os_release_map['osrelease_info'])
self.assertEqual(os_grains.get('osmajorrelease'), os_release_map['osmajorrelease']) self.assertEqual(os_grains.get('osmajorrelease'), os_release_map['osmajorrelease'])
def test_docker_virtual(self):
'''
Test if OS grains are parsed correctly in Ubuntu Xenial Xerus
'''
with patch.object(os.path, 'isdir', MagicMock(return_value=False)), \
patch.object(os.path,
'isfile',
MagicMock(side_effect=lambda x: True
if x == '/proc/1/cgroup' else False)):
for cgroup_substr in (':/system.slice/docker', ':/docker/',
':/docker-ce/'):
cgroup_data = \
'10:memory{0}a_long_sha256sum'.format(cgroup_substr)
log.debug(
'Testing Docker cgroup substring \'%s\'', cgroup_substr)
with patch('salt.utils.fopen', mock_open(read_data=cgroup_data)):
self.assertEqual(
core._virtual({'kernel': 'Linux'}).get('virtual_subtype'),
'Docker'
)
if __name__ == '__main__': if __name__ == '__main__':
from integration import run_tests from integration import run_tests