1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/modules/pkgng.py

541 lines
19 KiB
Python
Raw Permalink Normal View History

2020-03-09 10:11:07 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2013, bleader
2020-03-09 10:11:07 +01:00
# Written by bleader <bleader@ratonland.org>
# Based on pkgin module written by Shaun Zinck <shaun.zinck at gmail.com>
# that was based on pacman module written by Afterburn <https://github.com/afterburn>
# that was based on apt module written by Matthew Williams <matthew@flowroute.com>
#
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
2020-03-09 10:11:07 +01:00
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: pkgng
short_description: Package manager for FreeBSD >= 9.0
description:
- Manage binary packages for FreeBSD using 'pkgng' which is available in versions after 9.0.
extends_documentation_fragment:
- community.general.attributes
attributes:
check_mode:
support: full
diff_mode:
support: none
2020-03-09 10:11:07 +01:00
options:
name:
description:
- Name or list of names of packages to install/remove.
- "With I(name=*), I(state=latest) will operate, but I(state=present) and I(state=absent) will be noops."
- >
Warning: In Ansible 2.9 and earlier this module had a misfeature
where I(name=*) with I(state=latest) or I(state=present) would
install every package from every package repository, filling up
the machines disk. Avoid using them unless you are certain that
your role will only be used with newer versions.
2020-03-09 10:11:07 +01:00
required: true
aliases: [pkg]
type: list
elements: str
2020-03-09 10:11:07 +01:00
state:
description:
- State of the package.
- 'Note: C(latest) added in 2.7.'
2020-03-09 10:11:07 +01:00
choices: [ 'present', 'latest', 'absent' ]
required: false
default: present
type: str
2020-03-09 10:11:07 +01:00
cached:
description:
- Use local package base instead of fetching an updated one.
type: bool
required: false
default: false
2020-03-09 10:11:07 +01:00
annotation:
description:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
- A list of keyvalue-pairs of the form
2020-03-09 10:11:07 +01:00
C(<+/-/:><key>[=<value>]). A C(+) denotes adding an annotation, a
C(-) denotes removing an annotation, and C(:) denotes modifying an
annotation.
If setting or modifying annotations, a value must be provided.
required: false
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
type: list
elements: str
2020-03-09 10:11:07 +01:00
pkgsite:
description:
- For pkgng versions before 1.1.4, specify packagesite to use
for downloading packages. If not specified, use settings from
C(/usr/local/etc/pkg.conf).
- For newer pkgng versions, specify a the name of a repository
configured in C(/usr/local/etc/pkg/repos).
required: false
type: str
2020-03-09 10:11:07 +01:00
rootdir:
description:
- For pkgng versions 1.5 and later, pkg will install all packages
within the specified root directory.
- Can not be used together with I(chroot) or I(jail) options.
required: false
type: path
2020-03-09 10:11:07 +01:00
chroot:
description:
- Pkg will chroot in the specified environment.
- Can not be used together with I(rootdir) or I(jail) options.
required: false
type: path
2020-03-09 10:11:07 +01:00
jail:
description:
- Pkg will execute in the given jail name or id.
- Can not be used together with I(chroot) or I(rootdir) options.
type: str
2020-03-09 10:11:07 +01:00
autoremove:
description:
- Remove automatically installed packages which are no longer needed.
required: false
type: bool
default: false
[pkgng] present the 'ignore_osver' option to pkg (#1243) * [pkgng] introduce IGNORE_OSVERSION to pkgng * [pkgng] add small description about IGNORE_OSVERSION - source: https://www.freebsd.org/cgi/man.cgi?query=pkg.conf&sektion=5 * [pkgng] initialize default value of 'ignoreosver' as False * [pkgng] replace double quotes by single ones, when setting 'ignoreosver' * [pkgng] do not make 'ignoreosver' a required option for this module * [pkgng] mode the point we set IGNORE_OSVER into install_packages() * [pkgng] restrict the use of 'ignoreosver' to pkg versions >= 1.11 - https://github.com/freebsd/pkg/blob/release-1.11/NEWS#L51 * [pkgng] value of 'ignoreosver' passed to install_packages - install_packages() contains a taks to update local pkgs cache; - should that update be triggered, AND 'ignoreosver' is set to True, we must change existing "batch_var" and add the IGNORE_OSVERSION=yes to the environment; - there pkg running on STABLE or CURRENT FreeBSD machines will have a chance to proceed with the "update" action * [pkgng] create option description for "ignoreosver" and its defaults * [pkgng] make sure we do use IGNORE_OSVERSION when action is "update" * [pkgng] add more information on how 'ignoreosver' works Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] create changelog fragment for PR #1243 Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] add information about when we start to offer 'ignoreosver' Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] rename 'ignoreosver' -> 'ignore_osver' Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] remove unnecessary backslashes for install_packages() call Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] fix changelog fragment's formating Signed-off-by: Vinicius Zavam <egypcio@googlemail.com>
2020-11-17 07:17:16 +01:00
ignore_osver:
description:
- Ignore FreeBSD OS version check, useful on -STABLE and -CURRENT branches.
- Defines the C(IGNORE_OSVERSION) environment variable.
required: false
type: bool
default: false
[pkgng] present the 'ignore_osver' option to pkg (#1243) * [pkgng] introduce IGNORE_OSVERSION to pkgng * [pkgng] add small description about IGNORE_OSVERSION - source: https://www.freebsd.org/cgi/man.cgi?query=pkg.conf&sektion=5 * [pkgng] initialize default value of 'ignoreosver' as False * [pkgng] replace double quotes by single ones, when setting 'ignoreosver' * [pkgng] do not make 'ignoreosver' a required option for this module * [pkgng] mode the point we set IGNORE_OSVER into install_packages() * [pkgng] restrict the use of 'ignoreosver' to pkg versions >= 1.11 - https://github.com/freebsd/pkg/blob/release-1.11/NEWS#L51 * [pkgng] value of 'ignoreosver' passed to install_packages - install_packages() contains a taks to update local pkgs cache; - should that update be triggered, AND 'ignoreosver' is set to True, we must change existing "batch_var" and add the IGNORE_OSVERSION=yes to the environment; - there pkg running on STABLE or CURRENT FreeBSD machines will have a chance to proceed with the "update" action * [pkgng] create option description for "ignoreosver" and its defaults * [pkgng] make sure we do use IGNORE_OSVERSION when action is "update" * [pkgng] add more information on how 'ignoreosver' works Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] create changelog fragment for PR #1243 Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] add information about when we start to offer 'ignoreosver' Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] rename 'ignoreosver' -> 'ignore_osver' Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] remove unnecessary backslashes for install_packages() call Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] fix changelog fragment's formating Signed-off-by: Vinicius Zavam <egypcio@googlemail.com>
2020-11-17 07:17:16 +01:00
version_added: 1.3.0
2020-03-09 10:11:07 +01:00
author: "bleader (@bleader)"
notes:
- When using pkgsite, be careful that already in cache packages won't be downloaded again.
2022-06-22 22:43:48 +02:00
- When used with a C(loop:) each package will be processed individually,
it is much more efficient to pass the list directly to the I(name) option.
2020-03-09 10:11:07 +01:00
'''
EXAMPLES = '''
- name: Install package foo
community.general.pkgng:
2020-03-09 10:11:07 +01:00
name: foo
state: present
- name: Annotate package foo and bar
community.general.pkgng:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
name:
- foo
- bar
2020-03-09 10:11:07 +01:00
annotation: '+test1=baz,-test2,:test3=foobar'
- name: Remove packages foo and bar
community.general.pkgng:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
name:
- foo
- bar
2020-03-09 10:11:07 +01:00
state: absent
# "latest" support added in 2.7
- name: Upgrade package baz
community.general.pkgng:
2020-03-09 10:11:07 +01:00
name: baz
state: latest
- name: Upgrade all installed packages (see warning for the name option first!)
community.general.pkgng:
name: "*"
state: latest
2020-03-09 10:11:07 +01:00
'''
Pkgng many packages one command (#3393) * pkgng: join package list into one command Change the pkgng module so all packages being installed (or upgraded) are acted on in one command (per action). This will make installs and upgrades a bit faster, because pkg will be invoked fewer times per module run. More important, module actions will be more atomic, making it less likely that some packages are acted on because they appear earlier in the argument list. This change also improves the status reporting of packages acted on, specifying the number of packages for each action (install or upgrade). * pkgng: make upgrade check lazily evaluated Make upgrade_available an inner function so that the if statement that checks whether installed packages are up-to-date only runs the upgrade check on packages that are already installed. This gets lazily evaluated because of boolean operator short-circuiting: https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not Previously, the module would always check for upgrades, even for not-installed packages, when running with `state=latest`. * pkgng: add changelog fragment * pkgng: Apply changelog suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: resolve pep8 style issue Remove inline function. It's purpose would be confusing for future maintainers, and someone refactoring it to a variable, with good intentions, would introduce a performance regression. Including the `query_update()` call in the if expression makes the intent more legible and still ensures lazy evaluation of the function call if the first `and` is `False`. * pkgng: Fix changelog fragment syntax issue Need to escape quotes so YAML doesn't eat them Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: Improve output message English grammar Make word "package" plural only if reporting on more than one package Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-08 07:41:56 +02:00
from collections import defaultdict
2020-03-09 10:11:07 +01:00
import re
from ansible.module_utils.basic import AnsibleModule
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def query_package(module, run_pkgng, name):
2020-03-09 10:11:07 +01:00
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
rc, out, err = run_pkgng('info', '-g', '-e', name)
2020-03-09 10:11:07 +01:00
return rc == 0
2020-03-09 10:11:07 +01:00
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def query_update(module, run_pkgng, name):
2020-03-09 10:11:07 +01:00
# Check to see if a package upgrade is available.
# rc = 0, no updates available or package not installed
# rc = 1, updates available
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
rc, out, err = run_pkgng('upgrade', '-g', '-n', name)
2020-03-09 10:11:07 +01:00
return rc == 1
2020-03-09 10:11:07 +01:00
def pkgng_older_than(module, pkgng_path, compare_version):
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
rc, out, err = module.run_command([pkgng_path, '-v'])
2020-03-09 10:11:07 +01:00
version = [int(x) for x in re.split(r'[\._]', out)]
i = 0
new_pkgng = True
while compare_version[i] == version[i]:
i += 1
if i == min(len(compare_version), len(version)):
break
else:
if compare_version[i] > version[i]:
new_pkgng = False
return not new_pkgng
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def upgrade_packages(module, run_pkgng):
# Run a 'pkg upgrade', updating all packages.
upgraded_c = 0
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
pkgng_args = ['upgrade']
pkgng_args.append('-n' if module.check_mode else '-y')
rc, out, err = run_pkgng(*pkgng_args, check_rc=(not module.check_mode))
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
matches = re.findall('^Number of packages to be (?:upgraded|reinstalled): ([0-9]+)', out, re.MULTILINE)
for match in matches:
upgraded_c += int(match)
if upgraded_c > 0:
return (True, "updated %s package(s)" % upgraded_c, out, err)
return (False, "no packages need upgrades", out, err)
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def remove_packages(module, run_pkgng, packages):
2020-03-09 10:11:07 +01:00
remove_c = 0
stdout = ""
stderr = ""
2020-03-09 10:11:07 +01:00
# Using a for loop in case of error, we can report the package that failed
for package in packages:
# Query the package first, to see if we even need to remove
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if not query_package(module, run_pkgng, package):
2020-03-09 10:11:07 +01:00
continue
if not module.check_mode:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
rc, out, err = run_pkgng('delete', '-y', package)
stdout += out
stderr += err
2020-03-09 10:11:07 +01:00
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if not module.check_mode and query_package(module, run_pkgng, package):
module.fail_json(msg="failed to remove %s: %s" % (package, out), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
remove_c += 1
if remove_c > 0:
return (True, "removed %s package(s)" % remove_c, stdout, stderr)
2020-03-09 10:11:07 +01:00
return (False, "package(s) already absent", stdout, stderr)
2020-03-09 10:11:07 +01:00
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def install_packages(module, run_pkgng, packages, cached, state):
Pkgng many packages one command (#3393) * pkgng: join package list into one command Change the pkgng module so all packages being installed (or upgraded) are acted on in one command (per action). This will make installs and upgrades a bit faster, because pkg will be invoked fewer times per module run. More important, module actions will be more atomic, making it less likely that some packages are acted on because they appear earlier in the argument list. This change also improves the status reporting of packages acted on, specifying the number of packages for each action (install or upgrade). * pkgng: make upgrade check lazily evaluated Make upgrade_available an inner function so that the if statement that checks whether installed packages are up-to-date only runs the upgrade check on packages that are already installed. This gets lazily evaluated because of boolean operator short-circuiting: https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not Previously, the module would always check for upgrades, even for not-installed packages, when running with `state=latest`. * pkgng: add changelog fragment * pkgng: Apply changelog suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: resolve pep8 style issue Remove inline function. It's purpose would be confusing for future maintainers, and someone refactoring it to a variable, with good intentions, would introduce a performance regression. Including the `query_update()` call in the if expression makes the intent more legible and still ensures lazy evaluation of the function call if the first `and` is `False`. * pkgng: Fix changelog fragment syntax issue Need to escape quotes so YAML doesn't eat them Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: Improve output message English grammar Make word "package" plural only if reporting on more than one package Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-08 07:41:56 +02:00
action_queue = defaultdict(list)
action_count = defaultdict(int)
stdout = ""
stderr = ""
2020-03-09 10:11:07 +01:00
if not module.check_mode and not cached:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
rc, out, err = run_pkgng('update')
stdout += out
stderr += err
2020-03-09 10:11:07 +01:00
if rc != 0:
module.fail_json(msg="Could not update catalogue [%d]: %s %s" % (rc, out, err), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
for package in packages:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
already_installed = query_package(module, run_pkgng, package)
2020-03-09 10:11:07 +01:00
if already_installed and state == "present":
continue
Pkgng many packages one command (#3393) * pkgng: join package list into one command Change the pkgng module so all packages being installed (or upgraded) are acted on in one command (per action). This will make installs and upgrades a bit faster, because pkg will be invoked fewer times per module run. More important, module actions will be more atomic, making it less likely that some packages are acted on because they appear earlier in the argument list. This change also improves the status reporting of packages acted on, specifying the number of packages for each action (install or upgrade). * pkgng: make upgrade check lazily evaluated Make upgrade_available an inner function so that the if statement that checks whether installed packages are up-to-date only runs the upgrade check on packages that are already installed. This gets lazily evaluated because of boolean operator short-circuiting: https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not Previously, the module would always check for upgrades, even for not-installed packages, when running with `state=latest`. * pkgng: add changelog fragment * pkgng: Apply changelog suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: resolve pep8 style issue Remove inline function. It's purpose would be confusing for future maintainers, and someone refactoring it to a variable, with good intentions, would introduce a performance regression. Including the `query_update()` call in the if expression makes the intent more legible and still ensures lazy evaluation of the function call if the first `and` is `False`. * pkgng: Fix changelog fragment syntax issue Need to escape quotes so YAML doesn't eat them Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: Improve output message English grammar Make word "package" plural only if reporting on more than one package Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-08 07:41:56 +02:00
if (
already_installed and state == "latest"
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
and not query_update(module, run_pkgng, package)
Pkgng many packages one command (#3393) * pkgng: join package list into one command Change the pkgng module so all packages being installed (or upgraded) are acted on in one command (per action). This will make installs and upgrades a bit faster, because pkg will be invoked fewer times per module run. More important, module actions will be more atomic, making it less likely that some packages are acted on because they appear earlier in the argument list. This change also improves the status reporting of packages acted on, specifying the number of packages for each action (install or upgrade). * pkgng: make upgrade check lazily evaluated Make upgrade_available an inner function so that the if statement that checks whether installed packages are up-to-date only runs the upgrade check on packages that are already installed. This gets lazily evaluated because of boolean operator short-circuiting: https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not Previously, the module would always check for upgrades, even for not-installed packages, when running with `state=latest`. * pkgng: add changelog fragment * pkgng: Apply changelog suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: resolve pep8 style issue Remove inline function. It's purpose would be confusing for future maintainers, and someone refactoring it to a variable, with good intentions, would introduce a performance regression. Including the `query_update()` call in the if expression makes the intent more legible and still ensures lazy evaluation of the function call if the first `and` is `False`. * pkgng: Fix changelog fragment syntax issue Need to escape quotes so YAML doesn't eat them Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: Improve output message English grammar Make word "package" plural only if reporting on more than one package Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-08 07:41:56 +02:00
):
2020-03-09 10:11:07 +01:00
continue
Pkgng many packages one command (#3393) * pkgng: join package list into one command Change the pkgng module so all packages being installed (or upgraded) are acted on in one command (per action). This will make installs and upgrades a bit faster, because pkg will be invoked fewer times per module run. More important, module actions will be more atomic, making it less likely that some packages are acted on because they appear earlier in the argument list. This change also improves the status reporting of packages acted on, specifying the number of packages for each action (install or upgrade). * pkgng: make upgrade check lazily evaluated Make upgrade_available an inner function so that the if statement that checks whether installed packages are up-to-date only runs the upgrade check on packages that are already installed. This gets lazily evaluated because of boolean operator short-circuiting: https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not Previously, the module would always check for upgrades, even for not-installed packages, when running with `state=latest`. * pkgng: add changelog fragment * pkgng: Apply changelog suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: resolve pep8 style issue Remove inline function. It's purpose would be confusing for future maintainers, and someone refactoring it to a variable, with good intentions, would introduce a performance regression. Including the `query_update()` call in the if expression makes the intent more legible and still ensures lazy evaluation of the function call if the first `and` is `False`. * pkgng: Fix changelog fragment syntax issue Need to escape quotes so YAML doesn't eat them Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: Improve output message English grammar Make word "package" plural only if reporting on more than one package Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-08 07:41:56 +02:00
if already_installed:
action_queue["upgrade"].append(package)
else:
action_queue["install"].append(package)
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
# install/upgrade all named packages with one pkg command
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
pkgng_args = [action, '-g', '-U', '-y'] + package_list
rc, out, err = run_pkgng(*pkgng_args)
stdout += out
stderr += err
2020-03-09 10:11:07 +01:00
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
# individually verify packages are in requested state
for package in package_list:
verified = False
if action == 'install':
verified = query_package(module, run_pkgng, package)
elif action == 'upgrade':
verified = not query_update(module, run_pkgng, package)
Pkgng many packages one command (#3393) * pkgng: join package list into one command Change the pkgng module so all packages being installed (or upgraded) are acted on in one command (per action). This will make installs and upgrades a bit faster, because pkg will be invoked fewer times per module run. More important, module actions will be more atomic, making it less likely that some packages are acted on because they appear earlier in the argument list. This change also improves the status reporting of packages acted on, specifying the number of packages for each action (install or upgrade). * pkgng: make upgrade check lazily evaluated Make upgrade_available an inner function so that the if statement that checks whether installed packages are up-to-date only runs the upgrade check on packages that are already installed. This gets lazily evaluated because of boolean operator short-circuiting: https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not Previously, the module would always check for upgrades, even for not-installed packages, when running with `state=latest`. * pkgng: add changelog fragment * pkgng: Apply changelog suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: resolve pep8 style issue Remove inline function. It's purpose would be confusing for future maintainers, and someone refactoring it to a variable, with good intentions, would introduce a performance regression. Including the `query_update()` call in the if expression makes the intent more legible and still ensures lazy evaluation of the function call if the first `and` is `False`. * pkgng: Fix changelog fragment syntax issue Need to escape quotes so YAML doesn't eat them Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: Improve output message English grammar Make word "package" plural only if reporting on more than one package Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-08 07:41:56 +02:00
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if verified:
action_count[action] += 1
else:
module.fail_json(msg="failed to %s %s" % (action, package), stdout=stdout, stderr=stderr)
Pkgng many packages one command (#3393) * pkgng: join package list into one command Change the pkgng module so all packages being installed (or upgraded) are acted on in one command (per action). This will make installs and upgrades a bit faster, because pkg will be invoked fewer times per module run. More important, module actions will be more atomic, making it less likely that some packages are acted on because they appear earlier in the argument list. This change also improves the status reporting of packages acted on, specifying the number of packages for each action (install or upgrade). * pkgng: make upgrade check lazily evaluated Make upgrade_available an inner function so that the if statement that checks whether installed packages are up-to-date only runs the upgrade check on packages that are already installed. This gets lazily evaluated because of boolean operator short-circuiting: https://docs.python.org/3.8/library/stdtypes.html#boolean-operations-and-or-not Previously, the module would always check for upgrades, even for not-installed packages, when running with `state=latest`. * pkgng: add changelog fragment * pkgng: Apply changelog suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: resolve pep8 style issue Remove inline function. It's purpose would be confusing for future maintainers, and someone refactoring it to a variable, with good intentions, would introduce a performance regression. Including the `query_update()` call in the if expression makes the intent more legible and still ensures lazy evaluation of the function call if the first `and` is `False`. * pkgng: Fix changelog fragment syntax issue Need to escape quotes so YAML doesn't eat them Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: Improve output message English grammar Make word "package" plural only if reporting on more than one package Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-08 07:41:56 +02:00
if sum(action_count.values()) > 0:
past_tense = {'install': 'installed', 'upgrade': 'upgraded'}
messages = []
for (action, count) in action_count.items():
messages.append("%s %s package%s" % (past_tense.get(action, action), count, "s" if count != 1 else ""))
return (True, '; '.join(messages), stdout, stderr)
2020-03-09 10:11:07 +01:00
return (False, "package(s) already %s" % (state), stdout, stderr)
2020-03-09 10:11:07 +01:00
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def annotation_query(module, run_pkgng, package, tag):
rc, out, err = run_pkgng('info', '-g', '-A', package)
2020-03-09 10:11:07 +01:00
match = re.search(r'^\s*(?P<tag>%s)\s*:\s*(?P<value>\w+)' % tag, out, flags=re.MULTILINE)
if match:
return match.group('value')
return False
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def annotation_add(module, run_pkgng, package, tag, value):
_value = annotation_query(module, run_pkgng, package, tag)
2020-03-09 10:11:07 +01:00
if not _value:
# Annotation does not exist, add it.
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if not module.check_mode:
rc, out, err = run_pkgng('annotate', '-y', '-A', package, tag, data=value, binary_data=True)
if rc != 0:
module.fail_json(msg="could not annotate %s: %s"
% (package, out), stderr=err)
2020-03-09 10:11:07 +01:00
return True
elif _value != value:
# Annotation exists, but value differs
module.fail_json(
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
msg="failed to annotate %s, because %s is already set to %s, but should be set to %s"
2020-03-09 10:11:07 +01:00
% (package, tag, _value, value))
return False
else:
# Annotation exists, nothing to do
return False
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def annotation_delete(module, run_pkgng, package, tag, value):
_value = annotation_query(module, run_pkgng, package, tag)
2020-03-09 10:11:07 +01:00
if _value:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if not module.check_mode:
rc, out, err = run_pkgng('annotate', '-y', '-D', package, tag)
if rc != 0:
module.fail_json(msg="could not delete annotation to %s: %s"
% (package, out), stderr=err)
2020-03-09 10:11:07 +01:00
return True
return False
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def annotation_modify(module, run_pkgng, package, tag, value):
_value = annotation_query(module, run_pkgng, package, tag)
if not _value:
2020-03-09 10:11:07 +01:00
# No such tag
module.fail_json(msg="could not change annotation to %s: tag %s does not exist"
% (package, tag))
elif _value == value:
# No change in value
return False
else:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if not module.check_mode:
rc, out, err = run_pkgng('annotate', '-y', '-M', package, tag, data=value, binary_data=True)
# pkg sometimes exits with rc == 1, even though the modification succeeded
# Check the output for a success message
if (
rc != 0
and re.search(r'^%s-[^:]+: Modified annotation tagged: %s' % (package, tag), out, flags=re.MULTILINE) is None
):
module.fail_json(msg="failed to annotate %s, could not change annotation %s to %s: %s"
% (package, tag, value, out), stderr=err)
2020-03-09 10:11:07 +01:00
return True
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def annotate_packages(module, run_pkgng, packages, annotations):
2020-03-09 10:11:07 +01:00
annotate_c = 0
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if len(annotations) == 1:
# Split on commas with optional trailing whitespace,
# to support the old style of multiple annotations
# on a single line, rather than YAML list syntax
annotations = re.split(r'\s*,\s*', annotations[0])
2020-03-09 10:11:07 +01:00
operation = {
'+': annotation_add,
'-': annotation_delete,
':': annotation_modify
}
for package in packages:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
for annotation_string in annotations:
# Note to future maintainers: A dash (-) in a regex character class ([-+:] below)
# must appear as the first character in the class, or it will be interpreted
# as a range of characters.
annotation = \
re.match(r'(?P<operation>[-+:])(?P<tag>[^=]+)(=(?P<value>.+))?', annotation_string)
if annotation is None:
module.fail_json(
msg="failed to annotate %s, invalid annotate string: %s"
% (package, annotation_string)
)
annotation = annotation.groupdict()
if operation[annotation['operation']](module, run_pkgng, package, annotation['tag'], annotation['value']):
2020-03-09 10:11:07 +01:00
annotate_c += 1
if annotate_c > 0:
return (True, "added %s annotations." % annotate_c)
return (False, "changed no annotations")
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
def autoremove_packages(module, run_pkgng):
stdout = ""
stderr = ""
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
rc, out, err = run_pkgng('autoremove', '-n')
2020-03-09 10:11:07 +01:00
autoremove_c = 0
match = re.search('^Deinstallation has been requested for the following ([0-9]+) packages', out, re.MULTILINE)
if match:
autoremove_c = int(match.group(1))
if autoremove_c == 0:
return (False, "no package(s) to autoremove", stdout, stderr)
2020-03-09 10:11:07 +01:00
if not module.check_mode:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
rc, out, err = run_pkgng('autoremove', '-y')
stdout += out
stderr += err
2020-03-09 10:11:07 +01:00
return (True, "autoremoved %d package(s)" % (autoremove_c), stdout, stderr)
2020-03-09 10:11:07 +01:00
def main():
module = AnsibleModule(
argument_spec=dict(
state=dict(default="present", choices=["present", "latest", "absent"], required=False),
name=dict(aliases=["pkg"], required=True, type='list', elements='str'),
2020-03-09 10:11:07 +01:00
cached=dict(default=False, type='bool'),
[pkgng] present the 'ignore_osver' option to pkg (#1243) * [pkgng] introduce IGNORE_OSVERSION to pkgng * [pkgng] add small description about IGNORE_OSVERSION - source: https://www.freebsd.org/cgi/man.cgi?query=pkg.conf&sektion=5 * [pkgng] initialize default value of 'ignoreosver' as False * [pkgng] replace double quotes by single ones, when setting 'ignoreosver' * [pkgng] do not make 'ignoreosver' a required option for this module * [pkgng] mode the point we set IGNORE_OSVER into install_packages() * [pkgng] restrict the use of 'ignoreosver' to pkg versions >= 1.11 - https://github.com/freebsd/pkg/blob/release-1.11/NEWS#L51 * [pkgng] value of 'ignoreosver' passed to install_packages - install_packages() contains a taks to update local pkgs cache; - should that update be triggered, AND 'ignoreosver' is set to True, we must change existing "batch_var" and add the IGNORE_OSVERSION=yes to the environment; - there pkg running on STABLE or CURRENT FreeBSD machines will have a chance to proceed with the "update" action * [pkgng] create option description for "ignoreosver" and its defaults * [pkgng] make sure we do use IGNORE_OSVERSION when action is "update" * [pkgng] add more information on how 'ignoreosver' works Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] create changelog fragment for PR #1243 Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] add information about when we start to offer 'ignoreosver' Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] rename 'ignoreosver' -> 'ignore_osver' Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] remove unnecessary backslashes for install_packages() call Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] fix changelog fragment's formating Signed-off-by: Vinicius Zavam <egypcio@googlemail.com>
2020-11-17 07:17:16 +01:00
ignore_osver=dict(default=False, required=False, type='bool'),
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
annotation=dict(required=False, type='list', elements='str'),
pkgsite=dict(required=False),
rootdir=dict(required=False, type='path'),
chroot=dict(required=False, type='path'),
jail=dict(required=False, type='str'),
2020-03-09 10:11:07 +01:00
autoremove=dict(default=False, type='bool')),
supports_check_mode=True,
mutually_exclusive=[["rootdir", "chroot", "jail"]])
pkgng_path = module.get_bin_path('pkg', True)
p = module.params
pkgs = p["name"]
changed = False
msgs = []
stdout = ""
stderr = ""
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
dir_arg = None
2020-03-09 10:11:07 +01:00
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if p["rootdir"] is not None:
rootdir_not_supported = pkgng_older_than(module, pkgng_path, [1, 5, 0])
if rootdir_not_supported:
2020-03-09 10:11:07 +01:00
module.fail_json(msg="To use option 'rootdir' pkg version must be 1.5 or greater")
else:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
dir_arg = "--rootdir=%s" % (p["rootdir"])
2020-03-09 10:11:07 +01:00
[pkgng] present the 'ignore_osver' option to pkg (#1243) * [pkgng] introduce IGNORE_OSVERSION to pkgng * [pkgng] add small description about IGNORE_OSVERSION - source: https://www.freebsd.org/cgi/man.cgi?query=pkg.conf&sektion=5 * [pkgng] initialize default value of 'ignoreosver' as False * [pkgng] replace double quotes by single ones, when setting 'ignoreosver' * [pkgng] do not make 'ignoreosver' a required option for this module * [pkgng] mode the point we set IGNORE_OSVER into install_packages() * [pkgng] restrict the use of 'ignoreosver' to pkg versions >= 1.11 - https://github.com/freebsd/pkg/blob/release-1.11/NEWS#L51 * [pkgng] value of 'ignoreosver' passed to install_packages - install_packages() contains a taks to update local pkgs cache; - should that update be triggered, AND 'ignoreosver' is set to True, we must change existing "batch_var" and add the IGNORE_OSVERSION=yes to the environment; - there pkg running on STABLE or CURRENT FreeBSD machines will have a chance to proceed with the "update" action * [pkgng] create option description for "ignoreosver" and its defaults * [pkgng] make sure we do use IGNORE_OSVERSION when action is "update" * [pkgng] add more information on how 'ignoreosver' works Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] create changelog fragment for PR #1243 Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] add information about when we start to offer 'ignoreosver' Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] rename 'ignoreosver' -> 'ignore_osver' Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] remove unnecessary backslashes for install_packages() call Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] fix changelog fragment's formating Signed-off-by: Vinicius Zavam <egypcio@googlemail.com>
2020-11-17 07:17:16 +01:00
if p["ignore_osver"]:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
ignore_osver_not_supported = pkgng_older_than(module, pkgng_path, [1, 11, 0])
if ignore_osver_not_supported:
[pkgng] present the 'ignore_osver' option to pkg (#1243) * [pkgng] introduce IGNORE_OSVERSION to pkgng * [pkgng] add small description about IGNORE_OSVERSION - source: https://www.freebsd.org/cgi/man.cgi?query=pkg.conf&sektion=5 * [pkgng] initialize default value of 'ignoreosver' as False * [pkgng] replace double quotes by single ones, when setting 'ignoreosver' * [pkgng] do not make 'ignoreosver' a required option for this module * [pkgng] mode the point we set IGNORE_OSVER into install_packages() * [pkgng] restrict the use of 'ignoreosver' to pkg versions >= 1.11 - https://github.com/freebsd/pkg/blob/release-1.11/NEWS#L51 * [pkgng] value of 'ignoreosver' passed to install_packages - install_packages() contains a taks to update local pkgs cache; - should that update be triggered, AND 'ignoreosver' is set to True, we must change existing "batch_var" and add the IGNORE_OSVERSION=yes to the environment; - there pkg running on STABLE or CURRENT FreeBSD machines will have a chance to proceed with the "update" action * [pkgng] create option description for "ignoreosver" and its defaults * [pkgng] make sure we do use IGNORE_OSVERSION when action is "update" * [pkgng] add more information on how 'ignoreosver' works Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] create changelog fragment for PR #1243 Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] add information about when we start to offer 'ignoreosver' Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] rename 'ignoreosver' -> 'ignore_osver' Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] remove unnecessary backslashes for install_packages() call Signed-off-by: Vinicius Zavam <egypcio@googlemail.com> * [pkgng] fix changelog fragment's formating Signed-off-by: Vinicius Zavam <egypcio@googlemail.com>
2020-11-17 07:17:16 +01:00
module.fail_json(msg="To use option 'ignore_osver' pkg version must be 1.11 or greater")
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if p["chroot"] is not None:
dir_arg = '--chroot=%s' % (p["chroot"])
if p["jail"] is not None:
dir_arg = '--jail=%s' % (p["jail"])
# as of pkg-1.1.4, PACKAGESITE is deprecated in favor of repository definitions
# in /usr/local/etc/pkg/repos
repo_flag_not_supported = pkgng_older_than(module, pkgng_path, [1, 1, 4])
def run_pkgng(action, *args, **kwargs):
cmd = [pkgng_path, dir_arg, action]
pkgng_env = {'BATCH': 'yes'}
if p["ignore_osver"]:
pkgng_env['IGNORE_OSVERSION'] = 'yes'
if p['pkgsite'] is not None and action in ('update', 'install', 'upgrade',):
if repo_flag_not_supported:
pkgng_env['PACKAGESITE'] = p['pkgsite']
else:
cmd.append('--repository=%s' % (p['pkgsite'],))
# If environ_update is specified to be "passed through"
# to module.run_command, then merge its values into pkgng_env
pkgng_env.update(kwargs.pop('environ_update', dict()))
2020-03-09 10:11:07 +01:00
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
return module.run_command(cmd + list(args), environ_update=pkgng_env, **kwargs)
2020-03-09 10:11:07 +01:00
if pkgs == ['*'] and p["state"] == 'latest':
# Operate on all installed packages. Only state: latest makes sense here.
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
_changed, _msg, _stdout, _stderr = upgrade_packages(module, run_pkgng)
2020-03-09 10:11:07 +01:00
changed = changed or _changed
stdout += _stdout
stderr += _stderr
2020-03-09 10:11:07 +01:00
msgs.append(_msg)
# Operate on named packages
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if len(pkgs) == 1:
# The documentation used to show multiple packages specified in one line
# with comma or space delimiters. That doesn't result in a YAML list, and
# wrong actions (install vs upgrade) can be reported if those
# comma- or space-delimited strings make it to the pkg command line.
pkgs = re.split(r'[,\s]', pkgs[0])
named_packages = [pkg for pkg in pkgs if pkg != '*']
if p["state"] in ("present", "latest") and named_packages:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
_changed, _msg, _out, _err = install_packages(module, run_pkgng, named_packages,
p["cached"], p["state"])
stdout += _out
stderr += _err
changed = changed or _changed
msgs.append(_msg)
elif p["state"] == "absent" and named_packages:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
_changed, _msg, _out, _err = remove_packages(module, run_pkgng, named_packages)
stdout += _out
stderr += _err
2020-03-09 10:11:07 +01:00
changed = changed or _changed
msgs.append(_msg)
if p["autoremove"]:
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
_changed, _msg, _stdout, _stderr = autoremove_packages(module, run_pkgng)
2020-03-09 10:11:07 +01:00
changed = changed or _changed
stdout += _stdout
stderr += _stderr
2020-03-09 10:11:07 +01:00
msgs.append(_msg)
pkgng: add basic integration tests (#3526) * pkgng: package reinstallations count as changed `upgrade_packages()` only looked for the string "Number of packages to be upgraded", but the `pkg upgrade` command also reports "Number of packages to be reinstalled". Reinstallation occurs when package metadata other than version changes (e.g. build options, single architecture to `*` architecture). In any other respect, though, a required reinstallation is the same as an upgrade. * 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. * pkgng: add basic integration tests Test installing a package Test installing a package into a jail * pkgng: test with zsh not bash package Had been using bash package, because it's not likely to disappear from the package repository any time soon. Turns out that bash is already installed on the Ansible community.general FreeBSD CI VM image, which makes the test fail. Zsh probably isn't going away any time soon, either, and likely won't be installed on the CI image. * pkgng: remove redundant test for docker container These tests should be skip/docker, but the test playbook also redundantly checked whether it was running in a docker container. Checking whether `ansible_facts.distribution` is `FreeBSD` is really sufficient to be sure whether the test is running in an environment that supports the `pkgng` module. * pkgng: add state=absent test Also renumber `jail=...` test to make diffs less noisy when adding more non-jailed tests. * pkgng: add state=latest idemptency test Add test between state=present and state=absent that ensures state=latest runs successfully on an already up-to-date package with changed=false. * pkgng: add state=latest test Including small filesize out-of-date package from FreeBSD 11 package repository, because FreeBSD 11 is currently EOL. This test might fail at some point in the future if the pkg utility in a FreeBSD version > 14 makes breaking changes that prevents it from installing older package formats. If that occurs, the fix is to replace `files/freebsd-release-manifests-20210413__FreeBSD_11.pkg` with the version of freebsd-release-manifests from the oldest non-breaking release of FreeBSD, and update the references to it in tasks/main.yml accordingly. * pkgng: use hand-generated test package Instead of relying on a broken-ish installation of an older package from FreeBSD 11, hand-generate a package named `zsh` with no contents and a version number of `0`. It can be installed on any architecture and any revision of FreeBSD sucessfully, and it will always be eligible for upgrade. * pkgng: CI seems to dislike plus-signs in filenames * pkgng: refactor renaming test package manifests refactored to make intent of adding '+' sign clearer * pkgng: refactor upgraded pattern matching Implement russoz's suggestion to put all variants in the pattern. * pkgng: add cached=yes test Changed idempotency test, because this covers both. * pkgng: test pkg works on any FreeBSD version Removing `ansible_distribution_version`, because the test out-of-date zsh package should install on any FreeBSD version. * pkgng: move FreeBSD tasks to imported file Refactoring tests for more reuse and easier readability. * pkgng: refactor tests for task reuse Several tests need to install an out-of-date package to test whether upgrades occur successfully, so this commit refactors out the generation of the test package. Also, factor out things like the name and path of the test package so that is more maintainable should the target test package (currently `zsh`) ever need to change. * pkgng: test install multiple packages Multiple packages should install with a single invocation of the `pkg` utility. * pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. * pkgng: test state=latest for mixed install/upgrade Test that a list of packages given, one not installed and one installed but out-of-date are handled correctly. * pkgng: use YAML lists for multiple packages I had been following the documentation examples, and specifiying multiple packages on one line. The right way to do it is with YAML list syntax. * pkgng: add test for autoremove=yes Install package with known dependencies, then remove it and ask autoremove to run. * pkgng: test autoremove=yes only on FreeBSD > 12 The CI test runner on FreeBSD 12.0 times out when downloading large packages. * pkgng: test jail creation logging less verbose `ezjail-admin` had been spewing every filename copied into the jail onto stderr, making the `ansible-test -v` logs hard to scroll through. Changed it so that ezjail output only shows up in the ansible task logs if `ansible_verbosity > 1` (`-vv...`). Full `ezjail-admin` output is always logged to `/tmp/ezjail.log`. * pkgng: pass tests when package dependencies change Tests that install packages with dependencies were failing when those dependencies were not already installed, because the count of installed packages was greater than the count of requested to be installed packages. This change checks for a count of installed packages that is greater than or equal to the count of requested to be installed packages. Example: - Test installs packages `zsh` and `fish` - `fish` has a dependency on `pcre2` - `pkg` reports `Number of packages to be installed: 3` * pkgng: test annotation for single package Add/modify/remove annotation for a single package * pkgng: fix annotation operations Annotation has been broken at least since the migration to collections. There are some breaking typos and function argument omissions in the code with nothing in `git blame` but "Initial commit". New integration tests uncovered this breakage. Works now. * pkgng: test multiple annotations Test multiple annotations specified both in new (YAML list) syntax and old (comma-delimited) syntax. Rename some annotation test tags from the single-annotation tests to make sure that single and multiple annotation tests don't collide. * pkgng: test invalid annotate strings Ensure that some likely to occur, invalid strings given to the annotate module parameter 1. fail, 2. do not cause an exception, and 3. do not create any actual annotations on packages. * pkgng: fix check_mode for annotate Actions specified in the `annotate` parameter would always be performed, even if `check_mode=yes`. This commit fixes `check_mode` for the annotation functions and adds integration tests to ensure that check mode is honored in the future. * pkgng: call module.run_command with list Instead of calling `run_command` with a formatted string, call it with a list to maintain safer argument separation. Also, introduce a wrapper for `run_command`, `run_pkgng`, which manages the process environment and some common command line switches, based upon the module parameters. Introduced in this commit, also pass annotation values to `pkg annotate` via stdin, which is safer with long values than putting them in argv. * pkgng: update documentation to match annotate type Missed updating the documentation to match the change of the annotate parameter from comma-separated string to list of strings. * pkgng: fix syntax for Python 2 Seems *args, **kwargs have to be the last two arguments in Python 2 syntax. CI sanity tests were failing. * pkgng: oops, pkgsite should be from closure I put `pkgsite` as a keyword argument to `run_pkgng()', which wasn't correct at all and resulted it in capturing the second positional argument given by any caller. `pkgsite` should have been `p["pkgsite"]`, coming from the closure environment of `main()`. * pkgng: Fix changelog fragment quote formatting Co-authored-by: Felix Fontein <felix@fontein.de> * pkgng: add test prepare task Make sure test environment does not contain the test package. * pkgng: make integration test package more flexible Make integration test package building template-based, so more flexible if in the future the name of the test package, currently `zsh`, must change. * pkgng: convert generator to list for 2.9 Ansible 2.9 can't count items from a generator, so convert `select` filter output via `list` before passing to `count`. * pkgng: Ansible 2.9 has no "false" Jinja2 test Apparently Ansible 2.9 doesn't have the "false" test in Jinja2 contexts. Switching to use `rejectattr(...)` instead of `selectattr(..., "false")`. * pkgng: test pkgsite parameter * pkgng: add tests for chroot and rootdir params Refactor install tasks into an include file so they can be reused and tested with various parameters. * pkgng: clean up test package creation Make pkg json manifest more readable. Create package using FreeBSD's `pkg create` instead of manually using tar. This change also simplifies the manifest to a single file for the integration test role. * pkgng: fix #3428; install from local file Fixes a bug in which the module reported failure when installing from a local (to the target host) file path. Fixes #3428 * pkgng: changelog fragment formatting issue * pkgng: check for all test package extensions pkg recently changed file extensions, so for the tests to work on multiple recent versions of FreeBSD, we must check for `pkg create` output with varying filename. * Revert "pkgng: fix #3428; install from local file" As the module has invoked pkg with the `-g` flag for at least 7 years, I'm not sure when it was possible to install packages from files on the target host filesystem, because pkg rejects file paths when the `--glob` flag is enabled. I considered doing a rework of the pkg invocation to enable good support for installing from local files, but it looks like more of a job than for this PR. This reverts commit 5f94eac41ff040a79d4d00d2f723a9b3a148418e. * pkgng: Add minor_changes fragment for #3526 Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-10-22 07:32:35 +02:00
if p["annotation"] is not None:
_changed, _msg = annotate_packages(module, run_pkgng, pkgs, p["annotation"])
2020-03-09 10:11:07 +01:00
changed = changed or _changed
msgs.append(_msg)
module.exit_json(changed=changed, msg=", ".join(msgs), stdout=stdout, stderr=stderr)
2020-03-09 10:11:07 +01:00
if __name__ == '__main__':
main()