#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" TMPDIR="${TMPDIR:-/tmp}" mkdir -p "${TMPDIR}" AMDUATD_BIN="${ROOT_DIR}/build/amduatd" ASL_BIN="${ROOT_DIR}/build/vendor/amduat/amduat-asl" HTTP_HELPER="${ROOT_DIR}/build/amduatd_http_unix" USE_HTTP_HELPER=0 if [[ ! -x "${AMDUATD_BIN}" || ! -x "${ASL_BIN}" ]]; then echo "missing binaries; build amduatd and amduat-asl first" >&2 exit 1 fi if command -v curl >/dev/null 2>&1 && curl --help 2>/dev/null | grep -q -- '--unix-socket'; then USE_HTTP_HELPER=0 else USE_HTTP_HELPER=1 fi if [[ "${USE_HTTP_HELPER}" -eq 1 && ! -x "${HTTP_HELPER}" ]]; then echo "skip: curl lacks --unix-socket and helper missing" >&2 exit 77 fi tmp_root="$(mktemp -d -p "${TMPDIR}" amduatd-index-two-nodes-XXXXXX)" root="${tmp_root}/root" sock="${tmp_root}/amduatd.sock" space_id="app1" log_file="${tmp_root}/amduatd.log" cleanup() { if [[ -n "${pid:-}" ]]; then kill "${pid}" >/dev/null 2>&1 || true wait "${pid}" >/dev/null 2>&1 || true fi rm -rf "${tmp_root}" } trap cleanup EXIT wait_ready() { local i for i in $(seq 1 100); do if ! kill -0 "${pid}" >/dev/null 2>&1; then [[ -f "${log_file}" ]] && cat "${log_file}" >&2 return 1 fi if [[ ! -S "${sock}" ]]; then sleep 0.1 continue fi if [[ "${USE_HTTP_HELPER}" -eq 1 ]]; then if "${HTTP_HELPER}" --sock "${sock}" --method GET --path "/v2/readyz" >/dev/null 2>&1; then return 0 fi elif curl --silent --show-error --fail --unix-socket "${sock}" \ "http://localhost/v2/readyz" >/dev/null 2>&1; then return 0 fi sleep 0.1 done return 1 } post_node() { local name="$1" local payload="{\"name\":\"${name}\"}" if [[ "${USE_HTTP_HELPER}" -eq 1 ]]; then "${HTTP_HELPER}" --sock "${sock}" --method POST --path "/v2/graph/nodes" \ --header "Content-Type: application/json" \ --header "X-Amduat-Space: ${space_id}" \ --data "${payload}" else curl --silent --show-error --fail \ --unix-socket "${sock}" \ -H "Content-Type: application/json" \ -H "X-Amduat-Space: ${space_id}" \ -X POST --data-binary "${payload}" \ "http://localhost/v2/graph/nodes" fi } mkdir -p "${root}" "${ASL_BIN}" index init --root "${root}" --force >/dev/null "${AMDUATD_BIN}" --root "${root}" --sock "${sock}" --store-backend index --space "${space_id}" \ >"${log_file}" 2>&1 & pid=$! if ! wait_ready; then echo "daemon not ready" >&2 exit 1 fi resp1="$(post_node doca1)" resp2="$(post_node topica1)" echo "${resp1}" | grep -q '"name":"doca1"' || { echo "first node write failed: ${resp1}" >&2 [[ -f "${log_file}" ]] && cat "${log_file}" >&2 exit 1 } echo "${resp2}" | grep -q '"name":"topica1"' || { echo "second node write failed: ${resp2}" >&2 [[ -f "${log_file}" ]] && cat "${log_file}" >&2 exit 1 } echo "ok: two consecutive index-backed node writes succeeded"