| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright (c) 2021, Felix Fontein (@felixfontein) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
import re |
| 11 |
import sys |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from ansible_collections.community.routeros.plugins.module_utils._api_data import ( |
| 16 |
PATHS, |
| 17 |
) |
| 18 |
|
| 19 |
from ansible_collections.community.routeros.plugins.module_utils._api_helper import ( |
| 20 |
value_to_str, |
| 21 |
_test_rule_except_invert, |
| 22 |
validate_and_prepare_restrict, |
| 23 |
restrict_entry_accepted, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
VALUE_TO_STR = [ |
| 28 |
(None, u'', {'none_to_empty': True}), |
| 29 |
(None, None, {'none_to_empty': False}), |
| 30 |
(None, None, {}), |
| 31 |
('', u'', {}), |
| 32 |
('foo', u'foo', {}), |
| 33 |
(True, u'true', {'compat_bool': True}), |
| 34 |
(False, u'false', {'compat_bool': True}), |
| 35 |
(True, u'yes', {'compat_bool': False}), |
| 36 |
(False, u'no', {'compat_bool': False}), |
| 37 |
(True, u'yes', {}), |
| 38 |
(False, u'no', {}), |
| 39 |
('true', u'true', {}), |
| 40 |
('false', u'false', {}), |
| 41 |
('yes', u'yes', {}), |
| 42 |
('no', u'no', {}), |
| 43 |
([], u'[]', {}), |
| 44 |
({}, u'{}', {}), |
| 45 |
(1, u'1', {}), |
| 46 |
(-42, u'-42', {}), |
| 47 |
(1.5, u'1.5', {}), |
| 48 |
(1.0, u'1.0', {}), |
| 49 |
] |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.parametrize("value, expected, kwargs", VALUE_TO_STR) |
| 53 |
def test_value_to_str(value, expected, kwargs): |
| 54 |
result = value_to_str(value, **kwargs) |
| 55 |
print(repr(result)) |
| 56 |
assert result == expected |
| 57 |
|
| 58 |
|
| 59 |
TEST_RULE_EXCEPT_INVERT = [ |
| 60 |
( |
| 61 |
None, |
| 62 |
{ |
| 63 |
'field': u'foo', |
| 64 |
'match_disabled': False, |
| 65 |
'invert': False, |
| 66 |
}, |
| 67 |
False, |
| 68 |
), |
| 69 |
( |
| 70 |
None, |
| 71 |
{ |
| 72 |
'field': u'foo', |
| 73 |
'match_disabled': True, |
| 74 |
'invert': False, |
| 75 |
}, |
| 76 |
True, |
| 77 |
), |
| 78 |
( |
| 79 |
1, |
| 80 |
{ |
| 81 |
'field': u'foo', |
| 82 |
'match_disabled': False, |
| 83 |
'invert': False, |
| 84 |
'values': [1], |
| 85 |
}, |
| 86 |
True, |
| 87 |
), |
| 88 |
( |
| 89 |
1, |
| 90 |
{ |
| 91 |
'field': u'foo', |
| 92 |
'match_disabled': False, |
| 93 |
'invert': False, |
| 94 |
'values': ['1'], |
| 95 |
}, |
| 96 |
False, |
| 97 |
), |
| 98 |
( |
| 99 |
1, |
| 100 |
{ |
| 101 |
'field': u'foo', |
| 102 |
'match_disabled': False, |
| 103 |
'invert': False, |
| 104 |
'regex': re.compile(u'^1$'), |
| 105 |
'regex_source': u'^1$', |
| 106 |
}, |
| 107 |
True, |
| 108 |
), |
| 109 |
( |
| 110 |
1.10, |
| 111 |
{ |
| 112 |
'field': u'foo', |
| 113 |
'match_disabled': False, |
| 114 |
'invert': False, |
| 115 |
'regex': re.compile(u'^1\\.1$'), |
| 116 |
'regex_source': u'^1\\.1$', |
| 117 |
}, |
| 118 |
True, |
| 119 |
), |
| 120 |
( |
| 121 |
10, |
| 122 |
{ |
| 123 |
'field': u'foo', |
| 124 |
'match_disabled': False, |
| 125 |
'invert': False, |
| 126 |
'regex': re.compile(u'^1$'), |
| 127 |
'regex_source': u'^1$', |
| 128 |
}, |
| 129 |
False, |
| 130 |
), |
| 131 |
] |
| 132 |
|
| 133 |
|
| 134 |
@pytest.mark.parametrize("value, rule, expected", TEST_RULE_EXCEPT_INVERT) |
| 135 |
def test_rule_except_invert(value, rule, expected): |
| 136 |
result = _test_rule_except_invert(value, rule, compat=True) |
| 137 |
print(repr(result)) |
| 138 |
assert result == expected |
| 139 |
|
| 140 |
|
| 141 |
_test_path = PATHS[('ip', 'firewall', 'filter')] |
| 142 |
_test_path.provide_version('7.0') |
| 143 |
TEST_PATH = _test_path.get_data() |
| 144 |
|
| 145 |
|
| 146 |
class FailJsonExc(Exception): |
| 147 |
def __init__(self, msg, kwargs): |
| 148 |
self.msg = msg |
| 149 |
self.kwargs = kwargs |
| 150 |
|
| 151 |
|
| 152 |
class FakeModule(object): |
| 153 |
def __init__(self, restrict_value): |
| 154 |
self.params = { |
| 155 |
'restrict': restrict_value, |
| 156 |
} |
| 157 |
|
| 158 |
def fail_json(self, msg, **kwargs): |
| 159 |
raise FailJsonExc(msg, kwargs) |
| 160 |
|
| 161 |
|
| 162 |
TEST_VALIDATE_AND_PREPARE_RESTRICT = [ |
| 163 |
( |
| 164 |
[{ |
| 165 |
'field': u'chain', |
| 166 |
'match_disabled': False, |
| 167 |
'values': None, |
| 168 |
'regex': None, |
| 169 |
'invert': False, |
| 170 |
}], |
| 171 |
[{ |
| 172 |
'field': u'chain', |
| 173 |
'match_disabled': False, |
| 174 |
'invert': False, |
| 175 |
}], |
| 176 |
), |
| 177 |
( |
| 178 |
[{ |
| 179 |
'field': u'comment', |
| 180 |
'match_disabled': True, |
| 181 |
'values': None, |
| 182 |
'regex': None, |
| 183 |
'invert': False, |
| 184 |
}], |
| 185 |
[{ |
| 186 |
'field': u'comment', |
| 187 |
'match_disabled': True, |
| 188 |
'invert': False, |
| 189 |
}], |
| 190 |
), |
| 191 |
( |
| 192 |
[{ |
| 193 |
'field': u'comment', |
| 194 |
'match_disabled': False, |
| 195 |
'values': None, |
| 196 |
'regex': None, |
| 197 |
'invert': True, |
| 198 |
}], |
| 199 |
[{ |
| 200 |
'field': u'comment', |
| 201 |
'match_disabled': False, |
| 202 |
'invert': True, |
| 203 |
}], |
| 204 |
), |
| 205 |
] |
| 206 |
|
| 207 |
if sys.version_info >= (2, 7, 17): |
| 208 |
# Somewhere between Python 2.7.15 (used by Ansible 3.9) and 2.7.17 (used by ansible-base 2.10) |
| 209 |
# something changed with ``==`` for ``re.Pattern``, at least for some patterns |
| 210 |
# (my guess is: for ``re.compile(u'')``) |
| 211 |
TEST_VALIDATE_AND_PREPARE_RESTRICT.extend([ |
| 212 |
( |
| 213 |
[ |
| 214 |
{ |
| 215 |
'field': u'comment', |
| 216 |
'match_disabled': False, |
| 217 |
'values': [], |
| 218 |
'regex': None, |
| 219 |
'invert': False, |
| 220 |
}, |
| 221 |
{ |
| 222 |
'field': u'comment', |
| 223 |
'match_disabled': False, |
| 224 |
'values': [None, 1, 42.0, True, u'foo', [], {}], |
| 225 |
'regex': None, |
| 226 |
'invert': False, |
| 227 |
}, |
| 228 |
{ |
| 229 |
'field': u'chain', |
| 230 |
'match_disabled': False, |
| 231 |
'values': None, |
| 232 |
'regex': u'', |
| 233 |
'invert': True, |
| 234 |
}, |
| 235 |
{ |
| 236 |
'field': u'chain', |
| 237 |
'match_disabled': False, |
| 238 |
'values': None, |
| 239 |
'regex': u'foo', |
| 240 |
'invert': False, |
| 241 |
}, |
| 242 |
], |
| 243 |
[ |
| 244 |
{ |
| 245 |
'field': u'comment', |
| 246 |
'match_disabled': False, |
| 247 |
'invert': False, |
| 248 |
'values': [], |
| 249 |
}, |
| 250 |
{ |
| 251 |
'field': u'comment', |
| 252 |
'match_disabled': False, |
| 253 |
'invert': False, |
| 254 |
'values': [None, 1, 42.0, True, u'foo', [], {}], |
| 255 |
}, |
| 256 |
{ |
| 257 |
'field': u'chain', |
| 258 |
'match_disabled': False, |
| 259 |
'invert': True, |
| 260 |
'regex': re.compile(u''), |
| 261 |
'regex_source': u'', |
| 262 |
}, |
| 263 |
{ |
| 264 |
'field': u'chain', |
| 265 |
'match_disabled': False, |
| 266 |
'invert': False, |
| 267 |
'regex': re.compile(u'foo'), |
| 268 |
'regex_source': u'foo', |
| 269 |
}, |
| 270 |
], |
| 271 |
), |
| 272 |
]) |
| 273 |
|
| 274 |
|
| 275 |
@pytest.mark.parametrize("restrict_value, expected", TEST_VALIDATE_AND_PREPARE_RESTRICT) |
| 276 |
def test_validate_and_prepare_restrict(restrict_value, expected): |
| 277 |
fake_module = FakeModule(restrict_value) |
| 278 |
result = validate_and_prepare_restrict(fake_module, TEST_PATH) |
| 279 |
print(repr(result)) |
| 280 |
assert result == expected |
| 281 |
|
| 282 |
|
| 283 |
TEST_VALIDATE_AND_PREPARE_RESTRICT_FAIL = [ |
| 284 |
( |
| 285 |
[{ |
| 286 |
'field': u'!foo', |
| 287 |
'match_disabled': False, |
| 288 |
'values': None, |
| 289 |
'regex': None, |
| 290 |
'invert': False, |
| 291 |
}], |
| 292 |
['restrict: the field name "!foo" must not start with "!"'], |
| 293 |
), |
| 294 |
( |
| 295 |
[{ |
| 296 |
'field': u'foo', |
| 297 |
'match_disabled': False, |
| 298 |
'values': None, |
| 299 |
'regex': None, |
| 300 |
'invert': False, |
| 301 |
}], |
| 302 |
['restrict: the field "foo" does not exist for this path'], |
| 303 |
), |
| 304 |
( |
| 305 |
[{ |
| 306 |
'field': u'chain', |
| 307 |
'match_disabled': False, |
| 308 |
'values': None, |
| 309 |
'regex': u'(', |
| 310 |
'invert': False, |
| 311 |
}], |
| 312 |
[ |
| 313 |
'restrict: invalid regular expression "(": missing ), unterminated subpattern at position 0', |
| 314 |
'restrict: invalid regular expression "(": unbalanced parenthesis', |
| 315 |
] |
| 316 |
), |
| 317 |
] |
| 318 |
|
| 319 |
|
| 320 |
@pytest.mark.parametrize("restrict_value, expected", TEST_VALIDATE_AND_PREPARE_RESTRICT_FAIL) |
| 321 |
def test_validate_and_prepare_restrict_fail(restrict_value, expected): |
| 322 |
fake_module = FakeModule(restrict_value) |
| 323 |
with pytest.raises(FailJsonExc) as exc: |
| 324 |
validate_and_prepare_restrict(fake_module, TEST_PATH) |
| 325 |
print(repr(exc.value.msg)) |
| 326 |
assert exc.value.msg in expected |
| 327 |
|
| 328 |
|
| 329 |
TEST_RESTRICT_ENTRY_ACCEPTED = [ |
| 330 |
( |
| 331 |
{ |
| 332 |
'chain': 'input', |
| 333 |
}, |
| 334 |
[ |
| 335 |
{ |
| 336 |
'field': u'chain', |
| 337 |
'match_disabled': False, |
| 338 |
'invert': False, |
| 339 |
}, |
| 340 |
], |
| 341 |
False, |
| 342 |
), |
| 343 |
( |
| 344 |
{ |
| 345 |
'chain': 'input', |
| 346 |
}, |
| 347 |
[ |
| 348 |
{ |
| 349 |
'field': u'chain', |
| 350 |
'match_disabled': False, |
| 351 |
'invert': True, |
| 352 |
}, |
| 353 |
], |
| 354 |
True, |
| 355 |
), |
| 356 |
( |
| 357 |
{ |
| 358 |
'comment': 'foo', |
| 359 |
}, |
| 360 |
[ |
| 361 |
{ |
| 362 |
'field': u'comment', |
| 363 |
'match_disabled': True, |
| 364 |
'invert': False, |
| 365 |
}, |
| 366 |
], |
| 367 |
False, |
| 368 |
), |
| 369 |
( |
| 370 |
{}, |
| 371 |
[ |
| 372 |
{ |
| 373 |
'field': u'comment', |
| 374 |
'match_disabled': True, |
| 375 |
'invert': False, |
| 376 |
}, |
| 377 |
], |
| 378 |
True, |
| 379 |
), |
| 380 |
] |
| 381 |
|
| 382 |
|
| 383 |
@pytest.mark.parametrize("entry, restrict_data, expected", TEST_RESTRICT_ENTRY_ACCEPTED) |
| 384 |
def test_restrict_entry_accepted(entry, restrict_data, expected): |
| 385 |
result = restrict_entry_accepted(entry, TEST_PATH, restrict_data) |
| 386 |
print(repr(result)) |
| 387 |
assert result == expected |