pkgng: check_mode should count queued actions

Writing tests caught a bug in PR #3393, which enabled
installing more than one package per `pkg` execution.

In converting the module's install/upgrade code to a
queue structure, check_mode got broken because the count
of actions performed was only updated in the `if not check_mode`
block that invokes `pkg`. This two-line change counts
the number of actions in the queue when check mode is
enabled.
This commit is contained in:
Ross Williams 2021-10-08 19:29:36 +00:00
parent 4fe3aea3a9
commit 5aff694dc4
2 changed files with 29 additions and 22 deletions

View file

@ -1,2 +1,3 @@
bugfixes: bugfixes:
- 'pkgng - `name=* state=latest` check for upgrades did not count "Number of packages to be reinstalled" as a `changed` action, giving incorrect results in both regular and check mode' - 'pkgng - `name=* state=latest` check for upgrades did not count "Number of packages to be reinstalled" as a `changed` action, giving incorrect results in both regular and check mode'
- 'pkgng - PR #3393 (https://github.com/ansible-collections/community.general/pull/3393) broke `check_mode` so that the module always reports `not changed`; fix regression so module reports number of upgrade or install actions that would be performed'

View file

@ -277,9 +277,15 @@ def install_packages(module, pkgng_path, packages, cached, pkgsite, dir_arg, sta
else: else:
action_queue["install"].append(package) action_queue["install"].append(package)
if not module.check_mode:
# install/upgrade all named packages with one pkg command # install/upgrade all named packages with one pkg command
for (action, package_list) in action_queue.items(): for (action, package_list) in action_queue.items():
if module.check_mode:
# Do nothing, but count up how many actions
# would be performed so that the changed/msg
# is correct.
action_count[action] += len(package_list)
continue
packages = ' '.join(package_list) packages = ' '.join(package_list)
if old_pkgng: if old_pkgng:
rc, out, err = module.run_command("%s %s %s %s -g -U -y %s" % (batch_var, pkgsite, pkgng_path, action, packages)) rc, out, err = module.run_command("%s %s %s %s -g -U -y %s" % (batch_var, pkgsite, pkgng_path, action, packages))