From 9b8a649f2ea59475c7e59175acba6d376cad3aaf Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Tue, 21 Jul 2020 14:21:07 -0400 Subject: [PATCH] =?UTF-8?q?[stable-2.10]=20Handle=20Slackware=20OS=20versi?= =?UTF-8?q?on=20strings=20containing=20a=20plus=20(=E2=80=9C+=E2=80=9D)=20?= =?UTF-8?q?(#68142)=20(#70717)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A couple of years ago Slackware -current began using a plus (“+”) at the end of the distribution version string to indicate a future version work-in-progress. Rearrange distribution_files unit tests to easily support more tests - add conftest with common fixtures - use parametrize for testing multiple scenarios * Add changelog * Add unit tests for Slackware distribution parsing * Use correct fixtures for Slackware Data comes from /etc/slackware-version Co-authored-by: Sam Doran Co-authored-by: > (cherry picked from commit 566c5e6ce1) Co-authored-by: Eduard Rozenberg <2648417+edrozenberg@users.noreply.github.com> Co-authored-by: Eduard Rozenberg <2648417+edrozenberg@users.noreply.github.com> --- .../fragments/38760-slackware-os-dist.yml | 2 + .../module_utils/facts/system/distribution.py | 2 +- .../fixtures/distribution_files/ClearLinux | 10 ++++ .../facts/fixtures/distribution_files/CoreOS | 10 ++++ .../fixtures/distribution_files/LinuxMint | 12 +++++ .../fixtures/distribution_files/Slackware | 1 + .../distribution_files/SlackwareCurrent | 1 + .../facts/system/distribution/conftest.py | 21 ++++++++ ...test_parse_distribution_file_ClearLinux.py | 51 +++++++++++++++++++ .../test_parse_distribution_file_Slackware.py | 37 ++++++++++++++ 10 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/38760-slackware-os-dist.yml create mode 100644 test/units/module_utils/facts/fixtures/distribution_files/ClearLinux create mode 100644 test/units/module_utils/facts/fixtures/distribution_files/CoreOS create mode 100644 test/units/module_utils/facts/fixtures/distribution_files/LinuxMint create mode 100644 test/units/module_utils/facts/fixtures/distribution_files/Slackware create mode 100644 test/units/module_utils/facts/fixtures/distribution_files/SlackwareCurrent create mode 100644 test/units/module_utils/facts/system/distribution/conftest.py create mode 100644 test/units/module_utils/facts/system/distribution/test_parse_distribution_file_ClearLinux.py create mode 100644 test/units/module_utils/facts/system/distribution/test_parse_distribution_file_Slackware.py diff --git a/changelogs/fragments/38760-slackware-os-dist.yml b/changelogs/fragments/38760-slackware-os-dist.yml new file mode 100644 index 00000000000..1c610cd96bb --- /dev/null +++ b/changelogs/fragments/38760-slackware-os-dist.yml @@ -0,0 +1,2 @@ +bugfixes: + - facts - account for Slackware OS with ``+`` in the name (https://github.com/ansible/ansible/issues/38760) diff --git a/lib/ansible/module_utils/facts/system/distribution.py b/lib/ansible/module_utils/facts/system/distribution.py index 1ddaae8c116..f90aa69d0b9 100644 --- a/lib/ansible/module_utils/facts/system/distribution.py +++ b/lib/ansible/module_utils/facts/system/distribution.py @@ -211,7 +211,7 @@ class DistributionFiles: if 'Slackware' not in data: return False, slackware_facts # TODO: remove slackware_facts['distribution'] = name - version = re.findall(r'\w+[.]\w+', data) + version = re.findall(r'\w+[.]\w+\+?', data) if version: slackware_facts['distribution_version'] = version[0] return True, slackware_facts diff --git a/test/units/module_utils/facts/fixtures/distribution_files/ClearLinux b/test/units/module_utils/facts/fixtures/distribution_files/ClearLinux new file mode 100644 index 00000000000..a5442de4fd5 --- /dev/null +++ b/test/units/module_utils/facts/fixtures/distribution_files/ClearLinux @@ -0,0 +1,10 @@ +NAME="Clear Linux OS" +VERSION=1 +ID=clear-linux-os +ID_LIKE=clear-linux-os +VERSION_ID=28120 +PRETTY_NAME="Clear Linux OS" +ANSI_COLOR="1;35" +HOME_URL="https://clearlinux.org" +SUPPORT_URL="https://clearlinux.org" +BUG_REPORT_URL="mailto:dev@lists.clearlinux.org"', diff --git a/test/units/module_utils/facts/fixtures/distribution_files/CoreOS b/test/units/module_utils/facts/fixtures/distribution_files/CoreOS new file mode 100644 index 00000000000..806ce3065a4 --- /dev/null +++ b/test/units/module_utils/facts/fixtures/distribution_files/CoreOS @@ -0,0 +1,10 @@ +NAME="Container Linux by CoreOS" +ID=coreos +VERSION=1911.5.0 +VERSION_ID=1911.5.0 +BUILD_ID=2018-12-15-2317 +PRETTY_NAME="Container Linux by CoreOS 1911.5.0 (Rhyolite)" +ANSI_COLOR="38;5;75" +HOME_URL="https://coreos.com/" +BUG_REPORT_URL="https://issues.coreos.com" +COREOS_BOARD="amd64-usr" diff --git a/test/units/module_utils/facts/fixtures/distribution_files/LinuxMint b/test/units/module_utils/facts/fixtures/distribution_files/LinuxMint new file mode 100644 index 00000000000..850f6b782e1 --- /dev/null +++ b/test/units/module_utils/facts/fixtures/distribution_files/LinuxMint @@ -0,0 +1,12 @@ +NAME="Linux Mint" +VERSION="19.1 (Tessa)" +ID=linuxmint +ID_LIKE=ubuntu +PRETTY_NAME="Linux Mint 19.1" +VERSION_ID="19.1" +HOME_URL="https://www.linuxmint.com/" +SUPPORT_URL="https://forums.ubuntu.com/" +BUG_REPORT_URL="http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/" +PRIVACY_POLICY_URL="https://www.linuxmint.com/" +VERSION_CODENAME=tessa +UBUNTU_CODENAME=bionic diff --git a/test/units/module_utils/facts/fixtures/distribution_files/Slackware b/test/units/module_utils/facts/fixtures/distribution_files/Slackware new file mode 100644 index 00000000000..1147d2973a1 --- /dev/null +++ b/test/units/module_utils/facts/fixtures/distribution_files/Slackware @@ -0,0 +1 @@ +Slackware 14.1 diff --git a/test/units/module_utils/facts/fixtures/distribution_files/SlackwareCurrent b/test/units/module_utils/facts/fixtures/distribution_files/SlackwareCurrent new file mode 100644 index 00000000000..62c046c8a6c --- /dev/null +++ b/test/units/module_utils/facts/fixtures/distribution_files/SlackwareCurrent @@ -0,0 +1 @@ +Slackware 14.2+ diff --git a/test/units/module_utils/facts/system/distribution/conftest.py b/test/units/module_utils/facts/system/distribution/conftest.py new file mode 100644 index 00000000000..d27b97f07d2 --- /dev/null +++ b/test/units/module_utils/facts/system/distribution/conftest.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2020 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +import pytest + +from units.compat.mock import Mock + + +@pytest.fixture +def mock_module(): + mock_module = Mock() + mock_module.params = {'gather_subset': ['all'], + 'gather_timeout': 5, + 'filter': '*'} + mock_module.get_bin_path = Mock(return_value=None) + return mock_module diff --git a/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_ClearLinux.py b/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_ClearLinux.py new file mode 100644 index 00000000000..c0957566cef --- /dev/null +++ b/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_ClearLinux.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +import os +import pytest + +from ansible.module_utils.facts.system.distribution import DistributionFiles + + +@pytest.fixture +def test_input(): + return { + 'name': 'Clearlinux', + 'path': '/usr/lib/os-release', + 'collected_facts': None, + } + + +def test_parse_distribution_file_clear_linux(mock_module, test_input): + test_input['data'] = open(os.path.join(os.path.dirname(__file__), '../../fixtures/distribution_files/ClearLinux')).read() + + result = ( + True, + { + 'distribution': 'Clear Linux OS', + 'distribution_major_version': '28120', + 'distribution_release': 'clear-linux-os', + 'distribution_version': '28120' + } + ) + + distribution = DistributionFiles(module=mock_module()) + assert result == distribution.parse_distribution_file_ClearLinux(**test_input) + + +@pytest.mark.parametrize('distro_file', ('CoreOS', 'LinuxMint')) +def test_parse_distribution_file_clear_linux_no_match(mock_module, distro_file, test_input): + """ + Test against data from Linux Mint and CoreOS to ensure we do not get a reported + match from parse_distribution_file_ClearLinux() + """ + test_input['data'] = open(os.path.join(os.path.dirname(__file__), '../../fixtures/distribution_files', distro_file)).read() + + result = (False, {}) + + distribution = DistributionFiles(module=mock_module()) + assert result == distribution.parse_distribution_file_ClearLinux(**test_input) diff --git a/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_Slackware.py b/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_Slackware.py new file mode 100644 index 00000000000..53fd4ea1e91 --- /dev/null +++ b/test/units/module_utils/facts/system/distribution/test_parse_distribution_file_Slackware.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +import os +import pytest + +from ansible.module_utils.facts.system.distribution import DistributionFiles + + +@pytest.mark.parametrize( + ('distro_file', 'expected_version'), + ( + ('Slackware', '14.1'), + ('SlackwareCurrent', '14.2+'), + ) +) +def test_parse_distribution_file_slackware(mock_module, distro_file, expected_version): + test_input = { + 'name': 'Slackware', + 'data': open(os.path.join(os.path.dirname(__file__), '../../fixtures/distribution_files', distro_file)).read(), + 'path': '/etc/os-release', + 'collected_facts': None, + } + + result = ( + True, + { + 'distribution': 'Slackware', + 'distribution_version': expected_version + } + ) + distribution = DistributionFiles(module=mock_module()) + assert result == distribution.parse_distribution_file_Slackware(**test_input)