dev.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/env bash
  2. #
  3. # packaging/dev.sh - build and test inside the Debian 13 build image
  4. #
  5. # For a host that cannot build this project natively: no yyjson or lmdb
  6. # development packages, or a protobuf/gRPC that does not match the release
  7. # target. It runs the same `build.sh --local` build the packages are made
  8. # from, in the same image, so what you test is what ships.
  9. #
  10. # The repository is mounted at the SAME absolute path it has on the host.
  11. # That is the whole design. A CMake build tree records the absolute paths it
  12. # was configured with; mounting the checkout somewhere else (a fixed /src, or
  13. # one machine's old checkout path) leaves build/ pointing at a directory that
  14. # does not exist outside the container, so every host-side tool that reads
  15. # the tree is confused by it, and a host `cmake --build build` goes off and
  16. # creates that phantom directory. With the paths equal, build/ means the same
  17. # thing inside and outside, wherever the checkout lives.
  18. #
  19. set -euo pipefail
  20. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  21. PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  22. BASE_IMAGE_NAME="${BASE_IMAGE_NAME:-smartbotic-db-build-base:debian13}"
  23. DOCKER="${DOCKER:-docker}"
  24. BUILD_JOBS="${BUILD_JOBS:-$(nproc)}"
  25. log_info() { printf '\033[1;34m[INFO]\033[0m %s\n' "$*"; }
  26. log_success() { printf '\033[1;32m[OK]\033[0m %s\n' "$*"; }
  27. log_warn() { printf '\033[1;33m[WARN]\033[0m %s\n' "$*"; }
  28. log_error() { printf '\033[1;31m[ERROR]\033[0m %s\n' "$*" >&2; }
  29. usage() {
  30. cat <<EOF
  31. Usage: packaging/dev.sh <command> [args]
  32. Commands:
  33. build Configure if needed and build into ./build (Release)
  34. test [ctest args] Run the unit suite, e.g. test -R relation
  35. e2e List the client/server suites in tests/load_test
  36. e2e <name>... Run those suites, e.g. e2e test_relations test_indexes
  37. e2e all Run every tests/load_test/test_*.sh
  38. run <cmd> [args] Run one command in the container, from the repo root
  39. shell Interactive shell in the container
  40. Environment variables:
  41. BUILD_JOBS Parallel build jobs (default: nproc)
  42. BASE_IMAGE_NAME Build image (default: smartbotic-db-build-base:debian13;
  43. built from packaging/Dockerfile.base if missing)
  44. DOCKER Container CLI to use (default: docker)
  45. The container sees the repository at its real path:
  46. $PROJECT_DIR
  47. EOF
  48. }
  49. ensure_image() {
  50. if ! command -v "$DOCKER" &>/dev/null; then
  51. log_error "$DOCKER is not installed or not in PATH"
  52. exit 1
  53. fi
  54. if "$DOCKER" image inspect "$BASE_IMAGE_NAME" &>/dev/null; then
  55. return 0
  56. fi
  57. log_info "Base image not found, building ${BASE_IMAGE_NAME}..."
  58. "$DOCKER" buildx build \
  59. -f "$PROJECT_DIR/packaging/Dockerfile.base" \
  60. -t "$BASE_IMAGE_NAME" \
  61. "$PROJECT_DIR"
  62. log_success "Base image built: ${BASE_IMAGE_NAME}"
  63. }
  64. # Run "$@" in the build image, as the invoking user, from the repo root.
  65. in_container() {
  66. local args=(run --rm)
  67. if [[ -t 0 && -t 1 ]]; then
  68. args+=(-it)
  69. fi
  70. # As the invoking user, so nothing in build/ ends up owned by root. That
  71. # user has no home in the image; HOME=/tmp gives tools somewhere to write.
  72. args+=(-u "$(id -u):$(id -g)" -e HOME=/tmp)
  73. # Resolved on the host: inside the container git may be unable to answer
  74. # (a worktree's .git is a pointer to a directory outside this mount), and
  75. # CMake prefers this variable over asking git.
  76. local commit
  77. commit="$(git -C "$PROJECT_DIR" rev-parse --short HEAD 2>/dev/null || true)"
  78. if [[ -n "$commit" ]]; then
  79. args+=(-e "BUILD_GIT_COMMIT=$commit")
  80. fi
  81. args+=(-e "BUILD_JOBS=$BUILD_JOBS")
  82. args+=(-v "$PROJECT_DIR:$PROJECT_DIR" -w "$PROJECT_DIR")
  83. # tests/load_test/test_relations.sh drives three RPCs with grpcurl, which
  84. # Debian does not package, so the image has none. Borrow the host's when
  85. # there is one; without it that suite stops and says so.
  86. local host_grpcurl
  87. host_grpcurl="$(command -v grpcurl 2>/dev/null || true)"
  88. if [[ -n "$host_grpcurl" ]]; then
  89. args+=(-v "$host_grpcurl:/usr/local/bin/grpcurl:ro")
  90. fi
  91. "$DOCKER" "${args[@]}" "$BASE_IMAGE_NAME" "$@"
  92. }
  93. require_build() {
  94. if [[ ! -x "$PROJECT_DIR/build/service/smartbotic-database" ]]; then
  95. log_error "No build found. Run: packaging/dev.sh build"
  96. exit 1
  97. fi
  98. }
  99. cmd_build() {
  100. in_container packaging/build.sh --local --no-package --skip-tests
  101. }
  102. cmd_test() {
  103. require_build
  104. in_container bash -c 'cd build/tests && ctest --output-on-failure -j"$BUILD_JOBS" "$@"' ctest "$@"
  105. }
  106. cmd_e2e() {
  107. local dir="$PROJECT_DIR/tests/load_test"
  108. local names=()
  109. if [[ $# -eq 0 ]]; then
  110. echo "Suites in tests/load_test (run with: packaging/dev.sh e2e <name>...):"
  111. local f
  112. for f in "$dir"/test_*.sh; do
  113. echo " $(basename "$f" .sh)"
  114. done
  115. return 0
  116. fi
  117. if [[ "$1" == "all" ]]; then
  118. local f
  119. for f in "$dir"/test_*.sh; do
  120. names+=("$(basename "$f" .sh)")
  121. done
  122. else
  123. local n
  124. for n in "$@"; do
  125. n="${n%.sh}"
  126. if [[ ! -f "$dir/$n.sh" ]]; then
  127. log_error "No such suite: tests/load_test/$n.sh"
  128. exit 1
  129. fi
  130. names+=("$n")
  131. done
  132. fi
  133. require_build
  134. # One container for the whole list: the suites use fixed ports and /tmp
  135. # directories, which is safe exactly because they run one after another
  136. # in a filesystem and network namespace nothing else shares.
  137. in_container bash -c '
  138. failed=0
  139. for name in "$@"; do
  140. log="/tmp/e2e-$name.log"
  141. if bash "tests/load_test/$name.sh" > "$log" 2>&1; then
  142. printf " \033[1;32mPASS\033[0m %s\n" "$name"
  143. else
  144. printf " \033[1;31mFAIL\033[0m %s\n" "$name"
  145. tail -n 40 "$log" | sed "s/^/ /"
  146. failed=$((failed + 1))
  147. fi
  148. done
  149. if [ "$failed" -ne 0 ]; then
  150. echo ""
  151. echo "$failed of $# suite(s) failed"
  152. exit 1
  153. fi
  154. echo ""
  155. echo "all $# suite(s) passed"
  156. ' e2e "${names[@]}"
  157. }
  158. if [[ $# -eq 0 ]]; then
  159. usage
  160. exit 1
  161. fi
  162. command="$1"
  163. shift
  164. case "$command" in
  165. build)
  166. ensure_image
  167. cmd_build
  168. ;;
  169. test)
  170. ensure_image
  171. cmd_test "$@"
  172. ;;
  173. e2e)
  174. if [[ $# -gt 0 ]]; then
  175. ensure_image
  176. fi
  177. cmd_e2e "$@"
  178. ;;
  179. run)
  180. if [[ $# -eq 0 ]]; then
  181. log_error "run needs a command"
  182. exit 1
  183. fi
  184. ensure_image
  185. in_container "$@"
  186. ;;
  187. shell)
  188. ensure_image
  189. in_container bash
  190. ;;
  191. -h|--help|help)
  192. usage
  193. ;;
  194. *)
  195. log_error "Unknown command: $command"
  196. usage
  197. exit 1
  198. ;;
  199. esac