Skip to content

Commit

Permalink
Fix unintentional changes to ansible provisioner
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Nov 19, 2024
1 parent a40e8bf commit e1733f2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 42 deletions.
39 changes: 16 additions & 23 deletions src/molecule/provisioner/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,8 @@ def directory(self) -> str: # noqa: D102

def cleanup(self) -> None:
"""Execute `ansible-playbook` against the cleanup playbook and returns None."""
if self.playbooks.cleanup:
pb = self._get_ansible_playbook(self.playbooks.cleanup)
pb.execute()
pb = self._get_ansible_playbook(self.playbooks.cleanup)
pb.execute()

def connection_options(
self,
Expand All @@ -737,10 +736,9 @@ def connection_options(

def check(self) -> None:
"""Execute ``ansible-playbook`` against the converge playbook with the ``--check`` flag."""
if self.playbooks.converge:
pb = self._get_ansible_playbook(self.playbooks.converge)
pb.add_cli_arg("check", value=True)
pb.execute()
pb = self._get_ansible_playbook(self.playbooks.converge)
pb.add_cli_arg("check", value=True)
pb.execute()

def converge(self, playbook: str = "", **kwargs: object) -> str:
"""Execute ``ansible-playbook`` against the converge playbook. unless specified otherwise.
Expand All @@ -758,9 +756,8 @@ def converge(self, playbook: str = "", **kwargs: object) -> str:

def destroy(self) -> None:
"""Execute ``ansible-playbook`` against the destroy playbook and returns None."""
if self.playbooks.destroy:
pb = self._get_ansible_playbook(self.playbooks.destroy)
pb.execute()
pb = self._get_ansible_playbook(self.playbooks.destroy)
pb.execute()

def side_effect(self, action_args: list[str] | None = None) -> None:
"""Execute ``ansible-playbook`` against the side_effect playbook.
Expand All @@ -773,29 +770,25 @@ def side_effect(self, action_args: list[str] | None = None) -> None:
playbooks = [
self._get_ansible_playbook(self.abs_path(playbook)) for playbook in action_args
]
elif self.playbooks.side_effect:
playbooks = [self._get_ansible_playbook(self.playbooks.side_effect)]
playbooks = [self._get_ansible_playbook(self.playbooks.side_effect)]
for pb in playbooks:
pb.execute()

def create(self) -> None:
"""Execute ``ansible-playbook`` against the create playbook and returns None."""
if self.playbooks.create:
pb = self._get_ansible_playbook(self.playbooks.create)
pb.execute()
pb = self._get_ansible_playbook(self.playbooks.create)
pb.execute()

def prepare(self) -> None:
"""Execute ``ansible-playbook`` against the prepare playbook and returns None."""
if self.playbooks.prepare:
pb = self._get_ansible_playbook(self.playbooks.prepare)
pb.execute()
pb = self._get_ansible_playbook(self.playbooks.prepare)
pb.execute()

def syntax(self) -> None:
"""Execute `ansible-playbook` against the converge playbook with the -syntax-check flag."""
if self.playbooks.converge:
pb = self._get_ansible_playbook(self.playbooks.converge)
pb.add_cli_arg("syntax-check", value=True)
pb.execute()
pb = self._get_ansible_playbook(self.playbooks.converge)
pb.add_cli_arg("syntax-check", value=True)
pb.execute()

def verify(self, action_args: list[str] | None = None) -> None:
"""Execute ``ansible-playbook`` against the verify playbook.
Expand Down Expand Up @@ -910,7 +903,7 @@ def _link_or_update_vars(self) -> None:

def _get_ansible_playbook(
self,
playbook: str,
playbook: str | None,
verify: bool = False, # noqa: FBT001, FBT002
**kwargs: object,
) -> ansible_playbook.AnsiblePlaybook:
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/provisioner/ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AnsiblePlaybook:

def __init__(
self,
playbook: str,
playbook: str | None,
config: Config,
*,
verify: bool = False,
Expand Down
76 changes: 61 additions & 15 deletions tests/unit/provisioner/test_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,23 @@ def test_playbooks_side_effect_property(instance): # type: ignore[no-untyped-de
assert instance.playbooks.side_effect is None


def test_check(instance: ansible.Ansible, mocker: MockerFixture, _patched_ansible_playbook: Mock): # type: ignore[no-untyped-def] # noqa: ANN201, PT019, ARG001, D103
def test_check( # noqa: D103
instance: ansible.Ansible,
mocker: MockerFixture, # noqa: ARG001
_patched_ansible_playbook: Mock, # noqa: PT019
) -> None:
instance.check()

assert instance._config.provisioner

_patched_ansible_playbook.assert_called_once_with(
instance._config.provisioner.playbooks.converge,
instance._config,
verify=False,
)
_patched_ansible_playbook.return_value.add_cli_arg.assert_called_once_with(
"check",
True, # noqa: FBT003
value=True,
)
_patched_ansible_playbook.return_value.execute.assert_called_once_with()

Expand All @@ -375,23 +381,25 @@ def test_converge( # noqa: D103
) -> None:
result = instance.converge()

assert instance._config.provisioner

_patched_ansible_playbook.assert_called_once_with(
instance._config.provisioner.playbooks.converge,
instance._config,
verify=False,
)
# NOTE(retr0h): This is not the true return type. This is a mock return
# which didn't go through str.decode().
assert result == b"patched-ansible-playbook-stdout"
assert result == b"patched-ansible-playbook-stdout" # type: ignore[comparison-overlap]

_patched_ansible_playbook.return_value.execute.assert_called_once_with()


def test_converge_with_playbook( # type: ignore[no-untyped-def] # noqa: ANN201, D103
instance, # noqa: ANN001
def test_converge_with_playbook( # noqa: D103
instance: ansible.Ansible,
mocker: MockerFixture, # noqa: ARG001
_patched_ansible_playbook, # noqa: ANN001, PT019
):
_patched_ansible_playbook: Mock, # noqa: PT019
) -> None:
result = instance.converge("playbook")

_patched_ansible_playbook.assert_called_once_with(
Expand All @@ -401,14 +409,20 @@ def test_converge_with_playbook( # type: ignore[no-untyped-def] # noqa: ANN201
)
# NOTE(retr0h): This is not the true return type. This is a mock return
# which didn't go through str.decode().
assert result == b"patched-ansible-playbook-stdout"
assert result == b"patched-ansible-playbook-stdout" # type: ignore[comparison-overlap]

_patched_ansible_playbook.return_value.execute.assert_called_once_with()


def test_cleanup(instance: ansible.Ansible, mocker: MockerFixture, _patched_ansible_playbook: Mock): # type: ignore[no-untyped-def] # noqa: ANN201, PT019, ARG001, D103
def test_cleanup( # noqa: D103
instance: ansible.Ansible,
mocker: MockerFixture, # noqa: ARG001
_patched_ansible_playbook: Mock, # noqa: PT019
) -> None:
instance.cleanup()

assert instance._config.provisioner

_patched_ansible_playbook.assert_called_once_with(
instance._config.provisioner.playbooks.cleanup,
instance._config,
Expand All @@ -417,9 +431,15 @@ def test_cleanup(instance: ansible.Ansible, mocker: MockerFixture, _patched_ansi
_patched_ansible_playbook.return_value.execute.assert_called_once_with()


def test_destroy(instance: ansible.Ansible, mocker: MockerFixture, _patched_ansible_playbook: Mock): # type: ignore[no-untyped-def] # noqa: ANN201, PT019, ARG001, D103
def test_destroy( # noqa: D103
instance: ansible.Ansible,
mocker: MockerFixture, # noqa: ARG001
_patched_ansible_playbook: Mock, # noqa: PT019
) -> None:
instance.destroy()

assert instance._config.provisioner

_patched_ansible_playbook.assert_called_once_with(
instance._config.provisioner.playbooks.destroy,
instance._config,
Expand All @@ -435,6 +455,8 @@ def test_side_effect( # noqa: D103
) -> None:
instance.side_effect()

assert instance._config.provisioner

_patched_ansible_playbook.assert_called_once_with(
instance._config.provisioner.playbooks.side_effect,
instance._config,
Expand All @@ -443,9 +465,15 @@ def test_side_effect( # noqa: D103
_patched_ansible_playbook.return_value.execute.assert_called_once_with()


def test_create(instance: ansible.Ansible, mocker: MockerFixture, _patched_ansible_playbook: Mock): # type: ignore[no-untyped-def] # noqa: ANN201, PT019, ARG001, D103
def test_create( # noqa: D103
instance: ansible.Ansible,
mocker: MockerFixture, # noqa: ARG001
_patched_ansible_playbook: Mock, # noqa: PT019
) -> None:
instance.create()

assert instance._config.provisioner

_patched_ansible_playbook.assert_called_once_with(
instance._config.provisioner.playbooks.create,
instance._config,
Expand All @@ -454,9 +482,15 @@ def test_create(instance: ansible.Ansible, mocker: MockerFixture, _patched_ansib
_patched_ansible_playbook.return_value.execute.assert_called_once_with()


def test_prepare(instance: ansible.Ansible, mocker: MockerFixture, _patched_ansible_playbook: Mock): # type: ignore[no-untyped-def] # noqa: ANN201, PT019, ARG001, D103
def test_prepare( # noqa: D103
instance: ansible.Ansible,
mocker: MockerFixture, # noqa: ARG001
_patched_ansible_playbook: Mock, # noqa: PT019
) -> None:
instance.prepare()

assert instance._config.provisioner

_patched_ansible_playbook.assert_called_once_with(
instance._config.provisioner.playbooks.prepare,
instance._config,
Expand All @@ -465,24 +499,36 @@ def test_prepare(instance: ansible.Ansible, mocker: MockerFixture, _patched_ansi
_patched_ansible_playbook.return_value.execute.assert_called_once_with()


def test_syntax(instance: ansible.Ansible, mocker: MockerFixture, _patched_ansible_playbook: Mock): # type: ignore[no-untyped-def] # noqa: ANN201, PT019, ARG001, D103
def test_syntax( # noqa: D103
instance: ansible.Ansible,
mocker: MockerFixture, # noqa: ARG001
_patched_ansible_playbook: Mock, # noqa: PT019
) -> None:
instance.syntax()

assert instance._config.provisioner

_patched_ansible_playbook.assert_called_once_with(
instance._config.provisioner.playbooks.converge,
instance._config,
verify=False,
)
_patched_ansible_playbook.return_value.add_cli_arg.assert_called_once_with(
"syntax-check",
True, # noqa: FBT003
value=True,
)
_patched_ansible_playbook.return_value.execute.assert_called_once_with()


def test_verify(instance: ansible.Ansible, mocker: MockerFixture, _patched_ansible_playbook: Mock): # type: ignore[no-untyped-def] # noqa: ANN201, PT019, ARG001, D103
def test_verify( # noqa: D103
instance: ansible.Ansible,
mocker: MockerFixture, # noqa: ARG001
_patched_ansible_playbook: Mock, # noqa: PT019
) -> None:
instance.verify()

assert instance._config.provisioner

if instance._config.provisioner.playbooks.verify:
_patched_ansible_playbook.assert_called_once_with(
instance._config.provisioner.playbooks.verify,
Expand Down
8 changes: 5 additions & 3 deletions tests/unit/provisioner/test_ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


@pytest.fixture
def _instance(config_instance: config.Config): # type: ignore[no-untyped-def] # noqa: ANN202
def _instance(config_instance: config.Config) -> ansible_playbook.AnsiblePlaybook:
_instance = ansible_playbook.AnsiblePlaybook("playbook", config_instance)

return _instance # noqa: RET504
Expand Down Expand Up @@ -94,8 +94,10 @@ def _inventory_directory(_instance): # type: ignore[no-untyped-def] # noqa: AN
return _instance._config.provisioner.inventory_directory


def test_ansible_command_private_member(_instance): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, PT019, D103
assert _instance._ansible_command is None
def test_ansible_command_private_member( # noqa: D103
_instance: ansible_playbook.AnsiblePlaybook, # noqa: PT019
) -> None:
assert _instance._ansible_command == []


def test_ansible_playbook_private_member(_instance): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, PT019, D103
Expand Down

0 comments on commit e1733f2

Please sign in to comment.