Skip to content

Commit 93b9534

Browse files
committed
added get the last line of output
Signed-off-by: pengshanyu <[email protected]>
1 parent 29830da commit 93b9534

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

tests/ffi/qm-oom-score-adj/test.sh

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,27 @@ trap disk_cleanup EXIT
2020
prepare_test
2121
reload_config
2222

23+
# Function to inspect a container with optional exec into another container
24+
podman_inspect() {
25+
local outer_container=$1
26+
local container_name=$2
27+
local format_string=$3
28+
29+
if [[ -n "$outer_container" ]]; then
30+
podman exec -it "$outer_container" /bin/bash -c "podman inspect $container_name --format '$format_string'" | tail -1 | tr -d '\r'
31+
else
32+
podman inspect "$container_name" --format "$format_string" | tail -1 | tr -d '\r'
33+
fi
34+
}
35+
2336
# Function to retrieve the PID with retries for a container
2437
get_pid_with_retries() {
2538
local container_name=$1
2639
local retries=10
2740
local pid=""
2841

2942
while [[ $retries -gt 0 ]]; do
30-
pid=$(podman inspect "$container_name" --format '{{.State.Pid}}' | tr -d '\r')
43+
pid=$(podman_inspect "" "$container_name" '{{.State.Pid}}')
3144
if [[ -n "$pid" && "$pid" =~ ^[0-9]+$ ]]; then
3245
echo "$pid"
3346
return
@@ -36,7 +49,7 @@ get_pid_with_retries() {
3649
sleep 1
3750
done
3851

39-
echo "Error: Failed to retrieve a valid PID for the container '$container_name' after multiple attempts." >&2
52+
info_message "Error: Failed to retrieve a valid PID for the container '$container_name' after multiple attempts." >&2
4053
exit 1
4154
}
4255

@@ -45,9 +58,9 @@ get_oom_score_adj() {
4558
local pid=$1
4659

4760
if [[ -f "/proc/$pid/oom_score_adj" ]]; then
48-
tr -d '\r\n' < "/proc/$pid/oom_score_adj"
61+
tail -1 < "/proc/$pid/oom_score_adj" | tr -d '\r\n'
4962
else
50-
echo "Error: /proc/$pid/oom_score_adj does not exist" >&2
63+
info_message "Error: /proc/$pid/oom_score_adj does not exist" >&2
5164
exit 1
5265
fi
5366
}
@@ -56,31 +69,31 @@ get_oom_score_adj() {
5669
running_container_in_qm
5770

5871
# Check if ffi-qm container started successfully
59-
QM_FFI_STATUS=$(podman exec -it qm /bin/bash -c "podman inspect ffi-qm --format '{{.State.Status}}'" | tr -d '\r')
72+
QM_FFI_STATUS=$(podman_inspect "qm" "ffi-qm" '{{.State.Status}}')
6073
if [[ "$QM_FFI_STATUS" != "running" ]]; then
61-
echo "Error: ffi-qm container failed to start. Status: $QM_FFI_STATUS" >&2
74+
info_message "Error: ffi-qm container failed to start. Status: $QM_FFI_STATUS" >&2
6275
exit 1
6376
fi
6477

6578
# Retrieve the PID of the outer container (qm) with retries
6679
QM_PID=$(get_pid_with_retries "qm")
6780

6881
# Retrieve the PID of the inner container (ffi-qm) directly within the qm container
69-
QM_FFI_PID=$(podman exec -it qm /bin/bash -c "podman inspect ffi-qm --format '{{.State.Pid}}'" | tr -d '\r')
82+
QM_FFI_PID=$(podman_inspect "qm" "ffi-qm" '{{.State.Pid}}')
7083

7184
# Debugging: Output the retrieved PIDs
72-
echo "Retrieved QM_PID: $QM_PID"
73-
echo "Retrieved QM_FFI_PID: $QM_FFI_PID"
85+
info_message "Retrieved QM_PID: $QM_PID"
86+
info_message "Retrieved QM_FFI_PID: $QM_FFI_PID"
7487

7588
# Ensure that PIDs are valid before proceeding
7689
if [[ -z "$QM_PID" || ! "$QM_PID" =~ ^[0-9]+$ ]]; then
77-
echo "Error: Invalid QM_PID: $QM_PID" >&2
90+
info_message "Error: Invalid QM_PID: $QM_PID" >&2
7891
exit 1
7992
fi
8093

8194
if [[ -z "$QM_FFI_PID" || ! "$QM_FFI_PID" =~ ^[0-9]+$ ]]; then
82-
echo "Error: Invalid QM_FFI_PID: $QM_FFI_PID" >&2
83-
echo "Check if the ffi-qm container was successfully started."
95+
info_message "Error: Invalid QM_FFI_PID: $QM_FFI_PID" >&2
96+
info_message "Check if the ffi-qm container was successfully started."
8497
exit 1
8598
fi
8699

@@ -89,26 +102,26 @@ QM_OOM_SCORE_ADJ=$(get_oom_score_adj "$QM_PID")
89102
QM_FFI_OOM_SCORE_ADJ=$(podman exec -it qm /bin/bash -c "cat /proc/$QM_FFI_PID/oom_score_adj" | tr -d '\r\n')
90103

91104
# Debugging: Output the retrieved oom_score_adj values
92-
echo "Retrieved QM_OOM_SCORE_ADJ: '$QM_OOM_SCORE_ADJ'"
93-
echo "Retrieved QM_FFI_OOM_SCORE_ADJ: '$QM_FFI_OOM_SCORE_ADJ'"
105+
info_message "Retrieved QM_OOM_SCORE_ADJ: '$QM_OOM_SCORE_ADJ'"
106+
info_message "Retrieved QM_FFI_OOM_SCORE_ADJ: '$QM_FFI_OOM_SCORE_ADJ'"
94107

95108
# Define the expected oom_score_adj values
96109
OOM_SCORE_ADJ_EXPECTED=500
97110
FFI_OOM_SCORE_ADJ_EXPECTED=750
98111

99112
# Check the oom_score_adj for the outer container
100113
if [[ "$QM_OOM_SCORE_ADJ" -eq "$OOM_SCORE_ADJ_EXPECTED" ]]; then
101-
echo "PASS: qm.container oom_score_adj value == $OOM_SCORE_ADJ_EXPECTED"
114+
info_message "PASS: qm.container oom_score_adj value == $OOM_SCORE_ADJ_EXPECTED"
102115
else
103-
echo "FAIL: qm.container oom_score_adj value != $OOM_SCORE_ADJ_EXPECTED. Current value is ${QM_OOM_SCORE_ADJ}"
116+
info_message "FAIL: qm.container oom_score_adj value != $OOM_SCORE_ADJ_EXPECTED. Current value is ${QM_OOM_SCORE_ADJ}"
104117
exit 1
105118
fi
106119

107120
# Check the oom_score_adj for the inner container
108121
if [[ "$QM_FFI_OOM_SCORE_ADJ" -eq "$FFI_OOM_SCORE_ADJ_EXPECTED" ]]; then
109-
echo "PASS: ffi-qm container oom_score_adj == $FFI_OOM_SCORE_ADJ_EXPECTED"
122+
info_message "PASS: ffi-qm container oom_score_adj == $FFI_OOM_SCORE_ADJ_EXPECTED"
110123
else
111-
echo "FAIL: ffi-qm container oom_score_adj != $FFI_OOM_SCORE_ADJ_EXPECTED. Current value is '${QM_FFI_OOM_SCORE_ADJ}'"
124+
info_message "FAIL: ffi-qm container oom_score_adj != $FFI_OOM_SCORE_ADJ_EXPECTED. Current value is '${QM_FFI_OOM_SCORE_ADJ}'"
112125
exit 1
113126
fi
114127

0 commit comments

Comments
 (0)