Skip to content

Commit

Permalink
apt.py: use CalledProcessError.stderr in exceptions instead of output…
Browse files Browse the repository at this point in the history
…/stdout
  • Loading branch information
Perfect5th committed Dec 15, 2023
1 parent 405948a commit b9e6fa3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/charms/operator_libs_linux/v0/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/test_apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<charms.operator_libs_linux.v0.apt.PackageError>", 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")
Expand Down Expand Up @@ -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",
Expand All @@ -339,6 +359,7 @@ def test_will_throw_apt_errors(self, mock_subprocess_call, mock_subprocess_outpu

self.assertEqual("<charms.operator_libs_linux.v0.apt.PackageError>", 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", "")
Expand Down

0 comments on commit b9e6fa3

Please sign in to comment.