From cb7d478c3f23d797bf54d2231a52a0c4f2a79628 Mon Sep 17 00:00:00 2001 From: Nobuto Murata Date: Wed, 21 Aug 2024 12:15:34 +0900 Subject: [PATCH 1/2] Fail and retry on "Some index files failed to download" In scripting apt operations, adding `--error-on=any` to `apt-get update` is necessary to capture erros like "Some index files failed to download". Otherwise, charms move onto package installations with a stale index. equivalent to: https://github.com/juju/charm-helpers/pull/911 ref: https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1693900 --- lib/charms/operator_libs_linux/v0/apt.py | 2 +- tests/unit/test_apt.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/charms/operator_libs_linux/v0/apt.py b/lib/charms/operator_libs_linux/v0/apt.py index 1400df7f..b350b6aa 100644 --- a/lib/charms/operator_libs_linux/v0/apt.py +++ b/lib/charms/operator_libs_linux/v0/apt.py @@ -837,7 +837,7 @@ def remove_package( def update() -> None: """Update the apt cache via `apt-get update`.""" - subprocess.run(["apt-get", "update"], capture_output=True, check=True) + subprocess.run(["apt-get", "update", "--error-on=any"], capture_output=True, check=True) def import_key(key: str) -> str: diff --git a/tests/unit/test_apt.py b/tests/unit/test_apt.py index c57a3dda..99c43ca5 100644 --- a/tests/unit/test_apt.py +++ b/tests/unit/test_apt.py @@ -511,7 +511,9 @@ def test_refreshes_apt_cache_if_not_found(self, mock_subprocess, mock_subprocess apt_cache_aisleriot, ] pkg = apt.add_package("aisleriot") - mock_subprocess.assert_any_call(["apt-get", "update"], capture_output=True, check=True) + mock_subprocess.assert_any_call( + ["apt-get", "update", "--error-on=any"], capture_output=True, check=True + ) self.assertEqual(pkg.name, "aisleriot") self.assertEqual(pkg.present, True) @@ -527,7 +529,9 @@ def test_raises_package_not_found_error(self, mock_subprocess, mock_subprocess_o ] * 2 # Double up for the retry after update with self.assertRaises(apt.PackageError) as ctx: apt.add_package("nothere") - mock_subprocess.assert_any_call(["apt-get", "update"], capture_output=True, check=True) + mock_subprocess.assert_any_call( + ["apt-get", "update", "--error-on=any"], capture_output=True, check=True + ) self.assertEqual("", ctx.exception.name) self.assertIn("Failed to install packages: nothere", ctx.exception.message) From 9919ffcff20d39f1f080d04e2e087c6edde83f29 Mon Sep 17 00:00:00 2001 From: Nobuto Murata Date: Thu, 22 Aug 2024 08:20:22 +0900 Subject: [PATCH 2/2] Bump the patch version --- lib/charms/operator_libs_linux/v0/apt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/charms/operator_libs_linux/v0/apt.py b/lib/charms/operator_libs_linux/v0/apt.py index b350b6aa..b8913c0e 100644 --- a/lib/charms/operator_libs_linux/v0/apt.py +++ b/lib/charms/operator_libs_linux/v0/apt.py @@ -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 = 13 +LIBPATCH = 14 VALID_SOURCE_TYPES = ("deb", "deb-src")