install.fairie/home/dot_local/share/ansible/test/darwin/test.sh
2023-07-16 01:40:26 -04:00

54 lines
1.5 KiB
Bash

#!/usr/bin/env bash
# @file test/darwin/test.sh
# @brief A script that is used to test the role's macOS compatibility via a
# [GitHub action](https://gitlab.com/megabyte-labs/common/ansible/-/blob/master/files-role/.github/workflows/macOS.yml).
TEST_TYPE='darwin'
# @description Ensure Ansible is installed
if ! type ansible &> /dev/null; then
pip3 install ansible
fi
# @description Ensure Ansible Galaxy dependencies are installed
if [ -f requirements.yml ]; then
ansible-galaxy install -r requirements.yml
fi
# @description Symlink the Ansible Galaxy role name to the working directory one level up
ROLE_NAME="$(grep "role:" test/darwin/test.yml | sed 's^- role: ^^' | xargs)"
ln -s "$(basename "$PWD")" "../$ROLE_NAME"
# @description Back up files and then copy replacements
function backupAndCopyFiles() {
if [ -f ansible.cfg ]; then
cp ansible.cfg ansible.cfg.bak
fi
cp "test/$TEST_TYPE/ansible.cfg" ansible.cfg
if [ -f test.yml ]; then
cp test.yml test.yml.bak
fi
cp "test/$TEST_TYPE/test.yml" test.yml
}
# @description Restores files that were backed up
function restoreFiles() {
if [ -f ansible.cfg.bak ]; then
mv ansible.cfg.bak ansible.cfg
fi
if [ -f test.yml.bak ]; then
mv test.yml.bak test.yml
fi
}
# @description Calls [restoreFiles] and exits with an error
function restoreFilesAndExitError() {
restoreFiles
exit 1
}
# @description Back up files, run the play, and then restore files
backupAndCopyFiles
ansible-playbook -i "test/$TEST_TYPE/inventory" test.yml || restoreFilesAndExitError
restoreFiles