| index cb4cbbc..f0c26fc 100644 | ||
| --- a/src/plugin_docker.c | ||
| +++ b/src/plugin_docker.c | ||
| @@ -347,6 +347,37 @@ static void copy_field(char *dst, size_t dstsz, const char *src) | ||
| 347 | 347 | dst[i] = '\0'; |
| 348 | 348 | } |
| 349 | 349 | |
| 350 | +/* Normalise the runtime's state word. | |
| 351 | + * | |
| 352 | + * docker reports a tidy lowercase state ("running", "exited", "paused"), but | |
| 353 | + * podman 3.x does not implement {{.State}} at all and substitutes the human | |
| 354 | + * Status text ("Up 4 months ago"). Normalising here keeps the JSON contract | |
| 355 | + * identical no matter which runtime produced the line. | |
| 356 | + */ | |
| 357 | +static void normalize_state(char *dst, size_t dstsz, const char *raw) | |
| 358 | +{ | |
| 359 | + char low[64]; | |
| 360 | + size_t i = 0; | |
| 361 | + | |
| 362 | + if (!raw) raw = ""; | |
| 363 | + while (raw[i] && i + 1 < sizeof(low)) { | |
| 364 | + low[i] = (char)tolower((unsigned char)raw[i]); | |
| 365 | + i++; | |
| 366 | + } | |
| 367 | + low[i] = '\0'; | |
| 368 | + | |
| 369 | + if (strstr(low, "paused")) copy_field(dst, dstsz, "paused"); | |
| 370 | + else if (strstr(low, "restart")) copy_field(dst, dstsz, "restarting"); | |
| 371 | + else if (strstr(low, "dead")) copy_field(dst, dstsz, "dead"); | |
| 372 | + else if (strstr(low, "created")) copy_field(dst, dstsz, "created"); | |
| 373 | + else if (strstr(low, "exited") || strstr(low, "stopped") || strstr(low, "exit")) | |
| 374 | + copy_field(dst, dstsz, "exited"); | |
| 375 | + else if (strstr(low, "up") || strstr(low, "running")) | |
| 376 | + copy_field(dst, dstsz, "running"); | |
| 377 | + else if (low[0]) copy_field(dst, dstsz, low); | |
| 378 | + else copy_field(dst, dstsz, ""); | |
| 379 | +} | |
| 380 | + | |
| 350 | 381 | /* Parse `ps -a` output into g_containers. Returns line count parsed. */ |
| 351 | 382 | static int parse_ps(const char *text) |
| 352 | 383 | { |
| @@ -377,7 +408,11 @@ static int parse_ps(const char *text) | ||
| 377 | 408 | copy_field(c->id, sizeof(c->id), fields[0]); |
| 378 | 409 | copy_field(c->name, sizeof(c->name), nf > 1 ? fields[1] : ""); |
| 379 | 410 | copy_field(c->image, sizeof(c->image), nf > 2 ? fields[2] : ""); |
| 380 | - copy_field(c->state, sizeof(c->state), nf > 3 ? fields[3] : ""); | |
| 411 | + { | |
| 412 | + char raw[64]; | |
| 413 | + copy_field(raw, sizeof(raw), nf > 3 ? fields[3] : ""); | |
| 414 | + normalize_state(c->state, sizeof(c->state), raw); | |
| 415 | + } | |
| 381 | 416 | copy_field(c->status, sizeof(c->status), nf > 4 ? fields[4] : ""); |
| 382 | 417 | copy_field(c->ports, sizeof(c->ports), nf > 5 ? fields[5] : ""); |
| 383 | 418 | g_container_count++; |
| index 5a757f3..fcc7031 100755 | ||
| --- a/tests/run.sh | ||
| +++ b/tests/run.sh | ||
| @@ -166,6 +166,32 @@ check "no-runtime count 0" '"count":0' "$NRT" | ||
| 166 | 166 | LOGS2=$(DOCKER_BIN= PATH=/nonexistent QUERY_STRING='action=logs&cid=web' "$BIN") |
| 167 | 167 | check "no-runtime logs explains why" 'runtime_available' "$(body "$LOGS2")" |
| 168 | 168 | |
| 169 | +section "podman state normalisation" | |
| 170 | +PODMOCK="$MOCKDIR/podmock" | |
| 171 | +cat > "$PODMOCK" <<'PODEOF' | |
| 172 | +#!/bin/sh | |
| 173 | +case "$1" in | |
| 174 | + ps) | |
| 175 | + echo "8890ba09c621|registry|registry:2|Up 4 months ago|Up 4 months ago|" | |
| 176 | + echo "aa11bb22cc33|job|alpine:3.20|Exited (1) 2 days ago|Exited (1) 2 days ago|" | |
| 177 | + echo "dd44ee55ff66|paused-one|redis:7|Paused|Paused|" | |
| 178 | + exit 0 ;; | |
| 179 | + stats) exit 0 ;; | |
| 180 | + *) exit 0 ;; | |
| 181 | +esac | |
| 182 | +PODEOF | |
| 183 | +chmod +x "$PODMOCK" | |
| 184 | +POD=$(DOCKER_BIN="$PODMOCK" QUERY_STRING='action=list' "$BIN") | |
| 185 | +PBODY=$(body "$POD") | |
| 186 | +check "podman: Up becomes running" '"state":"running"' "$PBODY" | |
| 187 | +check "podman: Exited becomes exited" '"state":"exited"' "$PBODY" | |
| 188 | +check "podman: Paused becomes paused" '"state":"paused"' "$PBODY" | |
| 189 | +check "podman: running counted" '"running":1' "$PBODY" | |
| 190 | +check "podman: exited counted" '"exited":1' "$PBODY" | |
| 191 | +check "podman: paused counted" '"paused":1' "$PBODY" | |
| 192 | +check_not "podman: raw status not used as state" '"state":"Up 4 months ago"' "$PBODY" | |
| 193 | + | |
| 194 | +section "injection & validation" | |
| 169 | 195 | section "injection & validation" |
| 170 | 196 | INJ1=$(QUERY_STRING='action=logs&cid=a;id' "$BIN") |
| 171 | 197 | check "semicolon cid rejected" 'invalid_cid' "$(body "$INJ1")" |
| 172 | 198 |
| index cb4cbbc..f0c26fc 100644 | index cb4cbbc..f0c26fc 100644 | ||
| --- a/src/plugin_docker.c | --- a/src/plugin_docker.c | ||
| +++ b/src/plugin_docker.c | +++ b/src/plugin_docker.c | ||
| @@ -347,6 +347,37 @@ static void copy_field(char *dst, size_t dstsz, const char *src) | @@ -347,6 +347,37 @@ static void copy_field(char *dst, size_t dstsz, const char *src) | ||
| 347 | dst[i] = '\0'; | 347 | dst[i] = '\0'; |
| 348 | } | 348 | } |
| 349 | 349 | ||
| 350 | +/* Normalise the runtime's state word. | ||
| 351 | + * | ||
| 352 | + * docker reports a tidy lowercase state ("running", "exited", "paused"), but | ||
| 353 | + * podman 3.x does not implement {{.State}} at all and substitutes the human | ||
| 354 | + * Status text ("Up 4 months ago"). Normalising here keeps the JSON contract | ||
| 355 | + * identical no matter which runtime produced the line. | ||
| 356 | + */ | ||
| 357 | +static void normalize_state(char *dst, size_t dstsz, const char *raw) | ||
| 358 | +{ | ||
| 359 | + char low[64]; | ||
| 360 | + size_t i = 0; | ||
| 361 | + | ||
| 362 | + if (!raw) raw = ""; | ||
| 363 | + while (raw[i] && i + 1 < sizeof(low)) { | ||
| 364 | + low[i] = (char)tolower((unsigned char)raw[i]); | ||
| 365 | + i++; | ||
| 366 | + } | ||
| 367 | + low[i] = '\0'; | ||
| 368 | + | ||
| 369 | + if (strstr(low, "paused")) copy_field(dst, dstsz, "paused"); | ||
| 370 | + else if (strstr(low, "restart")) copy_field(dst, dstsz, "restarting"); | ||
| 371 | + else if (strstr(low, "dead")) copy_field(dst, dstsz, "dead"); | ||
| 372 | + else if (strstr(low, "created")) copy_field(dst, dstsz, "created"); | ||
| 373 | + else if (strstr(low, "exited") || strstr(low, "stopped") || strstr(low, "exit")) | ||
| 374 | + copy_field(dst, dstsz, "exited"); | ||
| 375 | + else if (strstr(low, "up") || strstr(low, "running")) | ||
| 376 | + copy_field(dst, dstsz, "running"); | ||
| 377 | + else if (low[0]) copy_field(dst, dstsz, low); | ||
| 378 | + else copy_field(dst, dstsz, ""); | ||
| 379 | +} | ||
| 380 | + | ||
| 350 | /* Parse `ps -a` output into g_containers. Returns line count parsed. */ | 381 | /* Parse `ps -a` output into g_containers. Returns line count parsed. */ |
| 351 | static int parse_ps(const char *text) | 382 | static int parse_ps(const char *text) |
| 352 | { | 383 | { |
| @@ -377,7 +408,11 @@ static int parse_ps(const char *text) | @@ -377,7 +408,11 @@ static int parse_ps(const char *text) | ||
| 377 | copy_field(c->id, sizeof(c->id), fields[0]); | 408 | copy_field(c->id, sizeof(c->id), fields[0]); |
| 378 | copy_field(c->name, sizeof(c->name), nf > 1 ? fields[1] : ""); | 409 | copy_field(c->name, sizeof(c->name), nf > 1 ? fields[1] : ""); |
| 379 | copy_field(c->image, sizeof(c->image), nf > 2 ? fields[2] : ""); | 410 | copy_field(c->image, sizeof(c->image), nf > 2 ? fields[2] : ""); |
| 380 | - copy_field(c->state, sizeof(c->state), nf > 3 ? fields[3] : ""); | 411 | + { |
| 412 | + char raw[64]; | ||
| 413 | + copy_field(raw, sizeof(raw), nf > 3 ? fields[3] : ""); | ||
| 414 | + normalize_state(c->state, sizeof(c->state), raw); | ||
| 415 | + } | ||
| 381 | copy_field(c->status, sizeof(c->status), nf > 4 ? fields[4] : ""); | 416 | copy_field(c->status, sizeof(c->status), nf > 4 ? fields[4] : ""); |
| 382 | copy_field(c->ports, sizeof(c->ports), nf > 5 ? fields[5] : ""); | 417 | copy_field(c->ports, sizeof(c->ports), nf > 5 ? fields[5] : ""); |
| 383 | g_container_count++; | 418 | g_container_count++; |
| index 5a757f3..fcc7031 100755 | index 5a757f3..fcc7031 100755 | ||
| --- a/tests/run.sh | --- a/tests/run.sh | ||
| +++ b/tests/run.sh | +++ b/tests/run.sh | ||
| @@ -166,6 +166,32 @@ check "no-runtime count 0" '"count":0' "$NRT" | @@ -166,6 +166,32 @@ check "no-runtime count 0" '"count":0' "$NRT" | ||
| 166 | LOGS2=$(DOCKER_BIN= PATH=/nonexistent QUERY_STRING='action=logs&cid=web' "$BIN") | 166 | LOGS2=$(DOCKER_BIN= PATH=/nonexistent QUERY_STRING='action=logs&cid=web' "$BIN") |
| 167 | check "no-runtime logs explains why" 'runtime_available' "$(body "$LOGS2")" | 167 | check "no-runtime logs explains why" 'runtime_available' "$(body "$LOGS2")" |
| 168 | 168 | ||
| 169 | +section "podman state normalisation" | ||
| 170 | +PODMOCK="$MOCKDIR/podmock" | ||
| 171 | +cat > "$PODMOCK" <<'PODEOF' | ||
| 172 | +#!/bin/sh | ||
| 173 | +case "$1" in | ||
| 174 | + ps) | ||
| 175 | + echo "8890ba09c621|registry|registry:2|Up 4 months ago|Up 4 months ago|" | ||
| 176 | + echo "aa11bb22cc33|job|alpine:3.20|Exited (1) 2 days ago|Exited (1) 2 days ago|" | ||
| 177 | + echo "dd44ee55ff66|paused-one|redis:7|Paused|Paused|" | ||
| 178 | + exit 0 ;; | ||
| 179 | + stats) exit 0 ;; | ||
| 180 | + *) exit 0 ;; | ||
| 181 | +esac | ||
| 182 | +PODEOF | ||
| 183 | +chmod +x "$PODMOCK" | ||
| 184 | +POD=$(DOCKER_BIN="$PODMOCK" QUERY_STRING='action=list' "$BIN") | ||
| 185 | +PBODY=$(body "$POD") | ||
| 186 | +check "podman: Up becomes running" '"state":"running"' "$PBODY" | ||
| 187 | +check "podman: Exited becomes exited" '"state":"exited"' "$PBODY" | ||
| 188 | +check "podman: Paused becomes paused" '"state":"paused"' "$PBODY" | ||
| 189 | +check "podman: running counted" '"running":1' "$PBODY" | ||
| 190 | +check "podman: exited counted" '"exited":1' "$PBODY" | ||
| 191 | +check "podman: paused counted" '"paused":1' "$PBODY" | ||
| 192 | +check_not "podman: raw status not used as state" '"state":"Up 4 months ago"' "$PBODY" | ||
| 193 | + | ||
| 194 | +section "injection & validation" | ||
| 169 | section "injection & validation" | 195 | section "injection & validation" |
| 170 | INJ1=$(QUERY_STRING='action=logs&cid=a;id' "$BIN") | 196 | INJ1=$(QUERY_STRING='action=logs&cid=a;id' "$BIN") |
| 171 | check "semicolon cid rejected" 'invalid_cid' "$(body "$INJ1")" | 197 | check "semicolon cid rejected" 'invalid_cid' "$(body "$INJ1")" |
| 172 | 198 |