Merge pull request #35227 from isbm/isbm-osfinger-ubuntu-fix

Isbm osfinger ubuntu fix
This commit is contained in:
Mike Place 2016-08-10 00:38:30 +09:00 committed by GitHub
commit ce7aeb6ca4
2 changed files with 78 additions and 5 deletions

View file

@ -1372,8 +1372,7 @@ def os_data():
grains.pop('lsb_distrib_release', None)
grains['osrelease'] = \
grains.get('lsb_distrib_release', osrelease).strip()
grains['oscodename'] = grains.get('lsb_distrib_codename',
oscodename).strip()
grains['oscodename'] = grains.get('lsb_distrib_codename', '').strip() or oscodename
if 'Red Hat' in grains['oscodename']:
grains['oscodename'] = oscodename
distroname = _REPLACE_LINUX_RE.sub('', grains['osfullname']).strip()
@ -1490,9 +1489,12 @@ def os_data():
continue
osrelease_info[idx] = int(value)
grains['osrelease_info'] = tuple(osrelease_info)
grains['osmajorrelease'] = str(grains['osrelease_info'][0]) # This will be an integer in the next release
os_name = 'os' if grains.get('os') in ('FreeBSD', 'OpenBSD', 'NetBSD', 'Mac', 'Raspbian') else 'osfullname'
grains['osfinger'] = '{0}-{1}'.format(grains[os_name], grains['osrelease_info'][0])
grains['osmajorrelease'] = str(grains['osrelease_info'][0]) # This will be an integer in the two releases
salt.utils.warn_until('Nitrogen', 'The "osmajorrelease" will be a type of an integer.')
os_name = grains['os' if grains.get('os') in (
'FreeBSD', 'OpenBSD', 'NetBSD', 'Mac', 'Raspbian') else 'osfullname']
grains['osfinger'] = '{0}-{1}'.format(
os_name, grains['osrelease'] if os_name in ('Ubuntu',) else grains['osrelease_info'][0])
return grains

View file

@ -375,6 +375,77 @@ PATCHLEVEL = 3
}
self._run_suse_os_grains_tests(_os_release_map)
@skipIf(not salt.utils.is_linux(), 'System is not Linux')
def test_suse_os_grains_ubuntu(self):
'''
Test if OS grains are parsed correctly in Ubuntu Xenial Xerus
'''
_os_release_map = {
'os_release_file': {
'NAME': 'Ubuntu',
'VERSION': '16.04.1 LTS (Xenial Xerus)',
'VERSION_ID': '16.04',
'PRETTY_NAME': '',
'ID': 'ubuntu',
},
'oscodename': 'xenial',
'osfullname': 'Ubuntu',
'osrelease': '16.04',
'osrelease_info': [16, 4],
'osmajorrelease': '16',
'osfinger': 'Ubuntu-16.04',
}
self._run_ubuntu_os_grains_tests(_os_release_map)
def _run_ubuntu_os_grains_tests(self, os_release_map):
path_isfile_mock = MagicMock(side_effect=lambda x: x in ['/etc/os-release'])
empty_mock = MagicMock(return_value={})
osarch_mock = MagicMock(return_value="amd64")
os_release_mock = MagicMock(return_value=os_release_map.get('os_release_file'))
orig_import = __import__
def _import_mock(name, *args):
if name == 'lsb_release':
raise ImportError('No module named lsb_release')
return orig_import(name, *args)
# Skip the first if statement
with patch.object(salt.utils, 'is_proxy',
MagicMock(return_value=False)):
# Skip the selinux/systemd stuff (not pertinent)
with patch.object(core, '_linux_bin_exists',
MagicMock(return_value=False)):
# Skip the init grain compilation (not pertinent)
with patch.object(os.path, 'exists', path_isfile_mock):
# Ensure that lsb_release fails to import
with patch('__builtin__.__import__',
side_effect=_import_mock):
# Skip all the /etc/*-release stuff (not pertinent)
with patch.object(os.path, 'isfile', path_isfile_mock):
with patch.object(core, '_parse_os_release', os_release_mock):
# Mock platform.linux_distribution to give us the
# OS name that we want.
distro_mock = MagicMock(return_value=('Ubuntu', '16.04', 'xenial'))
with patch("salt.utils.fopen", mock_open()) as suse_release_file:
suse_release_file.return_value.__iter__.return_value = os_release_map.get(
'suse_release_file', '').splitlines()
with patch.object(platform, 'linux_distribution', distro_mock):
with patch.object(core, '_linux_gpu_data', empty_mock):
with patch.object(core, '_linux_cpudata', empty_mock):
with patch.object(core, '_virtual', empty_mock):
# Mock the osarch
with patch.dict(core.__salt__, {'cmd.run': osarch_mock}):
os_grains = core.os_data()
self.assertEqual(os_grains.get('os'), 'Ubuntu')
self.assertEqual(os_grains.get('os_family'), 'Debian')
self.assertEqual(os_grains.get('osfullname'), os_release_map['osfullname'])
self.assertEqual(os_grains.get('oscodename'), os_release_map['oscodename'])
self.assertEqual(os_grains.get('osrelease'), os_release_map['osrelease'])
self.assertListEqual(list(os_grains.get('osrelease_info')), os_release_map['osrelease_info'])
self.assertEqual(os_grains.get('osmajorrelease'), os_release_map['osmajorrelease'])
if __name__ == '__main__':
from integration import run_tests