| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 3 |
# (c) 2016 Toshio Kuratomi <[email protected]> |
| 4 |
# (c) 2017-2018 Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from unittest.mock import patch |
| 12 |
|
| 13 |
import builtins |
| 14 |
|
| 15 |
# Functions being tested |
| 16 |
from ansible.module_utils.basic import get_platform |
| 17 |
from ansible.module_utils.basic import get_all_subclasses |
| 18 |
from ansible.module_utils.basic import get_distribution |
| 19 |
from ansible.module_utils.basic import get_distribution_version |
| 20 |
from ansible.module_utils.basic import load_platform_subclass |
| 21 |
|
| 22 |
|
| 23 |
realimport = builtins.__import__ |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture |
| 27 |
def platform_linux(mocker): |
| 28 |
mocker.patch('platform.system', return_value='Linux') |
| 29 |
|
| 30 |
|
| 31 |
# |
| 32 |
# get_platform tests |
| 33 |
# |
| 34 |
|
| 35 |
def test_get_platform(): |
| 36 |
with patch('platform.system', return_value='foo'): |
| 37 |
assert get_platform() == 'foo' |
| 38 |
|
| 39 |
|
| 40 |
# |
| 41 |
# get_distribution tests |
| 42 |
# |
| 43 |
|
| 44 |
@pytest.mark.usefixtures("platform_linux") |
| 45 |
class TestGetDistribution: |
| 46 |
"""Tests for get_distribution that have to find something""" |
| 47 |
def test_distro_known(self): |
| 48 |
with patch('ansible.module_utils.distro.id', return_value="alpine"): |
| 49 |
assert get_distribution() == "Alpine" |
| 50 |
|
| 51 |
with patch('ansible.module_utils.distro.id', return_value="arch"): |
| 52 |
assert get_distribution() == "Arch" |
| 53 |
|
| 54 |
with patch('ansible.module_utils.distro.id', return_value="centos"): |
| 55 |
assert get_distribution() == "Centos" |
| 56 |
|
| 57 |
with patch('ansible.module_utils.distro.id', return_value="clear-linux-os"): |
| 58 |
assert get_distribution() == "Clear-linux-os" |
| 59 |
|
| 60 |
with patch('ansible.module_utils.distro.id', return_value="coreos"): |
| 61 |
assert get_distribution() == "Coreos" |
| 62 |
|
| 63 |
with patch('ansible.module_utils.distro.id', return_value="debian"): |
| 64 |
assert get_distribution() == "Debian" |
| 65 |
|
| 66 |
with patch('ansible.module_utils.distro.id', return_value="flatcar"): |
| 67 |
assert get_distribution() == "Flatcar" |
| 68 |
|
| 69 |
with patch('ansible.module_utils.distro.id', return_value="linuxmint"): |
| 70 |
assert get_distribution() == "Linuxmint" |
| 71 |
|
| 72 |
with patch('ansible.module_utils.distro.id', return_value="opensuse"): |
| 73 |
assert get_distribution() == "Opensuse" |
| 74 |
|
| 75 |
with patch('ansible.module_utils.distro.id', return_value="oracle"): |
| 76 |
assert get_distribution() == "Oracle" |
| 77 |
|
| 78 |
with patch('ansible.module_utils.distro.id', return_value="raspian"): |
| 79 |
assert get_distribution() == "Raspian" |
| 80 |
|
| 81 |
with patch('ansible.module_utils.distro.id', return_value="rhel"): |
| 82 |
assert get_distribution() == "Redhat" |
| 83 |
|
| 84 |
with patch('ansible.module_utils.distro.id', return_value="ubuntu"): |
| 85 |
assert get_distribution() == "Ubuntu" |
| 86 |
|
| 87 |
with patch('ansible.module_utils.distro.id', return_value="virtuozzo"): |
| 88 |
assert get_distribution() == "Virtuozzo" |
| 89 |
|
| 90 |
with patch('ansible.module_utils.distro.id', return_value="foo"): |
| 91 |
assert get_distribution() == "Foo" |
| 92 |
|
| 93 |
def test_distro_unknown(self): |
| 94 |
with patch('ansible.module_utils.distro.id', return_value=""): |
| 95 |
assert get_distribution() == "OtherLinux" |
| 96 |
|
| 97 |
def test_distro_amazon_linux_short(self): |
| 98 |
with patch('ansible.module_utils.distro.id', return_value="amzn"): |
| 99 |
assert get_distribution() == "Amazon" |
| 100 |
|
| 101 |
def test_distro_amazon_linux_long(self): |
| 102 |
with patch('ansible.module_utils.distro.id', return_value="amazon"): |
| 103 |
assert get_distribution() == "Amazon" |
| 104 |
|
| 105 |
|
| 106 |
# |
| 107 |
# get_distribution_version tests |
| 108 |
# |
| 109 |
|
| 110 |
|
| 111 |
@pytest.mark.usefixtures("platform_linux") |
| 112 |
def test_distro_found(): |
| 113 |
with patch('ansible.module_utils.distro.version', return_value="1"): |
| 114 |
assert get_distribution_version() == "1" |
| 115 |
|
| 116 |
|
| 117 |
# |
| 118 |
# Tests for LoadPlatformSubclass |
| 119 |
# |
| 120 |
|
| 121 |
class TestLoadPlatformSubclass: |
| 122 |
class LinuxTest: |
| 123 |
pass |
| 124 |
|
| 125 |
class Foo(LinuxTest): |
| 126 |
platform = "Linux" |
| 127 |
distribution = None |
| 128 |
|
| 129 |
class Bar(LinuxTest): |
| 130 |
platform = "Linux" |
| 131 |
distribution = "Bar" |
| 132 |
|
| 133 |
def test_not_linux(self): |
| 134 |
# if neither match, the fallback should be the top-level class |
| 135 |
with patch('platform.system', return_value="Foo"): |
| 136 |
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value=None): |
| 137 |
assert isinstance(load_platform_subclass(self.LinuxTest), self.LinuxTest) |
| 138 |
|
| 139 |
@pytest.mark.usefixtures("platform_linux") |
| 140 |
def test_get_distribution_none(self): |
| 141 |
# match just the platform class, not a specific distribution |
| 142 |
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value=None): |
| 143 |
assert isinstance(load_platform_subclass(self.LinuxTest), self.Foo) |
| 144 |
|
| 145 |
@pytest.mark.usefixtures("platform_linux") |
| 146 |
def test_get_distribution_found(self): |
| 147 |
# match both the distribution and platform class |
| 148 |
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value="Bar"): |
| 149 |
assert isinstance(load_platform_subclass(self.LinuxTest), self.Bar) |
| 150 |
|
| 151 |
|
| 152 |
# |
| 153 |
# Tests for get_all_subclasses |
| 154 |
# |
| 155 |
|
| 156 |
class TestGetAllSubclasses: |
| 157 |
class Base: |
| 158 |
pass |
| 159 |
|
| 160 |
class BranchI(Base): |
| 161 |
pass |
| 162 |
|
| 163 |
class BranchII(Base): |
| 164 |
pass |
| 165 |
|
| 166 |
class BranchIA(BranchI): |
| 167 |
pass |
| 168 |
|
| 169 |
class BranchIB(BranchI): |
| 170 |
pass |
| 171 |
|
| 172 |
class BranchIIA(BranchII): |
| 173 |
pass |
| 174 |
|
| 175 |
class BranchIIB(BranchII): |
| 176 |
pass |
| 177 |
|
| 178 |
def test_bottom_level(self): |
| 179 |
assert get_all_subclasses(self.BranchIIB) == [] |
| 180 |
|
| 181 |
def test_one_inheritance(self): |
| 182 |
assert set(get_all_subclasses(self.BranchII)) == set([self.BranchIIA, self.BranchIIB]) |
| 183 |
|
| 184 |
def test_toplevel(self): |
| 185 |
assert set(get_all_subclasses(self.Base)) == set([self.BranchI, self.BranchII, |
| 186 |
self.BranchIA, self.BranchIB, |
| 187 |
self.BranchIIA, self.BranchIIB]) |