hyera.testing
Helpers for testing a :class:~hyera.backends.Backend: a hook runs the way
the engine runs it, from a unit test and without a hiera.yaml.
Nothing here imports pytest or an optional dependency.
NOT_FOUND = _NotFoundType()
module-attribute
BackendContract
Checks every backend must pass, as plain test_* methods.
A test module subclasses it, names the class under test in :attr:backend and
implements :meth:write_source; a runner such as pytest then collects the
subclass. Nothing here imports pytest: a check asks for the tmp_path
fixture by parameter name and fails with assert. Each check runs for every
hook kind the backend implements (data_hash, lookup_key, data_dig);
a kind the backend does not implement is left out.
backend
class-attribute
data = {'contract_text': 'value', 'contract_number': 7, 'contract_flag': True, 'contract_list': ['a', 'b'], 'contract_nested': {'key': 'v'}}
class-attribute
test_a_hook_returns_the_values_of_present_keys(tmp_path)
Each hook returns the source's value for every key it holds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tmp_path
|
Any
|
a fresh directory. |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
a value differs. |
Source code in src/hyera/testing.py
356 357 358 359 360 361 362 363 364 365 366 367 368 | |
test_a_lookup_through_hiera_returns_the_same_values(tmp_path)
A :class:~hyera.Hiera whose hierarchy names the backend's function
returns the source's values. Applies to a backend that registers a plain
function name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tmp_path
|
Any
|
a fresh directory. |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
a lookup differs from the source. |
Source code in src/hyera/testing.py
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 | |
test_a_malformed_source_raises_backend_error(tmp_path)
A source the backend cannot read raises :class:~hyera.BackendError
and no other exception type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tmp_path
|
Any
|
a fresh directory. |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
another exception type, or none, was raised. |
Source code in src/hyera/testing.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
test_a_missing_key_is_not_found(tmp_path)
A key the source does not hold is absent from a data_hash result,
and a lookup_key or data_dig hook calls context.not_found()
for it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tmp_path
|
Any
|
a fresh directory. |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
a missing key is found. |
Source code in src/hyera/testing.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
test_an_unreadable_source_raises_backend_error(tmp_path)
A source that has been removed raises :class:~hyera.BackendError and
no other exception type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tmp_path
|
Any
|
a fresh directory. |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
another exception type, or none, was raised. |
Source code in src/hyera/testing.py
420 421 422 423 424 425 426 427 428 429 430 431 432 | |
test_check_available_returns_or_raises_backend_error()
check_available() returns, or raises :class:~hyera.BackendError.
Raises:
| Type | Description |
|---|---|
AssertionError
|
it raised another type. |
Source code in src/hyera/testing.py
346 347 348 349 350 351 352 353 354 | |
test_every_name_resolves_to_the_class()
Every plain name in NAMES is lowercase and the registry resolves it
to the class.
Raises:
| Type | Description |
|---|---|
AssertionError
|
a name is not lowercase or resolves elsewhere. |
Source code in src/hyera/testing.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
test_every_returned_value_is_puppet_data(tmp_path)
Whatever a hook returns for the source is of a type Puppet data allows (a tuple counts as a list), the engine's own rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tmp_path
|
Any
|
a fresh directory. |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
a value is outside Puppet's data types. |
Source code in src/hyera/testing.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
test_implements_agrees_with_what_the_class_overrides()
implements() answers from the methods the class overrides, and at
least one hook kind is implemented.
Raises:
| Type | Description |
|---|---|
AssertionError
|
|
Source code in src/hyera/testing.py
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
write_malformed_source(directory)
Write a source the backend cannot read.
The default writes bytes that are not a document in any text format over
the file :meth:write_source made. A backend whose source is not a file
the default can spoil overrides it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Any
|
the directory to write into. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Mapping[str, Any]]
|
the level options for the source, or |
Source code in src/hyera/testing.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
write_source(directory, data)
Write a source holding data under directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Any
|
the directory to write into; it exists. |
required |
data
|
Mapping[str, Any]
|
the key/value pairs the source must hold. |
required |
Returns:
| Type | Description |
|---|---|
Mapping[str, Any]
|
the level options for the source. A |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
the subclass did not implement it. |
Source code in src/hyera/testing.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
data_dig(backend, segments, options=None, *, context=None)
Call backend's data_dig hook as the engine does.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
Union[Backend, Type[Backend]]
|
a backend instance, or a class to instantiate with no arguments. |
required |
segments
|
Sequence[str]
|
the already-split key the hook is asked for. |
required |
options
|
Optional[Mapping[str, Any]]
|
the hierarchy entry's |
None
|
context
|
Optional[LookupContext]
|
the context passed to the hook;
:meth: |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
the hook's value, or :data: |
Raises:
| Type | Description |
|---|---|
ConfigError
|
the backend does not implement |
BackendError
|
the hook returned a value outside Puppet's data types. |
Source code in src/hyera/testing.py
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 | |
data_hash(backend, path=None, options=None, *, context=None)
Call backend's data_hash hook as the engine does.
The hook must return a hash whose values are Puppet data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
Union[Backend, Type[Backend]]
|
a backend instance, or a class to instantiate with no arguments. |
required |
path
|
Optional[str]
|
the location's file path, or |
None
|
options
|
Optional[Mapping[str, Any]]
|
the hierarchy entry's |
None
|
context
|
Optional[LookupContext]
|
the context passed to the hook;
:meth: |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
the hash, with tuples read as lists. |
Raises:
| Type | Description |
|---|---|
ConfigError
|
the backend does not implement |
BackendError
|
the hook returned something other than a hash, or
called |
HieraLookupError
|
a value of the hash is outside Puppet's data types. |
Source code in src/hyera/testing.py
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 | |
lookup_key(backend, key, options=None, *, context=None)
Call backend's lookup_key hook as the engine does.
The hook must be implemented and its value must be one Hiera accepts; the value comes back with tuples read as lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
Union[Backend, Type[Backend]]
|
a backend instance, or a class to instantiate with no arguments. |
required |
key
|
str
|
the key the hook is asked for. |
required |
options
|
Optional[Mapping[str, Any]]
|
the hierarchy entry's |
None
|
context
|
Optional[LookupContext]
|
the context passed to the hook;
:meth: |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
the hook's value, or :data: |
Raises:
| Type | Description |
|---|---|
ConfigError
|
the backend does not implement |
BackendError
|
the hook returned a value outside Puppet's data types. |
Source code in src/hyera/testing.py
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 | |