Debian Codegraph @

test/units/controller_only_conftest.py

raw
1 from __future__ import annotations
2
3 import typing as t
4
5 import pytest
6
7 from pytest_mock import MockerFixture
8
9 from ansible._internal._errors._handler import ErrorHandler, ErrorAction
10 from ansible.parsing.vault import VaultSecretsContext, VaultSecret
11 from ansible._internal._templating._engine import _TemplateConfig
12 from units.mock.vault_helper import VaultTestHelper
13
14
15 # DTFIX-FUTURE: it'd be nice not to need to sync all controller-only fixtures here, but sunder values are excluded from `import *`,
16 # and if we don't sunder the fixtures, they'll be unused args. Could also be handled with a managed import, module getattr, or others.
17 __all__ = ['_empty_vault_secrets_context', '_ignore_untrusted_template', '_vault_secrets_context', '_zap_vault_secrets_context']
18
19
20 @pytest.fixture
21 def _zap_vault_secrets_context() -> t.Generator[None]:
22 """A fixture that disables any existing VaultSecretsContext both before and after the test."""
23 # DTFIX-FUTURE: come up with a decent way to share this kind of fixture between controller contexts; we can't put it in top-level conftest since
24 # modules can't use it. Import from a shared controller fixture location, area-specific fixture support files, or ?
25 VaultSecretsContext._current = None
26
27 try:
28 yield
29 finally:
30 VaultSecretsContext._current = None
31
32
33 @pytest.fixture
34 def _empty_vault_secrets_context(_zap_vault_secrets_context) -> t.Generator[None]:
35 """A fixture that provides a `VaultSecretsContext` with no secrets."""
36 VaultSecretsContext.initialize(VaultSecretsContext([]))
37
38 yield
39
40
41 @pytest.fixture
42 def _vault_secrets_context(_zap_vault_secrets_context) -> t.Generator[VaultTestHelper]:
43 """A fixture that initializes `VaultSecretsContext` with a single secret under the default ID and returns a `VaultTestHelper`."""
44 secret = VaultSecret(b'secretbytesblah')
45
46 VaultSecretsContext.initialize(VaultSecretsContext(secrets=[('default', secret)]))
47
48 yield VaultTestHelper()
49
50
51 @pytest.fixture
52 def _ignore_untrusted_template(mocker: MockerFixture) -> t.Generator[None]:
53 mocker.patch.object(_TemplateConfig, 'untrusted_template_handler', ErrorHandler(ErrorAction.IGNORE))
54
55 yield