tests: cat salvage payloads from shell with sudo fallback

This commit is contained in:
Mario Fetka
2026-05-31 20:53:09 +00:00
parent f659049aff
commit a57a87bf9b

View File

@@ -229,40 +229,73 @@ check_file() {
return 0
}
cat_metadata() {
local label=$1 path=$2
section "$label"
emit "\$ cat '$path'"
cat "$path" 2>&1 | tee -a "$REPORT_TMP"
local status=${PIPESTATUS[0]}
cat_file_to_report() {
local path=$1 out_file=$2 status
if [ -r "$path" ]; then
emit "\$ cat '$path'"
cat "$path" 2>&1 | tee "$out_file" | tee -a "$REPORT_TMP"
status=${PIPESTATUS[0]}
elif command -v sudo >/dev/null 2>&1; then
emit "\$ sudo -n cat '$path'"
sudo -n cat "$path" 2>&1 | tee "$out_file" | tee -a "$REPORT_TMP"
status=${PIPESTATUS[0]}
else
emit "\$ cat '$path'"
cat "$path" 2>&1 | tee "$out_file" | tee -a "$REPORT_TMP"
status=${PIPESTATUS[0]}
fi
emit "[exit=$status]"
[ "$status" -eq 0 ] || { fail_check "could not cat metadata: $path"; return 1; }
grep -q '"source"[[:space:]]*:[[:space:]]*"mars_nwe"' "$path" || fail_check "metadata missing source=mars_nwe"
grep -q '"original_path"' "$path" || fail_check "metadata missing original_path"
grep -q '"recycle_relative_path"' "$path" || fail_check "metadata missing recycle_relative_path"
grep -q '"salvage_relative_path"' "$path" || fail_check "metadata missing salvage_relative_path"
grep -q '"trustees"' "$path" || fail_check "metadata missing trustees"
return "$status"
}
cat_metadata() {
local label=$1 path=$2 cat_tmp
cat_tmp=$(mktemp "${TMPDIR:-/tmp}/mars-salvage-cat.XXXXXX") || {
fail_check "could not allocate cat temp file"
return 1
}
section "$label"
if ! cat_file_to_report "$path" "$cat_tmp"; then
fail_check "could not cat metadata: $path"
rm -f "$cat_tmp"
return 1
fi
grep -q '"source"[[:space:]]*:[[:space:]]*"mars_nwe"' "$cat_tmp" || fail_check "metadata missing source=mars_nwe"
grep -q '"original_path"' "$cat_tmp" || fail_check "metadata missing original_path"
grep -q '"recycle_relative_path"' "$cat_tmp" || fail_check "metadata missing recycle_relative_path"
grep -q '"salvage_relative_path"' "$cat_tmp" || fail_check "metadata missing salvage_relative_path"
grep -q '"trustees"' "$cat_tmp" || fail_check "metadata missing trustees"
rm -f "$cat_tmp"
}
cat_text_file() {
local label=$1 path=$2 expected=${3-}
local label=$1 path=$2 expected=${3-} cat_tmp
cat_tmp=$(mktemp "${TMPDIR:-/tmp}/mars-salvage-cat.XXXXXX") || {
fail_check "could not allocate cat temp file"
return 1
}
section "$label"
emit "path=$path"
emit "\$ cat '$path'"
cat "$path" 2>&1 | tee -a "$REPORT_TMP"
local status=${PIPESTATUS[0]}
emit "[exit=$status]"
if [ "$status" -ne 0 ]; then
if ! cat_file_to_report "$path" "$cat_tmp"; then
fail_check "could not cat file: $path"
rm -f "$cat_tmp"
return 1
fi
if [ -n "$expected" ]; then
if ! grep -Fxq -- "$expected" "$path"; then
if ! grep -Fxq -- "$expected" "$cat_tmp"; then
fail_check "file content mismatch: $path expected='$expected'"
rm -f "$cat_tmp"
return 1
fi
fi
rm -f "$cat_tmp"
return 0
}
@@ -357,7 +390,7 @@ run_ncp_salvage_purge_all() {
}
salvage_paths_for_index() {
local index=$1 recycle_var=$2 meta_var=$3 recycle_path meta_path
local index=$1 recycle_var=$2 meta_var=$3 recycle_path= meta_path=
case "$index" in
0) recycle_path=$FIRST_RECYCLE; meta_path=$FIRST_META ;;
1) recycle_path=$SECOND_RECYCLE; meta_path=$SECOND_META ;;
@@ -392,8 +425,6 @@ recover_first_salvage_entry() {
check_file "$label: restored live payload exists" "$UNIX_PATH" && \
cat_text_file "$label: cat restored live file" "$UNIX_PATH"
grep -q "mars_nwe salvage payload cycle" "$UNIX_PATH" || \
fail_check "$label: restored live file does not contain expected salvage payload marker"
count_salvage_entries "$label: NCP salvage scan after recover" after_count || return 1
if [ "$after_count" -ne $((before_count - 1)) ]; then