| 1 |
# unit tests for ansible fact collectors |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Ansible is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# Ansible is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
# |
| 17 |
|
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
from unittest.mock import Mock, patch |
| 21 |
|
| 22 |
from . base import BaseFactsTest |
| 23 |
|
| 24 |
from ansible.module_utils.facts import collector |
| 25 |
|
| 26 |
from ansible.module_utils.facts.system.apparmor import ApparmorFactCollector |
| 27 |
from ansible.module_utils.facts.system.caps import SystemCapabilitiesFactCollector |
| 28 |
from ansible.module_utils.facts.system.cmdline import CmdLineFactCollector |
| 29 |
from ansible.module_utils.facts.system.distribution import DistributionFactCollector |
| 30 |
from ansible.module_utils.facts.system.dns import DnsFactCollector |
| 31 |
from ansible.module_utils.facts.system.env import EnvFactCollector |
| 32 |
from ansible.module_utils.facts.system.fips import FipsFactCollector |
| 33 |
from ansible.module_utils.facts.system.pkg_mgr import PkgMgrFactCollector, OpenBSDPkgMgrFactCollector |
| 34 |
from ansible.module_utils.facts.system.platform import PlatformFactCollector |
| 35 |
from ansible.module_utils.facts.system.python import PythonFactCollector |
| 36 |
from ansible.module_utils.facts.system.selinux import SelinuxFactCollector |
| 37 |
from ansible.module_utils.facts.system.service_mgr import ServiceMgrFactCollector |
| 38 |
from ansible.module_utils.facts.system.ssh_pub_keys import SshPubKeyFactCollector |
| 39 |
from ansible.module_utils.facts.system.user import UserFactCollector |
| 40 |
|
| 41 |
from ansible.module_utils.facts.virtual.base import VirtualCollector |
| 42 |
from ansible.module_utils.facts.network.base import NetworkCollector |
| 43 |
from ansible.module_utils.facts.hardware.base import HardwareCollector |
| 44 |
|
| 45 |
|
| 46 |
class CollectorException(Exception): |
| 47 |
pass |
| 48 |
|
| 49 |
|
| 50 |
class ExceptionThrowingCollector(collector.BaseFactCollector): |
| 51 |
name = 'exc_throwing' |
| 52 |
|
| 53 |
def __init__(self, collectors=None, namespace=None, exception=None): |
| 54 |
super(ExceptionThrowingCollector, self).__init__(collectors, namespace) |
| 55 |
self._exception = exception or CollectorException('collection failed') |
| 56 |
|
| 57 |
def collect(self, module=None, collected_facts=None): |
| 58 |
raise self._exception |
| 59 |
|
| 60 |
|
| 61 |
class TestExceptionThrowingCollector(BaseFactsTest): |
| 62 |
__test__ = True |
| 63 |
gather_subset = ['exc_throwing'] |
| 64 |
valid_subsets = ['exc_throwing'] |
| 65 |
collector_class = ExceptionThrowingCollector |
| 66 |
|
| 67 |
def test_collect(self): |
| 68 |
module = self._mock_module() |
| 69 |
fact_collector = self.collector_class() |
| 70 |
self.assertRaises(CollectorException, |
| 71 |
fact_collector.collect, |
| 72 |
module=module, |
| 73 |
collected_facts=self.collected_facts) |
| 74 |
|
| 75 |
def test_collect_with_namespace(self): |
| 76 |
module = self._mock_module() |
| 77 |
fact_collector = self.collector_class() |
| 78 |
self.assertRaises(CollectorException, |
| 79 |
fact_collector.collect_with_namespace, |
| 80 |
module=module, |
| 81 |
collected_facts=self.collected_facts) |
| 82 |
|
| 83 |
|
| 84 |
class TestApparmorFacts(BaseFactsTest): |
| 85 |
__test__ = True |
| 86 |
gather_subset = ['!all', 'apparmor'] |
| 87 |
valid_subsets = ['apparmor'] |
| 88 |
fact_namespace = 'ansible_apparmor' |
| 89 |
collector_class = ApparmorFactCollector |
| 90 |
|
| 91 |
def test_collect(self): |
| 92 |
facts_dict = super(TestApparmorFacts, self)._test_collect() |
| 93 |
self.assertIn('status', facts_dict['apparmor']) |
| 94 |
|
| 95 |
|
| 96 |
class TestCapsFacts(BaseFactsTest): |
| 97 |
__test__ = True |
| 98 |
gather_subset = ['!all', 'caps'] |
| 99 |
valid_subsets = ['caps'] |
| 100 |
fact_namespace = 'ansible_system_capabilities' |
| 101 |
collector_class = SystemCapabilitiesFactCollector |
| 102 |
|
| 103 |
def _mock_module(self): |
| 104 |
mock_module = Mock() |
| 105 |
mock_module.params = {'gather_subset': self.gather_subset, |
| 106 |
'gather_timeout': 10, |
| 107 |
'filter': '*'} |
| 108 |
mock_module.get_bin_path = Mock(return_value='/usr/sbin/capsh') |
| 109 |
mock_module.run_command = Mock(return_value=(0, 'Current: =ep', '')) |
| 110 |
return mock_module |
| 111 |
|
| 112 |
|
| 113 |
class TestCmdLineFacts(BaseFactsTest): |
| 114 |
__test__ = True |
| 115 |
gather_subset = ['!all', 'cmdline'] |
| 116 |
valid_subsets = ['cmdline'] |
| 117 |
fact_namespace = 'ansible_cmdline' |
| 118 |
collector_class = CmdLineFactCollector |
| 119 |
|
| 120 |
def test_parse_proc_cmdline_uefi(self): |
| 121 |
uefi_cmdline = r'initrd=\70ef65e1a04a47aea04f7b5145ea3537\4.10.0-19-generic\initrd root=UUID=50973b75-4a66-4bf0-9764-2b7614489e64 ro quiet' |
| 122 |
expected = {'initrd': r'\70ef65e1a04a47aea04f7b5145ea3537\4.10.0-19-generic\initrd', |
| 123 |
'root': 'UUID=50973b75-4a66-4bf0-9764-2b7614489e64', |
| 124 |
'quiet': True, |
| 125 |
'ro': True} |
| 126 |
fact_collector = self.collector_class() |
| 127 |
facts_dict = fact_collector._parse_proc_cmdline(uefi_cmdline) |
| 128 |
|
| 129 |
self.assertDictEqual(facts_dict, expected) |
| 130 |
|
| 131 |
def test_parse_proc_cmdline_fedora(self): |
| 132 |
cmdline_fedora = r'BOOT_IMAGE=/vmlinuz-4.10.16-200.fc25.x86_64 root=/dev/mapper/fedora-root ro rd.lvm.lv=fedora/root rd.luks.uuid=luks-c80b7537-358b-4a07-b88c-c59ef187479b rd.lvm.lv=fedora/swap rhgb quiet LANG=en_US.UTF-8' # noqa |
| 133 |
|
| 134 |
expected = {'BOOT_IMAGE': '/vmlinuz-4.10.16-200.fc25.x86_64', |
| 135 |
'LANG': 'en_US.UTF-8', |
| 136 |
'quiet': True, |
| 137 |
'rd.luks.uuid': 'luks-c80b7537-358b-4a07-b88c-c59ef187479b', |
| 138 |
'rd.lvm.lv': 'fedora/swap', |
| 139 |
'rhgb': True, |
| 140 |
'ro': True, |
| 141 |
'root': '/dev/mapper/fedora-root'} |
| 142 |
|
| 143 |
fact_collector = self.collector_class() |
| 144 |
facts_dict = fact_collector._parse_proc_cmdline(cmdline_fedora) |
| 145 |
|
| 146 |
self.assertDictEqual(facts_dict, expected) |
| 147 |
|
| 148 |
def test_parse_proc_cmdline_dup_console(self): |
| 149 |
example = r'BOOT_IMAGE=/boot/vmlinuz-4.4.0-72-generic root=UUID=e12e46d9-06c9-4a64-a7b3-60e24b062d90 ro console=tty1 console=ttyS0' |
| 150 |
|
| 151 |
# FIXME: Two 'console' keywords? Using a dict for the fact value here loses info. Currently the 'last' one wins |
| 152 |
expected = {'BOOT_IMAGE': '/boot/vmlinuz-4.4.0-72-generic', |
| 153 |
'root': 'UUID=e12e46d9-06c9-4a64-a7b3-60e24b062d90', |
| 154 |
'ro': True, |
| 155 |
'console': 'ttyS0'} |
| 156 |
|
| 157 |
fact_collector = self.collector_class() |
| 158 |
facts_dict = fact_collector._parse_proc_cmdline(example) |
| 159 |
|
| 160 |
# TODO: fails because we lose a 'console' |
| 161 |
self.assertDictEqual(facts_dict, expected) |
| 162 |
|
| 163 |
|
| 164 |
class TestDistributionFacts(BaseFactsTest): |
| 165 |
__test__ = True |
| 166 |
gather_subset = ['!all', 'distribution'] |
| 167 |
valid_subsets = ['distribution'] |
| 168 |
fact_namespace = 'ansible_distribution' |
| 169 |
collector_class = DistributionFactCollector |
| 170 |
|
| 171 |
|
| 172 |
class TestDnsFacts(BaseFactsTest): |
| 173 |
|
| 174 |
__test__ = True |
| 175 |
gather_subset = ['!all', 'dns'] |
| 176 |
valid_subsets = ['dns'] |
| 177 |
fact_namespace = 'ansible_dns' |
| 178 |
collector_class = DnsFactCollector |
| 179 |
|
| 180 |
|
| 181 |
class TestEnvFacts(BaseFactsTest): |
| 182 |
|
| 183 |
__test__ = True |
| 184 |
gather_subset = ['!all', 'env'] |
| 185 |
valid_subsets = ['env'] |
| 186 |
fact_namespace = 'ansible_env' |
| 187 |
collector_class = EnvFactCollector |
| 188 |
|
| 189 |
def test_collect(self): |
| 190 |
facts_dict = super(TestEnvFacts, self)._test_collect() |
| 191 |
self.assertIn('HOME', facts_dict['env']) |
| 192 |
|
| 193 |
|
| 194 |
class TestFipsFacts(BaseFactsTest): |
| 195 |
__test__ = True |
| 196 |
gather_subset = ['!all', 'fips'] |
| 197 |
valid_subsets = ['fips'] |
| 198 |
fact_namespace = 'ansible_fips' |
| 199 |
collector_class = FipsFactCollector |
| 200 |
|
| 201 |
|
| 202 |
class TestHardwareCollector(BaseFactsTest): |
| 203 |
__test__ = True |
| 204 |
gather_subset = ['!all', 'hardware'] |
| 205 |
valid_subsets = ['hardware'] |
| 206 |
fact_namespace = 'ansible_hardware' |
| 207 |
collector_class = HardwareCollector |
| 208 |
collected_facts = {'ansible_architecture': 'x86_64'} |
| 209 |
|
| 210 |
|
| 211 |
class TestNetworkCollector(BaseFactsTest): |
| 212 |
__test__ = True |
| 213 |
gather_subset = ['!all', 'network'] |
| 214 |
valid_subsets = ['network'] |
| 215 |
fact_namespace = 'ansible_network' |
| 216 |
collector_class = NetworkCollector |
| 217 |
|
| 218 |
|
| 219 |
class TestPkgMgrFacts(BaseFactsTest): |
| 220 |
__test__ = True |
| 221 |
gather_subset = ['!all', 'pkg_mgr'] |
| 222 |
valid_subsets = ['pkg_mgr'] |
| 223 |
fact_namespace = 'ansible_pkgmgr' |
| 224 |
collector_class = PkgMgrFactCollector |
| 225 |
collected_facts = { |
| 226 |
"ansible_distribution": "Fedora", |
| 227 |
"ansible_distribution_major_version": "28", |
| 228 |
"ansible_os_family": "RedHat" |
| 229 |
} |
| 230 |
|
| 231 |
def test_collect(self): |
| 232 |
module = self._mock_module() |
| 233 |
fact_collector = self.collector_class() |
| 234 |
facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts) |
| 235 |
self.assertIsInstance(facts_dict, dict) |
| 236 |
self.assertIn('pkg_mgr', facts_dict) |
| 237 |
|
| 238 |
|
| 239 |
class TestMacOSXPkgMgrFacts(BaseFactsTest): |
| 240 |
__test__ = True |
| 241 |
gather_subset = ['!all', 'pkg_mgr'] |
| 242 |
valid_subsets = ['pkg_mgr'] |
| 243 |
fact_namespace = 'ansible_pkgmgr' |
| 244 |
collector_class = PkgMgrFactCollector |
| 245 |
collected_facts = { |
| 246 |
"ansible_distribution": "MacOSX", |
| 247 |
"ansible_distribution_major_version": "11", |
| 248 |
"ansible_os_family": "Darwin" |
| 249 |
} |
| 250 |
|
| 251 |
@patch('ansible.module_utils.facts.system.pkg_mgr.os.path.exists', side_effect=lambda x: x == '/opt/homebrew/bin/brew') |
| 252 |
def test_collect_opt_homebrew(self, p_exists): |
| 253 |
module = self._mock_module() |
| 254 |
fact_collector = self.collector_class() |
| 255 |
facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts) |
| 256 |
self.assertIsInstance(facts_dict, dict) |
| 257 |
self.assertIn('pkg_mgr', facts_dict) |
| 258 |
self.assertEqual(facts_dict['pkg_mgr'], 'homebrew') |
| 259 |
|
| 260 |
@patch('ansible.module_utils.facts.system.pkg_mgr.os.path.exists', side_effect=lambda x: x == '/usr/local/bin/brew') |
| 261 |
def test_collect_usr_homebrew(self, p_exists): |
| 262 |
module = self._mock_module() |
| 263 |
fact_collector = self.collector_class() |
| 264 |
facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts) |
| 265 |
self.assertIsInstance(facts_dict, dict) |
| 266 |
self.assertIn('pkg_mgr', facts_dict) |
| 267 |
self.assertEqual(facts_dict['pkg_mgr'], 'homebrew') |
| 268 |
|
| 269 |
@patch('ansible.module_utils.facts.system.pkg_mgr.os.path.exists', side_effect=lambda x: x == '/opt/local/bin/port') |
| 270 |
def test_collect_macports(self, p_exists): |
| 271 |
module = self._mock_module() |
| 272 |
fact_collector = self.collector_class() |
| 273 |
facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts) |
| 274 |
self.assertIsInstance(facts_dict, dict) |
| 275 |
self.assertIn('pkg_mgr', facts_dict) |
| 276 |
self.assertEqual(facts_dict['pkg_mgr'], 'macports') |
| 277 |
|
| 278 |
|
| 279 |
def _sanitize_os_path_apt_get(path): |
| 280 |
if path == '/usr/bin/apt-get': |
| 281 |
return True |
| 282 |
else: |
| 283 |
return False |
| 284 |
|
| 285 |
|
| 286 |
class TestPkgMgrFactsAptFedora(BaseFactsTest): |
| 287 |
__test__ = True |
| 288 |
gather_subset = ['!all', 'pkg_mgr'] |
| 289 |
valid_subsets = ['pkg_mgr'] |
| 290 |
fact_namespace = 'ansible_pkgmgr' |
| 291 |
collector_class = PkgMgrFactCollector |
| 292 |
collected_facts = { |
| 293 |
"ansible_distribution": "Fedora", |
| 294 |
"ansible_distribution_major_version": "28", |
| 295 |
"ansible_os_family": "RedHat", |
| 296 |
"ansible_pkg_mgr": "apt" |
| 297 |
} |
| 298 |
|
| 299 |
@patch('ansible.module_utils.facts.system.pkg_mgr.os.path.exists', side_effect=_sanitize_os_path_apt_get) |
| 300 |
def test_collect(self, mock_os_path_exists): |
| 301 |
module = self._mock_module() |
| 302 |
fact_collector = self.collector_class() |
| 303 |
facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts) |
| 304 |
self.assertIsInstance(facts_dict, dict) |
| 305 |
self.assertIn('pkg_mgr', facts_dict) |
| 306 |
|
| 307 |
|
| 308 |
class TestOpenBSDPkgMgrFacts(BaseFactsTest): |
| 309 |
__test__ = True |
| 310 |
gather_subset = ['!all', 'pkg_mgr'] |
| 311 |
valid_subsets = ['pkg_mgr'] |
| 312 |
fact_namespace = 'ansible_pkgmgr' |
| 313 |
collector_class = OpenBSDPkgMgrFactCollector |
| 314 |
|
| 315 |
def test_collect(self): |
| 316 |
module = self._mock_module() |
| 317 |
fact_collector = self.collector_class() |
| 318 |
facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts) |
| 319 |
self.assertIsInstance(facts_dict, dict) |
| 320 |
self.assertIn('pkg_mgr', facts_dict) |
| 321 |
self.assertEqual(facts_dict['pkg_mgr'], 'openbsd_pkg') |
| 322 |
|
| 323 |
|
| 324 |
class TestPlatformFactCollector(BaseFactsTest): |
| 325 |
__test__ = True |
| 326 |
gather_subset = ['!all', 'platform'] |
| 327 |
valid_subsets = ['platform'] |
| 328 |
fact_namespace = 'ansible_platform' |
| 329 |
collector_class = PlatformFactCollector |
| 330 |
|
| 331 |
|
| 332 |
class TestPythonFactCollector(BaseFactsTest): |
| 333 |
__test__ = True |
| 334 |
gather_subset = ['!all', 'python'] |
| 335 |
valid_subsets = ['python'] |
| 336 |
fact_namespace = 'ansible_python' |
| 337 |
collector_class = PythonFactCollector |
| 338 |
|
| 339 |
|
| 340 |
class TestSelinuxFacts(BaseFactsTest): |
| 341 |
__test__ = True |
| 342 |
gather_subset = ['!all', 'selinux'] |
| 343 |
valid_subsets = ['selinux'] |
| 344 |
fact_namespace = 'ansible_selinux' |
| 345 |
collector_class = SelinuxFactCollector |
| 346 |
|
| 347 |
def test_no_selinux(self): |
| 348 |
with patch('ansible.module_utils.facts.system.selinux.HAVE_SELINUX', False): |
| 349 |
module = self._mock_module() |
| 350 |
fact_collector = self.collector_class() |
| 351 |
facts_dict = fact_collector.collect(module=module) |
| 352 |
self.assertIsInstance(facts_dict, dict) |
| 353 |
self.assertEqual(facts_dict['selinux']['status'], 'Missing selinux Python library') |
| 354 |
|
| 355 |
|
| 356 |
class TestServiceMgrFacts(BaseFactsTest): |
| 357 |
__test__ = True |
| 358 |
gather_subset = ['!all', 'service_mgr'] |
| 359 |
valid_subsets = ['service_mgr'] |
| 360 |
fact_namespace = 'ansible_service_mgr' |
| 361 |
collector_class = ServiceMgrFactCollector |
| 362 |
|
| 363 |
# TODO: dedupe some of this test code |
| 364 |
|
| 365 |
@patch('ansible.module_utils.facts.system.service_mgr.get_file_content', return_value=None) |
| 366 |
def test_no_proc1_ps_random_init(self, mock_gfc): |
| 367 |
# no /proc/1/comm, ps returns '/sbin/sys11' which we dont know |
| 368 |
# should end up return 'sys11' |
| 369 |
module = self._mock_module() |
| 370 |
module.run_command = Mock(return_value=(0, '/sbin/sys11', '')) |
| 371 |
fact_collector = self.collector_class() |
| 372 |
facts_dict = fact_collector.collect(module=module) |
| 373 |
self.assertIsInstance(facts_dict, dict) |
| 374 |
self.assertEqual(facts_dict['service_mgr'], 'sys11') |
| 375 |
|
| 376 |
@patch('ansible.module_utils.facts.system.service_mgr.get_file_content', return_value='runit-init') |
| 377 |
def test_service_mgr_runit(self, mock_gfc): |
| 378 |
# /proc/1/comm contains 'runit-init', ps fails, service manager is runit |
| 379 |
# should end up return 'runit' |
| 380 |
module = self._mock_module() |
| 381 |
module.run_command = Mock(return_value=(1, '', '')) |
| 382 |
collected_facts = {'ansible_system': 'Linux'} |
| 383 |
fact_collector = self.collector_class() |
| 384 |
facts_dict = fact_collector.collect(module=module, |
| 385 |
collected_facts=collected_facts) |
| 386 |
self.assertIsInstance(facts_dict, dict) |
| 387 |
self.assertEqual(facts_dict['service_mgr'], 'runit') |
| 388 |
|
| 389 |
@patch('ansible.module_utils.facts.system.service_mgr.get_file_content', return_value=None) |
| 390 |
@patch('ansible.module_utils.facts.system.service_mgr.os.path.islink', side_effect=lambda x: x == '/sbin/init') |
| 391 |
@patch('ansible.module_utils.facts.system.service_mgr.os.readlink', side_effect=lambda x: '/sbin/runit-init' if x == '/sbin/init' else '/bin/false') |
| 392 |
def test_service_mgr_runit_no_comm(self, mock_gfc, mock_opl, mock_orl): |
| 393 |
# no /proc/1/comm, ps returns 'COMMAND\n', service manager is runit |
| 394 |
# should end up return 'runit' |
| 395 |
module = self._mock_module() |
| 396 |
module.run_command = Mock(return_value=(1, 'COMMAND\n', '')) |
| 397 |
collected_facts = {'ansible_system': 'Linux'} |
| 398 |
fact_collector = self.collector_class() |
| 399 |
facts_dict = fact_collector.collect(module=module, |
| 400 |
collected_facts=collected_facts) |
| 401 |
self.assertIsInstance(facts_dict, dict) |
| 402 |
self.assertEqual(facts_dict['service_mgr'], 'runit') |
| 403 |
|
| 404 |
|
| 405 |
class TestSshPubKeyFactCollector(BaseFactsTest): |
| 406 |
__test__ = True |
| 407 |
gather_subset = ['!all', 'ssh_pub_keys'] |
| 408 |
valid_subsets = ['ssh_pub_keys'] |
| 409 |
fact_namespace = 'ansible_ssh_pub_leys' |
| 410 |
collector_class = SshPubKeyFactCollector |
| 411 |
|
| 412 |
|
| 413 |
class TestUserFactCollector(BaseFactsTest): |
| 414 |
__test__ = True |
| 415 |
gather_subset = ['!all', 'user'] |
| 416 |
valid_subsets = ['user'] |
| 417 |
fact_namespace = 'ansible_user' |
| 418 |
collector_class = UserFactCollector |
| 419 |
|
| 420 |
|
| 421 |
class TestVirtualFacts(BaseFactsTest): |
| 422 |
__test__ = True |
| 423 |
gather_subset = ['!all', 'virtual'] |
| 424 |
valid_subsets = ['virtual'] |
| 425 |
fact_namespace = 'ansible_virtual' |
| 426 |
collector_class = VirtualCollector |