Files
ansible-freeipa/utils/shansible
Rafael Guterres Jeffman 68bca84481 utils: Rewrite run-tests.sh to use functions and extenal scripts
To modify Azure tests and depend on shell scripts and pytest instead
of molecule, the run-tests.sh script has been rewritten to depend on
bash functions and on a bash script that prepare and start a testing
container.

This patch adds a new script, 'utils/setup_test_container.sh' that
can be used to start a new container, using either podman or docker,
based on the available ansible-freeipa images. The new container can
then be used to run ansible-freeipa tests against it.

Also the following files with bash functions were added, and are
used by both scripts:

    utils/shansible: Functions to run playbooks in the container
    utils/shcontainer: Functions to setup/run a container
    utils/shfun: Generic shell helper functions (e.g.: log)
2024-08-02 17:19:39 +02:00

89 lines
2.9 KiB
Bash

#!/bin/bash -eu
# This file is meant to be source'd by other scripts
SCRIPTDIR="$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}")")"
# shellcheck source=utils/shfun
. "${SCRIPTDIR}/shfun"
install_ansible() {
ANSIBLE_VERSION="${1:-${ANSIBLE_VERSION:-"ansible-core"}}"
[ $# -gt 0 ] && shift
log info "Installing Ansible: ${ANSIBLE_VERSION}"
pip install --quiet "${ANSIBLE_VERSION}"
log debug "Ansible version: $(ansible --version | sed -n "1p")${RST}"
if [ -n "${ANSIBLE_COLLECTIONS}" ]
then
log warn "Installed collections will not be removed after execution."
log none "Installing: Ansible Collection ${ANSIBLE_COLLECTIONS}"
# shellcheck disable=SC2086
quiet ansible-galaxy collection install ${ANSIBLE_COLLECTIONS} || die "Failed to install Ansible collections."
fi
export ANSIBLE_VERSION
}
run_inline_playbook() {
local playbookdir playbook err
playbookdir=${1:-"playbooks"}
quiet mkdir -p "${playbookdir}"
playbook=$(mktemp "${playbookdir}/ansible-freeipa-test-playbook_ipa.XXXXXXXX" 2>/dev/null)
# In some configurations, it may not be possible to use another
# directory, so we store the playbook in the current one.
# [ -z "${playbook}" ] && playbook=$(mktemp "ansible-freeipa-test-playbook_ipa.XXXXXXXX")
inventory="${inventory:-${test_env:-"."}/inventory}"
quiet mkdir -p "${playbookdir}"
cat - >"${playbook}"
# shellcheck disable=SC2086
run_if_exists ansible-playbook ${ansible_options:-} -i "${inventory}" "${playbook}"
err=$?
rm -f "${playbook}"
return ${err}
}
make_inventory() {
local scenario pod_engine ansible_interpreter
scenario=$1
pod_engine="${engine:-${2:-podman}}"
ansible_interpreter="${3:-${ansible_interpreter:-"/usr/bin/python3"}}"
export inventory="${test_env:-"."}/inventory"
log info "Inventory file: ${inventory}"
cat << EOF > "${inventory}"
[ipaserver]
${scenario} ansible_connection=${pod_engine} ansible_python_interpreter=${ansible_interpreter}
[ipaserver:vars]
ipaserver_domain = test.local
ipaserver_realm = TEST.LOCAL
EOF
}
query_container_installed_software() {
# check image software versions.
run_inline_playbook "${test_env:-"/tmp"}/playbooks" <<EOF || die "Failed to verify software installation."
---
- name: Software environment.
hosts: ipaserver
become: yes
gather_facts: no
tasks:
- name: Retrieve versions.
ansible.builtin.shell: |
cat /etc/redhat-release
${python:-"python3"} --version
rpm -q freeipa-server freeipa-client ipa-server ipa-client 389-ds-base pki-ca krb5-server
uname -a
register: result
- name: Testing environment.
ansible.builtin.debug:
var: result.stdout_lines
EOF
}
ansible_ping() {
# shellcheck disable=SC2086
ansible ${ansible_options:-} -m ping -i "${1:-${inventory}}" all || die "Could not connect to container."
}
export ANSIBLE_VERSION=${ANSIBLE_VERSION:-'ansible-core'}