get_distribution - Return distribution for all platforms (#71641)

Since moving to distro, it is possible to return this information for all platforms, not just Linux.
Also return version information for all platfrom not just Linux.
Update unit tests.
Remove some duplicate unit tests though I think there are more to remove.

* Fix docstring formatting

* Minor docstring changes

* Mock distro.id for Solaris service test

* Update comment
This commit is contained in:
Sam Doran 2021-06-11 10:22:18 -04:00 committed by GitHub
parent 4c8c40fd3d
commit 9a21e24778
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 47 deletions

View file

@ -0,0 +1,7 @@
minor_changes:
- >
get_distribution - ``lib.ansible.module_utils.common.sys_info.get_distribution`` now returns
distribution information for all platforms not just Linux (https://github.com/ansible/ansible/issues/17587)
- >
get_distribution_version - ``lib.ansible.module_utils.common.sys_info.get_distribution_version`` now
returns the version for all platfroms not just Linux (https://github.com/ansible/ansible/issues/17587)

View file

@ -16,20 +16,18 @@ __all__ = ('get_distribution', 'get_distribution_version', 'get_platform_subclas
def get_distribution(): def get_distribution():
''' '''
Return the name of the distribution the module is running on Return the name of the distribution the module is running on.
:rtype: NativeString or None :rtype: NativeString or None
:returns: Name of the distribution the module is running on :returns: Name of the distribution the module is running on
This function attempts to determine what Linux distribution the code is running on and return This function attempts to determine what distribution the code is running
a string representing that value. If the distribution cannot be determined, it returns on and return a string representing that value. If the platform is Linux
``OtherLinux``. If not run on Linux it returns None. and the distribution cannot be determined, it returns ``OtherLinux``.
''' '''
distribution = None distribution = distro.id().capitalize()
if platform.system() == 'Linux': if platform.system() == 'Linux':
distribution = distro.id().capitalize()
if distribution == 'Amzn': if distribution == 'Amzn':
distribution = 'Amazon' distribution = 'Amazon'
elif distribution == 'Rhel': elif distribution == 'Rhel':
@ -42,11 +40,12 @@ def get_distribution():
def get_distribution_version(): def get_distribution_version():
''' '''
Get the version of the Linux distribution the code is running on Get the version of the distribution the code is running on
:rtype: NativeString or None :rtype: NativeString or None
:returns: A string representation of the version of the distribution. If it cannot determine :returns: A string representation of the version of the distribution. If it
the version, it returns empty string. If this is not run on a Linux machine it returns None cannot determine the version, it returns an empty string. If this is not run on
a Linux machine it returns None.
''' '''
version = None version = None
@ -55,28 +54,27 @@ def get_distribution_version():
u'debian', u'debian',
)) ))
if platform.system() == 'Linux': version = distro.version()
version = distro.version() distro_id = distro.id()
distro_id = distro.id()
if version is not None: if version is not None:
if distro_id in needs_best_version: if distro_id in needs_best_version:
version_best = distro.version(best=True) version_best = distro.version(best=True)
# CentoOS maintainers believe only the major version is appropriate # CentoOS maintainers believe only the major version is appropriate
# but Ansible users desire minor version information, e.g., 7.5. # but Ansible users desire minor version information, e.g., 7.5.
# https://github.com/ansible/ansible/issues/50141#issuecomment-449452781 # https://github.com/ansible/ansible/issues/50141#issuecomment-449452781
if distro_id == u'centos': if distro_id == u'centos':
version = u'.'.join(version_best.split(u'.')[:2]) version = u'.'.join(version_best.split(u'.')[:2])
# Debian does not include minor version in /etc/os-release. # Debian does not include minor version in /etc/os-release.
# Bug report filed upstream requesting this be added to /etc/os-release # Bug report filed upstream requesting this be added to /etc/os-release
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=931197 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=931197
if distro_id == u'debian': if distro_id == u'debian':
version = version_best version = version_best
else: else:
version = u'' version = u''
return version return version
@ -139,9 +137,9 @@ def get_platform_subclass(cls):
new_cls = get_platform_subclass(User) new_cls = get_platform_subclass(User)
return super(cls, new_cls).__new__(new_cls) return super(cls, new_cls).__new__(new_cls)
''' '''
this_platform = platform.system() this_platform = platform.system()
distribution = get_distribution() distribution = get_distribution()
subclass = None subclass = None
# get the most specific superclass for this platform # get the most specific superclass for this platform

View file

@ -42,12 +42,6 @@ def test_get_platform():
# get_distribution tests # get_distribution tests
# #
def test_get_distribution_not_linux():
"""If it's not Linux, then it has no distribution"""
with patch('platform.system', return_value='Foo'):
assert get_distribution() is None
@pytest.mark.usefixtures("platform_linux") @pytest.mark.usefixtures("platform_linux")
class TestGetDistribution: class TestGetDistribution:
"""Tests for get_distribution that have to find something""" """Tests for get_distribution that have to find something"""
@ -114,11 +108,6 @@ class TestGetDistribution:
# get_distribution_version tests # get_distribution_version tests
# #
def test_get_distribution_version_not_linux():
"""If it's not Linux, then it has no distribution"""
with patch('platform.system', return_value='Foo'):
assert get_distribution_version() is None
@pytest.mark.usefixtures("platform_linux") @pytest.mark.usefixtures("platform_linux")
def test_distro_found(): def test_distro_found():

View file

@ -31,10 +31,19 @@ def platform_linux(mocker):
# get_distribution tests # get_distribution tests
# #
def test_get_distribution_not_linux(): @pytest.mark.parametrize(
"""If it's not Linux, then it has no distribution""" ('system', 'dist'),
with patch('platform.system', return_value='Foo'): (
assert get_distribution() is None ('Darwin', 'Darwin'),
('SunOS', 'Solaris'),
('FreeBSD', 'Freebsd'),
),
)
def test_get_distribution_not_linux(system, dist, mocker):
"""For platforms other than Linux, return the distribution"""
mocker.patch('platform.system', return_value=system)
mocker.patch('ansible.module_utils.common.sys_info.distro.id', return_value=dist)
assert get_distribution() == dist
@pytest.mark.usefixtures("platform_linux") @pytest.mark.usefixtures("platform_linux")
@ -103,10 +112,19 @@ class TestGetDistribution:
# get_distribution_version tests # get_distribution_version tests
# #
def test_get_distribution_version_not_linux(): @pytest.mark.parametrize(
('system', 'version'),
(
('Darwin', '19.6.0'),
('SunOS', '11.4'),
('FreeBSD', '12.1'),
),
)
def test_get_distribution_version_not_linux(mocker, system, version):
"""If it's not Linux, then it has no distribution""" """If it's not Linux, then it has no distribution"""
with patch('platform.system', return_value='Foo'): mocker.patch('platform.system', return_value=system)
assert get_distribution_version() is None mocker.patch('ansible.module_utils.common.sys_info.distro.version', return_value=version)
assert get_distribution_version() == version
@pytest.mark.usefixtures("platform_linux") @pytest.mark.usefixtures("platform_linux")

View file

@ -43,6 +43,8 @@ def mocker_sunos_service(mocker):
service.Service, "get_service_status") service.Service, "get_service_status")
get_service_status.return_value = "" get_service_status.return_value = ""
mocker.patch('ansible.module_utils.common.sys_info.distro.id', return_value='')
@pytest.fixture @pytest.fixture
def mocked_sunos_service(mocker): def mocked_sunos_service(mocker):