Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/panel-scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches: [main]
paths:
- custom.sh
- debug.sh
- dev.sh
- plugin.sh
Expand All @@ -22,6 +23,7 @@ on:
- .github/workflows/panel-scripts.yml
pull_request:
paths:
- custom.sh
- debug.sh
- dev.sh
- plugin.sh
Expand All @@ -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
Expand All @@ -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
7 changes: 2 additions & 5 deletions custom.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=""

Expand Down Expand Up @@ -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"
}

Expand Down
41 changes: 41 additions & 0 deletions tests/scripts/choose_node_test.sh
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions utils/choose_node.sh
Original file line number Diff line number Diff line change
@@ -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
}
Loading