| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: Contributors to the Ansible project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from ansible.module_utils.facts.virtual.sysctl import VirtualSysctlDetectionMixin |
| 10 |
|
| 11 |
|
| 12 |
class MockVirtualSysctl(VirtualSysctlDetectionMixin): |
| 13 |
def __init__(self, module): |
| 14 |
self.module = module |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.parametrize("expected_path", ["/usr/sbin/sysctl", "/sbin/sysctl"]) |
| 18 |
def test_detect_sysctl(mocker, expected_path): |
| 19 |
module = mocker.Mock() |
| 20 |
module.get_bin_path.return_value = expected_path |
| 21 |
mixin = MockVirtualSysctl(module=module) |
| 22 |
mixin.detect_sysctl() |
| 23 |
|
| 24 |
assert mixin.sysctl_path == expected_path |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize( |
| 28 |
("virt_product", "expected_guest"), |
| 29 |
[ |
| 30 |
pytest.param( |
| 31 |
"KVM", |
| 32 |
"kvm", |
| 33 |
id="KVM-all-caps", |
| 34 |
), |
| 35 |
pytest.param( |
| 36 |
"kvm", |
| 37 |
"kvm", |
| 38 |
id="kvm", |
| 39 |
), |
| 40 |
pytest.param( |
| 41 |
"Bochs", |
| 42 |
"kvm", |
| 43 |
id="Bochs", |
| 44 |
), |
| 45 |
pytest.param( |
| 46 |
"SmartDC", |
| 47 |
"kvm", |
| 48 |
id="SmartDC", |
| 49 |
), |
| 50 |
pytest.param( |
| 51 |
"VMware", |
| 52 |
"VMware", |
| 53 |
id="VMware", |
| 54 |
), |
| 55 |
pytest.param( |
| 56 |
"VirtualBox", |
| 57 |
"virtualbox", |
| 58 |
id="VirtualBox", |
| 59 |
), |
| 60 |
pytest.param( |
| 61 |
"HVM domU", |
| 62 |
"xen", |
| 63 |
id="Xen-HVM", |
| 64 |
), |
| 65 |
pytest.param( |
| 66 |
"XenPVH", |
| 67 |
"xen", |
| 68 |
id="Xen-PVH", |
| 69 |
), |
| 70 |
pytest.param( |
| 71 |
"XenPV", |
| 72 |
"xen", |
| 73 |
id="Xen-PV", |
| 74 |
), |
| 75 |
pytest.param( |
| 76 |
"XenPVHVM", |
| 77 |
"xen", |
| 78 |
id="Xen-PVHVM", |
| 79 |
), |
| 80 |
pytest.param( |
| 81 |
"Hyper-V", |
| 82 |
"Hyper-V", |
| 83 |
id="Hyper-V", |
| 84 |
), |
| 85 |
pytest.param( |
| 86 |
"Parallels", |
| 87 |
"parallels", |
| 88 |
id="Parallels", |
| 89 |
), |
| 90 |
pytest.param( |
| 91 |
"RHEV Hypervisor", |
| 92 |
"RHEV", |
| 93 |
id="RHEV", |
| 94 |
), |
| 95 |
pytest.param( |
| 96 |
"1", |
| 97 |
"jails", |
| 98 |
id="Jails", |
| 99 |
), |
| 100 |
], |
| 101 |
) |
| 102 |
def test_detect_virt_product(mocker, virt_product, expected_guest): |
| 103 |
module = mocker.Mock() |
| 104 |
module.get_bin_path.return_value = "/usr/bin/sysctl" |
| 105 |
module.run_command.return_value = (0, virt_product, "") |
| 106 |
mixin = MockVirtualSysctl(module=module) |
| 107 |
guest_facts = mixin.detect_virt_product("security.jail.jailed") |
| 108 |
expected = { |
| 109 |
"virtualization_role": "guest", |
| 110 |
"virtualization_tech_guest": set([expected_guest]), |
| 111 |
"virtualization_tech_host": set(), |
| 112 |
"virtualization_type": expected_guest, |
| 113 |
} |
| 114 |
assert guest_facts == expected |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.parametrize( |
| 118 |
("virt_product", "expected_guest"), |
| 119 |
[ |
| 120 |
pytest.param( |
| 121 |
"QEMU", |
| 122 |
"kvm", |
| 123 |
id="QEMU", |
| 124 |
), |
| 125 |
pytest.param( |
| 126 |
"OpenBSD", |
| 127 |
"vmm", |
| 128 |
id="OpenBSD-vmm", |
| 129 |
), |
| 130 |
], |
| 131 |
) |
| 132 |
def test_detect_virt_vendor(mocker, virt_product, expected_guest): |
| 133 |
module = mocker.Mock() |
| 134 |
module.get_bin_path.return_value = "/usr/bin/sysctl" |
| 135 |
module.run_command.return_value = (0, virt_product, "") |
| 136 |
mixin = MockVirtualSysctl(module=module) |
| 137 |
guest_facts = mixin.detect_virt_vendor("security.jail.jailed") |
| 138 |
expected = { |
| 139 |
"virtualization_role": "guest", |
| 140 |
"virtualization_tech_guest": set([expected_guest]), |
| 141 |
"virtualization_tech_host": set(), |
| 142 |
"virtualization_type": expected_guest, |
| 143 |
} |
| 144 |
assert guest_facts == expected |