Skip to content

Commit

Permalink
Merge pull request #52672 from waynew/51451-fix-docker-virtual-grain
Browse files Browse the repository at this point in the history
Set virtual grain when virtual_subtype exists
  • Loading branch information
dwoz authored Apr 25, 2019
2 parents 8a0239c + cdf1656 commit 3c05c9b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
7 changes: 7 additions & 0 deletions salt/grains/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,11 +944,13 @@ def _virtual(osdata):
with salt.utils.files.fopen('/proc/1/cgroup', 'r') as fhr:
fhr_contents = fhr.read()
if ':/lxc/' in fhr_contents:
grains['virtual'] = 'container'
grains['virtual_subtype'] = 'LXC'
else:
if any(x in fhr_contents
for x in (':/system.slice/docker', ':/docker/',
':/docker-ce/')):
grains['virtual'] = 'container'
grains['virtual_subtype'] = 'Docker'
except IOError:
pass
Expand Down Expand Up @@ -1051,6 +1053,11 @@ def _virtual(osdata):
if os.path.isfile('/var/run/xenconsoled.pid'):
grains['virtual_subtype'] = 'Xen Dom0'

# If we have a virtual_subtype, we're virtual, but maybe we couldn't
# figure out what specific virtual type we were?
if grains.get('virtual_subtype') and grains['virtual'] == 'physical':
grains['virtual'] = 'virtual'

for command in failed_commands:
log.info(
"Although '%s' was found in path, the current user "
Expand Down
58 changes: 56 additions & 2 deletions tests/unit/grains/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ def test_bsd_memdata(self):
@skipIf(salt.utils.platform.is_windows(), 'System is Windows')
def test_docker_virtual(self):
'''
Test if OS grains are parsed correctly in Ubuntu Xenial Xerus
Test if virtual grains are parsed correctly in Docker.
'''
with patch.object(os.path, 'isdir', MagicMock(return_value=False)):
with patch.object(os.path,
Expand All @@ -810,10 +810,37 @@ def test_docker_virtual(self):
'Testing Docker cgroup substring \'%s\'', cgroup_substr)
with patch('salt.utils.files.fopen', mock_open(read_data=cgroup_data)):
with patch.dict(core.__salt__, {'cmd.run_all': MagicMock()}):
grains = core._virtual({'kernel': 'Linux'})
self.assertEqual(
core._virtual({'kernel': 'Linux'}).get('virtual_subtype'),
grains.get('virtual_subtype'),
'Docker'
)
self.assertEqual(
grains.get('virtual'),
'container',
)

@skipIf(salt.utils.platform.is_windows(), 'System is Windows')
def test_lxc_virtual(self):
'''
Test if virtual grains are parsed correctly in LXC.
'''
with patch.object(os.path, 'isdir', MagicMock(return_value=False)):
with patch.object(os.path,
'isfile',
MagicMock(side_effect=lambda x: True if x == '/proc/1/cgroup' else False)):
cgroup_data = '10:memory:/lxc/a_long_sha256sum'
with patch('salt.utils.files.fopen', mock_open(read_data=cgroup_data)):
with patch.dict(core.__salt__, {'cmd.run_all': MagicMock()}):
grains = core._virtual({'kernel': 'Linux'})
self.assertEqual(
grains.get('virtual_subtype'),
'LXC'
)
self.assertEqual(
grains.get('virtual'),
'container',
)

@skipIf(salt.utils.platform.is_windows(), 'System is Windows')
def test_xen_virtual(self):
Expand All @@ -830,6 +857,33 @@ def test_xen_virtual(self):
'Xen PV DomU'
)

def test_if_virtual_subtype_exists_virtual_should_fallback_to_virtual(self):
def mockstat(path):
if path == '/':
return 'fnord'
elif path == '/proc/1/root/.':
return 'roscivs'
return None
with patch.dict(
core.__salt__,
{
'cmd.run': MagicMock(return_value=''),
'cmd.run_all': MagicMock(return_value={'retcode': 0, 'stdout': ''}),
}
):
with patch.multiple(
os.path,
isfile=MagicMock(return_value=False),
isdir=MagicMock(side_effect=lambda x: x == '/proc'),
):
with patch.multiple(
os,
stat=MagicMock(side_effect=mockstat),
):
grains = core._virtual({'kernel': 'Linux'})
assert grains.get('virtual_subtype') is not None
assert grains.get('virtual') == 'virtual'

def _check_ipaddress(self, value, ip_v):
'''
check if ip address in a list is valid
Expand Down

0 comments on commit 3c05c9b

Please sign in to comment.