Port from plaform.dist to ansible.module_utils.distro.linux_distribution

ci_complete
This commit is contained in:
Toshio Kuratomi 2018-12-10 17:04:14 -08:00
commit 61b1daa65f
10 changed files with 248 additions and 114 deletions

View file

@ -7,8 +7,11 @@ __metaclass__ = type
from itertools import product
import mock
import pytest
from ansible.module_utils.six.moves import builtins
# the module we are actually testing (sort of)
from ansible.module_utils.facts.system.distribution import DistributionFactCollector
@ -1044,7 +1047,7 @@ def test_distribution_version(am, mocker, testcase):
* input files that are faked
* those should be complete and also include "irrelevant" files that might be mistaken as coming from other distributions
* all files that are not listed here are assumed to not exist at all
* the output of pythons platform.dist()
* the output of ansible.module_utils.distro.linux_distribution() [called platform.dist() for historical reasons]
* results for the ansible variables distribution* and os_family
"""
@ -1081,14 +1084,43 @@ def test_distribution_version(am, mocker, testcase):
def mock_platform_version():
return testcase.get('platform.version', '')
def mock_distro_name():
return testcase['platform.dist'][0]
def mock_distro_version():
return testcase['platform.dist'][1]
def mock_distro_codename():
return testcase['platform.dist'][2]
def mock_open(filename, mode='r'):
if filename in testcase['input']:
file_object = mocker.mock_open(read_data=testcase['input'][filename]).return_value
file_object.__iter__.return_value = testcase['input'][filename].splitlines(True)
else:
file_object = real_open(filename, mode)
return file_object
def mock_os_path_is_file(filename):
if filename in testcase['input']:
return True
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._file_exists', mock_file_exists)
mocker.patch('platform.dist', lambda: testcase['platform.dist'])
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.version', mock_distro_version)
mocker.patch('ansible.module_utils.distro.codename', mock_distro_codename)
mocker.patch('os.path.isfile', mock_os_path_is_file)
mocker.patch('platform.system', mock_platform_system)
mocker.patch('platform.release', mock_platform_release)
mocker.patch('platform.version', mock_platform_version)
real_open = builtins.open
mocker.patch.object(builtins, 'open', new=mock_open)
# run Facts()
distro_collector = DistributionFactCollector()
generated_facts = distro_collector.collect(am)