Skip to content

Commit 6b9a969

Browse files
committed
refact(api/log): splitted message and type, added tests
1 parent 29c0055 commit 6b9a969

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

‎system/cuos/api.sh‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ api_command_state() {
2828
local file_slot="${T_FILE_SLOT:-"/etc/active_slot"}"
2929
local file_version="${T_FILE_VERSION:-"/etc/image"}"
3030
local virt_type
31-
virt_type="$(systemd-detect-virt)"
31+
virt_type="$(systemd-detect-virt)" 2>/dev/null
3232

3333
local version
3434
version="$(cat "${file_version}")"
@@ -276,11 +276,14 @@ api_command_shutdown() {
276276

277277
## log - Get reports
278278
api_command_log() {
279-
journalctl -t cuos -n 100 -o json | jq -c '{
280-
date: (.["__REALTIME_TIMESTAMP"] | tonumber / 1000000 | strftime("%Y-%m-%dT%H:%M:%S")),
281-
level: ({"0":"EMERGENCY","1":"ALERT","2":"CRITICAL","3":"ERROR","4":"WARNING","5":"NOTICE","6":"INFO","7":"DEBUG"}[.PRIORITY] // "INFO"),
282-
message: .MESSAGE
283-
}' | jq -s .
279+
journalctl -t cuos -n 100 -o json | jq -c '
280+
(.MESSAGE | capture("^(?<type>.*?): (?<message>.*)$") // {type: null, message: .}) as $m
281+
| {
282+
date: (.["__REALTIME_TIMESTAMP"] | tonumber / 1000000 | strftime("%Y-%m-%dT%H:%M:%S")),
283+
level: ({"0":"emergency","1":"alert","2":"critical","3":"error","4":"warning","5":"notice","6":"info","7":"debug"}[.PRIORITY] // "info"),
284+
type: $m.type,
285+
message: $m.message
286+
}' | jq -s .
284287
}
285288

286289
api_command_log_follow() {
@@ -292,11 +295,15 @@ api_command_log_follow() {
292295
if [ -n "$since" ]; then
293296
journalctl_args+=(--since "$since")
294297
fi
295-
journalctl "${journalctl_args[@]}" | jq --unbuffered -c '{
296-
date: (.["__REALTIME_TIMESTAMP"] | tonumber / 1000000 | strftime("%Y-%m-%dT%H:%M:%S")),
297-
level: ({"0":"EMERGENCY","1":"ALERT","2":"CRITICAL","3":"ERROR","4":"WARNING","5":"NOTICE","6":"INFO","7":"DEBUG"}[.PRIORITY] // "INFO"),
298-
message: .MESSAGE
299-
}'
298+
299+
journalctl "${journalctl_args[@]}" | jq --unbuffered -c '
300+
(.MESSAGE | capture("^(?<type>.*?): (?<message>.*)$") // {type: null, message: .}) as $m
301+
| {
302+
date: (.["__REALTIME_TIMESTAMP"] | tonumber / 1000000 | strftime("%Y-%m-%dT%H:%M:%S")),
303+
level: ({"0":"emergency","1":"alert","2":"critical","3":"error","4":"warning","5":"notice","6":"info","7":"debug"}[.PRIORITY] // "info"),
304+
type: $m.type,
305+
message: $m.message
306+
}'
300307
}
301308

302309
## report_app_ready - App announces itself as ready.

‎system/cuos/api.test.sh‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,37 @@ source "${SCRIPT_DIR}/api.sh"
1212

1313
"${SCRIPT_DIR}/api.sh" help >/dev/null || exit 1
1414

15+
# mocks:
16+
systemd-detect-virt() {
17+
echo "test-system"
18+
}
1519

1620

1721
T_FILE_STATE="${T_FILE_STATE:-"${SCRIPT_DIR}/api.test.state.json"}"
1822
T_FILE_SLOT="${T_FILE_SLOT:-"${SCRIPT_DIR}/api.test.active_slot"}"
1923
T_FILE_VERSION="${T_FILE_VERSION:-"${SCRIPT_DIR}/api.test.image"}"
2024
expect \
2125
"api: state" \
22-
$'{\n "start_date": "2025-12-07T12:33:51Z",\n "last_update_check": "2025-12-01T22:59:28Z",\n "state": "running",\n "last_update_date": "2025-12-01T23:00:03Z",\n "update_state": "updated partition B to ghcr.io/cuos-dev/cuos-system-lxc:v0.4.0",\n "slot": "B",\n "version": "cuos-system-lxc:v0.4.0"\n}' \
26+
$'{\n "start_date": "2025-12-07T12:33:51Z",\n "last_update_check": "2025-12-01T22:59:28Z",\n "state": "running",\n "last_update_date": "2025-12-01T23:00:03Z",\n "update_state": "updated partition B to ghcr.io/cuos-dev/cuos-system-lxc:v0.4.0",\n "slot": "B",\n "version": "cuos-system-lxc:v0.4.0",\n "virt_type": "test-system"\n}' \
2327
api_main state
2428

29+
# mock of journalctl
30+
journalctl() {
31+
cat <<EOF
32+
{"SYSLOG_IDENTIFIER":"cuos","__CURSOR":"s=123;i=123;b=123;m=123;t=123;x=123","_CAP_EFFECTIVE":"123","SYSLOG_FACILITY":"3","_GID":"0","_RUNTIME_SCOPE":"system","__REALTIME_TIMESTAMP":"1773230248262606","SYSLOG_TIMESTAMP":"Mar 11 11:57:28 ","_SYSTEMD_SLICE":"system.slice","__MONOTONIC_TIMESTAMP":"5468251","PRIORITY":"6","_COMM":"logger","_UID":"0","_SYSTEMD_INVOCATION_ID":"123","MESSAGE":"cuos:startup:start: System starting up ...","_TRANSPORT":"syslog","_SYSTEMD_UNIT":"cuos-init.service","__SEQNUM_ID":"123","_PID":"123","__SEQNUM":"123","_SELINUX_CONTEXT":"system_u:system_r:kernel_t:s0","_SOURCE_REALTIME_TIMESTAMP":"1773230248261618","_BOOT_ID":"123","_SYSTEMD_CGROUP":"/system.slice/cuos-init.service","_HOSTNAME":"cuos-test"}
33+
{"_HOSTNAME":"cuos-test","__SEQNUM":"123","_CAP_EFFECTIVE":"123","_GID":"0","SYSLOG_FACILITY":"3","PRIORITY":"4","_SELINUX_CONTEXT":"system_u:system_r:kernel_t:s0","__SEQNUM_ID":"123","__CURSOR":"123","_BOOT_ID":"123","_SOURCE_REALTIME_TIMESTAMP":"1773230250152197","_COMM":"logger","_TRANSPORT":"syslog","SYSLOG_TIMESTAMP":"Mar 11 11:57:30 ","__MONOTONIC_TIMESTAMP":"7357897","SYSLOG_IDENTIFIER":"cuos","MESSAGE":"cuos:init:ssh_server: Start SSH server","__REALTIME_TIMESTAMP":"1773230250152252","_PID":"1037","_RUNTIME_SCOPE":"system","_UID":"0"}
34+
{"__CURSOR":"123","__REALTIME_TIMESTAMP":"1780498350663053","_TRANSPORT":"syslog","_BOOT_ID":"123","MESSAGE":"cuos:mytest:err: My Error","SYSLOG_IDENTIFIER":"cuos","__SEQNUM_ID":"123","_HOSTNAME":"cuos-test","_GID":"0","_MACHINE_ID":"123","_SOURCE_REALTIME_TIMESTAMP":"1780498350663004","_COMM":"logger","_UID":"0","__MONOTONIC_TIMESTAMP":"16680766204072","SYSLOG_TIMESTAMP":"Jun 3 14:52:30 ","_RUNTIME_SCOPE":"system","__SEQNUM":"123","_PID":"123","PRIORITY":"3","SYSLOG_FACILITY":"3"}
35+
EOF
36+
}
37+
38+
expect \
39+
"api: log" \
40+
$'[\n {\n "date": "2026-03-11T11:57:28",\n "level": "info",\n "type": "cuos:startup:start",\n "message": "System starting up ..."\n },\n {\n "date": "2026-03-11T11:57:30",\n "level": "warning",\n "type": "cuos:init:ssh_server",\n "message": "Start SSH server"\n },\n {\n "date": "2026-06-03T14:52:30",\n "level": "error",\n "type": "cuos:mytest:err",\n "message": "My Error"\n }\n]' \
41+
api_main log
42+
43+
expect \
44+
"api: log" \
45+
$'{"date":"2026-03-11T11:57:28","level":"info","type":"cuos:startup:start","message":"System starting up ..."}\n{"date":"2026-03-11T11:57:30","level":"warning","type":"cuos:init:ssh_server","message":"Start SSH server"}\n{"date":"2026-06-03T14:52:30","level":"error","type":"cuos:mytest:err","message":"My Error"}' \
46+
api_main log-follow
47+
2548
summary

0 commit comments

Comments
 (0)