Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apt.py: use CalledProcessError.stderr in exceptions instead of output/stdout #115

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/charms/operator_libs_linux/v0/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 12
LIBPATCH = 13


VALID_SOURCE_TYPES = ("deb", "deb-src")
Expand Down 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)
benhoyt marked this conversation as resolved.
Show resolved Hide resolved
) 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)
benhoyt marked this conversation as resolved.
Show resolved Hide resolved
) 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