From 557594c39247c992b78250ab88fa40582ca1f5af Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 23 Oct 2022 20:56:12 +0200 Subject: [PATCH] pkgng: fix error-handling when upgrading all (#5369) (#5410) * pkgng: fix error-handling when upgrading all * provide for rc=1 in check_mode + test * fix name of task in test * add changelog fragment (cherry picked from commit baa8bd52ab7fd08b3b0b05880a43d8b45e6c7dc6) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .../fragments/5369-pkgng-fix-update-all.yaml | 2 ++ plugins/modules/packaging/os/pkgng.py | 14 +++----- .../targets/pkgng/tasks/freebsd.yml | 36 +++++++++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/5369-pkgng-fix-update-all.yaml diff --git a/changelogs/fragments/5369-pkgng-fix-update-all.yaml b/changelogs/fragments/5369-pkgng-fix-update-all.yaml new file mode 100644 index 0000000000..783d461a9e --- /dev/null +++ b/changelogs/fragments/5369-pkgng-fix-update-all.yaml @@ -0,0 +1,2 @@ +bugfixes: + - pkgng - fix case when ``pkg`` fails when trying to upgrade all packages (https://github.com/ansible-collections/community.general/issues/5363). diff --git a/plugins/modules/packaging/os/pkgng.py b/plugins/modules/packaging/os/pkgng.py index 3ec8466c17..083202dae1 100644 --- a/plugins/modules/packaging/os/pkgng.py +++ b/plugins/modules/packaging/os/pkgng.py @@ -37,7 +37,7 @@ options: state: description: - State of the package. - - 'Note: "latest" added in 2.7' + - 'Note: C(latest) added in 2.7.' choices: [ 'present', 'latest', 'absent' ] required: false default: present @@ -148,10 +148,7 @@ def query_package(module, run_pkgng, name): rc, out, err = run_pkgng('info', '-g', '-e', name) - if rc == 0: - return True - - return False + return rc == 0 def query_update(module, run_pkgng, name): @@ -161,10 +158,7 @@ def query_update(module, run_pkgng, name): # rc = 1, updates available rc, out, err = run_pkgng('upgrade', '-g', '-n', name) - if rc == 1: - return True - - return False + return rc == 1 def pkgng_older_than(module, pkgng_path, compare_version): @@ -190,7 +184,7 @@ def upgrade_packages(module, run_pkgng): pkgng_args = ['upgrade'] pkgng_args.append('-n' if module.check_mode else '-y') - rc, out, err = run_pkgng(*pkgng_args) + rc, out, err = run_pkgng(*pkgng_args, check_rc=(not module.check_mode)) matches = re.findall('^Number of packages to be (?:upgraded|reinstalled): ([0-9]+)', out, re.MULTILINE) for match in matches: diff --git a/tests/integration/targets/pkgng/tasks/freebsd.yml b/tests/integration/targets/pkgng/tasks/freebsd.yml index abf5f2aaed..3acf12c7dd 100644 --- a/tests/integration/targets/pkgng/tasks/freebsd.yml +++ b/tests/integration/targets/pkgng/tasks/freebsd.yml @@ -136,6 +136,42 @@ - pkgng_example4.changed - not pkgng_example4_idempotent.changed +## +## pkgng - example - state=latest for out-of-date package without privileges +## +- name: Install intentionally out-of-date package and try to upgrade it with unprivileged user + block: + - ansible.builtin.user: + name: powerless + shell: /bin/bash + + - name: Create out-of-date test package + import_tasks: create-outofdate-pkg.yml + + - name: Install out-of-date test package + command: 'pkg add {{ pkgng_test_outofdate_pkg_path }}' + register: pkgng_example4_nopower_prepare + + - name: Check for any available package upgrades with unprivileged user + become: true + become_user: powerless + pkgng: + name: '*' + state: latest + register: pkgng_example4_nopower_wildcard + ignore_errors: true + + - name: Remove test out-of-date package + pkgng: + name: '{{ pkgng_test_pkg_name }}' + state: absent + + - name: Ensure pkgng upgrades package correctly + assert: + that: + - not pkgng_example4_nopower_prepare.failed + - pkgng_example4_nopower_wildcard.failed + ## ## pkgng - example - Install multiple packages in one command ##