| 1 |
"""Basic unit tests of parse_command_line().""" |
| 2 |
# pylint: disable=invalid-name,import-error |
| 3 |
# ruff: noqa: N803 |
| 4 |
|
| 5 |
import contextlib |
| 6 |
import copy |
| 7 |
import unittest |
| 8 |
import unittest.mock |
| 9 |
|
| 10 |
from foomuuri import CONFIG as BASE_CONFIG |
| 11 |
from foomuuri import INTERNAL as BASE_INTERNAL |
| 12 |
from foomuuri import minimal_config |
| 13 |
|
| 14 |
|
| 15 |
@unittest.mock.patch( |
| 16 |
'foomuuri.INTERNAL', new_callable=lambda: copy.deepcopy(BASE_INTERNAL) |
| 17 |
) |
| 18 |
@unittest.mock.patch( |
| 19 |
'foomuuri.CONFIG', new_callable=lambda: copy.deepcopy(BASE_CONFIG) |
| 20 |
) |
| 21 |
class TestParseConfigFoomuuri(unittest.TestCase): |
| 22 |
"""Test parse_config_foomuuri().""" |
| 23 |
|
| 24 |
@staticmethod |
| 25 |
@contextlib.contextmanager |
| 26 |
def mock_config_foomuuri(option, value): |
| 27 |
"""Return context manager of foomuuri.find_config_files() patch. |
| 28 |
|
| 29 |
foomuuri.find_config_files() is patched to return generated |
| 30 |
foomuuri configuration file, containing foomuuri{} section |
| 31 |
with single option/value. |
| 32 |
""" |
| 33 |
# MagicMock of pathlib.PosixPAth returning desired foomuuri config |
| 34 |
config_file = unittest.mock.MagicMock() |
| 35 |
config_file.read_text.return_value = f""" |
| 36 |
foomuuri {{ |
| 37 |
{option} {value} |
| 38 |
}} |
| 39 |
""" |
| 40 |
# foomuuri.find_config_files() is called by foomuuri.read_config() |
| 41 |
# to load configuration files. |
| 42 |
# read_config() tries loading share_config first, and etc_config then. |
| 43 |
# We only care about etc_config, therefore side_effect list passed |
| 44 |
# to foomuuri.find_config_files mock patch has two elemeents, |
| 45 |
# [] - empty share_config, and [config_file] - generated config. |
| 46 |
with unittest.mock.patch( |
| 47 |
'foomuuri.find_config_files', side_effect=[[], [config_file]] |
| 48 |
): |
| 49 |
yield config_file |
| 50 |
|
| 51 |
@unittest.mock.patch('foomuuri.warning') |
| 52 |
def test_try_reload_timeout(self, warning, CONFIG, *_): |
| 53 |
"""Test try-reload_timeout backwards compatible option.""" |
| 54 |
with self.mock_config_foomuuri( |
| 55 |
'try-reload_timeout', |
| 56 |
69, |
| 57 |
) as config_file: |
| 58 |
_ = minimal_config() |
| 59 |
self.assertEqual(CONFIG.try_reload_timeout, 69) |
| 60 |
warning.assert_called_once_with( |
| 61 |
f'File {config_file} line 3: foomuuri{{}} option ' |
| 62 |
f'"try-reload_timeout" is obsolete, use ' |
| 63 |
f'"try_reload_timeout" instead' |
| 64 |
) |
| 65 |
|
| 66 |
def test_append_value(self, CONFIG, *_): |
| 67 |
"""Test appending of option value.""" |
| 68 |
with self.mock_config_foomuuri('log_level', '+ append'): |
| 69 |
_ = minimal_config() |
| 70 |
self.assertEqual(CONFIG.log_level, 'level info flags skuid append') |
| 71 |
|
| 72 |
def test_unknown_option(self, *_): |
| 73 |
"""Test assigning unknown option.""" |
| 74 |
with ( |
| 75 |
self.mock_config_foomuuri( |
| 76 |
'unknown_option', 'value' |
| 77 |
) as config_file, |
| 78 |
unittest.mock.patch( |
| 79 |
'foomuuri.fail', side_effect=SystemExit |
| 80 |
) as fail, |
| 81 |
): |
| 82 |
self.assertRaises(SystemExit, minimal_config) |
| 83 |
fail.assert_called_once_with( |
| 84 |
f'File {config_file} line 3: Unknown foomuuri{{}}' |
| 85 |
' option: unknown_option value' |
| 86 |
) |
| 87 |
|
| 88 |
def test_invalid_value(self, *_): |
| 89 |
"""Test assigning invalid value.""" |
| 90 |
with ( |
| 91 |
self.mock_config_foomuuri( |
| 92 |
'priority_offset', 'not_an_int' |
| 93 |
) as config_file, |
| 94 |
unittest.mock.patch( |
| 95 |
'foomuuri.fail', side_effect=SystemExit |
| 96 |
) as fail, |
| 97 |
): |
| 98 |
self.assertRaises(SystemExit, minimal_config) |
| 99 |
fail.assert_called_once_with( |
| 100 |
f'File {config_file} line 3: Invalid foomuuri{{}}' |
| 101 |
' option value: priority_offset not_an_int' |
| 102 |
) |
| 103 |
|
| 104 |
@unittest.mock.patch('foomuuri.CONFIG_OVERRIDE', new_callable=dict) |
| 105 |
def test_config_override(self, CONFIG_OVERRIDE, CONFIG, *_): |
| 106 |
"""Test overriding of CONFIG from CONFIG_OVERRRIDE.""" |
| 107 |
|
| 108 |
def test_override(option, override, config): |
| 109 |
with self.mock_config_foomuuri(option, '0'): |
| 110 |
CONFIG_OVERRIDE[option] = override |
| 111 |
_ = minimal_config() |
| 112 |
self.assertEqual(CONFIG[option], config) |
| 113 |
|
| 114 |
# Overriding with correct value type (str) |
| 115 |
test_override(option='priority_offset', override='69', config=69) |
| 116 |
# Overriding with invalid value type (int). |
| 117 |
test_override(option='priority_offset', override=69, config=69) |
| 118 |
|
| 119 |
def test_postprocess_priority_offset(self, _, INTERNAL): |
| 120 |
"""Test postprocessing of priority_offset option.""" |
| 121 |
|
| 122 |
def test_priority_offset(config, internal): |
| 123 |
with self.mock_config_foomuuri('priority_offset', config): |
| 124 |
_ = minimal_config() |
| 125 |
self.assertEqual(INTERNAL.priority_offset, internal) |
| 126 |
|
| 127 |
test_priority_offset(config=-1, internal=' - 1') |
| 128 |
test_priority_offset(config=0, internal='') |
| 129 |
test_priority_offset(config=1, internal=' + 1') |
| 130 |
|
| 131 |
def test_postprocess_log_rates(self, CONFIG, *_): |
| 132 |
"""Test postprocessing of log_* rates options.""" |
| 133 |
with self.mock_config_foomuuri('log_rate', '1/second burst 3'): |
| 134 |
_ = minimal_config() |
| 135 |
self.assertTrue(CONFIG.log_rate.endswith(' packets')) |