diff --git a/.gitmodules b/.gitmodules index b7efcb4..0a8ca60 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/src/codex.bash b/src/codex.bash index 30fd46c..c2370ff 100644 --- a/src/codex.bash +++ b/src/codex.bash @@ -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 +} \ No newline at end of file diff --git a/test/codex b/test/codex new file mode 160000 index 0000000..e35aec7 --- /dev/null +++ b/test/codex @@ -0,0 +1 @@ +Subproject commit e35aec78700f529ddf0c9c6764b77927a0cc0165 diff --git a/test/test_codex.bats b/test/test_codex.bats index f629ba3..51d31d1 100755 --- a/test/test_codex.bats +++ b/test/test_codex.bats @@ -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" -} \ No newline at end of file +} + +@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" ] +#} \ No newline at end of file diff --git a/test/test_helper/common_setup.bash b/test/test_helper/common_setup.bash index 9cf41c7..6ac44ba 100644 --- a/test/test_helper/common_setup.bash +++ b/test/test_helper/common_setup.bash @@ -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 } \ No newline at end of file