From fe8412128b843003d4e25d1976635708286f2e04 Mon Sep 17 00:00:00 2001 From: Anatoly Pugachev Date: Thu, 24 Jan 2019 04:33:32 +0300 Subject: [PATCH] facts: solaris: introduce distribution_major version detection for Solaris (#43978) * facts: solaris: introduce distribution_major version detection for Solaris Currently, there's no distribution_major in facts module on Solaris OS. Use "uname -r" output to report major version. Before the patch we get this on Solaris 11.3 : $ ansible -o solaris11 -m setup -a filter=ansible_distribution_major_version solaris11 | SUCCESS => {"ansible_facts": {}, "changed": false} and after this patch, output is the following: $ ansible -o solaris11 -m setup -a filter=ansible_distribution_major_version solaris11 | SUCCESS => {"ansible_facts": {"ansible_distribution_major_version": "11"}, "changed": false} Tested with Solaris 11.3 and Solaris 10 (both are x86_64 VMs) Includes patch for test/units. Fixes #18197 * Try to fix test unit * should work now... * fixes for W291 (trailing whitespace) and E265 (block comment) * mock uname_release for solaris 10 and solaris 11 * facts: solaris: introduce distribution_major version detection for Solaris Currently, there's no distribution_major in facts module on Solaris OS. Use "uname -r" output to report major version. Before the patch we get this on Solaris 11.3 : $ ansible -o solaris11 -m setup -a filter=ansible_distribution_major_version solaris11 | SUCCESS => {"ansible_facts": {}, "changed": false} and after this patch, output is the following: $ ansible -o solaris11 -m setup -a filter=ansible_distribution_major_version solaris11 | SUCCESS => {"ansible_facts": {"ansible_distribution_major_version": "11"}, "changed": false} Tested with Solaris 11.3 and Solaris 10 (both are x86_64 VMs) Includes patch for test/units. Fixes #18197 * Try to fix test unit * should work now... * fixes for W291 (trailing whitespace) and E265 (block comment) * mock uname_release for solaris 10 and solaris 11 * typo uname_v -> uname_r * rebase * fix pep8 E302: 2 blank lines * remove int() cast to match test case * use single function for uname_r and uname_v * add solaris 11.4 OS to distribution test unit * fix pep8 sanity - E231 missing whitespace * distribution_major_version variable strip newline * mocker test function for mock_get_uname with parameters instead of two different functions * failed to make one fuction with test unit, revert to use 2 different functions * try to use single get_uname function * fix pep8: E703 --- .../module_utils/facts/system/distribution.py | 13 ++++-- .../module_utils/test_distribution_version.py | 46 +++++++++++++++++-- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/lib/ansible/module_utils/facts/system/distribution.py b/lib/ansible/module_utils/facts/system/distribution.py index 8de264efaf..14fa81f845 100644 --- a/lib/ansible/module_utils/facts/system/distribution.py +++ b/lib/ansible/module_utils/facts/system/distribution.py @@ -26,8 +26,12 @@ from ansible.module_utils.facts.utils import get_file_content from ansible.module_utils.facts.collector import BaseFactCollector -def get_uname_version(module): - rc, out, err = module.run_command(['uname', '-v']) +def get_uname(module, flags=('-v')): + if isinstance(flags, str): + flags = flags.split() + command = ['uname'] + command.extend(flags) + rc, out, err = module.run_command(command) if rc == 0: return out return None @@ -587,6 +591,8 @@ class Distribution(object): data = get_file_content('/etc/release').splitlines()[0] if 'Solaris' in data: + # for solaris 10 uname_r will contain 5.10, for solaris 11 it will have 5.11 + uname_r = get_uname(self.module, flags=['-r']) ora_prefix = '' if 'Oracle Solaris' in data: data = data.replace('Oracle ', '') @@ -594,9 +600,10 @@ class Distribution(object): sunos_facts['distribution'] = data.split()[0] sunos_facts['distribution_version'] = data.split()[1] sunos_facts['distribution_release'] = ora_prefix + data + sunos_facts['distribution_major_version'] = uname_r.split('.')[1].rstrip() return sunos_facts - uname_v = get_uname_version(self.module) + uname_v = get_uname(self.module, flags=['-v']) distribution_version = None if 'SmartOS' in data: diff --git a/test/units/module_utils/test_distribution_version.py b/test/units/module_utils/test_distribution_version.py index 025806d168..d88f7733dc 100644 --- a/test/units/module_utils/test_distribution_version.py +++ b/test/units/module_utils/test_distribution_version.py @@ -844,10 +844,12 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)" { "name": "Solaris 10", "uname_v": "Generic_141445-09", + "uname_r": "5.10", "result": { "distribution_release": "Solaris 10 10/09 s10x_u8wos_08a X86", "distribution": "Solaris", "os_family": "Solaris", + "distribution_major_version": "10", "distribution_version": "10" }, "platform.dist": [ @@ -865,10 +867,12 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)" { "name": "Solaris 11", "uname_v": "11.0", + "uname_r": "5.11", "result": { "distribution_release": "Oracle Solaris 11 11/11 X86", "distribution": "Solaris", "os_family": "Solaris", + "distribution_major_version": "11", "distribution_version": "11" }, "platform.dist": [ @@ -884,6 +888,7 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)" }, { "name": "Solaris 11.3", + "uname_r": "5.11", "platform.dist": [ "", "", @@ -891,8 +896,8 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)" ], "input": { "/etc/release": ( - " Oracle Solaris 11.3 X86\n Copyright (c) 1983, 2015, Oracle and/or its affiliates. " - "All rights reserved.\n Assembled 06 October 2015\n" + " Oracle Solaris 11.3 X86\n Copyright (c) 1983, 2018, Oracle and/or its affiliates. " + "All rights reserved.\n Assembled 09 May 2018\n" ) }, "platform.system": "SunOS", @@ -900,11 +905,36 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)" "distribution_release": "Oracle Solaris 11.3 X86", "distribution": "Solaris", "os_family": "Solaris", + "distribution_major_version": "11", "distribution_version": "11.3" } }, + { + "name": "Solaris 11.4", + "uname_r": "5.11", + "platform.dist": [ + "", + "", + "" + ], + "input": { + "/etc/release": ( + " Oracle Solaris 11.4 SPARC\n Copyright (c) 1983, 2018, Oracle and/or its affiliates." + " All rights reserved.\n Assembled 14 September 2018\n" + ) + }, + "platform.system": "SunOS", + "result": { + "distribution_release": "Oracle Solaris 11.4 SPARC", + "distribution": "Solaris", + "os_family": "Solaris", + "distribution_major_version": "11", + "distribution_version": "11.4" + } + }, { "name": "Solaris 10", + "uname_r": "5.10", "platform.dist": [ "", "", @@ -919,6 +949,7 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)" "distribution_release": "Oracle Solaris 10 1/13 s10x_u11wos_24a X86", "distribution": "Solaris", "os_family": "Solaris", + "distribution_major_version": "10", "distribution_version": "10" } }, @@ -1109,8 +1140,13 @@ def test_distribution_version(am, mocker, testcase): data = data.strip() return data - def mock_get_uname_version(am): - return testcase.get('uname_v', None) + def mock_get_uname(am, flags): + if '-v' in flags: + return testcase.get('uname_v', None) + elif '-r' in flags: + return testcase.get('uname_r', None) + else: + return None def mock_file_exists(fname, allow_empty=False): if fname not in testcase['input']: @@ -1152,7 +1188,7 @@ def test_distribution_version(am, mocker, testcase): return False mocker.patch('ansible.module_utils.facts.system.distribution.get_file_content', mock_get_file_content) - mocker.patch('ansible.module_utils.facts.system.distribution.get_uname_version', mock_get_uname_version) + mocker.patch('ansible.module_utils.facts.system.distribution.get_uname', mock_get_uname) mocker.patch('ansible.module_utils.facts.system.distribution._file_exists', mock_file_exists) mocker.patch('ansible.module_utils.distro.name', mock_distro_name) mocker.patch('ansible.module_utils.distro.id', mock_distro_name)