Skip to content
Open
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
50 changes: 50 additions & 0 deletions tests/cli-test-ut/test_info_helpers.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
#
#

"""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
21 changes: 15 additions & 6 deletions tests/cli/multipass/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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):
Expand Down
Loading