From b9e6fa39dd21addd3f80db7359778b866bb97548 Mon Sep 17 00:00:00 2001 From: Mitch Burton Date: Thu, 14 Dec 2023 16:01:41 -0800 Subject: [PATCH] apt.py: use CalledProcessError.stderr in exceptions instead of output/stdout --- lib/charms/operator_libs_linux/v0/apt.py | 4 ++-- tests/unit/test_apt.py | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/charms/operator_libs_linux/v0/apt.py b/lib/charms/operator_libs_linux/v0/apt.py index c3a2329..2a2205a 100644 --- a/lib/charms/operator_libs_linux/v0/apt.py +++ b/lib/charms/operator_libs_linux/v0/apt.py @@ -253,7 +253,7 @@ def _apt( subprocess.run(_cmd, capture_output=True, check=True, env=env) except CalledProcessError as e: raise PackageError( - "Could not {} package(s) [{}]: {}".format(command, [*package_names], e.output) + "Could not {} package(s) [{}]: {}".format(command, [*package_names], e.stderr) ) from None def _add(self) -> None: @@ -476,7 +476,7 @@ def from_apt_cache( ) except CalledProcessError as e: raise PackageError( - "Could not list packages in apt-cache: {}".format(e.output) + "Could not list packages in apt-cache: {}".format(e.stderr) ) from None pkg_groups = output.strip().split("\n\n") diff --git a/tests/unit/test_apt.py b/tests/unit/test_apt.py index 2cf3e9c..99e4849 100644 --- a/tests/unit/test_apt.py +++ b/tests/unit/test_apt.py @@ -275,6 +275,24 @@ def test_can_load_from_apt_cache_multi_arch(self, mock_subprocess): self.assertEqual(tester.fullversion, "1:1.2.3-4.i386") self.assertEqual(str(tester.version), "1:1.2.3-4") + @patch("charms.operator_libs_linux.v0.apt.check_output") + def test_will_throw_apt_cache_errors(self, mock_subprocess): + mock_subprocess.side_effect = [ + "amd64", + subprocess.CalledProcessError( + returncode=100, + cmd=["apt-cache", "show", "mocktester"], + stderr="N: Unable to locate package mocktester", + ), + ] + + with self.assertRaises(apt.PackageError) as ctx: + apt.DebianPackage.from_apt_cache("mocktester", arch="i386") + + self.assertEqual("", ctx.exception.name) + self.assertIn("Could not list packages in apt-cache", ctx.exception.message) + self.assertIn("Unable to locate package", ctx.exception.message) + @patch("charms.operator_libs_linux.v0.apt.check_output") @patch("charms.operator_libs_linux.v0.apt.subprocess.run") @patch("os.environ.copy") @@ -322,7 +340,9 @@ def test_can_run_apt_commands( @patch("charms.operator_libs_linux.v0.apt.subprocess.run") def test_will_throw_apt_errors(self, mock_subprocess_call, mock_subprocess_output): mock_subprocess_call.side_effect = subprocess.CalledProcessError( - returncode=1, cmd=["apt-get", "-y", "install"] + returncode=1, + cmd=["apt-get", "-y", "install"], + stderr="E: Unable to locate package mocktester", ) mock_subprocess_output.side_effect = [ "amd64", @@ -339,6 +359,7 @@ def test_will_throw_apt_errors(self, mock_subprocess_call, mock_subprocess_outpu self.assertEqual("", ctx.exception.name) self.assertIn("Could not install package", ctx.exception.message) + self.assertIn("Unable to locate package", ctx.exception.message) def test_can_compare_versions(self): old_version = apt.Version("1.0.0", "")