From de239c0a9d64e466db0e0dd9b8a08513e5b00c61 Mon Sep 17 00:00:00 2001 From: Alvin Schiller <103769832+AlvinSchiller@users.noreply.github.com> Date: Sun, 12 Nov 2023 22:07:07 +0100 Subject: [PATCH] added verify functions. --- installation/includes/02_helpers.sh | 76 +++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/installation/includes/02_helpers.sh b/installation/includes/02_helpers.sh index d2e1162f1..df9fb8a43 100644 --- a/installation/includes/02_helpers.sh +++ b/installation/includes/02_helpers.sh @@ -56,3 +56,79 @@ check_os_type() { fi } + + + +##### Test helpers + +# Check if the file(s) has/have the expected owner and modifications +verify_chmod_chown() { + local mod_expected=$1 + local user_expected=$2 + local group_expected=$3 + + for file in "$@" + do + test ! -f ${file} && exit_on_error "ERROR: ${file} does not exists or is not a file!" + + mod_actual=$(stat --format '%a' "${file}") + user_actual=$(stat -c '%U' "${file}") + group_actual=$(stat -c '%G' "${file}") + test ! "${mod_expected}" -eq "${mod_actual}" && exit_on_error "ERROR: ${file} actual mod (${mod_actual}) differs from expected (${mod_expected})!" + test ! "${user_expected}" == "${user_actual}" && exit_on_error "ERROR: ${file} actual owner (${user_actual}) differs from expected (${user_expected})!" + test ! "${group_expected}" == "${group_actual}" && exit_on_error "ERROR: ${file} actual group (${group_actual}) differs from expected (${group_expected})!" + done +} + +verify_file_contains_string() { + local string="$1" + local file="$2" + + if [[ ! $(grep -iw "${string}" "${file}") ]]; then + exit_on_error "ERROR: '${string}' not found in ${file}" + fi +} + +check_service_state() { + local service="$1" + local desired_state="$2" + + local actual_state=$(systemctl show -p ActiveState --value "${service}") + if [[ ! "${actual_state}" == "${desired_state}" ]]; then + exit_on_error "ERROR: service ${service} is not ${desired_state} (state: ${actual_state})." + fi +} + +check_service_enablement() { + local service="$1" + local desired_enablement="$2" + + local actual_enablement=$(systemctl is-enabled "${service}") + if [[ ! "${actual_enablement}" == "${desired_enablement}" ]]; then + exit_on_error "ERROR: service ${service} is not ${desired_enablement} (state: ${actual_enablement})." + fi +} + +# Reads a textfile and returns all lines as args. +# Does filter out comments, egg-prefixes and version suffixes +# Arguments: +# 1 : textfile to read +get_args_from_file() { + local package_file="$1" + sed 's/.*#egg=//g' ${package_file} | sed -E 's/(#|=|>|<).*//g' | xargs echo +} + +# Check if all passed packages are installed. Fail on first missing. +verify_apt_packages() { + local packages="$@" + + if [ -n "${packages}" ]; then + local apt_list_installed=$(apt -qq list --installed 2>/dev/null) + for package in ${packages} + do + if [[ ! $(echo "${apt_list_installed}" | grep -i "${package}.*installed") ]]; then + exit_on_error "ERROR: ${package} is not installed" + fi + done + fi +}