Skip to content

Commit

Permalink
added verify functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvinSchiller committed Nov 12, 2023
1 parent d27631e commit de239c0
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions installation/includes/02_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit de239c0

Please sign in to comment.