Skip to content

Commit

Permalink
fixup scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
rjeffman committed Jul 8, 2024
1 parent 1fde877 commit c93581b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 75 deletions.
4 changes: 2 additions & 2 deletions utils/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ read -r -a ENABLED_TESTS <<< "${IPA_ENABLED_MODULES:-""}"
IMAGE_TAG="fedora-latest"
scenario="freeipa-tests"
MEMORY=3
HOSTNAME="ipaserver.test.local"
IPA_HOSTNAME="ipaserver.test.local"
SEED="$(date "+%Y%m%d")"
GROUP=1
SPLITS=0
Expand Down Expand Up @@ -152,7 +152,7 @@ export ANSIBLE_LIBRARY="${TOPDIR}/plugins"
export ANSIBLE_MODULE_UTILS="${TOPDIR}/plugins/module_utils"

# Start container
"${SCRIPTDIR}/setup_test_container.sh" -e "${engine}" -m "${MEMORY}" -p "${ansible_interpreter}" -i "${IMAGE_TAG}" -n "${HOSTNAME}" "${scenario}" || die "Failed to setup test container"
"${SCRIPTDIR}/setup_test_container.sh" -e "${engine}" -m "${MEMORY}" -p "${ansible_interpreter}" -i "${IMAGE_TAG}" -n "${IPA_HOSTNAME}" -a "${scenario}" || die "Failed to setup test container"


# run tests
Expand Down
108 changes: 43 additions & 65 deletions utils/setup_test_container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SCRIPTDIR="$(readlink -f "$(dirname "$0")")"
usage() {
local prog="${0##*/}"
cat <<EOF
usage: ${prog} [-h] [-l] [-e ENGINE] [-i IMAGE] [-m MEMORY] [-n HOSTNAME] NAME
usage: ${prog} [-h] [-l] [-a] [-e ENGINE] [-i IMAGE] [-m MEMORY] [-n HOSTNAME] NAME
${prog} starts a container to test ansible-freeipa.
EOF
Expand All @@ -27,6 +27,7 @@ Options:
-h display this message and exit
-l list available images
-a Test Ansible connection.
-e ENGINE set the container engine to use
(default: ${WHITE}podman${RST}, if available)
-i IMAGE select image to run the tests (default: fedora-latest)
Expand All @@ -47,95 +48,72 @@ list_images() {

IMAGE_TAG="fedora-latest"
MEMORY="${MEMORY:-3}"
HOSTNAME="${HOSTNAME:-"ipaserver.test.local"}"
IPA_HOSTNAME="${IPA_HOSTNAME:-"ipaserver.test.local"}"
test_env="${test_env:-"/tmp"}"
ansible_interpreter="/usr/bin/python3"
engine="podman"
ansible_test=""

while getopts ":he:i:lm:n:p:" option
while getopts ":hae:i:lm:n:p:" option
do
case "$option" in
h) help && exit 0 ;;
a) ansible_test="yes" ;;
e) engine="${OPTARG}" ;;
i) IMAGE_TAG="${OPTARG}" ;;
l) list_images && exit 0 || exit 1;;
m) MEMORY="${OPTARG}" ;;
n) HOSTNAME="${OPTARG}" ;;
n) IPA_HOSTNAME="${OPTARG}" ;;
p) ansible_interpreter="${OPTARG}" ;;
*) die -u "Invalid option: ${OPTARG}" ;;
esac
done

export HOSTNAME MEMORY IMAGE_TAG scenario
export IPA_HOSTNAME MEMORY IMAGE_TAG scenario

shift $((OPTIND - 1))
[ $# == 1 ] || die -u "You must provide the name for a single container."
scenario="${1}"
shift

make_inventory "${scenario}" "${engine:-podman}" "${ansible_interpreter:-"/usr/bin/python3"}"
# shellcheck disable=SC2154
log info "Inventory path: [${inventory}]"
# shellcheck disable=SC2154
log debug "$(cat "${inventory}")"
prepare_container "${scenario}" "${IMAGE_TAG}"
start_container "${scenario}"

ansible_ping "${inventory}"

# configure ipaserver dns resolver to point to itself
run_inline_playbook "${test_env}/playbooks" <<EOF || die "Failed to verify IPA or KDC services."
---
- name: Set DNS resolver to localhost
hosts: ipaserver
become: true
gather_facts: false
tasks:
# /etc/resolv.conf on containers must be overwriten
# Both copy and file modules try to move data over it
# and it fails with EBUSY.
- name: Configure /etc/resolv.conf
ansible.builtin.shell: echo "nameserver 127.0.0.1" > /etc/resolv.conf
become: true
EOF
log info "Setting container DNS nameserver to localhost."
"${engine}" exec "${scenario}" /bin/sh -c 'echo "nameserver 127.0.0.1" > /etc/resolv.conf' || die "Failed to set DNS nameserver to localhost."

# wait for FreeIPA services to be available
run_inline_playbook "${test_env}/playbooks" <<EOF || die "Failed to verify IPA or KDC services."
---
- name: Wait for IPA services to be available
hosts: ipaserver
become: true
gather_facts: false
tasks:
- name: Wait for IPA to be started.
block:
- name: Start IPA service
ansible.builtin.shell: ipactl restart
register: result
until: not result.failed
retries: 5
delay: 30
rescue:
- name: Report failure
ansible.builtin.shell: ipactl status
failed_when: true
- name: Wait for Kerberos KDC to be started.
ansible.builtin.systemd:
name: krb5kdc
state: started
register: result
until: not result.failed
retries: 15
delay: 10
- name: Check if TGT is available for admin.
ansible.builtin.shell:
cmd: echo SomeADMINpassword | kinit -c ansible_freeipa_cache admin
register: result
until: not result.failed
retries: 5
delay: 10
- name: Cleanup TGT.
ansible.builtin.shell:
cmd: kdestroy -c ansible_freeipa_cache -A
...
EOF
log info "Restarting IPA services."
# shellcheck disable=SC2016
"${engine}" exec "${scenario}" /bin/sh -c 'for i in $(seq 5); do ipactl restart >/dev/null && break; sleep 20; done' || die "Failed to start IPA."

# wait for KDC services to be available
log info "Restarting IPA services."
# shellcheck disable=SC2016
"${engine}" exec "${scenario}" /bin/sh -c 'for i in $(seq 5); do systemctl restart krb5kdc >/dev/null && break; sleep 10; done' || die "Failed to start IPA KDC."

# ensure we can get a TGT for admin
log info "Testing kinit with admin."
# shellcheck disable=SC2016
"${engine}" exec "${scenario}" /bin/sh -c 'for i in $(seq 5); do echo "SomeADMINpassword" | kinit -c ansible_freeipa_cache admin && kdestroy -c ansible_freeipa_cache -A && break; sleep 10; done' || die "Failed to grant admin TGT."

# shellcheck disable=SC2154
log info "Creating inventory."
make_inventory "${scenario}" "${engine}" "${ansible_interpreter:-"/usr/bin/python3"}"
if [ -z "${inventory}" ]
then
log error "Could not create inventory file."
else
log info "Inventory path: [${inventory}]"
# shellcheck disable=SC2154
log debug "$(cat "${inventory}")"
if [ "${ansible_test}" == "yes" ]
then
log info "Testing Asnible connection."
run_if_exists ansible_ping "${inventory}"
log info "Querying installed software"
run_if_exists query_container_installed_software
fi
fi

2 changes: 1 addition & 1 deletion utils/shansible
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ run_inline_playbook() {
quiet mkdir -p "${playbookdir}"
cat - >"${playbook}"
# shellcheck disable=SC2086
ansible-playbook ${ansible_options:-} -i "${inventory}" "${playbook}"
run_if_exists ansible-playbook ${ansible_options:-} -i "${inventory}" "${playbook}"
err=$?
rm "${playbook}"
return ${err}
Expand Down
3 changes: 1 addition & 2 deletions utils/shcontainer
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ prepare_container() {
local IMAGE_TAG img_id CONFIG
container_id=""
container_status=("-f" "status=created" "-f" "status=running")
hostname="${HOSTNAME:-"ipaserver.test.local"}"
hostname="${IPA_HOSTNAME:-"ipaserver.test.local"}"
scenario="${1:-${scenario:-"freeipa-tests"}}"
IMAGE_TAG="${2:-${IMAGE_TAG:-fedora-latest}}"
[ -n "${scenario}" ] && container_id="$(${engine} ps --all -q -f "name=${scenario}" "${container_status[@]}")"
Expand All @@ -47,7 +47,6 @@ start_container() {
local scenario="${1:-${scenario}}"
log info "Starting container for ${scenario}..."
"${engine}" start "${scenario}"
run_if_exists query_container_installed_software
}

if [ -z "$(command -v podman)" ]
Expand Down
13 changes: 8 additions & 5 deletions utils/shfun
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interrupt_exception() {
trap - SIGINT
log warn "User interrupted test execution."
# shellcheck disable=SC2119
cleanup
cleanup "${scenario:-''}"
exit 1
}

Expand All @@ -57,12 +57,15 @@ run_if_exists() {

# shellcheck disable=SC2120
cleanup() {
local container container_engine
container="${1:-${scenario:-"freeipa-tests"}}"
container_engine="${2:-${engine:-"podman"}}"
if [ "${STOP_CONTAINER:-"Y"}" == "Y" ]
then
run_if_exists stop_container "${1:-${scenario:-"freeipa-tests"}}" "${2:-${engine:-"podman"}}"
run_if_exists stop_container "${container}" "${container_engine}"
[ -f "${inventory:-}" ] && rm "${inventory}"
else
log info "Keeping container: $(${engine:-"podman"} ps --format "{{.Names}} - {{.ID}}" --filter "name=${1}")"
log info "Keeping container: $(${container_engine} ps --format "{{.Names}} - {{.ID}}" --filter "name=${container}")"
fi
if [ "${STOP_VIRTUALENV:-"N"}" == "Y" ]
then
Expand Down Expand Up @@ -106,7 +109,7 @@ start_virtual_environment() {
fi
log info "Starting virtual environment: ${envdirectory}"
[ -f "${envdirectory}/bin/activate" ] || die "Failed to create virtual environment."
# shellcheck disable=SC1090
# shellcheck disable=SC1091
. "${envdirectory}/bin/activate" || die "Cannot activate virtual environment."
export STOP_VIRTUALENV="Y"
log info "Installing required tools."
Expand Down Expand Up @@ -145,7 +148,7 @@ die() {
fi
log error "${*}"
STOP_CONTAINER="N"
run_if_exists cleanup
cleanup "${scenario:-''}"
[ "${usg}" == "Y" ] && run_if_exists usage
exit 1
}
Expand Down

0 comments on commit c93581b

Please sign in to comment.