feat: add codex startup checks; check-in Codex submodule for integration tsting

This commit is contained in:
gmega 2025-06-18 15:43:57 -03:00
parent c39609bfde
commit 2d2c2dda6f
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
5 changed files with 88 additions and 1 deletions

3
.gitmodules vendored
View File

@ -7,3 +7,6 @@
[submodule "test/test_helper/bats-assert"]
path = test/test_helper/bats-assert
url = https://github.com/bats-core/bats-assert.git
[submodule "test/codex"]
path = test/codex
url = https://github.com/codex-storage/nim-codex

View File

@ -15,6 +15,8 @@ _cdx_base_api_port=8080
_cdx_base_disc_port=8190
_cdx_base_metrics_port=8290
_cdx_node_start_timeout=30
cdx_cmdline() {
local api_port\
disc_port\
@ -57,3 +59,50 @@ cdx_cmdline() {
" --log-file=${_cdx_logs}/codex-${node_index}.log --data-dir=${_cdx_data}/codex-${node_index}"\
" --api-port=${api_port} --disc-port=${disc_port} --loglevel=INFO"
}
cdx_get_spr() {
local node_index="$1" api_port spr
api_port=$((_cdx_base_api_port + node_index))
spr=$(curl --silent --fail "http://localhost:${api_port}/api/codex/v1/debug/info" | grep -oe 'spr:[^"]\+')
if [[ -z "$spr" ]]; then
echoerr "Error: unable to get SPR for node $node_index"
return 1
fi
echo "${spr}"
}
cdx_launch_node() {
_check_codex_binary
local codex_cmd
codex_cmd=$(cdx_cmdline "$@")
(
$codex_cmd
pm_job_exit $?
)&
pm_track_last_job
cdx_ensure_ready "$@"
}
cdx_ensure_ready() {
local node_index="$1" timeout=${2:-$_cdx_node_start_timeout} start now
start=$(date +%s)
while true; do
if cdx_get_spr "$node_index"; then
echoerr "Codex node $node_index is ready."
return 0
fi
now=$(date +%s)
if (( now - start > timeout )); then
echoerr "Codex node $node_index did not start within ${timeout} seconds."
return 1
fi
sleep 0.2
done
}

1
test/codex Submodule

@ -0,0 +1 @@
Subproject commit e35aec78700f529ddf0c9c6764b77927a0cc0165

View File

@ -32,4 +32,32 @@ setup() {
" --log-file=${_cdx_output}/logs/codex-0.log"\
" --data-dir=${_cdx_output}/data/codex-0"\
" --api-port=8080 --disc-port=8190 --loglevel=INFO"
}
}
@test "should fail readiness check if node is not running" {
refute cdx_ensure_ready 0 1
}
@test "should pass readiness check if node is running" {
data_dir=$(clh_output_folder "codex-temp")
"${CODEX_BINARY}" --nat:none --data-dir="$data_dir" &> /dev/null &
pid=$!
assert cdx_ensure_ready 0 3
kill -SIGKILL "$pid"
await "$pid"
rm -rf "$data_dir"
}
#@test "should launch a Codex node" {
# export CLH_CODEX_BINARY="${BATS_TEST_DIRNAME}/codex/build/codex"
#
# assert cdx_launch_node 0
#
# # Node must be running and ready.
# assert [ ! -z "$(cdx_get_spr 0)" ]
# # We should see a log file and a data directory.
# assert [ -f "${_cdx_output}/logs/codex-0.log" ]
# assert [ -d "${_cdx_output}/data/codex-0" ]
#}

View File

@ -4,4 +4,10 @@ common_setup() {
load test_helper/bats-assert/load
export LIB_SRC="${BATS_TEST_DIRNAME}/../src"
export CODEX_BINARY="${BATS_TEST_DIRNAME}/codex/build/codex"
if [ ! -f "$CODEX_BINARY" ]; then
echo "Codex binary not found at $CODEX_BINARY"
exit 1
fi
}