From 878d5d8029d458eb76de4e6de0d97d4723b903f2 Mon Sep 17 00:00:00 2001 From: James Garner Date: Fri, 6 Sep 2024 14:38:32 +1200 Subject: [PATCH] Cover error cases when using local install --- tests/unit/test_snap.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/unit/test_snap.py b/tests/unit/test_snap.py index 87506707..a8d5e34e 100644 --- a/tests/unit/test_snap.py +++ b/tests/unit/test_snap.py @@ -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 = ""