Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create instances during login if not created #4253

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading