| 1 |
# This file is part of Ansible |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import os |
| 22 |
|
| 23 |
import pytest |
| 24 |
|
| 25 |
# for testing |
| 26 |
import unittest |
| 27 |
from unittest.mock import Mock, patch |
| 28 |
|
| 29 |
from ansible.module_utils import facts |
| 30 |
from ansible.module_utils.facts import hardware |
| 31 |
from ansible.module_utils.facts import network |
| 32 |
from ansible.module_utils.facts import virtual |
| 33 |
|
| 34 |
|
| 35 |
class BaseTestFactsPlatform(unittest.TestCase): |
| 36 |
platform_id = 'Generic' |
| 37 |
fact_class = hardware.base.Hardware |
| 38 |
collector_class = None |
| 39 |
|
| 40 |
"""Verify that the automagic in Hardware.__new__ selects the right subclass.""" |
| 41 |
@patch('platform.system') |
| 42 |
def test_new(self, mock_platform): |
| 43 |
if not self.fact_class: |
| 44 |
pytest.skip('This platform (%s) does not have a fact_class.' % self.platform_id) |
| 45 |
mock_platform.return_value = self.platform_id |
| 46 |
inst = self.fact_class(module=Mock(), load_on_init=False) |
| 47 |
self.assertIsInstance(inst, self.fact_class) |
| 48 |
self.assertEqual(inst.platform, self.platform_id) |
| 49 |
|
| 50 |
def test_subclass(self): |
| 51 |
if not self.fact_class: |
| 52 |
pytest.skip('This platform (%s) does not have a fact_class.' % self.platform_id) |
| 53 |
# 'Generic' will try to map to platform.system() that we are not mocking here |
| 54 |
if self.platform_id == 'Generic': |
| 55 |
return |
| 56 |
inst = self.fact_class(module=Mock(), load_on_init=False) |
| 57 |
self.assertIsInstance(inst, self.fact_class) |
| 58 |
self.assertEqual(inst.platform, self.platform_id) |
| 59 |
|
| 60 |
def test_collector(self): |
| 61 |
if not self.collector_class: |
| 62 |
pytest.skip('This test class needs to be updated to specify collector_class') |
| 63 |
inst = self.collector_class() |
| 64 |
self.assertIsInstance(inst, self.collector_class) |
| 65 |
self.assertEqual(inst._platform, self.platform_id) |
| 66 |
|
| 67 |
|
| 68 |
class TestLinuxFactsPlatform(BaseTestFactsPlatform): |
| 69 |
platform_id = 'Linux' |
| 70 |
fact_class = hardware.linux.LinuxHardware |
| 71 |
collector_class = hardware.linux.LinuxHardwareCollector |
| 72 |
|
| 73 |
|
| 74 |
class TestHurdFactsPlatform(BaseTestFactsPlatform): |
| 75 |
platform_id = 'GNU' |
| 76 |
fact_class = hardware.hurd.HurdHardware |
| 77 |
collector_class = hardware.hurd.HurdHardwareCollector |
| 78 |
|
| 79 |
|
| 80 |
class TestSunOSHardware(BaseTestFactsPlatform): |
| 81 |
platform_id = 'SunOS' |
| 82 |
fact_class = hardware.sunos.SunOSHardware |
| 83 |
collector_class = hardware.sunos.SunOSHardwareCollector |
| 84 |
|
| 85 |
|
| 86 |
class TestOpenBSDHardware(BaseTestFactsPlatform): |
| 87 |
platform_id = 'OpenBSD' |
| 88 |
fact_class = hardware.openbsd.OpenBSDHardware |
| 89 |
collector_class = hardware.openbsd.OpenBSDHardwareCollector |
| 90 |
|
| 91 |
|
| 92 |
class TestFreeBSDHardware(BaseTestFactsPlatform): |
| 93 |
platform_id = 'FreeBSD' |
| 94 |
fact_class = hardware.freebsd.FreeBSDHardware |
| 95 |
collector_class = hardware.freebsd.FreeBSDHardwareCollector |
| 96 |
|
| 97 |
|
| 98 |
class TestDragonFlyHardware(BaseTestFactsPlatform): |
| 99 |
platform_id = 'DragonFly' |
| 100 |
fact_class = None |
| 101 |
collector_class = hardware.dragonfly.DragonFlyHardwareCollector |
| 102 |
|
| 103 |
|
| 104 |
class TestNetBSDHardware(BaseTestFactsPlatform): |
| 105 |
platform_id = 'NetBSD' |
| 106 |
fact_class = hardware.netbsd.NetBSDHardware |
| 107 |
collector_class = hardware.netbsd.NetBSDHardwareCollector |
| 108 |
|
| 109 |
|
| 110 |
class TestAIXHardware(BaseTestFactsPlatform): |
| 111 |
platform_id = 'AIX' |
| 112 |
fact_class = hardware.aix.AIXHardware |
| 113 |
collector_class = hardware.aix.AIXHardwareCollector |
| 114 |
|
| 115 |
|
| 116 |
class TestHPUXHardware(BaseTestFactsPlatform): |
| 117 |
platform_id = 'HP-UX' |
| 118 |
fact_class = hardware.hpux.HPUXHardware |
| 119 |
collector_class = hardware.hpux.HPUXHardwareCollector |
| 120 |
|
| 121 |
|
| 122 |
class TestDarwinHardware(BaseTestFactsPlatform): |
| 123 |
platform_id = 'Darwin' |
| 124 |
fact_class = hardware.darwin.DarwinHardware |
| 125 |
collector_class = hardware.darwin.DarwinHardwareCollector |
| 126 |
|
| 127 |
|
| 128 |
class TestGenericNetwork(BaseTestFactsPlatform): |
| 129 |
platform_id = 'Generic' |
| 130 |
fact_class = network.base.Network |
| 131 |
|
| 132 |
|
| 133 |
class TestHurdPfinetNetwork(BaseTestFactsPlatform): |
| 134 |
platform_id = 'GNU' |
| 135 |
fact_class = network.hurd.HurdPfinetNetwork |
| 136 |
collector_class = network.hurd.HurdNetworkCollector |
| 137 |
|
| 138 |
|
| 139 |
class TestLinuxNetwork(BaseTestFactsPlatform): |
| 140 |
platform_id = 'Linux' |
| 141 |
fact_class = network.linux.LinuxNetwork |
| 142 |
collector_class = network.linux.LinuxNetworkCollector |
| 143 |
|
| 144 |
|
| 145 |
class TestGenericBsdIfconfigNetwork(BaseTestFactsPlatform): |
| 146 |
platform_id = 'Generic_BSD_Ifconfig' |
| 147 |
fact_class = network.generic_bsd.GenericBsdIfconfigNetwork |
| 148 |
collector_class = None |
| 149 |
|
| 150 |
|
| 151 |
class TestHPUXNetwork(BaseTestFactsPlatform): |
| 152 |
platform_id = 'HP-UX' |
| 153 |
fact_class = network.hpux.HPUXNetwork |
| 154 |
collector_class = network.hpux.HPUXNetworkCollector |
| 155 |
|
| 156 |
|
| 157 |
class TestDarwinNetwork(BaseTestFactsPlatform): |
| 158 |
platform_id = 'Darwin' |
| 159 |
fact_class = network.darwin.DarwinNetwork |
| 160 |
collector_class = network.darwin.DarwinNetworkCollector |
| 161 |
|
| 162 |
|
| 163 |
class TestFreeBSDNetwork(BaseTestFactsPlatform): |
| 164 |
platform_id = 'FreeBSD' |
| 165 |
fact_class = network.freebsd.FreeBSDNetwork |
| 166 |
collector_class = network.freebsd.FreeBSDNetworkCollector |
| 167 |
|
| 168 |
|
| 169 |
class TestDragonFlyNetwork(BaseTestFactsPlatform): |
| 170 |
platform_id = 'DragonFly' |
| 171 |
fact_class = network.dragonfly.DragonFlyNetwork |
| 172 |
collector_class = network.dragonfly.DragonFlyNetworkCollector |
| 173 |
|
| 174 |
|
| 175 |
class TestAIXNetwork(BaseTestFactsPlatform): |
| 176 |
platform_id = 'AIX' |
| 177 |
fact_class = network.aix.AIXNetwork |
| 178 |
collector_class = network.aix.AIXNetworkCollector |
| 179 |
|
| 180 |
|
| 181 |
class TestNetBSDNetwork(BaseTestFactsPlatform): |
| 182 |
platform_id = 'NetBSD' |
| 183 |
fact_class = network.netbsd.NetBSDNetwork |
| 184 |
collector_class = network.netbsd.NetBSDNetworkCollector |
| 185 |
|
| 186 |
|
| 187 |
class TestOpenBSDNetwork(BaseTestFactsPlatform): |
| 188 |
platform_id = 'OpenBSD' |
| 189 |
fact_class = network.openbsd.OpenBSDNetwork |
| 190 |
collector_class = network.openbsd.OpenBSDNetworkCollector |
| 191 |
|
| 192 |
|
| 193 |
class TestSunOSNetwork(BaseTestFactsPlatform): |
| 194 |
platform_id = 'SunOS' |
| 195 |
fact_class = network.sunos.SunOSNetwork |
| 196 |
collector_class = network.sunos.SunOSNetworkCollector |
| 197 |
|
| 198 |
|
| 199 |
class TestLinuxVirtual(BaseTestFactsPlatform): |
| 200 |
platform_id = 'Linux' |
| 201 |
fact_class = virtual.linux.LinuxVirtual |
| 202 |
collector_class = virtual.linux.LinuxVirtualCollector |
| 203 |
|
| 204 |
|
| 205 |
class TestFreeBSDVirtual(BaseTestFactsPlatform): |
| 206 |
platform_id = 'FreeBSD' |
| 207 |
fact_class = virtual.freebsd.FreeBSDVirtual |
| 208 |
collector_class = virtual.freebsd.FreeBSDVirtualCollector |
| 209 |
|
| 210 |
|
| 211 |
class TestNetBSDVirtual(BaseTestFactsPlatform): |
| 212 |
platform_id = 'NetBSD' |
| 213 |
fact_class = virtual.netbsd.NetBSDVirtual |
| 214 |
collector_class = virtual.netbsd.NetBSDVirtualCollector |
| 215 |
|
| 216 |
|
| 217 |
class TestOpenBSDVirtual(BaseTestFactsPlatform): |
| 218 |
platform_id = 'OpenBSD' |
| 219 |
fact_class = virtual.openbsd.OpenBSDVirtual |
| 220 |
collector_class = virtual.openbsd.OpenBSDVirtualCollector |
| 221 |
|
| 222 |
|
| 223 |
class TestHPUXVirtual(BaseTestFactsPlatform): |
| 224 |
platform_id = 'HP-UX' |
| 225 |
fact_class = virtual.hpux.HPUXVirtual |
| 226 |
collector_class = virtual.hpux.HPUXVirtualCollector |
| 227 |
|
| 228 |
|
| 229 |
class TestSunOSVirtual(BaseTestFactsPlatform): |
| 230 |
platform_id = 'SunOS' |
| 231 |
fact_class = virtual.sunos.SunOSVirtual |
| 232 |
collector_class = virtual.sunos.SunOSVirtualCollector |
| 233 |
|
| 234 |
|
| 235 |
LSBLK_OUTPUT = b""" |
| 236 |
/dev/sda |
| 237 |
/dev/sda1 32caaec3-ef40-4691-a3b6-438c3f9bc1c0 |
| 238 |
/dev/sda2 66Ojcd-ULtu-1cZa-Tywo-mx0d-RF4O-ysA9jK |
| 239 |
/dev/mapper/fedora_dhcp129--186-swap eae6059d-2fbe-4d1c-920d-a80bbeb1ac6d |
| 240 |
/dev/mapper/fedora_dhcp129--186-root d34cf5e3-3449-4a6c-8179-a1feb2bca6ce |
| 241 |
/dev/mapper/fedora_dhcp129--186-home 2d3e4853-fa69-4ccf-8a6a-77b05ab0a42d |
| 242 |
/dev/sr0 |
| 243 |
/dev/loop0 0f031512-ab15-497d-9abd-3a512b4a9390 |
| 244 |
/dev/loop1 7c1b0f30-cf34-459f-9a70-2612f82b870a |
| 245 |
/dev/loop9 0f031512-ab15-497d-9abd-3a512b4a9390 |
| 246 |
/dev/loop9 7c1b4444-cf34-459f-9a70-2612f82b870a |
| 247 |
/dev/mapper/docker-253:1-1050967-pool |
| 248 |
/dev/loop2 |
| 249 |
/dev/mapper/docker-253:1-1050967-pool |
| 250 |
""" |
| 251 |
|
| 252 |
LSBLK_OUTPUT_2 = b""" |
| 253 |
/dev/sda |
| 254 |
/dev/sda1 32caaec3-ef40-4691-a3b6-438c3f9bc1c0 |
| 255 |
/dev/sda2 66Ojcd-ULtu-1cZa-Tywo-mx0d-RF4O-ysA9jK |
| 256 |
/dev/mapper/fedora_dhcp129--186-swap eae6059d-2fbe-4d1c-920d-a80bbeb1ac6d |
| 257 |
/dev/mapper/fedora_dhcp129--186-root d34cf5e3-3449-4a6c-8179-a1feb2bca6ce |
| 258 |
/dev/mapper/fedora_dhcp129--186-home 2d3e4853-fa69-4ccf-8a6a-77b05ab0a42d |
| 259 |
/dev/mapper/an-example-mapper with a space in the name 84639acb-013f-4d2f-9392-526a572b4373 |
| 260 |
/dev/sr0 |
| 261 |
/dev/loop0 0f031512-ab15-497d-9abd-3a512b4a9390 |
| 262 |
""" |
| 263 |
|
| 264 |
LSBLK_UUIDS = {'/dev/sda1': '66Ojcd-ULtu-1cZa-Tywo-mx0d-RF4O-ysA9jK'} |
| 265 |
|
| 266 |
UDEVADM_UUID = 'N/A' |
| 267 |
|
| 268 |
MTAB = r""" |
| 269 |
sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0 |
| 270 |
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0 |
| 271 |
devtmpfs /dev devtmpfs rw,seclabel,nosuid,size=8044400k,nr_inodes=2011100,mode=755 0 0 |
| 272 |
securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0 |
| 273 |
tmpfs /dev/shm tmpfs rw,seclabel,nosuid,nodev 0 0 |
| 274 |
devpts /dev/pts devpts rw,seclabel,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0 |
| 275 |
tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0 |
| 276 |
tmpfs /sys/fs/cgroup tmpfs ro,seclabel,nosuid,nodev,noexec,mode=755 0 0 |
| 277 |
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd 0 0 |
| 278 |
pstore /sys/fs/pstore pstore rw,seclabel,nosuid,nodev,noexec,relatime 0 0 |
| 279 |
cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0 |
| 280 |
cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0 |
| 281 |
cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0 |
| 282 |
cgroup /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0 |
| 283 |
cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0 |
| 284 |
cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0 |
| 285 |
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0 |
| 286 |
cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0 |
| 287 |
cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0 |
| 288 |
cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio 0 0 |
| 289 |
configfs /sys/kernel/config configfs rw,relatime 0 0 |
| 290 |
/dev/mapper/fedora_dhcp129--186-root / ext4 rw,seclabel,relatime,data=ordered 0 0 |
| 291 |
selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0 |
| 292 |
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=24,pgrp=1,timeout=0,minproto=5,maxproto=5,direct 0 0 |
| 293 |
debugfs /sys/kernel/debug debugfs rw,seclabel,relatime 0 0 |
| 294 |
hugetlbfs /dev/hugepages hugetlbfs rw,seclabel,relatime 0 0 |
| 295 |
tmpfs /tmp tmpfs rw,seclabel 0 0 |
| 296 |
mqueue /dev/mqueue mqueue rw,seclabel,relatime 0 0 |
| 297 |
/dev/loop0 /var/lib/machines btrfs rw,seclabel,relatime,space_cache,subvolid=5,subvol=/ 0 0 |
| 298 |
/dev/sda1 /boot ext4 rw,seclabel,relatime,data=ordered 0 0 |
| 299 |
/dev/mapper/fedora_dhcp129--186-home /home ext4 rw,seclabel,relatime,data=ordered 0 0 |
| 300 |
tmpfs /run/user/1000 tmpfs rw,seclabel,nosuid,nodev,relatime,size=1611044k,mode=700,uid=1000,gid=1000 0 0 |
| 301 |
gvfsd-fuse /run/user/1000/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0 |
| 302 |
fusectl /sys/fs/fuse/connections fusectl rw,relatime 0 0 |
| 303 |
grimlock.g.a: /home/adrian/sshfs-grimlock fuse.sshfs rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0 |
| 304 |
grimlock.g.a:test_path/path_with'single_quotes /home/adrian/sshfs-grimlock-single-quote fuse.sshfs rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0 |
| 305 |
grimlock.g.a:path_with'single_quotes /home/adrian/sshfs-grimlock-single-quote-2 fuse.sshfs rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0 |
| 306 |
grimlock.g.a:/mnt/data/foto's /home/adrian/fotos fuse.sshfs rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0 |
| 307 |
\\Windows\share /data/ cifs credentials=/root/.creds 0 0 |
| 308 |
""" |
| 309 |
|
| 310 |
MTAB_ENTRIES = [ |
| 311 |
[ |
| 312 |
'sysfs', |
| 313 |
'/sys', |
| 314 |
'sysfs', |
| 315 |
'rw,seclabel,nosuid,nodev,noexec,relatime', |
| 316 |
'0', |
| 317 |
'0' |
| 318 |
], |
| 319 |
['proc', '/proc', 'proc', 'rw,nosuid,nodev,noexec,relatime', '0', '0'], |
| 320 |
[ |
| 321 |
'devtmpfs', |
| 322 |
'/dev', |
| 323 |
'devtmpfs', |
| 324 |
'rw,seclabel,nosuid,size=8044400k,nr_inodes=2011100,mode=755', |
| 325 |
'0', |
| 326 |
'0' |
| 327 |
], |
| 328 |
[ |
| 329 |
'securityfs', |
| 330 |
'/sys/kernel/security', |
| 331 |
'securityfs', |
| 332 |
'rw,nosuid,nodev,noexec,relatime', |
| 333 |
'0', |
| 334 |
'0' |
| 335 |
], |
| 336 |
['tmpfs', '/dev/shm', 'tmpfs', 'rw,seclabel,nosuid,nodev', '0', '0'], |
| 337 |
[ |
| 338 |
'devpts', |
| 339 |
'/dev/pts', |
| 340 |
'devpts', |
| 341 |
'rw,seclabel,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000', |
| 342 |
'0', |
| 343 |
'0' |
| 344 |
], |
| 345 |
['tmpfs', '/run', 'tmpfs', 'rw,seclabel,nosuid,nodev,mode=755', '0', '0'], |
| 346 |
[ |
| 347 |
'tmpfs', |
| 348 |
'/sys/fs/cgroup', |
| 349 |
'tmpfs', |
| 350 |
'ro,seclabel,nosuid,nodev,noexec,mode=755', |
| 351 |
'0', |
| 352 |
'0' |
| 353 |
], |
| 354 |
[ |
| 355 |
'cgroup', |
| 356 |
'/sys/fs/cgroup/systemd', |
| 357 |
'cgroup', |
| 358 |
'rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd', |
| 359 |
'0', |
| 360 |
'0' |
| 361 |
], |
| 362 |
[ |
| 363 |
'pstore', |
| 364 |
'/sys/fs/pstore', |
| 365 |
'pstore', |
| 366 |
'rw,seclabel,nosuid,nodev,noexec,relatime', |
| 367 |
'0', |
| 368 |
'0' |
| 369 |
], |
| 370 |
[ |
| 371 |
'cgroup', |
| 372 |
'/sys/fs/cgroup/devices', |
| 373 |
'cgroup', |
| 374 |
'rw,nosuid,nodev,noexec,relatime,devices', |
| 375 |
'0', |
| 376 |
'0' |
| 377 |
], |
| 378 |
[ |
| 379 |
'cgroup', |
| 380 |
'/sys/fs/cgroup/freezer', |
| 381 |
'cgroup', |
| 382 |
'rw,nosuid,nodev,noexec,relatime,freezer', |
| 383 |
'0', |
| 384 |
'0' |
| 385 |
], |
| 386 |
[ |
| 387 |
'cgroup', |
| 388 |
'/sys/fs/cgroup/memory', |
| 389 |
'cgroup', |
| 390 |
'rw,nosuid,nodev,noexec,relatime,memory', |
| 391 |
'0', |
| 392 |
'0' |
| 393 |
], |
| 394 |
[ |
| 395 |
'cgroup', |
| 396 |
'/sys/fs/cgroup/pids', |
| 397 |
'cgroup', |
| 398 |
'rw,nosuid,nodev,noexec,relatime,pids', |
| 399 |
'0', |
| 400 |
'0' |
| 401 |
], |
| 402 |
[ |
| 403 |
'cgroup', |
| 404 |
'/sys/fs/cgroup/blkio', |
| 405 |
'cgroup', |
| 406 |
'rw,nosuid,nodev,noexec,relatime,blkio', |
| 407 |
'0', |
| 408 |
'0' |
| 409 |
], |
| 410 |
[ |
| 411 |
'cgroup', |
| 412 |
'/sys/fs/cgroup/cpuset', |
| 413 |
'cgroup', |
| 414 |
'rw,nosuid,nodev,noexec,relatime,cpuset', |
| 415 |
'0', |
| 416 |
'0' |
| 417 |
], |
| 418 |
[ |
| 419 |
'cgroup', |
| 420 |
'/sys/fs/cgroup/cpu,cpuacct', |
| 421 |
'cgroup', |
| 422 |
'rw,nosuid,nodev,noexec,relatime,cpu,cpuacct', |
| 423 |
'0', |
| 424 |
'0' |
| 425 |
], |
| 426 |
[ |
| 427 |
'cgroup', |
| 428 |
'/sys/fs/cgroup/hugetlb', |
| 429 |
'cgroup', |
| 430 |
'rw,nosuid,nodev,noexec,relatime,hugetlb', |
| 431 |
'0', |
| 432 |
'0' |
| 433 |
], |
| 434 |
[ |
| 435 |
'cgroup', |
| 436 |
'/sys/fs/cgroup/perf_event', |
| 437 |
'cgroup', |
| 438 |
'rw,nosuid,nodev,noexec,relatime,perf_event', |
| 439 |
'0', |
| 440 |
'0' |
| 441 |
], |
| 442 |
[ |
| 443 |
'cgroup', |
| 444 |
'/sys/fs/cgroup/net_cls,net_prio', |
| 445 |
'cgroup', |
| 446 |
'rw,nosuid,nodev,noexec,relatime,net_cls,net_prio', |
| 447 |
'0', |
| 448 |
'0' |
| 449 |
], |
| 450 |
['configfs', '/sys/kernel/config', 'configfs', 'rw,relatime', '0', '0'], |
| 451 |
[ |
| 452 |
'/dev/mapper/fedora_dhcp129--186-root', |
| 453 |
'/', |
| 454 |
'ext4', |
| 455 |
'rw,seclabel,relatime,data=ordered', |
| 456 |
'0', |
| 457 |
'0' |
| 458 |
], |
| 459 |
['selinuxfs', '/sys/fs/selinux', 'selinuxfs', 'rw,relatime', '0', '0'], |
| 460 |
[ |
| 461 |
'systemd-1', |
| 462 |
'/proc/sys/fs/binfmt_misc', |
| 463 |
'autofs', |
| 464 |
'rw,relatime,fd=24,pgrp=1,timeout=0,minproto=5,maxproto=5,direct', |
| 465 |
'0', |
| 466 |
'0' |
| 467 |
], |
| 468 |
['debugfs', '/sys/kernel/debug', 'debugfs', 'rw,seclabel,relatime', '0', '0'], |
| 469 |
[ |
| 470 |
'hugetlbfs', |
| 471 |
'/dev/hugepages', |
| 472 |
'hugetlbfs', |
| 473 |
'rw,seclabel,relatime', |
| 474 |
'0', |
| 475 |
'0' |
| 476 |
], |
| 477 |
['tmpfs', '/tmp', 'tmpfs', 'rw,seclabel', '0', '0'], |
| 478 |
['mqueue', '/dev/mqueue', 'mqueue', 'rw,seclabel,relatime', '0', '0'], |
| 479 |
[ |
| 480 |
'/dev/loop0', |
| 481 |
'/var/lib/machines', |
| 482 |
'btrfs', |
| 483 |
'rw,seclabel,relatime,space_cache,subvolid=5,subvol=/', |
| 484 |
'0', |
| 485 |
'0' |
| 486 |
], |
| 487 |
['/dev/sda1', '/boot', 'ext4', 'rw,seclabel,relatime,data=ordered', '0', '0'], |
| 488 |
# A 'none' fstype |
| 489 |
['/dev/sdz3', '/not/a/real/device', 'none', 'rw,seclabel,relatime,data=ordered', '0', '0'], |
| 490 |
# lets assume this is a bindmount |
| 491 |
['/dev/sdz4', '/not/a/real/bind_mount', 'ext4', 'rw,seclabel,relatime,data=ordered', '0', '0'], |
| 492 |
[ |
| 493 |
'/dev/mapper/fedora_dhcp129--186-home', |
| 494 |
'/home', |
| 495 |
'ext4', |
| 496 |
'rw,seclabel,relatime,data=ordered', |
| 497 |
'0', |
| 498 |
'0' |
| 499 |
], |
| 500 |
[ |
| 501 |
'tmpfs', |
| 502 |
'/run/user/1000', |
| 503 |
'tmpfs', |
| 504 |
'rw,seclabel,nosuid,nodev,relatime,size=1611044k,mode=700,uid=1000,gid=1000', |
| 505 |
'0', |
| 506 |
'0' |
| 507 |
], |
| 508 |
[ |
| 509 |
'gvfsd-fuse', |
| 510 |
'/run/user/1000/gvfs', |
| 511 |
'fuse.gvfsd-fuse', |
| 512 |
'rw,nosuid,nodev,relatime,user_id=1000,group_id=1000', |
| 513 |
'0', |
| 514 |
'0' |
| 515 |
], |
| 516 |
['fusectl', '/sys/fs/fuse/connections', 'fusectl', 'rw,relatime', '0', '0'], |
| 517 |
# Mount path with space in the name |
| 518 |
# The space is encoded as \040 since the fields in /etc/mtab are space-delimited |
| 519 |
['/dev/sdz9', r'/mnt/foo\040bar', 'ext4', 'rw,relatime', '0', '0'], |
| 520 |
['\\\\Windows\\share', '/data/', 'cifs', 'credentials=/root/.creds', '0', '0'], |
| 521 |
] |
| 522 |
|
| 523 |
BIND_MOUNTS = ['/not/a/real/bind_mount'] |
| 524 |
|
| 525 |
with open(os.path.join(os.path.dirname(__file__), 'fixtures/findmount_output.txt')) as f: |
| 526 |
FINDMNT_OUTPUT = f.read() |
| 527 |
|
| 528 |
|
| 529 |
class TestFactsLinuxHardwareGetMountFacts(unittest.TestCase): |
| 530 |
|
| 531 |
# FIXME: mock.patch instead |
| 532 |
def setUp(self): |
| 533 |
# The @timeout tracebacks if there isn't a GATHER_TIMEOUT is None (the default until get_all_facts sets it via global) |
| 534 |
facts.GATHER_TIMEOUT = 10 |
| 535 |
|
| 536 |
def tearDown(self): |
| 537 |
facts.GATHER_TIMEOUT = None |
| 538 |
|
| 539 |
# The Hardware subclasses freakout if instaniated directly, so |
| 540 |
# mock platform.system and inst Hardware() so we get a LinuxHardware() |
| 541 |
# we can test. |
| 542 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._mtab_entries', return_value=MTAB_ENTRIES) |
| 543 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._find_bind_mounts', return_value=BIND_MOUNTS) |
| 544 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._lsblk_uuid', return_value=LSBLK_UUIDS) |
| 545 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._udevadm_uuid', return_value=UDEVADM_UUID) |
| 546 |
def test_get_mount_facts(self, |
| 547 |
mock_lsblk_uuid, |
| 548 |
mock_find_bind_mounts, |
| 549 |
mock_mtab_entries, |
| 550 |
mock_udevadm_uuid): |
| 551 |
module = Mock() |
| 552 |
# Returns a LinuxHardware-ish |
| 553 |
lh = hardware.linux.LinuxHardware(module=module, load_on_init=False) |
| 554 |
|
| 555 |
# Nothing returned, just self.facts modified as a side effect |
| 556 |
mount_facts = lh.get_mount_facts() |
| 557 |
self.assertIsInstance(mount_facts, dict) |
| 558 |
self.assertIn('mounts', mount_facts) |
| 559 |
self.assertIsInstance(mount_facts['mounts'], list) |
| 560 |
self.assertIsInstance(mount_facts['mounts'][0], dict) |
| 561 |
|
| 562 |
# Find mounts with space in the mountpoint path |
| 563 |
mounts_with_space = [x for x in mount_facts['mounts'] if ' ' in x['mount']] |
| 564 |
self.assertEqual(len(mounts_with_space), 1) |
| 565 |
self.assertEqual(mounts_with_space[0]['mount'], '/mnt/foo bar') |
| 566 |
|
| 567 |
@patch('ansible.module_utils.facts.hardware.linux.get_file_content', return_value=MTAB) |
| 568 |
def test_get_mtab_entries(self, mock_get_file_content): |
| 569 |
|
| 570 |
module = Mock() |
| 571 |
lh = hardware.linux.LinuxHardware(module=module, load_on_init=False) |
| 572 |
mtab_entries = lh._mtab_entries() |
| 573 |
self.assertIsInstance(mtab_entries, list) |
| 574 |
self.assertIsInstance(mtab_entries[0], list) |
| 575 |
self.assertEqual(len(mtab_entries), 39) |
| 576 |
|
| 577 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._run_findmnt', return_value=(0, FINDMNT_OUTPUT, '')) |
| 578 |
def test_find_bind_mounts(self, mock_run_findmnt): |
| 579 |
module = Mock() |
| 580 |
lh = hardware.linux.LinuxHardware(module=module, load_on_init=False) |
| 581 |
bind_mounts = lh._find_bind_mounts() |
| 582 |
|
| 583 |
# If bind_mounts becomes another seq type, feel free to change |
| 584 |
self.assertIsInstance(bind_mounts, set) |
| 585 |
self.assertEqual(len(bind_mounts), 1) |
| 586 |
self.assertIn('/not/a/real/bind_mount', bind_mounts) |
| 587 |
|
| 588 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._run_findmnt', return_value=(37, '', '')) |
| 589 |
def test_find_bind_mounts_non_zero(self, mock_run_findmnt): |
| 590 |
module = Mock() |
| 591 |
lh = hardware.linux.LinuxHardware(module=module, load_on_init=False) |
| 592 |
bind_mounts = lh._find_bind_mounts() |
| 593 |
|
| 594 |
self.assertIsInstance(bind_mounts, set) |
| 595 |
self.assertEqual(len(bind_mounts), 0) |
| 596 |
|
| 597 |
def test_find_bind_mounts_no_findmnts(self): |
| 598 |
module = Mock() |
| 599 |
module.get_bin_path = Mock(return_value=None) |
| 600 |
lh = hardware.linux.LinuxHardware(module=module, load_on_init=False) |
| 601 |
bind_mounts = lh._find_bind_mounts() |
| 602 |
|
| 603 |
self.assertIsInstance(bind_mounts, set) |
| 604 |
self.assertEqual(len(bind_mounts), 0) |
| 605 |
|
| 606 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._run_lsblk', return_value=(0, LSBLK_OUTPUT, '')) |
| 607 |
def test_lsblk_uuid(self, mock_run_lsblk): |
| 608 |
module = Mock() |
| 609 |
lh = hardware.linux.LinuxHardware(module=module, load_on_init=False) |
| 610 |
lsblk_uuids = lh._lsblk_uuid() |
| 611 |
|
| 612 |
self.assertIsInstance(lsblk_uuids, dict) |
| 613 |
self.assertIn(b'/dev/loop9', lsblk_uuids) |
| 614 |
self.assertIn(b'/dev/sda1', lsblk_uuids) |
| 615 |
self.assertEqual(lsblk_uuids[b'/dev/sda1'], b'32caaec3-ef40-4691-a3b6-438c3f9bc1c0') |
| 616 |
|
| 617 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._run_lsblk', return_value=(37, LSBLK_OUTPUT, '')) |
| 618 |
def test_lsblk_uuid_non_zero(self, mock_run_lsblk): |
| 619 |
module = Mock() |
| 620 |
lh = hardware.linux.LinuxHardware(module=module, load_on_init=False) |
| 621 |
lsblk_uuids = lh._lsblk_uuid() |
| 622 |
|
| 623 |
self.assertIsInstance(lsblk_uuids, dict) |
| 624 |
self.assertEqual(len(lsblk_uuids), 0) |
| 625 |
|
| 626 |
def test_lsblk_uuid_no_lsblk(self): |
| 627 |
module = Mock() |
| 628 |
module.get_bin_path = Mock(return_value=None) |
| 629 |
lh = hardware.linux.LinuxHardware(module=module, load_on_init=False) |
| 630 |
lsblk_uuids = lh._lsblk_uuid() |
| 631 |
|
| 632 |
self.assertIsInstance(lsblk_uuids, dict) |
| 633 |
self.assertEqual(len(lsblk_uuids), 0) |
| 634 |
|
| 635 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._run_lsblk', return_value=(0, LSBLK_OUTPUT_2, '')) |
| 636 |
def test_lsblk_uuid_dev_with_space_in_name(self, mock_run_lsblk): |
| 637 |
module = Mock() |
| 638 |
lh = hardware.linux.LinuxHardware(module=module, load_on_init=False) |
| 639 |
lsblk_uuids = lh._lsblk_uuid() |
| 640 |
self.assertIsInstance(lsblk_uuids, dict) |
| 641 |
self.assertIn(b'/dev/loop0', lsblk_uuids) |
| 642 |
self.assertIn(b'/dev/sda1', lsblk_uuids) |
| 643 |
self.assertEqual(lsblk_uuids[b'/dev/mapper/an-example-mapper with a space in the name'], b'84639acb-013f-4d2f-9392-526a572b4373') |
| 644 |
self.assertEqual(lsblk_uuids[b'/dev/sda1'], b'32caaec3-ef40-4691-a3b6-438c3f9bc1c0') |