Skip to content

Commit 79c9da3

Browse files
committed
Enhancements: added podman_inspect()
Signed-off-by: pengshanyu <yupengshan@hotmail.com>
1 parent 289f5e0 commit 79c9da3

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,34 @@ 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'" | tr -d '\r'
31+
else
32+
podman inspect "$container_name" --format "$format_string" | tr -d '\r'
33+
fi
34+
35+
# Handle errors
36+
exit_code=${PIPESTATUS[0]}
37+
if [[ $exit_code -ne 0 ]]; then
38+
info_message "Error: Failed to inspect container '$container_name'. Exit code: $exit_code" >&2
39+
return "$exit_code"
40+
fi
41+
}
42+
2343
# Function to retrieve the PID with retries for a container
2444
get_pid_with_retries() {
2545
local container_name=$1
2646
local retries=10
2747
local pid=""
2848

2949
while [[ $retries -gt 0 ]]; do
30-
pid=$(podman inspect "$container_name" --format '{{.State.Pid}}' | tr -d '\r')
50+
pid=$(podman_inspect "" "$container_name" '{{.State.Pid}}')
3151
if [[ -n "$pid" && "$pid" =~ ^[0-9]+$ ]]; then
3252
echo "$pid"
3353
return
@@ -36,7 +56,7 @@ get_pid_with_retries() {
3656
sleep 1
3757
done
3858

39-
echo "Error: Failed to retrieve a valid PID for the container '$container_name' after multiple attempts." >&2
59+
info_message "Error: Failed to retrieve a valid PID for the container '$container_name' after multiple attempts." >&2
4060
exit 1
4161
}
4262

@@ -45,9 +65,9 @@ get_oom_score_adj() {
4565
local pid=$1
4666

4767
if [[ -f "/proc/$pid/oom_score_adj" ]]; then
48-
tr -d '\r\n' < "/proc/$pid/oom_score_adj"
68+
tail -1 < "/proc/$pid/oom_score_adj" | tr -d '\r\n'
4969
else
50-
echo "Error: /proc/$pid/oom_score_adj does not exist" >&2
70+
info_message "Error: /proc/$pid/oom_score_adj does not exist" >&2
5171
exit 1
5272
fi
5373
}
@@ -56,31 +76,31 @@ get_oom_score_adj() {
5676
running_container_in_qm
5777

5878
# 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')
79+
QM_FFI_STATUS=$(podman_inspect "qm" "ffi-qm" '{{.State.Status}}')
6080
if [[ "$QM_FFI_STATUS" != "running" ]]; then
61-
echo "Error: ffi-qm container failed to start. Status: $QM_FFI_STATUS" >&2
81+
info_message "Error: ffi-qm container failed to start. Status: $QM_FFI_STATUS" >&2
6282
exit 1
6383
fi
6484

6585
# Retrieve the PID of the outer container (qm) with retries
6686
QM_PID=$(get_pid_with_retries "qm")
6787

6888
# 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')
89+
QM_FFI_PID=$(podman_inspect "qm" "ffi-qm" '{{.State.Pid}}')
7090

7191
# Debugging: Output the retrieved PIDs
72-
echo "Retrieved QM_PID: $QM_PID"
73-
echo "Retrieved QM_FFI_PID: $QM_FFI_PID"
92+
info_message "Retrieved QM_PID: $QM_PID"
93+
info_message "Retrieved QM_FFI_PID: $QM_FFI_PID"
7494

7595
# Ensure that PIDs are valid before proceeding
7696
if [[ -z "$QM_PID" || ! "$QM_PID" =~ ^[0-9]+$ ]]; then
77-
echo "Error: Invalid QM_PID: $QM_PID" >&2
97+
info_message "Error: Invalid QM_PID: $QM_PID" >&2
7898
exit 1
7999
fi
80100

81101
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."
102+
info_message "Error: Invalid QM_FFI_PID: $QM_FFI_PID" >&2
103+
info_message "Check if the ffi-qm container was successfully started."
84104
exit 1
85105
fi
86106

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

91111
# 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'"
112+
info_message "Retrieved QM_OOM_SCORE_ADJ: '$QM_OOM_SCORE_ADJ'"
113+
info_message "Retrieved QM_FFI_OOM_SCORE_ADJ: '$QM_FFI_OOM_SCORE_ADJ'"
94114

95115
# Define the expected oom_score_adj values
96116
OOM_SCORE_ADJ_EXPECTED=500
97117
FFI_OOM_SCORE_ADJ_EXPECTED=750
98118

99119
# Check the oom_score_adj for the outer container
100120
if [[ "$QM_OOM_SCORE_ADJ" -eq "$OOM_SCORE_ADJ_EXPECTED" ]]; then
101-
echo "PASS: qm.container oom_score_adj value == $OOM_SCORE_ADJ_EXPECTED"
121+
info_message "PASS: qm.container oom_score_adj value == $OOM_SCORE_ADJ_EXPECTED"
102122
else
103-
echo "FAIL: qm.container oom_score_adj value != $OOM_SCORE_ADJ_EXPECTED. Current value is ${QM_OOM_SCORE_ADJ}"
123+
info_message "FAIL: qm.container oom_score_adj value != $OOM_SCORE_ADJ_EXPECTED. Current value is ${QM_OOM_SCORE_ADJ}"
104124
exit 1
105125
fi
106126

107127
# Check the oom_score_adj for the inner container
108128
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"
129+
info_message "PASS: ffi-qm container oom_score_adj == $FFI_OOM_SCORE_ADJ_EXPECTED"
110130
else
111-
echo "FAIL: ffi-qm container oom_score_adj != $FFI_OOM_SCORE_ADJ_EXPECTED. Current value is '${QM_FFI_OOM_SCORE_ADJ}'"
131+
info_message "FAIL: ffi-qm container oom_score_adj != $FFI_OOM_SCORE_ADJ_EXPECTED. Current value is '${QM_FFI_OOM_SCORE_ADJ}'"
112132
exit 1
113133
fi
114134

0 commit comments

Comments
 (0)