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

modules/apt: add support for apt-patterns #83070

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/83070-add-support-for-apt-patterns.yml
@@ -0,0 +1,2 @@
minor_changes:
- apt - add support for apt-patterns (https://github.com/ansible/ansible/pull/83070)
7 changes: 5 additions & 2 deletions lib/ansible/modules/apt.py
Expand Up @@ -634,8 +634,11 @@ def expand_pkgspec_from_fnmatches(m, pkgspec, cache):

pkgname_pattern, version_cmp, version = package_split(pkgspec_pattern)

if pkgname_pattern.startswith(('?', '~', '(?', '(~')):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmechnich Do you mind adding an integration test for this change? I am not quite sure if I understand how this change works.

Copy link
Author

@jmechnich jmechnich Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Akasurde I cannot really think of a way to test this without actually calling apt-get or similar in a appropriately set up OS. Right now, the tests emulate certain operations using an artificial package cache.
The general idea of this patch is to exclude package names starting with ? or ~ from normal processing as they are not legal patterns for i.e. apt-cache operations.
The issue with testing this in the realm of ansible is mainly that the effect of those patterns is determined on each target host, individually, at execution time.
Maybe it is sufficient that the other tests are still working as expected?

# this is an apt-pattern (https://manpages.debian.org/bookworm/apt/apt-patterns.7.en.html)
new_pkgspec.append(pkgspec_pattern)
# note that none of these chars is allowed in a (debian) pkgname
if frozenset('*?[]!').intersection(pkgname_pattern):
elif frozenset('*?[]!').intersection(pkgname_pattern):
Akasurde marked this conversation as resolved.
Show resolved Hide resolved
# handle multiarch pkgnames, the idea is that "apt*" should
# only select native packages. But "apt*:i386" should still work
if ":" not in pkgname_pattern:
Expand Down Expand Up @@ -952,7 +955,7 @@ def remove(m, pkgspec, cache, purge=False, force=False,
for package in pkgspec:
name, version_cmp, version = package_split(package)
installed, installed_version, upgradable, has_files = package_status(m, name, version_cmp, version, None, cache, state='remove')
if installed_version or (has_files and purge):
if installed_version or (has_files and purge) or package.startswith(('?', '~', '(?', '(~')):
pkg_list.append("'%s'" % package)
packages = ' '.join(pkg_list)

Expand Down
5 changes: 5 additions & 0 deletions test/units/modules/test_apt.py
Expand Up @@ -38,6 +38,11 @@
["apt", "apt-utils"],
id="pkgname-expands",
),
pytest.param(
["?config-files"],
["?config-files"],
id="apt-pattern",
),
],
)
def test_expand_pkgspec_from_fnmatches(test_input, expected):
Expand Down