2017-12-18 18:17:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
# (c) 2016 Toshio Kuratomi <tkuratomi@ansible.com>
|
2018-12-11 01:04:14 +00:00
|
|
|
# (c) 2017-2018 Ansible Project
|
2017-12-18 18:17:13 +00:00
|
|
|
# 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
|
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-12-18 18:17:13 +00:00
|
|
|
from units.mock.procenv import ModuleTestCase
|
|
|
|
|
2018-10-13 03:01:14 +00:00
|
|
|
from units.compat.mock import patch
|
2018-12-11 01:04:14 +00:00
|
|
|
|
2017-12-18 18:17:13 +00:00
|
|
|
from ansible.module_utils.six.moves import builtins
|
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
# Functions being tested
|
2018-12-31 23:10:51 +00:00
|
|
|
from ansible.module_utils.basic import get_platform
|
|
|
|
from ansible.module_utils.basic import get_all_subclasses
|
|
|
|
from ansible.module_utils.basic import get_distribution
|
|
|
|
from ansible.module_utils.basic import get_distribution_version
|
|
|
|
from ansible.module_utils.basic import load_platform_subclass
|
2018-12-11 01:04:14 +00:00
|
|
|
|
|
|
|
|
2017-12-18 18:17:13 +00:00
|
|
|
realimport = builtins.__import__
|
|
|
|
|
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def platform_linux(mocker):
|
|
|
|
mocker.patch('platform.system', return_value='Linux')
|
2017-12-18 18:17:13 +00:00
|
|
|
|
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
#
|
|
|
|
# get_platform tests
|
|
|
|
#
|
2017-12-18 18:17:13 +00:00
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
def test_get_platform():
|
|
|
|
with patch('platform.system', return_value='foo'):
|
|
|
|
assert get_platform() == 'foo'
|
2017-12-18 18:17:13 +00:00
|
|
|
|
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
#
|
|
|
|
# get_distribution tests
|
|
|
|
#
|
2017-12-18 18:17:13 +00:00
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
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
|
2017-12-18 18:17:13 +00:00
|
|
|
|
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
@pytest.mark.usefixtures("platform_linux")
|
|
|
|
class TestGetDistribution:
|
2019-03-07 18:25:59 +00:00
|
|
|
"""Tests for get_distribution that have to find something"""
|
2018-12-11 01:04:14 +00:00
|
|
|
def test_distro_known(self):
|
2019-03-07 18:25:59 +00:00
|
|
|
with patch('ansible.module_utils.distro.id', return_value="alpine"):
|
|
|
|
assert get_distribution() == "Alpine"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="arch"):
|
|
|
|
assert get_distribution() == "Arch"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="centos"):
|
|
|
|
assert get_distribution() == "Centos"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="clear-linux-os"):
|
|
|
|
assert get_distribution() == "Clear-linux-os"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="coreos"):
|
|
|
|
assert get_distribution() == "Coreos"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="debian"):
|
|
|
|
assert get_distribution() == "Debian"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="linuxmint"):
|
|
|
|
assert get_distribution() == "Linuxmint"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="opensuse"):
|
|
|
|
assert get_distribution() == "Opensuse"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="oracle"):
|
|
|
|
assert get_distribution() == "Oracle"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="raspian"):
|
|
|
|
assert get_distribution() == "Raspian"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="rhel"):
|
|
|
|
assert get_distribution() == "Redhat"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="ubuntu"):
|
|
|
|
assert get_distribution() == "Ubuntu"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="virtuozzo"):
|
|
|
|
assert get_distribution() == "Virtuozzo"
|
|
|
|
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="foo"):
|
2018-12-11 01:04:14 +00:00
|
|
|
assert get_distribution() == "Foo"
|
2017-12-18 18:17:13 +00:00
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
def test_distro_unknown(self):
|
2019-03-07 18:25:59 +00:00
|
|
|
with patch('ansible.module_utils.distro.id', return_value=""):
|
2018-12-11 01:04:14 +00:00
|
|
|
assert get_distribution() == "OtherLinux"
|
2017-12-18 18:17:13 +00:00
|
|
|
|
2019-03-07 18:25:59 +00:00
|
|
|
def test_distro_amazon_linux_short(self):
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="amzn"):
|
|
|
|
assert get_distribution() == "Amazon"
|
2017-12-18 18:17:13 +00:00
|
|
|
|
2019-03-07 18:25:59 +00:00
|
|
|
def test_distro_amazon_linux_long(self):
|
|
|
|
with patch('ansible.module_utils.distro.id', return_value="amazon"):
|
2018-12-11 01:04:14 +00:00
|
|
|
assert get_distribution() == "Amazon"
|
2017-12-18 18:17:13 +00:00
|
|
|
|
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
#
|
|
|
|
# get_distribution_version tests
|
|
|
|
#
|
2017-12-18 18:17:13 +00:00
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
def test_get_distribution_version_not_linux():
|
|
|
|
"""If it's not Linux, then it has no distribution"""
|
|
|
|
with patch('platform.system', return_value='Foo'):
|
|
|
|
assert get_distribution_version() is None
|
2017-12-18 18:17:13 +00:00
|
|
|
|
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
@pytest.mark.usefixtures("platform_linux")
|
|
|
|
def test_distro_found():
|
|
|
|
with patch('ansible.module_utils.distro.version', return_value="1"):
|
|
|
|
assert get_distribution_version() == "1"
|
2017-12-18 18:17:13 +00:00
|
|
|
|
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
#
|
|
|
|
# Tests for LoadPlatformSubclass
|
|
|
|
#
|
2017-12-18 18:17:13 +00:00
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
class TestLoadPlatformSubclass:
|
|
|
|
class LinuxTest:
|
|
|
|
pass
|
2017-12-18 18:17:13 +00:00
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
class Foo(LinuxTest):
|
|
|
|
platform = "Linux"
|
|
|
|
distribution = None
|
2017-12-18 18:17:13 +00:00
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
class Bar(LinuxTest):
|
|
|
|
platform = "Linux"
|
|
|
|
distribution = "Bar"
|
2017-12-18 18:17:13 +00:00
|
|
|
|
2018-12-11 01:04:14 +00:00
|
|
|
def test_not_linux(self):
|
2017-12-18 18:17:13 +00:00
|
|
|
# if neither match, the fallback should be the top-level class
|
2018-12-31 23:10:51 +00:00
|
|
|
with patch('platform.system', return_value="Foo"):
|
2018-12-07 18:21:11 +00:00
|
|
|
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value=None):
|
2018-12-11 01:04:14 +00:00
|
|
|
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_distribution', return_value=None):
|
|
|
|
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_distribution', return_value="Bar"):
|
|
|
|
assert isinstance(load_platform_subclass(self.LinuxTest), self.Bar)
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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])
|