Skip to content

Commit

Permalink
Create instances during login if not created (#4253)
Browse files Browse the repository at this point in the history
Completes #2469.
  • Loading branch information
cavcrosby authored Jul 31, 2024
1 parent 63c895e commit c670360
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/molecule/command/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def execute(self, action_args=None): # type: ignore[no-untyped-def] # noqa: AN
"""Execute the actions necessary to perform a `molecule login` and returns None."""
c = self._config
if (not c.state.created) and c.driver.managed:
msg = "Instances not created. Please create instances first."
util.sysexit_with_message(msg)
base.execute_subcommand(c, "create")

hosts = [d["name"] for d in self._config.platforms.instances]
hostname = self._get_hostname(hosts) # type: ignore[no-untyped-call]
Expand Down
19 changes: 11 additions & 8 deletions tests/unit/command/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,25 @@ def test_login_execute(mocker: MockerFixture, _instance): # type: ignore[no-unt
m.assert_called_once_with("instance-1")


@pytest.mark.skip(reason="needs rewrite after switch to delegated")
@pytest.mark.parametrize(
"config_instance",
["command_driver_delegated_managed_section_data"], # noqa: PT007
indirect=True,
)
def test_execute_raises_when_not_created(caplog, _instance): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, PT019, D103
def test_login_execute_instance_creation(mocker: MockerFixture, _instance): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, PT019, D103
_instance._config.command_args = {"host": "instance-1"}
_instance._config.state.change_state("created", False) # noqa: FBT003

with pytest.raises(SystemExit) as e:
_instance.execute()

assert e.value.code == 1
mocker.patch("molecule.command.login.Login._get_login")
patched_execute_subcommand = mocker.patch("molecule.command.base.execute_subcommand")
patched_execute_subcommand.side_effect = lambda _config, _: _config.state.change_state(
key="created",
value=True,
)
_instance.execute()

msg = "Instances not created. Please create instances first."
assert msg in caplog.text
patched_execute_subcommand.assert_called_once_with(_instance._config, "create")
assert _instance._config.state.created


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

0 comments on commit c670360

Please sign in to comment.