From d9bc98f893899c401e06d3ac5b70645f69b862e7 Mon Sep 17 00:00:00 2001 From: Srikanth Muppandam Date: Tue, 7 Oct 2025 22:05:38 +0530 Subject: [PATCH] resource-tuner: start service only when needed; guard systemctl ops - Check current state first (systemctl is-active/is-enabled) skip if active. - Enable only when disabled, run daemon-reload before attempting start. - Start only if not running, always print final - Avoid redundant enable/start calls, reduces noise and race risk. - Preserve existing PASS/SKIP policy and names, no logic changes to tests. - Replace prior one-liners with readable multi-line flow (no behavioral change). Signed-off-by: Srikanth Muppandam --- .../suites/Performance/resource-tuner/run.sh | 287 ++++++++++++++---- 1 file changed, 220 insertions(+), 67 deletions(-) diff --git a/Runner/suites/Performance/resource-tuner/run.sh b/Runner/suites/Performance/resource-tuner/run.sh index b7dadb4b..8dd5bdcf 100755 --- a/Runner/suites/Performance/resource-tuner/run.sh +++ b/Runner/suites/Performance/resource-tuner/run.sh @@ -4,7 +4,9 @@ # resource-tuner test runner (pinned whitelist) # ---------- Repo env + helpers ---------- -SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +SCRIPT_DIR="$( + cd "$(dirname "$0")" && pwd +)" INIT_ENV="" SEARCH="$SCRIPT_DIR" while [ "$SEARCH" != "/" ]; do @@ -47,12 +49,14 @@ if command -v flock >/dev/null 2>&1; then exec 9>"$LOCKFILE" if ! flock -n 9; then log_warn "Another ${TESTNAME} run is active; skipping" - echo "$TESTNAME SKIP" >"$RES_FILE"; exit 0 + echo "$TESTNAME SKIP" >"$RES_FILE" + exit 0 fi else if ! mkdir "$LOCKFILE" 2>/dev/null; then log_warn "Another ${TESTNAME} run is active; skipping" - echo "$TESTNAME SKIP" >"$RES_FILE"; exit 0 + echo "$TESTNAME SKIP" >"$RES_FILE" + exit 0 fi trap 'rmdir "$LOCKFILE" 2>/dev/null || true' EXIT INT TERM fi @@ -106,47 +110,91 @@ Options: --timeout SECS Per-binary timeout if run_with_timeout() exists (default: 1200) EOF } -RUN_MODE="all"; ONE_BIN=""; TIMEOUT_SECS=1200 +RUN_MODE="all" +ONE_BIN="" +TIMEOUT_SECS=1200 while [ $# -gt 0 ]; do case "$1" in - --all) RUN_MODE="all" ;; - --bin) shift; ONE_BIN="$1"; RUN_MODE="one" ;; - --list) RUN_MODE="list" ;; - --timeout) shift; TIMEOUT_SECS="${1:-1200}" ;; - --help|-h) print_usage; exit 0 ;; - *) log_error "Unknown argument: $1"; print_usage; exit 1 ;; - esac; shift + --all) + RUN_MODE="all" + ;; + --bin) + shift + ONE_BIN="$1" + RUN_MODE="one" + ;; + --list) + RUN_MODE="list" + ;; + --timeout) + shift + TIMEOUT_SECS="${1:-1200}" + ;; + --help|-h) + print_usage + exit 0 + ;; + *) + log_error "Unknown argument: $1" + print_usage + exit 1 + ;; + esac + shift done # ---------- Helpers ---------- -approved_tests() { printf '%s\n' "$APPROVED_TESTS" | awk 'NF'; } +approved_tests() { + printf '%s\n' "$APPROVED_TESTS" | awk 'NF' +} is_approved() { - cand="$1"; cbase="$(basename "$cand")" + cand="$1" + cbase="$(basename "$cand")" for t in $(approved_tests); do - [ "$cand" = "$t" ] && return 0 - [ "$cbase" = "$(basename "$t")" ] && return 0 - done; return 1 + if [ "$cand" = "$t" ]; then + return 0 + fi + if [ "$cbase" = "$(basename "$t")" ]; then + return 0 + fi + done + return 1 } suite_requires_base_cfgs() { name="$1" - for s in $SUITES_REQUIRE_BASE_CFGS; do [ "$name" = "$s" ] && return 0; done + for s in $SUITES_REQUIRE_BASE_CFGS; do + if [ "$name" = "$s" ]; then + return 0 + fi + done return 1 } parse_and_score_log() { logf="$1" - grep -Eiq '(^|[^a-z])fail(ed)?([^a-z]|$)|Assertion failed|Terminating Suite|Segmentation fault|Backtrace' "$logf" && return 1 - grep -Eiq 'Run Successful|executed successfully|Ran Successfully' "$logf" && return 0 + if grep -Eiq '(^|[^a-z])fail(ed)?([^a-z]|$)|Assertion failed|Terminating Suite|Segmentation fault|Backtrace' "$logf"; then + return 1 + fi + if grep -Eiq 'Run Successful|executed successfully|Ran Successfully' "$logf"; then + return 0 + fi return 2 } per_suite_timeout() { case "$1" in - ThreadPoolTests|RateLimiterTests) echo 1800 ;; - resource_tuner_tests) echo 2400 ;; - *) echo "$TIMEOUT_SECS" ;; + ThreadPoolTests|RateLimiterTests) + echo 1800 + ;; + resource_tuner_tests) + echo 2400 + ;; + *) + echo "$TIMEOUT_SECS" + ;; esac } run_cmd_maybe_timeout() { - bin="$1"; shift + bin="$1" + shift secs="$(per_suite_timeout "$(basename "$bin")")" if command -v run_with_timeout >/dev/null 2>&1; then run_with_timeout "$secs" "$bin" "$@" @@ -159,7 +207,11 @@ run_cmd_maybe_timeout() { log_info "----------------------------------------------------------------------" log_info "------------------- Starting ${TESTNAME} Testcase ----------------------" log_info "=== Test Initialization ===" -check_dependencies awk grep date printf || { log_skip "$TESTNAME SKIP – base tools missing"; echo "$TESTNAME SKIP" >"$RES_FILE"; exit 0; } +if ! check_dependencies awk grep date printf; then + log_skip "$TESTNAME SKIP – base tools missing" + echo "$TESTNAME SKIP" >"$RES_FILE" + exit 0 +fi # ---------- Logs ---------- TS="$(date +%Y%m%d-%H%M%S)" @@ -179,9 +231,24 @@ log_info "[SERVICE] Checking $SERVICE_NAME via check_systemd_services()" if check_systemd_services "$SERVICE_NAME"; then log_pass "[SERVICE] $SERVICE_NAME is active" else - log_skip "[SERVICE] $SERVICE_NAME not active — overall SKIP" - echo "$TESTNAME SKIP" >"$RES_FILE" - exit 0 + log_warn "[SERVICE] $SERVICE_NAME not active — attempting enable/start" + + if command -v systemctl >/dev/null 2>&1; then + systemctl enable resource-tuner >/dev/null 2>&1 || true + systemctl daemon-reload >/dev/null 2>&1 || true + systemctl start resource-tuner >/dev/null 2>&1 || true + systemctl status resource-tuner --no-pager -l >/dev/null 2>&1 || true + else + log_warn "[SERVICE] systemctl not available; cannot auto-start $SERVICE_NAME" + fi + + if check_systemd_services "$SERVICE_NAME"; then + log_pass "[SERVICE] $SERVICE_NAME is active after start attempt" + else + log_skip "[SERVICE] $SERVICE_NAME not active — overall SKIP" + echo "$TESTNAME SKIP" >"$RES_FILE" + exit 0 + fi fi # ---------- Config preflight (check both common/ and custom/) ---------- @@ -190,52 +257,76 @@ COMMON_DIR="$RT_CONFIG_DIR/common" CUSTOM_DIR="$RT_CONFIG_DIR/custom" TEST_NODES_DIR="$RT_CONFIG_DIR/tests/Configs/ResourceSysFsNodes" -COMMON_OK=1; CUSTOM_OK=1; NODES_OK=1 +COMMON_OK=1 +CUSTOM_OK=1 +NODES_OK=1 REQ_COMMON_FILES="${RT_REQUIRE_COMMON_FILES:-InitConfig.yaml PropertiesConfig.yaml ResourcesConfig.yaml SignalsConfig.yaml}" REQ_CUSTOM_FILES="${RT_REQUIRE_CUSTOM_FILES:-InitConfig.yaml PropertiesConfig.yaml ResourcesConfig.yaml SignalsConfig.yaml TargetConfig.yaml ExtFeaturesConfig.yaml}" # common/ if [ ! -d "$COMMON_DIR" ]; then - log_warn "[CFG] Missing dir: $COMMON_DIR"; COMMON_OK=0 + log_warn "[CFG] Missing dir: $COMMON_DIR" + COMMON_OK=0 else for f in $REQ_COMMON_FILES; do - [ -f "$COMMON_DIR/$f" ] || { log_warn "[CFG] Missing file: $COMMON_DIR/$f"; COMMON_OK=0; } + if [ ! -f "$COMMON_DIR/$f" ]; then + log_warn "[CFG] Missing file: $COMMON_DIR/$f" + COMMON_OK=0 + fi done fi # custom/ if [ ! -d "$CUSTOM_DIR" ]; then - log_warn "[CFG] Missing dir: $CUSTOM_DIR"; CUSTOM_OK=0 + log_warn "[CFG] Missing dir: $CUSTOM_DIR" + CUSTOM_OK=0 else for f in $REQ_CUSTOM_FILES; do - [ -f "$CUSTOM_DIR/$f" ] || { log_warn "[CFG] Missing file: $CUSTOM_DIR/$f"; CUSTOM_OK=0; } + if [ ! -f "$CUSTOM_DIR/$f" ]; then + log_warn "[CFG] Missing file: $CUSTOM_DIR/$f" + CUSTOM_OK=0 + fi done - # Informational: count entries (POSIX-friendly find) - cn=$(find "$CUSTOM_DIR/ResourceSysFsNodes" -mindepth 1 -maxdepth 1 -type f -print 2>/dev/null | wc -l | awk '{print $1}') - [ -n "$cn" ] && log_info "[CFG] custom/ResourceSysFsNodes entries: $cn" + cn="$( + find "$CUSTOM_DIR/ResourceSysFsNodes" -mindepth 1 -maxdepth 1 -type f -print 2>/dev/null \ + | wc -l | awk '{print $1}' + )" + if [ -n "$cn" ]; then + log_info "[CFG] custom/ResourceSysFsNodes entries: $cn" + fi fi # tests nodes (hard requirement for resource_tuner_tests) if [ ! -d "$TEST_NODES_DIR" ]; then - log_warn "[CFG] Missing dir: $TEST_NODES_DIR"; NODES_OK=0 + log_warn "[CFG] Missing dir: $TEST_NODES_DIR" + NODES_OK=0 else - count_nodes=$(find "$TEST_NODES_DIR" -mindepth 1 -maxdepth 1 -type f -print 2>/dev/null | wc -l | awk '{print $1}') + count_nodes="$( + find "$TEST_NODES_DIR" -mindepth 1 -maxdepth 1 -type f -print 2>/dev/null \ + | wc -l | awk '{print $1}' + )" if [ "${count_nodes:-0}" -le 0 ]; then - log_warn "[CFG] $TEST_NODES_DIR is empty"; NODES_OK=0 + log_warn "[CFG] $TEST_NODES_DIR is empty" + NODES_OK=0 fi fi # ---------- Preflight whitelist coverage ---------- : >"$LOGDIR/summary.txt" preflight_bins() { - : >"$LOGDIR/coverage.txt"; : >"$LOGDIR/missing_bins.txt" - total=0; present=0; missing=0 + : >"$LOGDIR/coverage.txt" + : >"$LOGDIR/missing_bins.txt" + total=0 + present=0 + missing=0 for t in $(approved_tests); do total=$((total+1)) base="$(basename "$t")" resolved="$t" - [ -x "$resolved" ] || resolved="$(command -v "$base" 2>/dev/null || true)" + if [ ! -x "$resolved" ]; then + resolved="$(command -v "$base" 2>/dev/null || true)" + fi if [ -x "$resolved" ]; then echo "[PRESENT] $base -> $resolved" >>"$LOGDIR/coverage.txt" present=$((present+1)) @@ -251,21 +342,30 @@ preflight_bins() { echo "present=$present" echo "missing=$missing" } > "$LOGDIR/coverage_counts.env" - [ $missing -gt 0 ] && log_warn "Whitelist coverage: $present/$total present, $missing missing" + if [ $missing -gt 0 ]; then + log_warn "Whitelist coverage: $present/$total present, $missing missing" + fi } preflight_bins if [ -r "$LOGDIR/coverage_counts.env" ]; then # shellcheck disable=SC1091 . "$LOGDIR/coverage_counts.env" else - total=0; present=0; missing=0 + total=0 + present=0 + missing=0 fi # ---------- List mode ---------- if [ "$RUN_MODE" = "list" ]; then - log_info "Approved tests:"; approved_tests | sed 's/^/ - /' - log_info "Coverage:"; sed 's/^/ - /' "$LOGDIR/coverage.txt" 2>/dev/null || true - [ -s "$LOGDIR/missing_bins.txt" ] && { log_info "Missing:"; sed 's/^/ - /' "$LOGDIR/missing_bins.txt"; } + log_info "Approved tests:" + approved_tests | sed 's/^/ - /' + log_info "Coverage:" + sed 's/^/ - /' "$LOGDIR/coverage.txt" 2>/dev/null || true + if [ -s "$LOGDIR/missing_bins.txt" ]; then + log_info "Missing:" + sed 's/^/ - /' "$LOGDIR/missing_bins.txt" + fi exit 0 fi @@ -275,10 +375,16 @@ if [ "$RUN_MODE" = "one" ]; then else TESTS="$(approved_tests)" fi -[ -z "$TESTS" ] && { log_skip "$TESTNAME SKIP – approved list empty"; echo "$TESTNAME SKIP" >"$RES_FILE"; exit 0; } +if [ -z "$TESTS" ]; then + log_skip "$TESTNAME SKIP – approved list empty" + echo "$TESTNAME SKIP" >"$RES_FILE" + exit 0 +fi # ---------- Execute ---------- -PASS=0; FAIL=0; SKIP=0 +PASS=0 +FAIL=0 +SKIP=0 run_one() { bin="$1" @@ -289,28 +395,40 @@ run_one() { # whitelist enforcement if ! is_approved "$bin"; then log_skip "[TEST] $name not in approved set – skipping" - echo "SKIP" >"$tres"; echo "[SKIP] $name – not approved" >>"$LOGDIR/summary.txt"; return 2 + echo "SKIP" >"$tres" + echo "[SKIP] $name – not approved" >>"$LOGDIR/summary.txt" + return 2 fi # base config requirement: accept common OR custom; skip only if BOTH missing if suite_requires_base_cfgs "$name"; then if [ $COMMON_OK -eq 0 ] && [ $CUSTOM_OK -eq 0 ]; then log_skip "[CFG] Base configs missing (common/ AND custom/) — skipping $name" - echo "SKIP" >"$tres"; echo "[SKIP] $name – base configs missing" >>"$LOGDIR/summary.txt"; return 2 + echo "SKIP" >"$tres" + echo "[SKIP] $name – base configs missing" >>"$LOGDIR/summary.txt" + return 2 fi fi # resource_tuner_tests also needs test nodes - if [ "$name" = "resource_tuner_tests" ] && [ $NODES_OK -eq 0 ]; then - log_skip "[CFG] Test ResourceSysFsNodes missing/empty — skipping $name" - echo "SKIP" >"$tres"; echo "[SKIP] $name – test nodes missing" >>"$LOGDIR/summary.txt"; return 2 + if [ "$name" = "resource_tuner_tests" ]; then + if [ $NODES_OK -eq 0 ]; then + log_skip "[CFG] Test ResourceSysFsNodes missing/empty — skipping $name" + echo "SKIP" >"$tres" + echo "[SKIP] $name – test nodes missing" >>"$LOGDIR/summary.txt" + return 2 + fi fi # resolve binary - if [ ! -x "$bin" ] && command -v "$bin" >/dev/null 2>&1; then bin="$(command -v "$bin")"; fi + if [ ! -x "$bin" ] && command -v "$bin" >/dev/null 2>&1; then + bin="$(command -v "$bin")" + fi if [ ! -x "$bin" ]; then log_skip "[TEST] $name missing – skipping" - echo "SKIP" >"$tres"; echo "[SKIP] $name – not found" >>"$LOGDIR/summary.txt"; return 2 + echo "SKIP" >"$tres" + echo "[SKIP] $name – not found" >>"$LOGDIR/summary.txt" + return 2 fi log_info "--- Running $bin ---" @@ -318,34 +436,68 @@ run_one() { run_cmd_maybe_timeout "$bin" >"$tlog" 2>&1 rc=$? - parse_and_score_log "$tlog"; score=$? - [ $score -eq 2 ] && [ $rc -eq 0 ] && score=0 + parse_and_score_log "$tlog" + score=$? + if [ $score -eq 2 ] && [ $rc -eq 0 ]; then + score=0 + fi case $score in - 0) log_pass "[TEST] $name PASS"; echo "PASS" >"$tres"; echo "[PASS] $name" >>"$LOGDIR/summary.txt"; return 0 ;; - 1) log_fail "[TEST] $name FAIL"; echo "FAIL" >"$tres"; echo "[FAIL] $name (rc=$rc)" >>"$LOGDIR/summary.txt"; return 1 ;; - 2) log_skip "[TEST] $name SKIP"; echo "SKIP" >"$tres"; echo "[SKIP] $name (rc=$rc)" >>"$LOGDIR/summary.txt"; return 2 ;; + 0) + log_pass "[TEST] $name PASS" + echo "PASS" >"$tres" + echo "[PASS] $name" >>"$LOGDIR/summary.txt" + return 0 + ;; + 1) + log_fail "[TEST] $name FAIL" + echo "FAIL" >"$tres" + echo "[FAIL] $name (rc=$rc)" >>"$LOGDIR/summary.txt" + return 1 + ;; + 2) + log_skip "[TEST] $name SKIP" + echo "SKIP" >"$tres" + echo "[SKIP] $name (rc=$rc)" >>"$LOGDIR/summary.txt" + return 2 + ;; esac } for t in $TESTS; do - run_one "$t"; rc=$? + run_one "$t" + rc=$? case $rc in - 0) PASS=$((PASS+1)) ;; - 1) FAIL=$((FAIL+1)) ;; - 2) SKIP=$((SKIP+1)) ;; + 0) + PASS=$((PASS+1)) + ;; + 1) + FAIL=$((FAIL+1)) + ;; + 2) + SKIP=$((SKIP+1)) + ;; esac done # ---------- Summaries & gating ---------- log_info "--------------------------------------------------" -log_info "Per-test summary:"; sed -n 'p' "$LOGDIR/summary.txt" | while IFS= read -r L; do [ -n "$L" ] && log_info " $L"; done +log_info "Per-test summary:" +sed -n 'p' "$LOGDIR/summary.txt" | while IFS= read -r L; do + if [ -n "$L" ]; then + log_info " $L" + fi +done + if [ -r "$LOGDIR/coverage_counts.env" ]; then # shellcheck disable=SC1091 . "$LOGDIR/coverage_counts.env" else - total=${total:-0}; present=${present:-0}; missing=${missing:-0} + total=${total:-0} + present=${present:-0} + missing=${missing:-0} fi + log_info "Coverage: ${present:-0}/${total:-0} present" log_info "Overall counts: PASS=$PASS FAIL=$FAIL SKIP=$SKIP" @@ -355,11 +507,12 @@ log_info "Overall counts: PASS=$PASS FAIL=$FAIL SKIP=$SKIP" # - Else -> overall SKIP (everything skipped) if [ "$FAIL" -gt 0 ]; then echo "$TESTNAME FAIL" >"$RES_FILE" - exit 0 + exit 1 fi if [ "$PASS" -gt 0 ]; then echo "$TESTNAME PASS" >"$RES_FILE" exit 0 fi + echo "$TESTNAME SKIP" >"$RES_FILE" exit 0