diff --git a/.github/workflows/panel-scripts.yml b/.github/workflows/panel-scripts.yml index 936d76f..f6b1580 100644 --- a/.github/workflows/panel-scripts.yml +++ b/.github/workflows/panel-scripts.yml @@ -4,6 +4,7 @@ on: push: branches: [main] paths: + - custom.sh - debug.sh - dev.sh - plugin.sh @@ -22,6 +23,7 @@ on: - .github/workflows/panel-scripts.yml pull_request: paths: + - custom.sh - debug.sh - dev.sh - plugin.sh @@ -44,7 +46,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - run: shellcheck -x service.sh utils/ensure_auth_cookie_domain.sh utils/ensure_node_ip.sh utils/dev_game_server.sh utils/gpu_nodes.sh tests/scripts/*.sh tests/scripts/stub/kubectl + - run: shellcheck -x service.sh utils/ensure_auth_cookie_domain.sh utils/ensure_node_ip.sh utils/dev_game_server.sh utils/gpu_nodes.sh utils/choose_node.sh tests/scripts/*.sh tests/scripts/stub/kubectl - run: bash tests/scripts/setup_env_test.sh - run: bash tests/scripts/apply_overlay_test.sh - run: bash tests/scripts/debug_test.sh @@ -58,3 +60,4 @@ jobs: - run: bash tests/scripts/gpu_nodes_test.sh - run: bash tests/scripts/service_test.sh - run: bash tests/scripts/postgres_backup_test.sh + - run: bash tests/scripts/choose_node_test.sh diff --git a/custom.sh b/custom.sh index 79f1d27..c5f4685 100755 --- a/custom.sh +++ b/custom.sh @@ -4,6 +4,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # utils.sh reassigns SCRIPT_DIR to utils/, so keep our own copy of the repo root PANEL_DIR="$SCRIPT_DIR" source "$SCRIPT_DIR/utils/utils.sh" "$@" +source "$PANEL_DIR/utils/choose_node.sh" CUSTOM_DIR="" @@ -59,11 +60,7 @@ select_node() { ALL_NODES=$(get_all_nodes) echo "Select the node to deploy the custom resource to:" IFS=' ' read -r -a NODE_ARRAY <<< "$ALL_NODES" - for i in "${!NODE_ARRAY[@]}"; do - echo "$((i+1)). ${NODE_ARRAY[$i]}" - done - read -p "Enter the number of the node: " NODE_INDEX - NODE_NAME=${NODE_ARRAY[$((NODE_INDEX-1))]} + NODE_NAME="$(choose_node "${NODE_ARRAY[@]}")" || exit 1 echo "Selected node: $NODE_NAME" } diff --git a/tests/scripts/choose_node_test.sh b/tests/scripts/choose_node_test.sh new file mode 100644 index 0000000..17bb906 --- /dev/null +++ b/tests/scripts/choose_node_test.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# shellcheck source=tests/scripts/lib.sh +source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" +# shellcheck source=utils/choose_node.sh +source "$TEST_ROOT/utils/choose_node.sh" + +pick() { + printf '%s\n' "$@" | choose_node node-a node-b node-c 2>"$WORK_DIR/asked" +} + +test_a_number_picks_that_node() { + assert_eq "$(pick 2)" "node-b" +} + +test_leading_zeros_count_as_decimal() { + assert_eq "$(pick 03)" "node-c" +} + +test_a_number_out_of_range_is_asked_again() { + assert_eq "$(pick 0 4 1)" "node-a" + assert_contains "$(cat "$WORK_DIR/asked")" "Enter a number from 1 to 3." +} + +test_only_digits_count_as_an_answer() { + assert_eq "$(pick two 1+1 -1 2x 1)" "node-a" +} + +test_no_answer_chooses_nothing() { + if pick >/dev/null; then + fail "chose a node without an answer" + fi +} + +test_no_nodes_chooses_nothing() { + if printf '1\n' | choose_node 2>/dev/null; then + fail "chose a node from an empty list" + fi +} + +run_tests diff --git a/utils/choose_node.sh b/utils/choose_node.sh new file mode 100644 index 0000000..ee8ed67 --- /dev/null +++ b/utils/choose_node.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +choose_node() { + local nodes=("$@") answer i + [ "${#nodes[@]}" -gt 0 ] || return 1 + for i in "${!nodes[@]}"; do + echo "$((i + 1)). ${nodes[$i]}" >&2 + done + while true; do + read -r -p "Enter the number of the node: " answer || return 1 + if [[ "$answer" =~ ^[0-9]{1,9}$ ]] && ((10#$answer >= 1 && 10#$answer <= ${#nodes[@]})); then + echo "${nodes[$((10#$answer - 1))]}" + return 0 + fi + echo "Enter a number from 1 to ${#nodes[@]}." >&2 + done +}