Skip to content

Commit

Permalink
Fail and retry on "Some index files failed to download" (#130)
Browse files Browse the repository at this point in the history
* 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: juju/charm-helpers#911
ref: https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1693900

* Bump the patch version
  • Loading branch information
nobuto-m authored Aug 21, 2024
1 parent 9c7f617 commit 30ea2e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 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 @@ -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")
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test_apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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("<charms.operator_libs_linux.v0.apt.PackageError>", ctx.exception.name)
self.assertIn("Failed to install packages: nothere", ctx.exception.message)

Expand Down

0 comments on commit 30ea2e7

Please sign in to comment.