- tests/common.bats: PROJECT_ROOT detection, is_cmd, timestamp, data_dir, logging functions, color handling, require_root - tests/detect.bats: GPU sysfs reads with mock sysfs tree, kernel param parsing (word boundary, dot escaping, edge positions), recommended GTT/pages computation (64GB, 128GB, tiny, zero), firmware bad detection, stack detection - tests/format.bats: human_bytes (0, KiB, MiB, GiB boundaries, 64GiB), human_mib (sub-GiB, exact-GiB, recommended values, empty input) - tests/benchmark_compare.bats: improvement/regression display, empty results, missing files, usage output, config change detection - tests/log_metrics.bats: CSV header, data format, field count, input validation, unknown argument handling - tests/test_helper.sh: mock sysfs tree builder, bats-assert/support setup Makefile: add 'make test' target Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
72 lines
2.3 KiB
Bash
72 lines
2.3 KiB
Bash
#!/usr/bin/env bats
|
|
# Tests for scripts/monitor/log-metrics.sh — metric collection
|
|
|
|
load test_helper.sh
|
|
|
|
setup() {
|
|
source_lib common.sh
|
|
setup_mock_sysfs
|
|
OUTPUT_FILE="$(mktemp)"
|
|
}
|
|
|
|
teardown() {
|
|
teardown_mock_sysfs
|
|
rm -f "$OUTPUT_FILE"
|
|
}
|
|
|
|
@test "log-metrics: produces CSV with correct header" {
|
|
run bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
|
|
--duration 2 --interval 1 --output "$OUTPUT_FILE"
|
|
assert_success
|
|
head -1 "$OUTPUT_FILE" | grep -q "timestamp,gpu_busy_pct,vram_used_mib,gtt_used_mib,gpu_temp_c,gpu_power_w,cpu_pct,ram_used_mib"
|
|
}
|
|
|
|
@test "log-metrics: produces at least 1 data row in 3 seconds" {
|
|
bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
|
|
--duration 3 --interval 1 --output "$OUTPUT_FILE" 2>/dev/null
|
|
local lines
|
|
lines=$(wc -l < "$OUTPUT_FILE")
|
|
(( lines >= 2 )) # header + at least 1 data row
|
|
}
|
|
|
|
@test "log-metrics: data rows have 8 comma-separated fields" {
|
|
bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
|
|
--duration 3 --interval 1 --output "$OUTPUT_FILE" 2>/dev/null
|
|
# Check second line (first data row)
|
|
local row
|
|
row=$(sed -n '2p' "$OUTPUT_FILE")
|
|
local field_count
|
|
field_count=$(echo "$row" | awk -F, '{print NF}')
|
|
[ "$field_count" -eq 8 ]
|
|
}
|
|
|
|
@test "log-metrics: rejects non-numeric interval" {
|
|
run bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
|
|
--interval abc --duration 1 --output "$OUTPUT_FILE"
|
|
assert_failure
|
|
assert_output --partial "positive integer"
|
|
}
|
|
|
|
@test "log-metrics: rejects non-numeric duration" {
|
|
run bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
|
|
--duration abc --interval 1 --output "$OUTPUT_FILE"
|
|
assert_failure
|
|
assert_output --partial "positive integer"
|
|
}
|
|
|
|
@test "log-metrics: creates output file in specified path" {
|
|
local custom_output
|
|
custom_output="$(mktemp -d)/custom-metrics.csv"
|
|
bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
|
|
--duration 2 --interval 1 --output "$custom_output" 2>/dev/null
|
|
[ -f "$custom_output" ]
|
|
rm -f "$custom_output"
|
|
rmdir "$(dirname "$custom_output")"
|
|
}
|
|
|
|
@test "log-metrics: warns on unknown argument" {
|
|
run bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
|
|
--bogus-flag --duration 1 --interval 1 --output "$OUTPUT_FILE"
|
|
assert_output --partial "Unknown argument"
|
|
}
|