Skip to content

Commit

Permalink
add test_comp
Browse files Browse the repository at this point in the history
  • Loading branch information
SaswatPadhi committed Sep 7, 2024
1 parent 3407a9b commit 7a772ce
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 43 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/self_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Self Test Script"

on:
pull_request:
branches: master
push:
branches: master

jobs:
test:
runs-on: ubuntu-latest
steps:
-
name: "Git: Checkout"
uses: actions/checkout@v4
-
name: "Run: Test Script"
run: _scripts/test_comp.sh
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Services
# Services <a href='https://github.com/padhi-homelab/services/actions/workflows/self_test.yml'><img align='right' src='https://img.shields.io/github/actions/workflow/status/padhi-homelab/services/self_test.yml?branch=master&logo=github&logoWidth=24&style=for-the-badge&label=self test'></img></a>


<details>
<summary><h3>Why <code>comp</code>?</h3></summary>
Expand Down
41 changes: 41 additions & 0 deletions _scripts/test_comp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

set -Eumo pipefail

SELF_DIR="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
cd "$SELF_DIR/.."

TEST_COMP_TMP_PATH=/tmp/__test_comp_intermediate_storage_dir__

rm -rf "$TEST_COMP_TMP_PATH"
mkdir -p "$TEST_COMP_TMP_PATH"

source <( grep -P "^EXIT_CODE_[A-Z_]+=.+" ./comp )

function __test_comp () {
echo "[>] Test '$1': "

local io_path_prefix="$TEST_COMP_TMP_PATH/$1"
local err_path="$io_path_prefix.err"
local out_path="$io_path_prefix.out"

./comp $2 >"$out_path" 2>"$err_path"
local ret=$?

eval "$3" \
&& echo "[+] SUCCESS!" \
|| echo "[-] FAILURE [$4]"
echo
}

__test_comp \
"Run without args" \
"" \
'[ $ret -eq $EXIT_CODE_USAGE_ERROR ]' \
"Expected EXIT_CODE_USAGE_ERROR"

__test_comp \
"Run on non-existent composition" \
"status unknown_comp" \
'[ $ret -eq $EXIT_CODE_COMPOSITION_NOT_FOUND ] && grep -sq "is not a base directory" "$err_path"' \
"Expected EXIT_CODE_COMPOSITION_NOT_FOUND and helpful error message"
77 changes: 35 additions & 42 deletions comp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ YQ_CMD="yq"
FINAL_EXIT_CODE=0
EXIT_CODE_GENERIC_ERROR=1

EXIT_CODE_USAGE_ERROR=-10
EXIT_CODE_DEP_INSTALL_FAILURE=-20
EXIT_CODE_COMPOSITION_NOT_FOUND=-30
EXIT_CODE_USAGE_ERROR=250
EXIT_CODE_OPTIONS_CONF_ERROR=240
EXIT_CODE_DEP_INSTALL_FAILURE=230
EXIT_CODE_COMPOSITION_NOT_FOUND=220

EXIT_CODE_GEN_ERROR=10
EXIT_CODE_PRE_HOOK_SCRIPT_ERROR=20
Expand All @@ -35,12 +36,6 @@ OPTION_LABELS=""
OPTION_LOGGING=""
OPTION_PORTS=""

ARG_DEVICES="auto"
ARG_HOOKS="auto"
ARG_LABELS="auto"
ARG_LOGGING="auto"
ARG_PORTS="auto"

: "${COMP_INTERNAL_CALL:=}"

# # # # # # # # # # # # # # # # # # # HELPER ROUTINES # # # # # # # # # # # # # # # # # # #
Expand Down Expand Up @@ -79,7 +74,6 @@ __create_external_networks () {

__do_prereqs () {
[ "$FLAG_SKIP_PREREQS" != "yes" ] || return 0

[ -f "$PREREQS_FILENAME" ] || return 0

local verb="$1"
Expand Down Expand Up @@ -155,30 +149,21 @@ __maybe_fail_fast () {

__read_option () {
local OPTION="OPTION_$1"
local OVERRIDE_OPTION=""

local ARG="ARG_$1"
case "${!ARG}" in
yes ) printf -v "$OPTION" "%s" "yes"
return 0 ;;
no ) printf -v "$OPTION" "%s" "no"
printf "$opt_line_head: $1 (arg) "
opt_line_head=''
return 0 ;;
esac

if [ "$FLAG_SKIP_OVERRIDES" != "yes" ]; then
local OVERRIDE_COMP_OPTION="$(cat options.override.conf 2>/dev/null | grep $1 | cut -d= -f2)"
if ! [ -z "$OVERRIDE_COMP_OPTION" ] ; then
printf -v "$OPTION" "%s" "$OVERRIDE_COMP_OPTION"
if [ "$OVERRIDE_COMP_OPTION" != "yes" ]; then
printf "$opt_line_head: $1 (conf) "
opt_line_head=''
fi
return 0
if [ -z "${!OPTION}" ] && [ "$FLAG_SKIP_OVERRIDES" != "yes" ]; then
OVERRIDE_OPTION="$(cat options.override.conf 2>/dev/null | grep "$1" | cut -d= -f2)"
if [ -n "$OVERRIDE_OPTION" ] ; then
validate_and_set_option "$OVERRIDE_OPTION" "$1" override || return 1
fi
fi

printf -v "$OPTION" "%s" "yes"
[ -n "${!OPTION}" ] || printf -v "$OPTION" "%s" "yes"

if [ "${!OPTION}" != "yes" ] ; then
printf "$opt_line_head: $1 "
[ -z "$OVERRIDE_OPTION" ] && printf "(arg) " || printf "(conf) "
opt_line_head=''
fi
}

__run_hooks () {
Expand Down Expand Up @@ -357,8 +342,6 @@ Compositions Found ($num_comps):" >&2

[ $# -gt 0 ] || usage

VERBS="${1//,/ }"

expand_verbs () {
local expanded=""
while read -n1 char; do
Expand All @@ -376,6 +359,7 @@ expand_verbs () {
VERBS="$expanded"
}

VERBS="${1//,/ }"
for VERB in $VERBS ; do
[ "$VERB" = "clean" ] || \
[ "$VERB" = "down" ] || \
Expand Down Expand Up @@ -409,11 +393,16 @@ for opt in "$@" ; do
esac
done

validate_optarg () {
validate_and_set_option () {
local OPTION="OPTION_$2"
case "$1" in
yes | no ) return 0 ;;
yes | no ) printf -v "$OPTION" "%s" "$1"
return 0
esac
usage "Unknown option argument: '$1'"
[ "$3" = "override" ] \
&& ( echo ; __error "Invalid value '$1' for '${2,,}' option in options.override.conf" ) \
|| usage "Invalid value '$1' for '${2,,}' option"
return 1
}

OPTIND=1
Expand All @@ -424,11 +413,11 @@ while getopts ':FOPRd:g:h:l:p:' OPTION ; do
"P" ) FLAG_SKIP_PREREQS="yes" ;;
"R" ) FLAG_SKIP_REGENERATE="yes" ;;

"d" ) validate_optarg "$OPTARG" && ARG_DEVICES="$OPTARG" ;;
"g" ) validate_optarg "$OPTARG" && ARG_LOGGING="$OPTARG" ;;
"h" ) validate_optarg "$OPTARG" && ARG_HOOKS="$OPTARG" ;;
"l" ) validate_optarg "$OPTARG" && ARG_LABELS="$OPTARG" ;;
"p" ) validate_optarg "$OPTARG" && ARG_PORTS="$OPTARG" ;;
"d" ) validate_and_set_option "$OPTARG" DEVICES usage ;;
"g" ) validate_and_set_option "$OPTARG" LOGGING usage ;;
"h" ) validate_and_set_option "$OPTARG" HOOKS usage ;;
"l" ) validate_and_set_option "$OPTARG" LABELS usage ;;
"p" ) validate_and_set_option "$OPTARG" PORTS usage ;;

* ) usage "Unrecognized option: -$OPTARG." ;;
esac
Expand Down Expand Up @@ -501,10 +490,14 @@ perform () {

cd "$SELF_DIR/$comp"

local opt_error=0
opt_line_head='[o] Disabled options'
for opt in DEVICES HOOKS LABELS LOGGING PORTS ; do
__read_option $opt
__read_option $opt && continue
opt_error=1
done
[ "$opt_error" -eq 0 ] \
|| __maybe_fail_fast $EXIT_CODE_OPTIONS_CONF_ERROR || continue
[ -n "$opt_line_head" ] || echo

[ "$SIMPLE_VERB" = "clean" ] || __gen_env \
Expand Down

0 comments on commit 7a772ce

Please sign in to comment.