Skip to content

Commit

Permalink
Cover error cases when using local install
Browse files Browse the repository at this point in the history
  • Loading branch information
james-garner-canonical committed Sep 6, 2024
1 parent 417722c commit 878d5d8
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/unit/test_snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,30 @@ def test_install_local_args(self, mock_subprocess):
)
mock_subprocess.reset_mock()

@patch("charms.operator_libs_linux.v2.snap.subprocess.check_output")
def test_install_local_snap_api_error(self, mock_subprocess: MagicMock):
"""install_local raises a SnapError if cache access raises a SnapAPIError."""

class APIErrorCache:
def __getitem__(self, key):
raise snap.SnapAPIError(body={}, code=123, status="status", message="message")

mock_subprocess.return_value = "curl XXX installed"
with patch.object(snap, "SnapCache", new=APIErrorCache):
with self.assertRaises(snap.SnapError) as ctx:
snap.install_local("./curl.snap")
self.assertEqual(ctx.exception.message, "Failed to find snap curl in Snap cache")

@patch("charms.operator_libs_linux.v2.snap.subprocess.check_output")
def test_install_local_called_process_error(self, mock_subprocess: MagicMock):
"""install_local raises a SnapError if the subprocess raises a CalledProcessError."""
mock_subprocess.side_effect = CalledProcessError(
returncode=1, cmd="cmd", output="dummy-output"
)
with self.assertRaises(snap.SnapError) as ctx:
snap.install_local("./curl.snap")
self.assertEqual(ctx.exception.message, "Could not install snap ./curl.snap: dummy-output")

@patch("charms.operator_libs_linux.v2.snap.subprocess.check_output")
def test_alias(self, mock_subprocess):
mock_subprocess.return_value = ""
Expand Down

0 comments on commit 878d5d8

Please sign in to comment.