| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #!/usr/bin/env bash
- #
- # packaging/dev.sh - build and test inside the Debian 13 build image
- #
- # For a host that cannot build this project natively: no yyjson or lmdb
- # development packages, or a protobuf/gRPC that does not match the release
- # target. It runs the same `build.sh --local` build the packages are made
- # from, in the same image, so what you test is what ships.
- #
- # The repository is mounted at the SAME absolute path it has on the host.
- # That is the whole design. A CMake build tree records the absolute paths it
- # was configured with; mounting the checkout somewhere else (a fixed /src, or
- # one machine's old checkout path) leaves build/ pointing at a directory that
- # does not exist outside the container, so every host-side tool that reads
- # the tree is confused by it, and a host `cmake --build build` goes off and
- # creates that phantom directory. With the paths equal, build/ means the same
- # thing inside and outside, wherever the checkout lives.
- #
- set -euo pipefail
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
- PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
- BASE_IMAGE_NAME="${BASE_IMAGE_NAME:-smartbotic-db-build-base:debian13}"
- DOCKER="${DOCKER:-docker}"
- BUILD_JOBS="${BUILD_JOBS:-$(nproc)}"
- log_info() { printf '\033[1;34m[INFO]\033[0m %s\n' "$*"; }
- log_success() { printf '\033[1;32m[OK]\033[0m %s\n' "$*"; }
- log_warn() { printf '\033[1;33m[WARN]\033[0m %s\n' "$*"; }
- log_error() { printf '\033[1;31m[ERROR]\033[0m %s\n' "$*" >&2; }
- usage() {
- cat <<EOF
- Usage: packaging/dev.sh <command> [args]
- Commands:
- build Configure if needed and build into ./build (Release)
- test [ctest args] Run the unit suite, e.g. test -R relation
- e2e List the client/server suites in tests/load_test
- e2e <name>... Run those suites, e.g. e2e test_relations test_indexes
- e2e all Run every tests/load_test/test_*.sh
- run <cmd> [args] Run one command in the container, from the repo root
- shell Interactive shell in the container
- Environment variables:
- BUILD_JOBS Parallel build jobs (default: nproc)
- BASE_IMAGE_NAME Build image (default: smartbotic-db-build-base:debian13;
- built from packaging/Dockerfile.base if missing)
- DOCKER Container CLI to use (default: docker)
- The container sees the repository at its real path:
- $PROJECT_DIR
- EOF
- }
- ensure_image() {
- if ! command -v "$DOCKER" &>/dev/null; then
- log_error "$DOCKER is not installed or not in PATH"
- exit 1
- fi
- if "$DOCKER" image inspect "$BASE_IMAGE_NAME" &>/dev/null; then
- return 0
- fi
- log_info "Base image not found, building ${BASE_IMAGE_NAME}..."
- "$DOCKER" buildx build \
- -f "$PROJECT_DIR/packaging/Dockerfile.base" \
- -t "$BASE_IMAGE_NAME" \
- "$PROJECT_DIR"
- log_success "Base image built: ${BASE_IMAGE_NAME}"
- }
- # Run "$@" in the build image, as the invoking user, from the repo root.
- in_container() {
- local args=(run --rm)
- if [[ -t 0 && -t 1 ]]; then
- args+=(-it)
- fi
- # As the invoking user, so nothing in build/ ends up owned by root. That
- # user has no home in the image; HOME=/tmp gives tools somewhere to write.
- args+=(-u "$(id -u):$(id -g)" -e HOME=/tmp)
- # Resolved on the host: inside the container git may be unable to answer
- # (a worktree's .git is a pointer to a directory outside this mount), and
- # CMake prefers this variable over asking git.
- local commit
- commit="$(git -C "$PROJECT_DIR" rev-parse --short HEAD 2>/dev/null || true)"
- if [[ -n "$commit" ]]; then
- args+=(-e "BUILD_GIT_COMMIT=$commit")
- fi
- args+=(-e "BUILD_JOBS=$BUILD_JOBS")
- args+=(-v "$PROJECT_DIR:$PROJECT_DIR" -w "$PROJECT_DIR")
- # tests/load_test/test_relations.sh drives three RPCs with grpcurl, which
- # Debian does not package, so the image has none. Borrow the host's when
- # there is one; without it that suite stops and says so.
- local host_grpcurl
- host_grpcurl="$(command -v grpcurl 2>/dev/null || true)"
- if [[ -n "$host_grpcurl" ]]; then
- args+=(-v "$host_grpcurl:/usr/local/bin/grpcurl:ro")
- fi
- "$DOCKER" "${args[@]}" "$BASE_IMAGE_NAME" "$@"
- }
- require_build() {
- if [[ ! -x "$PROJECT_DIR/build/service/smartbotic-database" ]]; then
- log_error "No build found. Run: packaging/dev.sh build"
- exit 1
- fi
- }
- cmd_build() {
- in_container packaging/build.sh --local --no-package --skip-tests
- }
- cmd_test() {
- require_build
- in_container bash -c 'cd build/tests && ctest --output-on-failure -j"$BUILD_JOBS" "$@"' ctest "$@"
- }
- cmd_e2e() {
- local dir="$PROJECT_DIR/tests/load_test"
- local names=()
- if [[ $# -eq 0 ]]; then
- echo "Suites in tests/load_test (run with: packaging/dev.sh e2e <name>...):"
- local f
- for f in "$dir"/test_*.sh; do
- echo " $(basename "$f" .sh)"
- done
- return 0
- fi
- if [[ "$1" == "all" ]]; then
- local f
- for f in "$dir"/test_*.sh; do
- names+=("$(basename "$f" .sh)")
- done
- else
- local n
- for n in "$@"; do
- n="${n%.sh}"
- if [[ ! -f "$dir/$n.sh" ]]; then
- log_error "No such suite: tests/load_test/$n.sh"
- exit 1
- fi
- names+=("$n")
- done
- fi
- require_build
- # One container for the whole list: the suites use fixed ports and /tmp
- # directories, which is safe exactly because they run one after another
- # in a filesystem and network namespace nothing else shares.
- in_container bash -c '
- failed=0
- for name in "$@"; do
- log="/tmp/e2e-$name.log"
- if bash "tests/load_test/$name.sh" > "$log" 2>&1; then
- printf " \033[1;32mPASS\033[0m %s\n" "$name"
- else
- printf " \033[1;31mFAIL\033[0m %s\n" "$name"
- tail -n 40 "$log" | sed "s/^/ /"
- failed=$((failed + 1))
- fi
- done
- if [ "$failed" -ne 0 ]; then
- echo ""
- echo "$failed of $# suite(s) failed"
- exit 1
- fi
- echo ""
- echo "all $# suite(s) passed"
- ' e2e "${names[@]}"
- }
- if [[ $# -eq 0 ]]; then
- usage
- exit 1
- fi
- command="$1"
- shift
- case "$command" in
- build)
- ensure_image
- cmd_build
- ;;
- test)
- ensure_image
- cmd_test "$@"
- ;;
- e2e)
- if [[ $# -gt 0 ]]; then
- ensure_image
- fi
- cmd_e2e "$@"
- ;;
- run)
- if [[ $# -eq 0 ]]; then
- log_error "run needs a command"
- exit 1
- fi
- ensure_image
- in_container "$@"
- ;;
- shell)
- ensure_image
- in_container bash
- ;;
- -h|--help|help)
- usage
- ;;
- *)
- log_error "Unknown command: $command"
- usage
- exit 1
- ;;
- esac
|