cleanup tests

* use nose test generator
* more comments
* move facts import inside the skipped function, fix python3 warning
This commit is contained in:
Robin Roth 2016-04-14 19:44:06 +02:00 committed by Robin Roth
parent 2b104fe6ad
commit 37188ea336
3 changed files with 302 additions and 233 deletions

View file

@ -282,6 +282,11 @@ class Facts(object):
# platform.dist() is deprecated in 2.6 # platform.dist() is deprecated in 2.6
# in 2.6 and newer, you should use platform.linux_distribution() # in 2.6 and newer, you should use platform.linux_distribution()
def get_distribution_facts(self): def get_distribution_facts(self):
"""
Fills facts about the distribution name and version.
This is unit tested. Please extend the tests to cover all distributions if you have them available.
"""
# A list with OS Family members # A list with OS Family members
OS_FAMILY = dict( OS_FAMILY = dict(

View file

@ -3,7 +3,7 @@
""" """
This script generated test_cases for test_distribution_version.py. This script generated test_cases for test_distribution_version.py.
To do so it outputs the relevant files from /etc/*release, the ouput of platform.dist() and the current ansible_facts regarding the distribution version. To do so it outputs the relevant files from /etc/*release, the output of platform.dist() and the current ansible_facts regarding the distribution version.
This assumes a working ansible version in the path. This assumes a working ansible version in the path.
""" """

View file

@ -29,112 +29,94 @@ from ansible.utils.unicode import to_bytes
# for testing # for testing
from ansible.compat.tests import unittest from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch from ansible.compat.tests.mock import patch
from nose.tools import assert_in, assert_equal
import ansible.module_utils.facts # the module we are actually testing
@unittest.skipIf(sys.version_info[0] >= 3, "Python 3 is not supported on targets (yet)") # to generate the testcase data, you can use the script gen_distribution_version_testcase.py in hacking/tests
class TestModuleUtilsFactsDistribution(unittest.TestCase): TESTSETS = [
{
def setUp(self): "name" : "openSUSE Leap 42.1",
self.real_stdin = sys.stdin "input": {
from ansible.module_utils import basic "/etc/os-release":
"""
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}, ANSIBLE_MODULE_CONSTANTS={})) NAME="openSUSE Leap"
if PY3: VERSION="42.1"
sys.stdin = StringIO(args) VERSION_ID="42.1"
sys.stdin.buffer = BytesIO(to_bytes(args)) PRETTY_NAME="openSUSE Leap 42.1 (x86_64)"
else: ID=opensuse
sys.stdin = BytesIO(to_bytes(args)) ANSI_COLOR="0;32"
self.module = basic.AnsibleModule(argument_spec=dict()) CPE_NAME="cpe:/o:opensuse:opensuse:42.1"
BUG_REPORT_URL="https://bugs.opensuse.org"
def tearDown(self): HOME_URL="https://opensuse.org/"
sys.stdin = self.real_stdin ID_LIKE="suse"
""",
def clear_modules(self, mods): "/etc/SuSE-release":"""
for mod in mods: openSUSE 42.1 (x86_64)
if mod in sys.modules: VERSION = 42.1
del sys.modules[mod] CODENAME = Malachite
# /etc/SuSE-release is deprecated and will be removed in the future, use /etc/os-release instead
def test_distribution_from_files(self): """
"""tests the distribution parsing code of the Facts class },
"platform.dist": ['SuSE', '42.1', 'x86_64'],
testsets have "result":{
* a name (for output/debugging only) "distribution": "openSUSE Leap",
* input files that are faked "distribution_major_version": "42",
* those should be complete and also include "irrelevant" files that might be mistaken as coming from other distributions "distribution_release": "x86_64",
* all files that are not listed here are assumed to not exist at all "distribution_version": "42.1",
* the output of pythons platform.dist() }
* results for the ansible variables distribution* },
""" {
'name': 'openSUSE 13.2',
testsets = [ 'input': {'/etc/SuSE-release': """openSUSE 13.2 (x86_64)
{ VERSION = 13.2
"name" : "openSUSE Leap 42.1", CODENAME = Harlequin
"input": { # /etc/SuSE-release is deprecated and will be removed in the future, use /etc/os-release instead
"/etc/os-release": """,
"""NAME="openSUSE Leap" '/etc/os-release': """NAME=openSUSE
VERSION="42.1" VERSION="13.2 (Harlequin)"
VERSION_ID="42.1" VERSION_ID="13.2"
PRETTY_NAME="openSUSE Leap 42.1 (x86_64)" PRETTY_NAME="openSUSE 13.2 (Harlequin) (x86_64)"
ID=opensuse ID=opensuse
ANSI_COLOR="0;32" ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:opensuse:42.1" CPE_NAME="cpe:/o:opensuse:opensuse:13.2"
BUG_REPORT_URL="https://bugs.opensuse.org" BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://opensuse.org/" HOME_URL="https://opensuse.org/"
ID_LIKE="suse" ID_LIKE="suse"
""", """},
"/etc/SuSE-release":""" 'platform.dist': ('SuSE', '13.2', 'x86_64'),
openSUSE 42.1 (x86_64) 'result': {'distribution': u'openSUSE',
VERSION = 42.1 'distribution_major_version': u'13',
CODENAME = Malachite 'distribution_release': u'Harlequin',
# /etc/SuSE-release is deprecated and will be removed in the future, use /etc/os-release instead 'distribution_version': u'13.2'}
""" },
}, { # see https://github.com/ansible/ansible/issues/14837
"platform.dist": ['SuSE', '42.1', 'x86_64'], "name": "SLES 11.3",
"result":{ "input": {
"distribution": "openSUSE Leap", "/etc/SuSE-release":"""
"distribution_major_version": "42",
"distribution_release": "x86_64",
"distribution_version": "42.1",
}
},
{
'name': 'openSUSE 13.2',
'input': {'/etc/SuSE-release': 'openSUSE 13.2 (x86_64)\nVERSION = 13.2\nCODENAME = Harlequin\n# /etc/SuSE-release is deprecated and will be removed in the future, use /etc/os-release instead\n',
'/etc/os-release': 'NAME=openSUSE\nVERSION="13.2 (Harlequin)"\nVERSION_ID="13.2"\nPRETTY_NAME="openSUSE 13.2 (Harlequin) (x86_64)"\nID=opensuse\nANSI_COLOR="0;32"\nCPE_NAME="cpe:/o:opensuse:opensuse:13.2"\nBUG_REPORT_URL="https://bugs.opensuse.org"\nHOME_URL="https://opensuse.org/"\nID_LIKE="suse"\n'},
'platform.dist': ('SuSE', '13.2', 'x86_64'),
'result': {'distribution': u'openSUSE',
'distribution_major_version': u'13',
'distribution_release': u'Harlequin',
'distribution_version': u'13.2'}
},
{ # see https://github.com/ansible/ansible/issues/14837
"name": "SLES 11.3",
"input": {
"/etc/SuSE-release":"""
SUSE Linux Enterprise Server 11 (x86_64) SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11 VERSION = 11
PATCHLEVEL = 3 PATCHLEVEL = 3
""" """
}, },
"platform.dist": ['SuSE', '11', 'x86_64'], "platform.dist": ['SuSE', '11', 'x86_64'],
"result":{ "result":{
"distribution": "SLES", "distribution": "SLES",
"distribution_major_version": "11", "distribution_major_version": "11",
"distribution_release": "3", "distribution_release": "3",
"distribution_version": "11.3", "distribution_version": "11.3",
} }
}, },
{ # see https://github.com/ansible/ansible/issues/14837 { # see https://github.com/ansible/ansible/issues/14837
"name": "SLES 11.4", "name": "SLES 11.4",
"input": { "input": {
"/etc/SuSE-release":""" "/etc/SuSE-release":"""
SUSE Linux Enterprise Server 11 (x86_64) SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11 VERSION = 11
PATCHLEVEL = 4 PATCHLEVEL = 4
""", """,
"/etc/os-release":""" "/etc/os-release":"""
NAME="SLES" NAME="SLES"
VERSION="11.4" VERSION="11.4"
VERSION_ID="11.4" VERSION_ID="11.4"
@ -142,27 +124,27 @@ PRETTY_NAME="SUSE Linux Enterprise Server 11 SP4"
ID="sles" ID="sles"
ANSI_COLOR="0;32" ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:11:4" CPE_NAME="cpe:/o:suse:sles:11:4"
""", """,
}, },
"platform.dist": ['SuSE', '11', 'x86_64'], "platform.dist": ['SuSE', '11', 'x86_64'],
"result":{ "result":{
"distribution": "SLES", "distribution": "SLES",
"distribution_major_version": "11", "distribution_major_version": "11",
"distribution_release": "4", "distribution_release": "4",
"distribution_version": "11.4", "distribution_version": "11.4",
} }
}, },
{ # see https://github.com/ansible/ansible/issues/14837 { # see https://github.com/ansible/ansible/issues/14837
"name": "SLES 12 SP0", "name": "SLES 12 SP0",
"input": { "input": {
"/etc/SuSE-release":""" "/etc/SuSE-release":"""
SUSE Linux Enterprise Server 12 (x86_64) SUSE Linux Enterprise Server 12 (x86_64)
VERSION = 12 VERSION = 12
PATCHLEVEL = 0 PATCHLEVEL = 0
# This file is deprecated and will be removed in a future service pack or release. # This file is deprecated and will be removed in a future service pack or release.
# Please check /etc/os-release for details about this release. # Please check /etc/os-release for details about this release.
""", """,
"/etc/os-release":""" "/etc/os-release":"""
NAME="SLES" NAME="SLES"
VERSION="12" VERSION="12"
VERSION_ID="12" VERSION_ID="12"
@ -170,28 +152,28 @@ PRETTY_NAME="SUSE Linux Enterprise Server 12"
ID="sles" ID="sles"
ANSI_COLOR="0;32" ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:12" CPE_NAME="cpe:/o:suse:sles:12"
""", """,
}, },
"platform.dist": ['SuSE', '12', 'x86_64'], "platform.dist": ['SuSE', '12', 'x86_64'],
"result":{ "result":{
"distribution": "SLES", "distribution": "SLES",
"distribution_major_version": "12", "distribution_major_version": "12",
"distribution_release": "0", "distribution_release": "0",
"distribution_version": "12", "distribution_version": "12",
} }
}, },
{ # see https://github.com/ansible/ansible/issues/14837 { # see https://github.com/ansible/ansible/issues/14837
"name": "SLES 12 SP1", "name": "SLES 12 SP1",
"input": { "input": {
"/etc/SuSE-release":""" "/etc/SuSE-release":"""
SUSE Linux Enterprise Server 12 (x86_64) SUSE Linux Enterprise Server 12 (x86_64)
VERSION = 12 VERSION = 12
PATCHLEVEL = 0 PATCHLEVEL = 0
# This file is deprecated and will be removed in a future service pack or release. # This file is deprecated and will be removed in a future service pack or release.
# Please check /etc/os-release for details about this release. # Please check /etc/os-release for details about this release.
""", """,
"/etc/os-release":""" "/etc/os-release":"""
NAME="SLES" NAME="SLES"
VERSION="12-SP1" VERSION="12-SP1"
VERSION_ID="12.1" VERSION_ID="12.1"
@ -199,76 +181,105 @@ PRETTY_NAME="SUSE Linux Enterprise Server 12 SP1"
ID="sles" ID="sles"
ANSI_COLOR="0;32" ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:12:sp1" CPE_NAME="cpe:/o:suse:sles:12:sp1"
""", """,
}, },
"platform.dist": ['SuSE', '12', 'x86_64'], "platform.dist": ['SuSE', '12', 'x86_64'],
"result":{ "result":{
"distribution": "SLES", "distribution": "SLES",
"distribution_major_version": "12", "distribution_major_version": "12",
"distribution_release": "1", "distribution_release": "1",
"distribution_version": "12.1", "distribution_version": "12.1",
} }
}, },
{ {
"name": "Debian stretch/sid", "name": "Debian stretch/sid",
"input": { "input": {
"/etc/os-release":""" "/etc/os-release":"""
PRETTY_NAME="Debian GNU/Linux stretch/sid" PRETTY_NAME="Debian GNU/Linux stretch/sid"
NAME="Debian GNU/Linux" NAME="Debian GNU/Linux"
ID=debian ID=debian
HOME_URL="https://www.debian.org/" HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support" SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/" BUG_REPORT_URL="https://bugs.debian.org/"
""", """,
"/etc/debian_version":""" "/etc/debian_version":"""
stretch/sid stretch/sid
""", """,
}, },
"platform.dist": ('debian', 'stretch/sid', ''), "platform.dist": ('debian', 'stretch/sid', ''),
"result":{ "result":{
"distribution": "Debian", "distribution": "Debian",
"distribution_major_version": "stretch/sid", "distribution_major_version": "stretch/sid",
"distribution_release": "NA", "distribution_release": "NA",
"distribution_version": "stretch/sid", "distribution_version": "stretch/sid",
} }
}, },
{ {
'name': "Debian 7.9", 'name': "Debian 7.9",
'input': {'/etc/os-release': 'PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"\nNAME="Debian GNU/Linux"' 'input': {'/etc/os-release': """PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"
'\nVERSION_ID="7"\nVERSION="7 (wheezy)"\nID=debian\nANSI_COLOR="1;31"\n' NAME="Debian GNU/Linux"
'HOME_URL="http://www.debian.org/"\nSUPPORT_URL="http://www.debian.org/support/' VERSION_ID="7"
'"\nBUG_REPORT_URL="http://bugs.debian.org/"\n'}, VERSION="7 (wheezy)"
'platform.dist': ('debian', '7.9', ''), ID=debian
'result': {'distribution': u'Debian', ANSI_COLOR="1;31"
'distribution_major_version': u'7', HOME_URL="http://www.debian.org/"
'distribution_release': u'wheezy', SUPPORT_URL="http://www.debian.org/support/"
'distribution_version': u'7.9'} BUG_REPORT_URL="http://bugs.debian.org/"
}, """},
{ 'platform.dist': ('debian', '7.9', ''),
'name': "Ubuntu 14.04", 'result': {'distribution': u'Debian',
'input': {'/etc/lsb-release': 'DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=14.04\nDISTRIB_CODENAME=trusty\nDISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"\n', 'distribution_major_version': u'7',
'/etc/os-release': 'NAME="Ubuntu"\nVERSION="14.04.4 LTS, Trusty Tahr"\nID=ubuntu\nID_LIKE=debian\nPRETTY_NAME="Ubuntu 14.04.4 LTS"\nVERSION_ID="14.04"\nHOME_URL="http://www.ubuntu.com/"\nSUPPORT_URL="http://help.ubuntu.com/"\nBUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"\n'}, 'distribution_release': u'wheezy',
'platform.dist': ('Ubuntu', '14.04', 'trusty'), 'distribution_version': u'7.9'}
'result': {'distribution': u'Ubuntu', },
'distribution_major_version': u'14', {
'distribution_release': u'trusty', 'name': "Ubuntu 14.04",
'distribution_version': u'14.04'} 'input': {'/etc/lsb-release': """DISTRIB_ID=Ubuntu
}, DISTRIB_RELEASE=14.04
{ DISTRIB_CODENAME=trusty
'name': "Ubuntu 12.04", DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"
'input': {'/etc/lsb-release': 'DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=12.04\nDISTRIB_CODENAME=precise\nDISTRIB_DESCRIPTION="Ubuntu 12.04.5 LTS"\n', """,
'/etc/os-release': 'NAME="Ubuntu"\nVERSION="12.04.5 LTS, Precise Pangolin"\nID=ubuntu\nID_LIKE=debian\nPRETTY_NAME="Ubuntu precise (12.04.5 LTS)"\nVERSION_ID="12.04"\n'}, '/etc/os-release': """NAME="Ubuntu"
'platform.dist': ('Ubuntu', '12.04', 'precise'), VERSION="14.04.4 LTS, Trusty Tahr"
'result': {'distribution': u'Ubuntu', ID=ubuntu
'distribution_major_version': u'12', ID_LIKE=debian
'distribution_release': u'precise', PRETTY_NAME="Ubuntu 14.04.4 LTS"
'distribution_version': u'12.04'} VERSION_ID="14.04"
}, HOME_URL="http://www.ubuntu.com/"
{ SUPPORT_URL="http://help.ubuntu.com/"
'name': 'Core OS', BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
'input': { """},
'/etc/os-release':""" 'platform.dist': ('Ubuntu', '14.04', 'trusty'),
'result': {'distribution': u'Ubuntu',
'distribution_major_version': u'14',
'distribution_release': u'trusty',
'distribution_version': u'14.04'}
},
{
'name': "Ubuntu 12.04",
'input': {'/etc/lsb-release': """DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.5 LTS"
""",
'/etc/os-release': """NAME="Ubuntu"
VERSION="12.04.5 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.5 LTS)"
VERSION_ID="12.04"
"""},
'platform.dist': ('Ubuntu', '12.04', 'precise'),
'result': {'distribution': u'Ubuntu',
'distribution_major_version': u'12',
'distribution_release': u'precise',
'distribution_version': u'12.04'}
},
{
'name': 'Core OS',
'input': {
'/etc/os-release':"""
NAME=CoreOS NAME=CoreOS
ID=coreos ID=coreos
VERSION=976.0.0 VERSION=976.0.0
@ -278,51 +289,104 @@ PRETTY_NAME="CoreOS 976.0.0 (Coeur Rouge)"
ANSI_COLOR="1;32" ANSI_COLOR="1;32"
HOME_URL="https://coreos.com/" HOME_URL="https://coreos.com/"
BUG_REPORT_URL="https://github.com/coreos/bugs/issues" BUG_REPORT_URL="https://github.com/coreos/bugs/issues"
""", """,
'/etc/lsb-release':'DISTRIB_ID=CoreOS\nDISTRIB_RELEASE=976.0.0\nDISTRIB_CODENAME="Coeur Rouge"\nDISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)"\n', '/etc/lsb-release':"""DISTRIB_ID=CoreOS
}, DISTRIB_RELEASE=976.0.0
'platform.dist': ('', '', ''), DISTRIB_CODENAME="Coeur Rouge"
'result' : { DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)"
"distribution": "CoreOS", """,
"distribution_major_version": "NA", },
"distribution_release": "NA", 'platform.dist': ('', '', ''),
"distribution_version": "976.0.0", 'result' : {
} "distribution": "CoreOS",
} "distribution_major_version": "NA",
"distribution_release": "NA",
"distribution_version": "976.0.0",
}
}
] ]
for t in testsets: @unittest.skipIf(sys.version_info[0] >= 3, "Python 3 is not supported on targets (yet)")
def test_distribution_version():
"""tests the distribution parsing code of the Facts class
def mock_get_file_content(fname, default=None, strip=True): testsets have
data = default * a name (for output/debugging only)
if fname in t['input']: * input files that are faked
# for debugging * those should be complete and also include "irrelevant" files that might be mistaken as coming from other distributions
print('faked '+fname+' for '+t['name']) * all files that are not listed here are assumed to not exist at all
data = t['input'][fname].strip() * the output of pythons platform.dist()
if strip and data is not None: * results for the ansible variables distribution*
data = data.strip() """
return data
def mock_path_exists(fname): # needs to be in here, because the import fails with python3 still
return fname in t['input'] import ansible.module_utils.facts
def mock_path_getsize(fname): real_stdin = sys.stdin
if fname in t['input']: from ansible.module_utils import basic
return len(t['input'][fname])
else: args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}, ANSIBLE_MODULE_CONSTANTS={}))
return 0 if PY3:
sys.stdin = StringIO(args)
sys.stdin.buffer = BytesIO(to_bytes(args))
else:
sys.stdin = BytesIO(to_bytes(args))
module = basic.AnsibleModule(argument_spec=dict())
for t in TESTSETS:
# run individual tests via generator
# set nicer stdout output for nosetest
_test_one_distribution.description = "check distribution_version for %s" % t['name']
yield _test_one_distribution, module, t
with patch('ansible.module_utils.facts.get_file_content', side_effect=mock_get_file_content): sys.stdin = real_stdin
with patch('os.path.exists', side_effect=mock_path_exists):
with patch('os.path.getsize', side_effect=mock_path_getsize):
with patch('platform.dist', return_value=t['platform.dist']): def _test_one_distribution(module, testcase):
with patch('platform.system', return_value='Linux'): """run the test on one distribution testcase
generated_facts = ansible.module_utils.facts.Facts(self.module).populate()
for key, val in t['result'].items(): * prepare some mock functions to get the testdata in
self.assertIn(key, generated_facts) * run Facts()
msg = 'Comparing value of %s on %s, should: %s, is: %s' %\ * compare with the expected output
(key, t['name'], val, generated_facts[key]) """
self.assertEqual(generated_facts[key], val, msg)
def mock_get_file_content(fname, default=None, strip=True):
"""give fake content if it exists, otherwise pretend the file is empty"""
data = default
if fname in testcase['input']:
# for debugging
print('faked '+fname+' for '+testcase['name'])
data = testcase['input'][fname].strip()
if strip and data is not None:
data = data.strip()
return data
def mock_path_exists(fname):
return fname in testcase['input']
def mock_path_getsize(fname):
if fname in testcase['input']:
# the len is not used, but why not be honest if you can be?
return len(testcase['input'][fname])
else:
return 0
@patch('ansible.module_utils.facts.get_file_content', mock_get_file_content)
@patch('os.path.exists', mock_path_exists)
@patch('os.path.getsize', mock_path_getsize)
@patch('platform.dist', lambda: testcase['platform.dist'])
@patch('platform.system', lambda: 'Linux')
def get_facts(testcase):
return ansible.module_utils.facts.Facts(module).populate()
generated_facts = get_facts(testcase)
# testcase['result'] has a list of variables and values it expects Facts() to set
for key, val in testcase['result'].items():
assert_in(key, generated_facts)
msg = 'Comparing value of %s on %s, should: %s, is: %s' %\
(key, testcase['name'], val, generated_facts[key])
assert_equal(generated_facts[key], val, msg)