Skip to content

Commit

Permalink
tests: update lxd install detector unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Matsuoka <[email protected]>
  • Loading branch information
cmatsuoka committed Jun 21, 2024
1 parent 711e060 commit b1e8962
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion craft_providers/lxd/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def is_installed() -> bool:
# for completeness
try:
status = snap_info.json()["result"]["status"]
except KeyError:
except (TypeError, KeyError):
raise errors.ProviderError(brief="Unexpected response from snapd service.")

logger.debug(f"LXD snap status: {status}")
Expand Down
2 changes: 1 addition & 1 deletion craft_providers/lxd/lxc.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def info(
),
) from error

def launch( # noqa: PLR0912
def launch(
self,
*,
instance_name: str,
Expand Down
34 changes: 29 additions & 5 deletions tests/unit/lxd/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
#

import os
import shutil
import sys
from unittest import mock
from unittest.mock import call

import pytest
from craft_providers.errors import ProviderError
from craft_providers.lxd import (
LXC,
LXD,
Expand Down Expand Up @@ -302,12 +303,35 @@ def test_is_initialized_no_disk_device(devices):


@pytest.mark.parametrize(
("which", "installed"), [("/path/to/lxd", True), (None, False)]
("status", "exception", "installed"),
[
({"result": {"status": "active"}}, None, True),
({"result": {"status": "foo"}}, None, False),
({}, ProviderError, False),
(None, ProviderError, False),
],
)
def test_is_installed(which, installed, monkeypatch):
monkeypatch.setattr(shutil, "which", lambda x: which)
def test_is_installed(mocker, status, exception, installed):
class FakeSnapInfo:
def raise_for_status(self) -> None:
pass

def json(self) -> dict[str, any]:
return status

mock_get = mocker.patch("requests_unixsocket.get", return_value=FakeSnapInfo())

if exception:
with pytest.raises(exception):
is_installed()
else:
assert is_installed() == installed

assert mock_get.mock_calls[0] == call(
url="http+unix://%2Frun%2Fsnapd.socket/v2/snaps/lxd",
params={"select": "enabled"},
)

assert is_installed() == installed


@pytest.mark.skipif(sys.platform != "linux", reason=f"unsupported on {sys.platform}")
Expand Down

0 comments on commit b1e8962

Please sign in to comment.