diff --git a/lib/charms/operator_libs_linux/v0/apt.py b/lib/charms/operator_libs_linux/v0/apt.py index bf2fe98a..1c67d400 100644 --- a/lib/charms/operator_libs_linux/v0/apt.py +++ b/lib/charms/operator_libs_linux/v0/apt.py @@ -1428,14 +1428,16 @@ def add( # noqa: D417 # undocumented-param: default_filename intentionally und """Add a new repository to the system using add-apt-repository. Args: - repo: a DebianRepository object where repo.enabled is True - raises: - ValueError: if repo.enabled is False + repo: a DebianRepository object + if repo.enabled is falsey, will return without adding the repository + Raises: CalledProcessError: if there's an error running apt-add-repository WARNING: Does not associate the repository with a signing key. Use `import_key` to add a signing key globally. + WARNING: if repo.enabled is falsey, will return without adding the repository + WARNING: Don't forget to call `apt.update` before installing any packages! Or call `apt.add_package` with `update_cache=True`. @@ -1443,7 +1445,15 @@ def add( # noqa: D417 # undocumented-param: default_filename intentionally und only. It is not used, and was not used in the previous revision of this library. """ if not repo.enabled: - raise ValueError("{repo}.enabled is {value}".format(repo=repo, value=repo.enabled)) + logger.warning( + ( + "Returning from RepositoryMapping.add(repo=%s) without adding the repo" + " because repo.enabled is %s" + ), + repo, + repo.enabled, + ) + return _add_repository(repo) self._repository_map[_repo_to_identifier(repo)] = repo diff --git a/tests/unit/test_deb822.py b/tests/unit/test_deb822.py index df2142e7..96bf88bc 100644 --- a/tests/unit/test_deb822.py +++ b/tests/unit/test_deb822.py @@ -334,12 +334,15 @@ def test_add_with_deb822(repo_mapping: apt.RepositoryMapping): # we re-raise CalledProcessError after logging error = apt.CalledProcessError(1, "cmd") error.stdout = error.stderr = b"" - with patch.object(apt.logger, "error") as mock_logging_error: - with patch.object(apt.subprocess, "run", side_effect=error): + with patch.object(apt.subprocess, "run", side_effect=error): + with patch.object(apt.logger, "error") as mock_error: with pytest.raises(apt.CalledProcessError): repo_mapping.add(repo) - mock_logging_error.assert_called_once() + mock_error.assert_called_once() # call add with a disabled repository repo._enabled = False - with pytest.raises(ValueError): - repo_mapping.add(repo) + with patch.object(apt, "_add_repository") as mock_add: + with patch.object(apt.logger, "warning") as mock_warning: + repo_mapping.add(repo) + mock_add.assert_not_called() + mock_warning.assert_called_once()