#!/bin/bash #======================================================================= # FILE: ci/build-test.sh # DESC: Build the project, run unit tests + a startup smoke test, and # generate a visual test report (markdown + junit.xml). # Executed INSIDE the aarch64 build container (ci/Dockerfile). #======================================================================= set -uo pipefail SRC_DIR="${SRC_DIR:-/src}" cd "${SRC_DIR}" REPORT_DIR="${SRC_DIR}/reports" mkdir -p "${REPORT_DIR}" ARCH="$(uname -m)" COMMIT="$(git -C "${SRC_DIR}" rev-parse --short HEAD 2>/dev/null || echo "${GITHUB_SHA:-unknown}")" BUILD_DATE="$(date -u +'%Y-%m-%d %H:%M:%S UTC')" # Run link (env vars passed by the workflow via docker exec) RUN_LINK="" if [ -n "${GITHUB_SERVER_URL:-}" ] && [ -n "${GITHUB_REPOSITORY:-}" ] && [ -n "${GITHUB_RUN_ID:-}" ]; then RUN_LINK="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" fi #----------------------------------------------------------------------- # Result trackers #----------------------------------------------------------------------- BUILD_OK=0; SQLITE_OK=0; SMOKE_OK=0 BUILD_TIME=0; SQLITE_TIME=0; SMOKE_TIME=0 now_ms() { date +%s; } #----------------------------------------------------------------------- # 1. Build #----------------------------------------------------------------------- echo ">>> Building (aarch64, Release)..." rm -rf build mkdir -p build cd build t0=$(now_ms) if cmake -DCMAKE_BUILD_TYPE=Release .. > /tmp/cmake.log 2>&1 \ && make -j"$(nproc)" > /tmp/make.log 2>&1; then BUILD_OK=1 else echo "--- build failed ---" >&2 tail -40 /tmp/make.log >&2 fi BUILD_TIME=$(( $(now_ms) - t0 )) cd "${SRC_DIR}" BIN_DIR="${SRC_DIR}/bin" if [ "${BUILD_OK}" = "1" ]; then echo ">>> Build artifacts:" ls -la "${BIN_DIR}" fi #----------------------------------------------------------------------- # 2. Unit test (SQLiteTest) #----------------------------------------------------------------------- if [ "${BUILD_OK}" = "1" ]; then echo ">>> Running unit test: SQLiteTest" t0=$(now_ms) if "${BIN_DIR}/SQLiteTest" > /tmp/sqlitetest.log 2>&1; then SQLITE_OK=1 echo ">>> SQLiteTest passed" else echo ">>> SQLiteTest FAILED" >&2 tail -40 /tmp/sqlitetest.log >&2 fi SQLITE_TIME=$(( $(now_ms) - t0 )) fi #----------------------------------------------------------------------- # 3. Startup smoke test: MOOSDB + pPowerManger liveness #----------------------------------------------------------------------- if [ "${BUILD_OK}" = "1" ]; then echo ">>> Smoke test: launch MOOSDB + pPowerManger" MOOSDB="$(command -v MOOSDB || true)" if [ -z "${MOOSDB}" ]; then MOOSDB="$(ls /opt/moos-ivp/bin/MOOSDB 2>/dev/null || true)" fi MISSION_FILE="/tmp/smoke.moos" cat > "${MISSION_FILE}" < /tmp/moosdb_smoke.log 2>&1 & MOOSDB_PID=$! sleep 3 ./pPowerManger "${MISSION_FILE}" > /tmp/pm_smoke.log 2>&1 & PM_PID=$! sleep 5 if kill -0 "${PM_PID}" 2>/dev/null; then SMOKE_OK=1 echo ">>> Smoke test PASSED (pPowerManger alive after 5s)" else echo ">>> Smoke test FAILED (pPowerManger exited)" >&2 fi kill "${PM_PID}" "${MOOSDB_PID}" 2>/dev/null || true wait "${PM_PID}" "${MOOSDB_PID}" 2>/dev/null || true SMOKE_TIME=$(( $(now_ms) - t0 )) cd "${SRC_DIR}" fi #----------------------------------------------------------------------- # 4. Generate visual test report (markdown + junit.xml) #----------------------------------------------------------------------- mark() { [ "$1" = "1" ] && echo "✅ 通过" || echo "❌ 失败"; } ALL_OK=0 [ "${BUILD_OK}" = "1" ] && [ "${SQLITE_OK}" = "1" ] && [ "${SMOKE_OK}" = "1" ] && ALL_OK=1 REPORT="${REPORT_DIR}/test-report.md" : > "$REPORT" md() { printf '%s\n' "$*" >> "$REPORT"; } md "## H100PowerManger CI 测试报告" md "" md "**总体结论: $([ "${ALL_OK}" = "1" ] && echo "✅ 全部通过" || echo "❌ 存在失败")**" md "" md "| 步骤 | 结果 | 耗时 |" md "| :--- | :--- | :--- |" md "| 构建 (${ARCH}) | $(mark ${BUILD_OK}) | ${BUILD_TIME}s |" md "| 单元测试 SQLiteTest | $(mark ${SQLITE_OK}) | ${SQLITE_TIME}s |" md "| 冒烟测试 MOOSDB+pPowerManger | $(mark ${SMOKE_OK}) | ${SMOKE_TIME}s |" md "" if [ "${ALL_OK}" != "1" ]; then md "## 失败详情" md "" if [ "${BUILD_OK}" != "1" ]; then md "### 构建 (${ARCH}) — ❌" md "" md '
make.log 末尾' md "" md '```text' tail -30 /tmp/make.log >> "$REPORT" 2>/dev/null || true md '```' md '
' md "" fi if [ "${SQLITE_OK}" != "1" ]; then md "### 单元测试 SQLiteTest — ❌" md "" md '
SQLiteTest 输出末尾' md "" md '```text' tail -30 /tmp/sqlitetest.log >> "$REPORT" 2>/dev/null || true md '```' md '
' md "" fi if [ "${SMOKE_OK}" != "1" ]; then md "### 冒烟测试 MOOSDB+pPowerManger — ❌" md "" md '
pPowerManger 日志末尾' md "" md '```text' tail -20 /tmp/pm_smoke.log >> "$REPORT" 2>/dev/null || true md '```' md '' md '
MOOSDB 日志末尾' md "" md '```text' tail -10 /tmp/moosdb_smoke.log >> "$REPORT" 2>/dev/null || true md '```' md '
' md "" fi fi md "---" md "" md "- 架构: \`${ARCH}\`" md "- Commit: \`${COMMIT}\`" md "- 时间: ${BUILD_DATE}" if [ -n "${RUN_LINK}" ]; then md "- 运行: [${RUN_LINK}](${RUN_LINK})" fi # --- JUnit XML report --- cat > "${REPORT_DIR}/junit.xml" < $([ "${BUILD_OK}" = "1" ] || echo "") $([ "${SQLITE_OK}" = "1" ] || echo "") $([ "${SMOKE_OK}" = "1" ] || echo "") EOF echo ">>> Reports generated in ${REPORT_DIR}:" ls -la "${REPORT_DIR}" #----------------------------------------------------------------------- # 5. Exit status #----------------------------------------------------------------------- [ "${ALL_OK}" = "1" ]