1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
This commit is contained in:
Anatoly Pugachev 2019-01-24 04:33:32 +03:00 committed by Brian Coca
parent 96b3ef5553
commit fe8412128b
2 changed files with 51 additions and 8 deletions

View file

@ -26,8 +26,12 @@ from ansible.module_utils.facts.utils import get_file_content
from ansible.module_utils.facts.collector import BaseFactCollector from ansible.module_utils.facts.collector import BaseFactCollector
def get_uname_version(module): def get_uname(module, flags=('-v')):
rc, out, err = module.run_command(['uname', '-v']) if isinstance(flags, str):
flags = flags.split()
command = ['uname']
command.extend(flags)
rc, out, err = module.run_command(command)
if rc == 0: if rc == 0:
return out return out
return None return None
@ -587,6 +591,8 @@ class Distribution(object):
data = get_file_content('/etc/release').splitlines()[0] data = get_file_content('/etc/release').splitlines()[0]
if 'Solaris' in data: 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 = '' ora_prefix = ''
if 'Oracle Solaris' in data: if 'Oracle Solaris' in data:
data = data.replace('Oracle ', '') data = data.replace('Oracle ', '')
@ -594,9 +600,10 @@ class Distribution(object):
sunos_facts['distribution'] = data.split()[0] sunos_facts['distribution'] = data.split()[0]
sunos_facts['distribution_version'] = data.split()[1] sunos_facts['distribution_version'] = data.split()[1]
sunos_facts['distribution_release'] = ora_prefix + data sunos_facts['distribution_release'] = ora_prefix + data
sunos_facts['distribution_major_version'] = uname_r.split('.')[1].rstrip()
return sunos_facts return sunos_facts
uname_v = get_uname_version(self.module) uname_v = get_uname(self.module, flags=['-v'])
distribution_version = None distribution_version = None
if 'SmartOS' in data: if 'SmartOS' in data:

View file

@ -844,10 +844,12 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)"
{ {
"name": "Solaris 10", "name": "Solaris 10",
"uname_v": "Generic_141445-09", "uname_v": "Generic_141445-09",
"uname_r": "5.10",
"result": { "result": {
"distribution_release": "Solaris 10 10/09 s10x_u8wos_08a X86", "distribution_release": "Solaris 10 10/09 s10x_u8wos_08a X86",
"distribution": "Solaris", "distribution": "Solaris",
"os_family": "Solaris", "os_family": "Solaris",
"distribution_major_version": "10",
"distribution_version": "10" "distribution_version": "10"
}, },
"platform.dist": [ "platform.dist": [
@ -865,10 +867,12 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)"
{ {
"name": "Solaris 11", "name": "Solaris 11",
"uname_v": "11.0", "uname_v": "11.0",
"uname_r": "5.11",
"result": { "result": {
"distribution_release": "Oracle Solaris 11 11/11 X86", "distribution_release": "Oracle Solaris 11 11/11 X86",
"distribution": "Solaris", "distribution": "Solaris",
"os_family": "Solaris", "os_family": "Solaris",
"distribution_major_version": "11",
"distribution_version": "11" "distribution_version": "11"
}, },
"platform.dist": [ "platform.dist": [
@ -884,6 +888,7 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)"
}, },
{ {
"name": "Solaris 11.3", "name": "Solaris 11.3",
"uname_r": "5.11",
"platform.dist": [ "platform.dist": [
"", "",
"", "",
@ -891,8 +896,8 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)"
], ],
"input": { "input": {
"/etc/release": ( "/etc/release": (
" Oracle Solaris 11.3 X86\n Copyright (c) 1983, 2015, Oracle and/or its affiliates. " " Oracle Solaris 11.3 X86\n Copyright (c) 1983, 2018, Oracle and/or its affiliates. "
"All rights reserved.\n Assembled 06 October 2015\n" "All rights reserved.\n Assembled 09 May 2018\n"
) )
}, },
"platform.system": "SunOS", "platform.system": "SunOS",
@ -900,11 +905,36 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)"
"distribution_release": "Oracle Solaris 11.3 X86", "distribution_release": "Oracle Solaris 11.3 X86",
"distribution": "Solaris", "distribution": "Solaris",
"os_family": "Solaris", "os_family": "Solaris",
"distribution_major_version": "11",
"distribution_version": "11.3" "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", "name": "Solaris 10",
"uname_r": "5.10",
"platform.dist": [ "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_release": "Oracle Solaris 10 1/13 s10x_u11wos_24a X86",
"distribution": "Solaris", "distribution": "Solaris",
"os_family": "Solaris", "os_family": "Solaris",
"distribution_major_version": "10",
"distribution_version": "10" "distribution_version": "10"
} }
}, },
@ -1109,8 +1140,13 @@ def test_distribution_version(am, mocker, testcase):
data = data.strip() data = data.strip()
return data return data
def mock_get_uname_version(am): def mock_get_uname(am, flags):
return testcase.get('uname_v', None) 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): def mock_file_exists(fname, allow_empty=False):
if fname not in testcase['input']: if fname not in testcase['input']:
@ -1152,7 +1188,7 @@ def test_distribution_version(am, mocker, testcase):
return False 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_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.facts.system.distribution._file_exists', mock_file_exists)
mocker.patch('ansible.module_utils.distro.name', mock_distro_name) mocker.patch('ansible.module_utils.distro.name', mock_distro_name)
mocker.patch('ansible.module_utils.distro.id', mock_distro_name) mocker.patch('ansible.module_utils.distro.id', mock_distro_name)