| 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.common.sys_info import get_distribution |
| 17 |
from ansible.module_utils.common.sys_info import get_distribution_version |
| 18 |
from ansible.module_utils.common.sys_info import get_platform_subclass |
| 19 |
|
| 20 |
|
| 21 |
realimport = builtins.__import__ |
| 22 |
|
| 23 |
|
| 24 |
@pytest.fixture |
| 25 |
def platform_linux(mocker): |
| 26 |
mocker.patch('platform.system', return_value='Linux') |
| 27 |
|
| 28 |
|
| 29 |
# |
| 30 |
# get_distribution tests |
| 31 |
# |
| 32 |
|
| 33 |
@pytest.mark.parametrize( |
| 34 |
('system', 'dist'), |
| 35 |
( |
| 36 |
('Darwin', 'Darwin'), |
| 37 |
('SunOS', 'Solaris'), |
| 38 |
('FreeBSD', 'Freebsd'), |
| 39 |
), |
| 40 |
) |
| 41 |
def test_get_distribution_not_linux(system, dist, mocker): |
| 42 |
"""For platforms other than Linux, return the distribution""" |
| 43 |
mocker.patch('platform.system', return_value=system) |
| 44 |
mocker.patch('ansible.module_utils.common.sys_info.distro.id', return_value=dist) |
| 45 |
assert get_distribution() == dist |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.usefixtures("platform_linux") |
| 49 |
class TestGetDistribution: |
| 50 |
"""Tests for get_distribution that have to find something""" |
| 51 |
def test_distro_known(self): |
| 52 |
with patch('ansible.module_utils.distro.id', return_value="alpine"): |
| 53 |
assert get_distribution() == "Alpine" |
| 54 |
|
| 55 |
with patch('ansible.module_utils.distro.id', return_value="arch"): |
| 56 |
assert get_distribution() == "Arch" |
| 57 |
|
| 58 |
with patch('ansible.module_utils.distro.id', return_value="centos"): |
| 59 |
assert get_distribution() == "Centos" |
| 60 |
|
| 61 |
with patch('ansible.module_utils.distro.id', return_value="clear-linux-os"): |
| 62 |
assert get_distribution() == "Clear-linux-os" |
| 63 |
|
| 64 |
with patch('ansible.module_utils.distro.id', return_value="coreos"): |
| 65 |
assert get_distribution() == "Coreos" |
| 66 |
|
| 67 |
with patch('ansible.module_utils.distro.id', return_value="debian"): |
| 68 |
assert get_distribution() == "Debian" |
| 69 |
|
| 70 |
with patch('ansible.module_utils.distro.id', return_value="flatcar"): |
| 71 |
assert get_distribution() == "Flatcar" |
| 72 |
|
| 73 |
with patch('ansible.module_utils.distro.id', return_value="linuxmint"): |
| 74 |
assert get_distribution() == "Linuxmint" |
| 75 |
|
| 76 |
with patch('ansible.module_utils.distro.id', return_value="opensuse"): |
| 77 |
assert get_distribution() == "Opensuse" |
| 78 |
|
| 79 |
with patch('ansible.module_utils.distro.id', return_value="oracle"): |
| 80 |
assert get_distribution() == "Oracle" |
| 81 |
|
| 82 |
with patch('ansible.module_utils.distro.id', return_value="raspian"): |
| 83 |
assert get_distribution() == "Raspian" |
| 84 |
|
| 85 |
with patch('ansible.module_utils.distro.id', return_value="rhel"): |
| 86 |
assert get_distribution() == "Redhat" |
| 87 |
|
| 88 |
with patch('ansible.module_utils.distro.id', return_value="ubuntu"): |
| 89 |
assert get_distribution() == "Ubuntu" |
| 90 |
|
| 91 |
with patch('ansible.module_utils.distro.id', return_value="virtuozzo"): |
| 92 |
assert get_distribution() == "Virtuozzo" |
| 93 |
|
| 94 |
with patch('ansible.module_utils.distro.id', return_value="foo"): |
| 95 |
assert get_distribution() == "Foo" |
| 96 |
|
| 97 |
def test_distro_unknown(self): |
| 98 |
with patch('ansible.module_utils.distro.id', return_value=""): |
| 99 |
assert get_distribution() == "OtherLinux" |
| 100 |
|
| 101 |
def test_distro_amazon_linux_short(self): |
| 102 |
with patch('ansible.module_utils.distro.id', return_value="amzn"): |
| 103 |
assert get_distribution() == "Amazon" |
| 104 |
|
| 105 |
def test_distro_amazon_linux_long(self): |
| 106 |
with patch('ansible.module_utils.distro.id', return_value="amazon"): |
| 107 |
assert get_distribution() == "Amazon" |
| 108 |
|
| 109 |
|
| 110 |
# |
| 111 |
# get_distribution_version tests |
| 112 |
# |
| 113 |
|
| 114 |
@pytest.mark.parametrize( |
| 115 |
('system', 'version'), |
| 116 |
( |
| 117 |
('Darwin', '19.6.0'), |
| 118 |
('SunOS', '11.4'), |
| 119 |
('FreeBSD', '12.1'), |
| 120 |
), |
| 121 |
) |
| 122 |
def test_get_distribution_version_not_linux(mocker, system, version): |
| 123 |
"""If it's not Linux, then it has no distribution""" |
| 124 |
mocker.patch('platform.system', return_value=system) |
| 125 |
mocker.patch('ansible.module_utils.common.sys_info.distro.version', return_value=version) |
| 126 |
assert get_distribution_version() == version |
| 127 |
|
| 128 |
|
| 129 |
@pytest.mark.usefixtures("platform_linux") |
| 130 |
def test_distro_found(): |
| 131 |
with patch('ansible.module_utils.distro.version', return_value="1"): |
| 132 |
assert get_distribution_version() == "1" |
| 133 |
|
| 134 |
|
| 135 |
# |
| 136 |
# Tests for get_platform_subclass |
| 137 |
# |
| 138 |
|
| 139 |
class TestGetPlatformSubclass: |
| 140 |
class LinuxTest: |
| 141 |
pass |
| 142 |
|
| 143 |
class Foo(LinuxTest): |
| 144 |
platform = "Linux" |
| 145 |
distribution = None |
| 146 |
|
| 147 |
class Bar(LinuxTest): |
| 148 |
platform = "Linux" |
| 149 |
distribution = "Bar" |
| 150 |
|
| 151 |
def test_not_linux(self): |
| 152 |
# if neither match, the fallback should be the top-level class |
| 153 |
with patch('platform.system', return_value="Foo"): |
| 154 |
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value=None): |
| 155 |
assert get_platform_subclass(self.LinuxTest) is self.LinuxTest |
| 156 |
|
| 157 |
@pytest.mark.usefixtures("platform_linux") |
| 158 |
def test_get_distribution_none(self): |
| 159 |
# match just the platform class, not a specific distribution |
| 160 |
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value=None): |
| 161 |
assert get_platform_subclass(self.LinuxTest) is self.Foo |
| 162 |
|
| 163 |
@pytest.mark.usefixtures("platform_linux") |
| 164 |
def test_get_distribution_found(self): |
| 165 |
# match both the distribution and platform class |
| 166 |
with patch('ansible.module_utils.common.sys_info.get_distribution', return_value="Bar"): |
| 167 |
assert get_platform_subclass(self.LinuxTest) is self.Bar |