|
|
|
@ -1,84 +1,96 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
|
# (c) 2016 Toshio Kuratomi <tkuratomi@ansible.com>
|
|
|
|
|
# (c) 2017 Ansible Project
|
|
|
|
|
# (c) 2017-2018 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.mock.procenv import ModuleTestCase
|
|
|
|
|
|
|
|
|
|
from units.compat.mock import patch
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.six.moves import builtins
|
|
|
|
|
|
|
|
|
|
# Functions being tested
|
|
|
|
|
from ansible.module_utils.common.sys_info import get_all_subclasses
|
|
|
|
|
from ansible.module_utils.common.sys_info import get_distribution
|
|
|
|
|
from ansible.module_utils.common.sys_info import get_distribution_version
|
|
|
|
|
from ansible.module_utils.common.sys_info import get_platform
|
|
|
|
|
from ansible.module_utils.common.sys_info import load_platform_subclass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
realimport = builtins.__import__
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPlatform(ModuleTestCase):
|
|
|
|
|
def test_module_utils_basic_get_platform(self):
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def platform_linux(mocker):
|
|
|
|
|
mocker.patch('platform.system', return_value='Linux')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# get_platform tests
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
def test_get_platform():
|
|
|
|
|
with patch('platform.system', return_value='foo'):
|
|
|
|
|
from ansible.module_utils.common.sys_info import get_platform
|
|
|
|
|
self.assertEqual(get_platform(), 'foo')
|
|
|
|
|
assert get_platform() == 'foo'
|
|
|
|
|
|
|
|
|
|
def test_module_utils_basic_get_distribution(self):
|
|
|
|
|
from ansible.module_utils.common.sys_info import get_distribution
|
|
|
|
|
|
|
|
|
|
with patch('platform.system', return_value='Foo'):
|
|
|
|
|
self.assertEqual(get_distribution(), None)
|
|
|
|
|
#
|
|
|
|
|
# get_distribution tests
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
with patch('platform.system', return_value='Linux'):
|
|
|
|
|
with patch('platform.linux_distribution', return_value=["foo"]):
|
|
|
|
|
self.assertEqual(get_distribution(), "Foo")
|
|
|
|
|
def test_get_distribution_not_linux():
|
|
|
|
|
"""If it's not Linux, then it has no distribution"""
|
|
|
|
|
with patch('platform.system', return_value='Foo'):
|
|
|
|
|
assert get_distribution() is None
|
|
|
|
|
|
|
|
|
|
with patch('os.path.isfile', return_value=True):
|
|
|
|
|
with patch('platform.linux_distribution', side_effect=[("AmazonFooBar", )]):
|
|
|
|
|
self.assertEqual(get_distribution(), "Amazonfoobar")
|
|
|
|
|
|
|
|
|
|
with patch('platform.linux_distribution', side_effect=(("", ), ("AmazonFooBam",))):
|
|
|
|
|
self.assertEqual(get_distribution(), "Amazon")
|
|
|
|
|
@pytest.mark.usefixtures("platform_linux")
|
|
|
|
|
class TestGetDistribution:
|
|
|
|
|
""" Tests for get_distribution that have to find somethine"""
|
|
|
|
|
def test_distro_known(self):
|
|
|
|
|
with patch('ansible.module_utils.distro.name', return_value="foo"):
|
|
|
|
|
assert get_distribution() == "Foo"
|
|
|
|
|
|
|
|
|
|
with patch('platform.linux_distribution', side_effect=[("", ), ("", )]):
|
|
|
|
|
self.assertEqual(get_distribution(), "OtherLinux")
|
|
|
|
|
def test_distro_unknown(self):
|
|
|
|
|
with patch('ansible.module_utils.distro.name', return_value=""):
|
|
|
|
|
assert get_distribution() == "OtherLinux"
|
|
|
|
|
|
|
|
|
|
def _dist(distname='', version='', id='', supported_dists=(), full_distribution_name=1):
|
|
|
|
|
if supported_dists != ():
|
|
|
|
|
return ("Bar", "2", "Two")
|
|
|
|
|
else:
|
|
|
|
|
return ("", "", "")
|
|
|
|
|
def test_distro_amazon_part_of_another_name(self):
|
|
|
|
|
with patch('ansible.module_utils.distro.name', return_value="AmazonFooBar"):
|
|
|
|
|
assert get_distribution() == "Amazonfoobar"
|
|
|
|
|
|
|
|
|
|
with patch('platform.linux_distribution', side_effect=_dist):
|
|
|
|
|
self.assertEqual(get_distribution(), "Bar")
|
|
|
|
|
def test_distro_amazon_linux(self):
|
|
|
|
|
with patch('ansible.module_utils.distro.name', return_value="Amazon Linux AMI"):
|
|
|
|
|
assert get_distribution() == "Amazon"
|
|
|
|
|
|
|
|
|
|
with patch('platform.linux_distribution', side_effect=Exception("boo")):
|
|
|
|
|
with patch('platform.dist', return_value=("bar", "2", "Two")):
|
|
|
|
|
self.assertEqual(get_distribution(), "Bar")
|
|
|
|
|
|
|
|
|
|
def test_module_utils_basic_get_distribution_version(self):
|
|
|
|
|
from ansible.module_utils.common.sys_info import get_distribution_version
|
|
|
|
|
#
|
|
|
|
|
# get_distribution_version tests
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
def test_get_distribution_version_not_linux():
|
|
|
|
|
"""If it's not Linux, then it has no distribution"""
|
|
|
|
|
with patch('platform.system', return_value='Foo'):
|
|
|
|
|
self.assertEqual(get_distribution_version(), None)
|
|
|
|
|
assert get_distribution_version() is None
|
|
|
|
|
|
|
|
|
|
with patch('platform.system', return_value='Linux'):
|
|
|
|
|
with patch('platform.linux_distribution', return_value=("foo", "1", "One")):
|
|
|
|
|
self.assertEqual(get_distribution_version(), "1")
|
|
|
|
|
|
|
|
|
|
with patch('os.path.isfile', return_value=True):
|
|
|
|
|
def _dist(distname='', version='', id='', supported_dists=(), full_distribution_name=1):
|
|
|
|
|
if supported_dists != ():
|
|
|
|
|
return ("AmazonFooBar", "2", "")
|
|
|
|
|
else:
|
|
|
|
|
return ("", "", "")
|
|
|
|
|
@pytest.mark.usefixtures("platform_linux")
|
|
|
|
|
def test_distro_found():
|
|
|
|
|
with patch('ansible.module_utils.distro.version', return_value="1"):
|
|
|
|
|
assert get_distribution_version() == "1"
|
|
|
|
|
|
|
|
|
|
with patch('platform.linux_distribution', side_effect=_dist):
|
|
|
|
|
self.assertEqual(get_distribution_version(), "2")
|
|
|
|
|
|
|
|
|
|
with patch('platform.linux_distribution', side_effect=Exception("boo")):
|
|
|
|
|
with patch('platform.dist', return_value=("bar", "3", "Three")):
|
|
|
|
|
self.assertEqual(get_distribution_version(), "3")
|
|
|
|
|
#
|
|
|
|
|
# Tests for LoadPlatformSubclass
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
def test_module_utils_basic_load_platform_subclass(self):
|
|
|
|
|
class TestLoadPlatformSubclass:
|
|
|
|
|
class LinuxTest:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
@ -90,19 +102,58 @@ class TestPlatform(ModuleTestCase):
|
|
|
|
|
platform = "Linux"
|
|
|
|
|
distribution = "Bar"
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.common.sys_info import load_platform_subclass
|
|
|
|
|
def test_not_linux(self):
|
|
|
|
|
# if neither match, the fallback should be the top-level class
|
|
|
|
|
with patch('ansible.module_utils.common.sys_info.get_platform', return_value="Foo"):
|
|
|
|
|
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value=None):
|
|
|
|
|
assert isinstance(load_platform_subclass(self.LinuxTest), self.LinuxTest)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("platform_linux")
|
|
|
|
|
def test_get_distribution_none(self):
|
|
|
|
|
# match just the platform class, not a specific distribution
|
|
|
|
|
with patch('ansible.module_utils.common.sys_info.get_platform', return_value="Linux"):
|
|
|
|
|
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value=None):
|
|
|
|
|
self.assertIs(type(load_platform_subclass(LinuxTest)), Foo)
|
|
|
|
|
assert isinstance(load_platform_subclass(self.LinuxTest), self.Foo)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("platform_linux")
|
|
|
|
|
def test_get_distribution_found(self):
|
|
|
|
|
# match both the distribution and platform class
|
|
|
|
|
with patch('ansible.module_utils.common.sys_info.get_platform', return_value="Linux"):
|
|
|
|
|
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value="Bar"):
|
|
|
|
|
self.assertIs(type(load_platform_subclass(LinuxTest)), Bar)
|
|
|
|
|
assert isinstance(load_platform_subclass(self.LinuxTest), self.Bar)
|
|
|
|
|
|
|
|
|
|
# if neither match, the fallback should be the top-level class
|
|
|
|
|
with patch('ansible.module_utils.common.sys_info.get_platform', return_value="Foo"):
|
|
|
|
|
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value=None):
|
|
|
|
|
self.assertIs(type(load_platform_subclass(LinuxTest)), LinuxTest)
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Tests for get_all_subclasses
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
class TestGetAllSubclasses:
|
|
|
|
|
class Base:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class BranchI(Base):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class BranchII(Base):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class BranchIA(BranchI):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class BranchIB(BranchI):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class BranchIIA(BranchII):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class BranchIIB(BranchII):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_bottom_level(self):
|
|
|
|
|
assert get_all_subclasses(self.BranchIIB) == []
|
|
|
|
|
|
|
|
|
|
def test_one_inheritance(self):
|
|
|
|
|
assert set(get_all_subclasses(self.BranchII)) == set([self.BranchIIA, self.BranchIIB])
|
|
|
|
|
|
|
|
|
|
def test_toplevel(self):
|
|
|
|
|
assert set(get_all_subclasses(self.Base)) == set([self.BranchI, self.BranchII,
|
|
|
|
|
self.BranchIA, self.BranchIB,
|
|
|
|
|
self.BranchIIA, self.BranchIIB])
|
|
|
|
|