| 1 |
# This file is part of Ansible |
| 2 |
# |
| 3 |
# Ansible is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# Ansible is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
from __future__ import annotations |
| 17 |
|
| 18 |
import os |
| 19 |
|
| 20 |
import unittest |
| 21 |
from unittest.mock import Mock, patch |
| 22 |
|
| 23 |
from ansible.module_utils.facts import timeout |
| 24 |
|
| 25 |
from ansible.module_utils.facts.hardware import linux |
| 26 |
|
| 27 |
from . linux_data import LSBLK_OUTPUT, LSBLK_OUTPUT_2, LSBLK_UUIDS, MTAB, MTAB_ENTRIES, BIND_MOUNTS, STATVFS_INFO, UDEVADM_UUID, UDEVADM_OUTPUT, SG_INQ_OUTPUTS |
| 28 |
|
| 29 |
with open(os.path.join(os.path.dirname(__file__), '../fixtures/findmount_output.txt')) as f: |
| 30 |
FINDMNT_OUTPUT = f.read() |
| 31 |
|
| 32 |
|
| 33 |
def mock_get_mount_size(mountpoint): |
| 34 |
return STATVFS_INFO.get(mountpoint, {}) |
| 35 |
|
| 36 |
|
| 37 |
class TestFactsLinuxHardwareGetMountFacts(unittest.TestCase): |
| 38 |
|
| 39 |
# FIXME: mock.patch instead |
| 40 |
def setUp(self): |
| 41 |
timeout.GATHER_TIMEOUT = 10 |
| 42 |
|
| 43 |
def tearDown(self): |
| 44 |
timeout.GATHER_TIMEOUT = None |
| 45 |
|
| 46 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._mtab_entries', return_value=MTAB_ENTRIES) |
| 47 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._find_bind_mounts', return_value=BIND_MOUNTS) |
| 48 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._lsblk_uuid', return_value=LSBLK_UUIDS) |
| 49 |
@patch('ansible.module_utils.facts.hardware.linux.get_mount_size', side_effect=mock_get_mount_size) |
| 50 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._udevadm_uuid', return_value=UDEVADM_UUID) |
| 51 |
def test_get_mount_facts(self, |
| 52 |
mock_get_mount_size, |
| 53 |
mock_lsblk_uuid, |
| 54 |
mock_find_bind_mounts, |
| 55 |
mock_mtab_entries, |
| 56 |
mock_udevadm_uuid): |
| 57 |
module = Mock() |
| 58 |
# Returns a LinuxHardware-ish |
| 59 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 60 |
|
| 61 |
# Nothing returned, just self.facts modified as a side effect |
| 62 |
mount_facts = lh.get_mount_facts() |
| 63 |
self.assertIsInstance(mount_facts, dict) |
| 64 |
self.assertIn('mounts', mount_facts) |
| 65 |
self.assertIsInstance(mount_facts['mounts'], list) |
| 66 |
self.assertIsInstance(mount_facts['mounts'][0], dict) |
| 67 |
|
| 68 |
home_expected = {'block_available': 1001578731, |
| 69 |
'block_size': 4096, |
| 70 |
'block_total': 105871006, |
| 71 |
'block_used': 5713133, |
| 72 |
'device': '/dev/mapper/fedora_dhcp129--186-home', |
| 73 |
'dump': 0, |
| 74 |
'fstype': 'ext4', |
| 75 |
'inode_available': 26860880, |
| 76 |
'inode_total': 26902528, |
| 77 |
'inode_used': 41648, |
| 78 |
'mount': '/home', |
| 79 |
'options': 'rw,seclabel,relatime,data=ordered', |
| 80 |
'passno': 0, |
| 81 |
'size_available': 410246647808, |
| 82 |
'size_total': 433647640576, |
| 83 |
'uuid': 'N/A'} |
| 84 |
home_info = [x for x in mount_facts['mounts'] if x['mount'] == '/home'][0] |
| 85 |
|
| 86 |
self.maxDiff = 4096 |
| 87 |
self.assertDictEqual(home_info, home_expected) |
| 88 |
|
| 89 |
@patch('ansible.module_utils.facts.hardware.linux.get_file_content', return_value=MTAB) |
| 90 |
def test_get_mtab_entries(self, mock_get_file_content): |
| 91 |
|
| 92 |
module = Mock() |
| 93 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 94 |
mtab_entries = lh._mtab_entries() |
| 95 |
self.assertIsInstance(mtab_entries, list) |
| 96 |
self.assertIsInstance(mtab_entries[0], list) |
| 97 |
self.assertEqual(len(mtab_entries), 38) |
| 98 |
|
| 99 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._run_findmnt', return_value=(0, FINDMNT_OUTPUT, '')) |
| 100 |
def test_find_bind_mounts(self, mock_run_findmnt): |
| 101 |
module = Mock() |
| 102 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 103 |
bind_mounts = lh._find_bind_mounts() |
| 104 |
|
| 105 |
# If bind_mounts becomes another seq type, feel free to change |
| 106 |
self.assertIsInstance(bind_mounts, set) |
| 107 |
self.assertEqual(len(bind_mounts), 1) |
| 108 |
self.assertIn('/not/a/real/bind_mount', bind_mounts) |
| 109 |
|
| 110 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._run_findmnt', return_value=(37, '', '')) |
| 111 |
def test_find_bind_mounts_non_zero(self, mock_run_findmnt): |
| 112 |
module = Mock() |
| 113 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 114 |
bind_mounts = lh._find_bind_mounts() |
| 115 |
|
| 116 |
self.assertIsInstance(bind_mounts, set) |
| 117 |
self.assertEqual(len(bind_mounts), 0) |
| 118 |
|
| 119 |
def test_find_bind_mounts_no_findmnts(self): |
| 120 |
module = Mock() |
| 121 |
module.get_bin_path = Mock(return_value=None) |
| 122 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 123 |
bind_mounts = lh._find_bind_mounts() |
| 124 |
|
| 125 |
self.assertIsInstance(bind_mounts, set) |
| 126 |
self.assertEqual(len(bind_mounts), 0) |
| 127 |
|
| 128 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._run_lsblk', return_value=(0, LSBLK_OUTPUT, '')) |
| 129 |
def test_lsblk_uuid(self, mock_run_lsblk): |
| 130 |
module = Mock() |
| 131 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 132 |
lsblk_uuids = lh._lsblk_uuid() |
| 133 |
|
| 134 |
self.assertIsInstance(lsblk_uuids, dict) |
| 135 |
self.assertIn(b'/dev/loop9', lsblk_uuids) |
| 136 |
self.assertIn(b'/dev/sda1', lsblk_uuids) |
| 137 |
self.assertEqual(lsblk_uuids[b'/dev/sda1'], b'32caaec3-ef40-4691-a3b6-438c3f9bc1c0') |
| 138 |
|
| 139 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._run_lsblk', return_value=(37, LSBLK_OUTPUT, '')) |
| 140 |
def test_lsblk_uuid_non_zero(self, mock_run_lsblk): |
| 141 |
module = Mock() |
| 142 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 143 |
lsblk_uuids = lh._lsblk_uuid() |
| 144 |
|
| 145 |
self.assertIsInstance(lsblk_uuids, dict) |
| 146 |
self.assertEqual(len(lsblk_uuids), 0) |
| 147 |
|
| 148 |
def test_lsblk_uuid_no_lsblk(self): |
| 149 |
module = Mock() |
| 150 |
module.get_bin_path = Mock(return_value=None) |
| 151 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 152 |
lsblk_uuids = lh._lsblk_uuid() |
| 153 |
|
| 154 |
self.assertIsInstance(lsblk_uuids, dict) |
| 155 |
self.assertEqual(len(lsblk_uuids), 0) |
| 156 |
|
| 157 |
@patch('ansible.module_utils.facts.hardware.linux.LinuxHardware._run_lsblk', return_value=(0, LSBLK_OUTPUT_2, '')) |
| 158 |
def test_lsblk_uuid_dev_with_space_in_name(self, mock_run_lsblk): |
| 159 |
module = Mock() |
| 160 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 161 |
lsblk_uuids = lh._lsblk_uuid() |
| 162 |
self.assertIsInstance(lsblk_uuids, dict) |
| 163 |
self.assertIn(b'/dev/loop0', lsblk_uuids) |
| 164 |
self.assertIn(b'/dev/sda1', lsblk_uuids) |
| 165 |
self.assertEqual(lsblk_uuids[b'/dev/mapper/an-example-mapper with a space in the name'], b'84639acb-013f-4d2f-9392-526a572b4373') |
| 166 |
self.assertEqual(lsblk_uuids[b'/dev/sda1'], b'32caaec3-ef40-4691-a3b6-438c3f9bc1c0') |
| 167 |
|
| 168 |
def test_udevadm_uuid(self): |
| 169 |
module = Mock() |
| 170 |
module.run_command = Mock(return_value=(0, UDEVADM_OUTPUT, '')) # (rc, out, err) |
| 171 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 172 |
udevadm_uuid = lh._udevadm_uuid('mock_device') |
| 173 |
|
| 174 |
self.assertEqual(udevadm_uuid, '57b1a3e7-9019-4747-9809-7ec52bba9179') |
| 175 |
|
| 176 |
def test_get_sg_inq_serial(self): |
| 177 |
# Valid outputs |
| 178 |
for sq_inq_output in SG_INQ_OUTPUTS: |
| 179 |
module = Mock() |
| 180 |
module.run_command = Mock(return_value=(0, sq_inq_output, '')) # (rc, out, err) |
| 181 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 182 |
sg_inq_serial = lh._get_sg_inq_serial('/usr/bin/sg_inq', 'nvme0n1') |
| 183 |
self.assertEqual(sg_inq_serial, 'vol0123456789') |
| 184 |
|
| 185 |
# Invalid output |
| 186 |
module = Mock() |
| 187 |
module.run_command = Mock(return_value=(0, '', '')) # (rc, out, err) |
| 188 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 189 |
sg_inq_serial = lh._get_sg_inq_serial('/usr/bin/sg_inq', 'nvme0n1') |
| 190 |
self.assertEqual(sg_inq_serial, None) |
| 191 |
|
| 192 |
# Non zero rc |
| 193 |
module = Mock() |
| 194 |
module.run_command = Mock(return_value=(42, '', 'Error 42')) # (rc, out, err) |
| 195 |
lh = linux.LinuxHardware(module=module, load_on_init=False) |
| 196 |
sg_inq_serial = lh._get_sg_inq_serial('/usr/bin/sg_inq', 'nvme0n1') |
| 197 |
self.assertEqual(sg_inq_serial, None) |