1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
|
import pytest
import warnings
from unittest import mock
from hvac import exceptions
from hvac.utils import (
generate_method_deprecation_message,
generate_property_deprecation_message,
generate_parameter_deprecation_message,
aliased_parameter,
comma_delimited_to_list,
get_token_from_env,
validate_list_of_strings_param,
getattr_with_deprecated_properties,
deprecated_method,
)
@pytest.fixture
def aliasable_func():
def _func(pos0, pos1=None, *, kw0, kw1="kwone", kw2=None):
return {
"pos0": pos0,
"pos1": pos1,
"kw0": kw0,
"kw1": kw1,
"kw2": kw2,
}
return _func
@pytest.fixture
def deprecatable_method():
def _meth():
"""docstring"""
return mock.sentinel.dep_meth_retval
return _meth
class ClassWithDeprecatedProperties:
class _Sub:
old1 = "new1"
new1 = "NG"
old2 = "old2"
new2 = "new2"
sub = _Sub()
DEPRECATED_PROPERTIES = {
"old1": dict(
to_be_removed_in_version="99.88.77",
client_property="sub",
),
"old2": dict(
to_be_removed_in_version="99.99.99",
client_property="sub",
new_property="new2",
),
}
class TestUtils:
@pytest.mark.parametrize("removed_in_version", ["99.88.77", "99.99.99"])
def test_deprecated_method_no_new(self, removed_in_version, deprecatable_method):
old_method = deprecatable_method
with mock.patch(
"hvac.utils.generate_method_deprecation_message",
new=mock.Mock(return_value=mock.sentinel.dep_meth_msg),
) as gen_msg:
wrapped = deprecated_method(
to_be_removed_in_version=removed_in_version,
)(old_method)
gen_msg.assert_called_once_with(
to_be_removed_in_version=removed_in_version,
old_method_name=old_method.__name__,
method_name=None,
module_name=None,
)
assert wrapped.__doc__ == mock.sentinel.dep_meth_msg
with mock.patch("warnings.warn") as warn:
result = wrapped()
warn.assert_called_once_with(
message=mock.sentinel.dep_meth_msg,
category=DeprecationWarning,
stacklevel=2,
)
assert result == mock.sentinel.dep_meth_retval
@pytest.mark.parametrize("removed_in_version", ["99.88.77", "99.99.99"])
@pytest.mark.parametrize("new_meth_doc", [None, "newdoc"])
def test_deprecated_method_new(
self, removed_in_version, deprecatable_method, new_meth_doc
):
old_method = deprecatable_method
dep_meth_msg = "depmsg"
def new_method():
pass
new_method.__doc__ = new_meth_doc
with mock.patch(
"hvac.utils.generate_method_deprecation_message",
new=mock.Mock(return_value=dep_meth_msg),
) as gen_msg:
wrapped = deprecated_method(
to_be_removed_in_version=removed_in_version,
new_method=new_method,
)(old_method)
gen_msg.assert_called_once_with(
to_be_removed_in_version=removed_in_version,
old_method_name=old_method.__name__,
method_name=new_method.__name__,
module_name=__name__,
)
if new_meth_doc is not None:
assert new_meth_doc in wrapped.__doc__
else:
assert "N/A" in wrapped.__doc__
assert dep_meth_msg in wrapped.__doc__
assert (
"Docstring content from this method's replacement copied below"
in wrapped.__doc__
)
with mock.patch("warnings.warn") as warn:
result = wrapped()
warn.assert_called_once_with(
message=dep_meth_msg,
category=DeprecationWarning,
stacklevel=2,
)
assert result == mock.sentinel.dep_meth_retval
@pytest.mark.parametrize(
["item", "expected_new_prop", "expected_value", "expected_version"],
[("old1", "old1", "new1", "99.88.77"), ("old2", "new2", "new2", "99.99.99")],
)
def test_getattr_with_deprecated_properties(
self, item, expected_new_prop, expected_value, expected_version
):
with mock.patch(
"hvac.utils.generate_property_deprecation_message",
new=mock.Mock(return_value=mock.sentinel.dep_prop_msg),
) as gen_msg, mock.patch("warnings.warn") as warn:
result = getattr_with_deprecated_properties(
ClassWithDeprecatedProperties,
item,
ClassWithDeprecatedProperties.DEPRECATED_PROPERTIES,
)
gen_msg.assert_called_once_with(
to_be_removed_in_version=expected_version,
old_name=item,
new_name=expected_new_prop,
new_attribute="sub",
)
warn.assert_called_once_with(
message=mock.sentinel.dep_prop_msg,
category=DeprecationWarning,
stacklevel=2,
)
assert result == expected_value
@pytest.mark.parametrize("item", ["old9", "old8"])
def test_getattr_with_deprecated_properties_no_item(self, item):
with pytest.raises(AttributeError, match=rf"has no attribute '{item}'"):
getattr_with_deprecated_properties(
ClassWithDeprecatedProperties,
item,
ClassWithDeprecatedProperties.DEPRECATED_PROPERTIES,
)
@pytest.mark.parametrize("token", ["token", "token2 ", " ", "\n"])
def test_get_token_from_env_env_var(self, token):
with mock.patch.dict("os.environ", {"VAULT_TOKEN": token}):
with mock.patch("builtins.open", mock.mock_open()) as mopen:
result = get_token_from_env()
mopen.assert_not_called()
assert result == token
@mock.patch.dict("os.environ", clear=True)
@mock.patch("os.path.expanduser", mock.Mock(return_value="/a/b/c/token"))
@pytest.mark.parametrize("token", ["token", "token2 ", "", " ", "\n"])
@pytest.mark.parametrize("exists", [True, False])
def test_get_token_from_env_token_sink(self, token, exists):
with mock.patch("os.path.exists", lambda x: exists):
with mock.patch("builtins.open", mock.mock_open(read_data=token)) as mopen:
result = get_token_from_env()
if exists:
mopen.assert_called_once_with("/a/b/c/token")
if token.strip():
assert result == token.strip()
else:
assert result is None
else:
mopen.assert_not_called()
assert result is None
@pytest.mark.parametrize("param_name", ["PARAM1", "PARAM2"])
@pytest.mark.parametrize(
"param_argument", [None, [], ["1", "2"], "1,2,3", "", ",,"]
)
def test_validate_list_of_strings_param_pass(self, param_name, param_argument):
result = validate_list_of_strings_param(param_name, param_argument)
assert result is None
@pytest.mark.parametrize("param_name", ["PARAM1", "PARAM2"])
@pytest.mark.parametrize(
"param_argument", [[None], [1], ["1", 2], ("1,2,3",), [["a"]]]
)
def test_validate_list_of_strings_param_fail(self, param_name, param_argument):
with pytest.raises(exceptions.ParamValidationError) as e:
validate_list_of_strings_param(param_name, param_argument)
msg = str(e.value)
assert str(param_name) in msg
assert str(param_argument) in msg
assert str(type(param_argument)) in msg
@pytest.mark.parametrize(
"list_param",
[[], ["one"], [1, "two"], [1, "2", None], ["1", None, ["!", "@"], {}]],
)
def test_comma_delimited_to_list_from_list(self, list_param):
result = comma_delimited_to_list(list_param=list_param)
assert result == list_param
@pytest.mark.parametrize(
"list_param",
[{}, {"a": 1}, None, 7, b"X,Y,Z"],
)
def test_comma_delimited_to_list_from_other(self, list_param):
result = comma_delimited_to_list(list_param=list_param)
assert result == []
@pytest.mark.parametrize(
("list_param", "expected"),
[
("", [""]),
("a", ["a"]),
("a,b,c", ["a", "b", "c"]),
("a, b, c", ["a", " b", " c"]),
("a,,c", ["a", "", "c"]),
],
)
def test_comma_delimited_to_list_from_str(self, list_param, expected):
result = comma_delimited_to_list(list_param=list_param)
assert result == expected
@pytest.mark.parametrize("to_be_removed_in_version", ["99.0.0"])
@pytest.mark.parametrize("old_name", ["old_one"])
@pytest.mark.parametrize("new_name", ["new_one"])
@pytest.mark.parametrize("new_attribute", ["new_attr"])
@pytest.mark.parametrize("module_name", ["Client", "modulename"])
def test_generate_property_deprecation_message(
self,
to_be_removed_in_version,
old_name,
new_name,
new_attribute,
module_name,
):
result = generate_property_deprecation_message(
to_be_removed_in_version=to_be_removed_in_version,
old_name=old_name,
new_name=new_name,
new_attribute=new_attribute,
module_name=module_name,
)
assert to_be_removed_in_version in result
assert old_name in result
assert new_name in result
assert module_name in result
@pytest.mark.parametrize("to_be_removed_in_version", ["99.0.0"])
@pytest.mark.parametrize("old_method_name", ["old_one"])
@pytest.mark.parametrize("method_name", [None, "new_one"])
@pytest.mark.parametrize("module_name", [None, "modulename"])
def test_generate_method_deprecation_message(
self,
to_be_removed_in_version,
old_method_name,
method_name,
module_name,
):
result = generate_method_deprecation_message(
to_be_removed_in_version=to_be_removed_in_version,
old_method_name=old_method_name,
method_name=method_name,
module_name=module_name,
)
assert to_be_removed_in_version in result
assert old_method_name in result
if method_name is not None and module_name is not None:
assert method_name in result and module_name in result
@pytest.mark.parametrize("to_be_removed_in_version", ["99.0.0"])
@pytest.mark.parametrize("old_parameter_name", ["old_one"])
@pytest.mark.parametrize("new_parameter_name", [None, "new_one"])
@pytest.mark.parametrize("extra_notes", [None, "See also whatever."])
def test_generate_parameter_deprecation_message(
self,
to_be_removed_in_version,
old_parameter_name,
new_parameter_name,
extra_notes,
):
result = generate_parameter_deprecation_message(
to_be_removed_in_version=to_be_removed_in_version,
old_parameter_name=old_parameter_name,
new_parameter_name=new_parameter_name,
extra_notes=extra_notes,
)
assert to_be_removed_in_version in result
assert old_parameter_name in result
assert (new_parameter_name is None) or (new_parameter_name in result)
assert (extra_notes is None) or (extra_notes in result)
@pytest.mark.parametrize("removed_in_version", ["abc", "123", None])
@pytest.mark.parametrize(
["raise_on_multiple", "position", "i_args", "i_kwargs", "raises", "p1exp"],
[
(True, 1, ["p0"], {"kw0": "kw0", "alias8": "eight"}, None, "eight"),
(True, None, ["p0"], {"kw0": "kw0", "alias8": "eight"}, None, "eight"),
(
True,
1,
["p0"],
{"kw0": "kw0", "alias8": "eight", "pos1": "p1"},
ValueError,
None,
),
(
True,
None,
["p0"],
{"kw0": "kw0", "alias8": "eight", "pos1": "p1"},
ValueError,
None,
),
(
True,
1,
["p0"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine", "pos1": "p1"},
ValueError,
None,
),
(
True,
None,
["p0"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine", "pos1": "p1"},
ValueError,
None,
),
(
True,
1,
["p0"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine"},
ValueError,
None,
),
(
True,
None,
["p0"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine"},
ValueError,
None,
),
(
True,
1,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "pos1": "peeone"},
ValueError,
None,
),
(
True,
None,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "pos1": "p1"},
ValueError,
None,
),
(
True,
1,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight"},
ValueError,
None,
),
(
True,
None,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight"},
TypeError,
None,
),
(
True,
1,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine", "pos1": "p1"},
ValueError,
None,
),
(
True,
None,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine", "pos1": "peeone"},
ValueError,
None,
),
(
True,
1,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine"},
ValueError,
None,
),
(
True,
None,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine"},
ValueError,
None,
),
(True, 1, ["p0", "p1"], {"kw0": "kw0", "pos1": "peeone"}, TypeError, None),
(
True,
None,
["p0", "p1"],
{"kw0": "kw0", "pos1": "peeone"},
TypeError,
None,
),
(True, 1, ["p0", "p1"], {"kw0": "kw0"}, None, "p1"),
(True, None, ["p0", "p1"], {"kw0": "kw0"}, None, "p1"),
(False, 1, ["p0"], {"kw0": "kw0", "alias8": "eight"}, None, "eight"),
(False, None, ["p0"], {"kw0": "kw0", "alias8": "eight"}, None, "eight"),
(
False,
1,
["p0"],
{"kw0": "kw0", "alias8": "eight", "pos1": "p1"},
None,
"p1",
),
(
False,
None,
["p0"],
{"kw0": "kw0", "alias8": "eight", "pos1": "p1"},
None,
"p1",
),
(
False,
1,
["p0"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine", "pos1": "p1"},
None,
"p1",
),
(
False,
None,
["p0"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine", "pos1": "p1"},
None,
"p1",
),
(
False,
1,
["p0"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine"},
None,
"nine",
),
(
False,
None,
["p0"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine"},
None,
"nine",
),
(
False,
1,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "pos1": "peeone"},
TypeError,
None,
),
(
False,
None,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "pos1": "peeone"},
TypeError,
None,
),
(False, 1, ["p0", "p1"], {"kw0": "kw0", "alias8": "eight"}, None, "p1"),
(
False,
None,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight"},
TypeError,
None,
),
(
False,
1,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine", "pos1": "peeone"},
TypeError,
None,
),
(
False,
None,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine", "pos1": "peeone"},
TypeError,
None,
),
(
False,
1,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine"},
None,
"p1",
),
(
False,
None,
["p0", "p1"],
{"kw0": "kw0", "alias8": "eight", "alias9": "nine"},
TypeError,
None,
),
(False, 1, ["p0", "p1"], {"kw0": "kw0", "pos1": "peeone"}, TypeError, None),
(
False,
None,
["p0", "p1"],
{"kw0": "kw0", "pos1": "peeone"},
TypeError,
None,
),
(False, 1, ["p0", "p1"], {"kw0": "kw0"}, None, "p1"),
(False, None, ["p0", "p1"], {"kw0": "kw0"}, None, "p1"),
],
)
def test_aliased_parameter(
self,
aliasable_func,
removed_in_version,
raise_on_multiple,
i_args,
i_kwargs,
raises,
p1exp,
position,
):
wrapped = aliased_parameter(
"pos1",
"alias9",
"alias8",
removed_in_version=removed_in_version,
position=position,
raise_on_multiple=raise_on_multiple,
)(aliasable_func)
alias_count = 0
for a in i_kwargs.keys():
if "alias" in a:
alias_count += 1
if raises is not None:
with pytest.raises(raises, match=r"pos1"):
if removed_in_version is None or alias_count == 0:
with warnings.catch_warnings():
warnings.simplefilter("error")
result = wrapped(*i_args, **i_kwargs)
else:
with pytest.warns(DeprecationWarning) as wrecs:
result = wrapped(*i_args, **i_kwargs)
assert len(wrecs) == alias_count
for w in wrecs:
assert removed_in_version in str(w)
else:
if removed_in_version is None or alias_count == 0:
with warnings.catch_warnings():
warnings.simplefilter("error")
result = wrapped(*i_args, **i_kwargs)
else:
with pytest.warns(DeprecationWarning) as wrecs:
result = wrapped(*i_args, **i_kwargs)
assert len(wrecs) == alias_count
for w in wrecs:
assert removed_in_version in str(w)
assert result["pos0"] == "p0"
assert result["pos1"] == p1exp
assert result["kw0"] == "kw0"
assert result["kw2"] is None
assert "alias8" not in result
assert "alias9" not in result
|