From dc3428aa61e1b1d38fdb41ef916b5aef56e0aac2 Mon Sep 17 00:00:00 2001 From: Mustafa Kemal Gilor Date: Mon, 23 Nov 2026 16:15:12 +0300 Subject: [PATCH] [tests-cli] Make state() call non-runtime info() State call is only interested in reading the state, which does not require the runtime fields in info. --- tests/cli-test-ut/test_info_helpers.py | 50 ++++++++++++++++++++++++++ tests/cli/multipass/helpers.py | 21 +++++++---- 2 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 tests/cli-test-ut/test_info_helpers.py diff --git a/tests/cli-test-ut/test_info_helpers.py b/tests/cli-test-ut/test_info_helpers.py new file mode 100644 index 0000000000..d8543e7a48 --- /dev/null +++ b/tests/cli-test-ut/test_info_helpers.py @@ -0,0 +1,50 @@ +# +# Copyright (C) Canonical, Ltd. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 3. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# + +"""Unit tests for `multipass info` helpers.""" + +import json +from unittest.mock import patch + +import pytest + +from cli.multipass.cmd_output import Output +from cli.multipass.helpers import info, mounts, state + +NO_RUNTIME_INFO = "--no-runtime-information" + + +@pytest.fixture +def fake_multipass(): + payload = {"info": {"foo": {"state": "Running", "mounts": {}}}} + with patch( + "cli.multipass.helpers.multipass", + return_value=Output(json.dumps(payload), 0), + ) as mock: + yield mock + + +def test_state_skips_runtime_info(fake_multipass): + # Runtime info needs SSH, which may not be up yet right after start/restart + assert state("foo") == "Running" + assert NO_RUNTIME_INFO in fake_multipass.call_args.args + + +@pytest.mark.parametrize("helper", [info, mounts]) +def test_other_helpers_keep_runtime_info(fake_multipass, helper): + helper("foo") + assert NO_RUNTIME_INFO not in fake_multipass.call_args.args diff --git a/tests/cli/multipass/helpers.py b/tests/cli/multipass/helpers.py index f061bb25d1..0799fea18f 100644 --- a/tests/cli/multipass/helpers.py +++ b/tests/cli/multipass/helpers.py @@ -31,11 +31,11 @@ from .multipass_cmd import multipass -def _retrieve_info_field(name, key): +def _retrieve_info_field(name, key, runtime_info=True): """Run `multipass info` for the given instance and return the specified field from the JSON output.""" - info_r = info(name) + info_r = info(name, runtime_info=runtime_info) assert name in info_r, f"`info` output does not contain `{name}` key: {str(info_r)}" assert ( @@ -50,11 +50,17 @@ def debug_interactive_shell(name): sh.interact() -def info(name): +def info(name, runtime_info=True): """ Get 'multipass info' JSON for instance. + + With runtime_info=False, the in-instance stats (load, disk, memory, IPs, ...) are + skipped, so the call doesn't need SSH to be reachable. """ - with multipass("info", "--format=json", f"{name}").json() as output: + args = ["info", "--format=json", f"{name}"] + if not runtime_info: + args.append("--no-runtime-information") + with multipass(*args).json() as output: assert output, f"info({name}) failed ({output.exitstatus}): {str(output)}" assert ( "info" in output @@ -63,8 +69,11 @@ def info(name): def state(name): - """Return the current state (e.g., 'Running', 'Stopped') of the instance.""" - return _retrieve_info_field(name, "state") + """Return the current state (e.g., 'Running', 'Stopped') of the instance. + + Skips runtime info, so this reflects the daemon's view only and doesn't check + that SSH is reachable.""" + return _retrieve_info_field(name, "state", runtime_info=False) def mounts(name):