Debian Codegraph @

test/units/module_utils/facts/test_sysctl.py

raw
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 # for testing
22 import unittest
23 from unittest.mock import MagicMock
24
25 from ansible.module_utils.facts.sysctl import get_sysctl
26
27
28 # `sysctl hw` on an openbsd machine
29 OPENBSD_SYSCTL_HW = """
30 hw.machine=amd64
31 hw.model=AMD EPYC Processor (with IBPB)
32 hw.ncpu=1
33 hw.byteorder=1234
34 hw.pagesize=4096
35 hw.disknames=cd0:,sd0:9e1bd96cb20ab429,fd0:
36 hw.diskcount=3
37 hw.sensors.viomb0.raw0=0 (desired)
38 hw.sensors.viomb0.raw1=0 (current)
39 hw.cpuspeed=3394
40 hw.vendor=QEMU
41 hw.product=Standard PC (i440FX + PIIX, 1996)
42 hw.version=pc-i440fx-5.1
43 hw.uuid=5833415a-eefc-964f-a306-fa434d44d117
44 hw.physmem=1056804864
45 hw.usermem=1056792576
46 hw.ncpufound=1
47 hw.allowpowerdown=1
48 hw.smt=0
49 hw.ncpuonline=1
50 """
51
52 # partial output of `sysctl kern` on an openbsd machine
53 # for testing multiline parsing
54 OPENBSD_SYSCTL_KERN_PARTIAL = """
55 kern.ostype=OpenBSD
56 kern.osrelease=6.7
57 kern.osrevision=202005
58 kern.version=OpenBSD 6.7 (GENERIC) #179: Thu May 7 11:02:37 MDT 2020
59 [email protected]:/usr/src/sys/arch/amd64/compile/GENERIC
60
61 kern.maxvnodes=12447
62 kern.maxproc=1310
63 kern.maxfiles=7030
64 kern.argmax=524288
65 kern.securelevel=1
66 kern.hostname=openbsd67.vm.home.elrod.me
67 kern.hostid=0
68 kern.clockrate=tick = 10000, tickadj = 40, hz = 100, profhz = 100, stathz = 100
69 kern.posix1version=200809
70 """
71
72 # partial output of `sysctl vm` on Linux. The output has tabs in it and Linux
73 # sysctl has spaces around the =
74 LINUX_SYSCTL_VM_PARTIAL = """
75 vm.dirty_background_ratio = 10
76 vm.dirty_bytes = 0
77 vm.dirty_expire_centisecs = 3000
78 vm.dirty_ratio = 20
79 vm.dirty_writeback_centisecs = 500
80 vm.dirtytime_expire_seconds = 43200
81 vm.extfrag_threshold = 500
82 vm.hugetlb_shm_group = 0
83 vm.laptop_mode = 0
84 vm.legacy_va_layout = 0
85 vm.lowmem_reserve_ratio = 256 256 32 0
86 vm.max_map_count = 65530
87 vm.min_free_kbytes = 22914
88 vm.min_slab_ratio = 5
89 """
90
91 # partial output of `sysctl vm` on macOS. The output is colon-separated.
92 MACOS_SYSCTL_VM_PARTIAL = """
93 vm.loadavg: { 1.28 1.18 1.13 }
94 vm.swapusage: total = 2048.00M used = 1017.50M free = 1030.50M (encrypted)
95 vm.cs_force_kill: 0
96 vm.cs_force_hard: 0
97 vm.cs_debug: 0
98 vm.cs_debug_fail_on_unsigned_code: 0
99 vm.cs_debug_unsigned_exec_failures: 0
100 vm.cs_debug_unsigned_mmap_failures: 0
101 vm.cs_all_vnodes: 0
102 vm.cs_system_enforcement: 1
103 vm.cs_process_enforcement: 0
104 vm.cs_enforcement_panic: 0
105 vm.cs_library_validation: 0
106 vm.global_user_wire_limit: 3006477107
107 """
108
109 # Invalid/bad output
110 BAD_SYSCTL = """
111 this.output.is.invalid
112 it.has.no.equals.sign.or.colon
113 so.it.should.fail.to.parse
114 """
115
116 # Mixed good/bad output
117 GOOD_BAD_SYSCTL = """
118 bad.output.here
119 hw.smt=0
120 and.bad.output.here
121 """
122
123
124 class TestSysctlParsingInFacts(unittest.TestCase):
125
126 def test_get_sysctl_missing_binary(self):
127 module = MagicMock()
128 module.get_bin_path.return_value = '/usr/sbin/sysctl'
129 module.run_command.side_effect = ValueError
130 self.assertRaises(ValueError, get_sysctl, module, ['vm'])
131
132 def test_get_sysctl_nonzero_rc(self):
133 module = MagicMock()
134 module.get_bin_path.return_value = '/usr/sbin/sysctl'
135 module.run_command.return_value = (1, '', '')
136 sysctl = get_sysctl(module, ['hw'])
137 self.assertEqual(sysctl, {})
138
139 def test_get_sysctl_command_error(self):
140 module = MagicMock()
141 module.get_bin_path.return_value = '/usr/sbin/sysctl'
142 module.reset_mock()
143 module.run_command.side_effect = OSError('foo')
144 sysctl = get_sysctl(module, ['hw'])
145 for call in module.error_as_warning.call_args_list:
146 self.assertIn('Unable to read sysctl', call[0][0])
147 self.assertEqual(sysctl, {})
148
149 def test_get_sysctl_all_invalid_output(self):
150 module = MagicMock()
151 module.get_bin_path.return_value = '/sbin/sysctl'
152 module.run_command.return_value = (0, BAD_SYSCTL, '')
153 sysctl = get_sysctl(module, ['hw'])
154 module.run_command.assert_called_once_with(['/sbin/sysctl', 'hw'])
155 lines = [l for l in BAD_SYSCTL.splitlines() if l]
156 for call in module.error_as_warning.call_args_list:
157 self.assertIn('Unable to split sysctl line', call[0][0])
158 self.assertEqual(module.error_as_warning.call_count, len(lines))
159 self.assertEqual(sysctl, {})
160
161 def test_get_sysctl_mixed_invalid_output(self):
162 module = MagicMock()
163 module.get_bin_path.return_value = '/sbin/sysctl'
164 module.run_command.return_value = (0, GOOD_BAD_SYSCTL, '')
165 sysctl = get_sysctl(module, ['hw'])
166 module.run_command.assert_called_once_with(['/sbin/sysctl', 'hw'])
167 bad_lines = ['bad.output.here', 'and.bad.output.here']
168 for call in module.error_as_warning.call_args_list:
169 self.assertIn('Unable to split sysctl line', call[0][0])
170 self.assertEqual(module.error_as_warning.call_count, 2)
171 self.assertEqual(sysctl, {'hw.smt': '0'})
172
173 def test_get_sysctl_openbsd_hw(self):
174 expected_lines = [l for l in OPENBSD_SYSCTL_HW.splitlines() if l]
175 module = MagicMock()
176 module.get_bin_path.return_value = '/sbin/sysctl'
177 module.run_command.return_value = (0, OPENBSD_SYSCTL_HW, '')
178 sysctl = get_sysctl(module, ['hw'])
179 module.run_command.assert_called_once_with(['/sbin/sysctl', 'hw'])
180 self.assertEqual(len(sysctl), len(expected_lines))
181 self.assertEqual(sysctl['hw.machine'], 'amd64') # first line
182 self.assertEqual(sysctl['hw.smt'], '0') # random line
183 self.assertEqual(sysctl['hw.ncpuonline'], '1') # last line
184 # weird chars in value
185 self.assertEqual(
186 sysctl['hw.disknames'],
187 'cd0:,sd0:9e1bd96cb20ab429,fd0:')
188 # more symbols/spaces in value
189 self.assertEqual(
190 sysctl['hw.product'],
191 'Standard PC (i440FX + PIIX, 1996)')
192
193 def test_get_sysctl_openbsd_kern(self):
194 module = MagicMock()
195 module.get_bin_path.return_value = '/sbin/sysctl'
196 module.run_command.return_value = (0, OPENBSD_SYSCTL_KERN_PARTIAL, '')
197 sysctl = get_sysctl(module, ['kern'])
198 module.run_command.assert_called_once_with(['/sbin/sysctl', 'kern'])
199 self.assertEqual(
200 len(sysctl),
201 len(
202 [l for l
203 in OPENBSD_SYSCTL_KERN_PARTIAL.splitlines()
204 if l.startswith('kern')]))
205 self.assertEqual(sysctl['kern.ostype'], 'OpenBSD') # first line
206 self.assertEqual(sysctl['kern.maxproc'], '1310') # random line
207 self.assertEqual(sysctl['kern.posix1version'], '200809') # last line
208 # multiline
209 self.assertEqual(
210 sysctl['kern.version'],
211 'OpenBSD 6.7 (GENERIC) #179: Thu May 7 11:02:37 MDT 2020\n '
212 '[email protected]:/usr/src/sys/arch/amd64/compile/GENERIC')
213 # more symbols/spaces in value
214 self.assertEqual(
215 sysctl['kern.clockrate'],
216 'tick = 10000, tickadj = 40, hz = 100, profhz = 100, stathz = 100')
217
218 def test_get_sysctl_linux_vm(self):
219 module = MagicMock()
220 module.get_bin_path.return_value = '/usr/sbin/sysctl'
221 module.run_command.return_value = (0, LINUX_SYSCTL_VM_PARTIAL, '')
222 sysctl = get_sysctl(module, ['vm'])
223 module.run_command.assert_called_once_with(['/usr/sbin/sysctl', 'vm'])
224 self.assertEqual(
225 len(sysctl),
226 len([l for l in LINUX_SYSCTL_VM_PARTIAL.splitlines() if l]))
227 self.assertEqual(sysctl['vm.dirty_background_ratio'], '10')
228 self.assertEqual(sysctl['vm.laptop_mode'], '0')
229 self.assertEqual(sysctl['vm.min_slab_ratio'], '5')
230 # tabs
231 self.assertEqual(sysctl['vm.lowmem_reserve_ratio'], '256\t256\t32\t0')
232
233 def test_get_sysctl_macos_vm(self):
234 module = MagicMock()
235 module.get_bin_path.return_value = '/usr/sbin/sysctl'
236 module.run_command.return_value = (0, MACOS_SYSCTL_VM_PARTIAL, '')
237 sysctl = get_sysctl(module, ['vm'])
238 module.run_command.assert_called_once_with(['/usr/sbin/sysctl', 'vm'])
239 self.assertEqual(
240 len(sysctl),
241 len([l for l in MACOS_SYSCTL_VM_PARTIAL.splitlines() if l]))
242 self.assertEqual(sysctl['vm.loadavg'], '{ 1.28 1.18 1.13 }')
243 self.assertEqual(
244 sysctl['vm.swapusage'],
245 'total = 2048.00M used = 1017.50M free = 1030.50M (encrypted)')