Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ pub fn build(b: *std.Build) void {
suite.expectExitCode(0);
suite.has_side_effects = true;
// The same invocations through the shell implementation and this one.
const diff = b.addSystemCommand(&.{ "bash", "-c", "TOOL_BIN=\"$0\" exec bash tests/differential.sh" });
const diff = b.addSystemCommand(&.{ "bash", "-c", "TOOL_BIN=\"$0\" TEST_OPS_BIN=\"$1\" exec bash tests/differential.sh" });
diff.addArtifactArg(exe);
diff.addArtifactArg(test_ops_exe);
diff.setCwd(b.path("."));
diff.expectExitCode(0);
diff.has_side_effects = true;
Expand Down
16 changes: 15 additions & 1 deletion eb-keep-logs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,14 @@ else:
sd = " ".join(f'{k[2:]}="{sd_esc(v)}"' for k, v in pairs if k.startswith("P:"))
line = f"<{pri}>1 {ts} {host} eb-keep-logs {pid} {msgid} [ebKeepLogs@{pen} {sd}] {one_line(msg)}"
payload = line.encode("utf-8", "replace")
cap = os.environ.get("EB_KEEP_LOGS_SYSLOG_CAPTURE")
if cap:
out = payload
if scheme == "tcp":
out = f"{len(payload)} ".encode("ascii") + payload
with open(cap, "wb") as fh:
fh.write(out)
sys.exit(0)
if scheme == "unix":
ok = send_unix(hp, payload, True)
elif scheme == "udp":
Expand Down Expand Up @@ -681,7 +689,13 @@ _update_metrics() {
bytes="${new_bytes}"
# Choose completion time only after taking the metrics lock. Keep a later
# completion already recorded by a concurrent, out-of-order rotation.
[ "${prune}" != "now" ] || prune=$(date +%s)
if [ "${prune}" = "now" ]; then
if [ -n "${EB_KEEP_LOGS_CLOCK:-}" ]; then
prune=$(tr -d '[:space:]' < "${EB_KEEP_LOGS_CLOCK}")
else
prune=$(date +%s)
fi
fi
if [ -n "${prune}" ] && _u64_gt "${prune}" "${last}"; then
last="${prune}"
fi
Expand Down
66 changes: 5 additions & 61 deletions src/lifecycle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,7 @@ test "parsePolicy reads thresholds, optional fields and keep globs" {
\\keep = [
\\ "GCCcore/*",
\\ "OpenMPI/5.0*",
\\ "Foo/1.?-*",
\\]
\\warn_message = "Module {module} will be forbidden on {after}."
;
Expand All @@ -1355,8 +1356,11 @@ test "parsePolicy reads thresholds, optional fields and keep globs" {
try std.testing.expectEqual(@as(u64, 365), pol.remove_after_days);
try std.testing.expectEqual(@as(u64, 14), pol.forbid_grace_days);
try std.testing.expectEqual(@as(u64, 2), pol.min_hits);
try std.testing.expectEqual(@as(usize, 2), pol.keep.len);
try std.testing.expectEqual(@as(usize, 3), pol.keep.len);
try std.testing.expect(matchesKeep(pol.keep, "GCCcore/15.2.0"));
try std.testing.expect(!matchesKeep(pol.keep, "GCC/15.2.0"));
try std.testing.expect(matchesKeep(pol.keep, "Foo/1.0-foss"));
try std.testing.expect(!matchesKeep(pol.keep, "Foo/10.0-foss"));
try std.testing.expect(!matchesKeep(pol.keep, "GROMACS/2026.1"));
}

Expand Down Expand Up @@ -1384,66 +1388,6 @@ test "parsePolicy refuses bad order or unknown keys" {
try std.testing.expectError(error.BadPolicy, parsePolicy(arena, unknown_key, &err_msg));
}

test "globMatch handles stars and question marks" {
try std.testing.expect(globMatch("GCCcore/*", "GCCcore/15.2.0"));
try std.testing.expect(!globMatch("GCCcore/*", "GCC/15.2.0"));
try std.testing.expect(globMatch("Foo/1.?-*", "Foo/1.0-foss"));
try std.testing.expect(!globMatch("Foo/1.?-*", "Foo/10.0-foss"));
}

test "formatModulerc escapes Lua strings and emits warn, forbid and hide_version" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const pol: Policy = .{
.min_observation_days = 90,
.warn_after_days = 60,
.forbid_after_days = 180,
.remove_after_days = 365,
};
const entries = [_]Entry{
.{
.name = "KeepMe",
.version = "1.0",
.module = "KeepMe/1.0",
.easyconfig = "KeepMe-1.0.eb",
.action = .keep,
.hits = 10,
.last = "1700000000",
.idle_days = 1,
.reason = "idle 1d < warn_after_days 60",
},
.{
.name = "OldSim",
.version = "2.1",
.module = "OldSim/2.1",
.easyconfig = "OldSim-2.1.eb",
.action = .@"forbid-after",
.hits = 1,
.last = "1700000000",
.idle_days = 200,
.forbid_after = "2026-12-17",
.reason = "idle 200d >= forbid_after_days 180",
},
.{
.name = "DeadPkg",
.version = "0.9",
.module = "DeadPkg/0.9",
.easyconfig = "DeadPkg-0.9.eb",
.action = .@"candidate-remove",
.hits = 0,
.last = null,
.idle_days = 400,
.reason = "idle 400d >= remove_after_days 365; 0 dependents",
},
};
const lua = try formatModulerc(arena, pol, &entries);
try std.testing.expect(std.mem.indexOf(u8, lua, "KeepMe/1.0") == null);
try std.testing.expect(std.mem.indexOf(u8, lua, "name = \"OldSim/2.1\"") != null);
try std.testing.expect(std.mem.indexOf(u8, lua, "after = \"2026-12-17\"") != null);
try std.testing.expect(std.mem.indexOf(u8, lua, "hide_version(\"DeadPkg/0.9\")") != null);
}

test "formatModulerc escapes a raw newline in a module name before the comment line, not only the string literal" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
Expand Down
29 changes: 0 additions & 29 deletions src/loads.zig
Original file line number Diff line number Diff line change
Expand Up @@ -175,35 +175,6 @@ pub fn cmdLoads(gpa: Allocator, args: []const []const u8) u8 {
return 0;
}

test "a tracking record keeps a syslog prefix and any field order" {
const line = "Nov 17 12:00:01 login01 ModuleUsageTracking: time=1731840001.250000 host=login01 path=/cvmfs/g.lua module=GROMACS/2024.1 user=alice";
const rec = parseRecord(line).?;
try std.testing.expectEqualStrings("alice", rec.user);
try std.testing.expectEqualStrings("GROMACS/2024.1", rec.module);
try std.testing.expectEqualStrings("/cvmfs/g.lua", rec.path);
try std.testing.expectEqualStrings("login01", rec.host);
try std.testing.expectEqualStrings("1731840001.250000", rec.time);
try std.testing.expect(parseRecord("user=a module=M path=/p time=1") == null);
}

test "loads of one module keep file order and a gap is named" {
const text =
\\user=alice module=GROMACS/2024.1 path=/cvmfs/g.lua host=login01 time=1
\\user=bob module=GCC/13.2.0 path=/cvmfs/gcc.lua host=login02 time=2
\\not a record
\\user=cara module=GROMACS/2024.1 path=/other.lua host=login01 time=3
;
const got = try formatLoads(std.testing.allocator, text, "GROMACS/2024.1");
defer std.testing.allocator.free(got);
try std.testing.expectEqualStrings(
"user=alice host=login01 time=1 path=/cvmfs/g.lua\nuser=cara host=login01 time=3 path=/other.lua\n",
got,
);
const none = try formatLoads(std.testing.allocator, text, "Missing/1.0");
defer std.testing.allocator.free(none);
try std.testing.expectEqualStrings("no load of Missing/1.0\n", none);
}

test "a load record with a bad byte in its path is replaced, not leaked raw" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
Expand Down
62 changes: 8 additions & 54 deletions src/localtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -429,25 +429,16 @@ pub fn load(gpa: Allocator, tz: ?[]const u8, tzdir: ?[]const u8) Zone {
return .{ .footer = p };
}

test "posix rules" {
const ams = try parsePosix("CET-1CEST,M3.5.0,M10.5.0/3");
const z: Zone = .{ .footer = ams };
// 2026-11-13 00:00 local is 2026-11-12 23:00 UTC.
try std.testing.expectEqual(@as(?i64, daysFromCivil(2026, 11, 13) * 86400 - 3600), z.fromLocal(daysFromCivil(2026, 11, 13) * 86400));
// 2026-07-01 00:00 local is CEST.
try std.testing.expectEqual(@as(?i64, daysFromCivil(2026, 7, 1) * 86400 - 7200), z.fromLocal(daysFromCivil(2026, 7, 1) * 86400));
const chi: Zone = .{ .footer = try parsePosix("CST6CDT,M3.2.0,M11.1.0") };
try std.testing.expectEqual(@as(?i64, daysFromCivil(2026, 11, 13) * 86400 + 6 * 3600), chi.fromLocal(daysFromCivil(2026, 11, 13) * 86400));
const utc: Zone = .{};
try std.testing.expectEqual(@as(?i64, 0), utc.fromLocal(0));
try std.testing.expectError(error.Bad, parsePosix("X1"));
}

test "a rule string is read and applied as glibc reads and applies it" {
const gpa = std.testing.allocator;
// TZ, a date, and what `TZ=... date -d DATE +%s` prints with glibc 2.39,
// null where it refuses the date.
const rows = [_]struct { tz: []const u8, y: i64, m: u8, d: u8, want: ?i64 }{
// The three zones the shell date checks also follow: winter CET,
// summer CEST, and November CST.
.{ .tz = "CET-1CEST,M3.5.0,M10.5.0/3", .y = 2026, .m = 11, .d = 13, .want = daysFromCivil(2026, 11, 13) * 86400 - 3600 },
.{ .tz = "CET-1CEST,M3.5.0,M10.5.0/3", .y = 2026, .m = 7, .d = 1, .want = daysFromCivil(2026, 7, 1) * 86400 - 7200 },
.{ .tz = "CST6CDT,M3.2.0,M11.1.0", .y = 2026, .m = 11, .d = 13, .want = daysFromCivil(2026, 11, 13) * 86400 + 6 * 3600 },
// Text after the rules is not read.
.{ .tz = "CET-1CEST,M3.5.0,M10.5.0/3x", .y = 2026, .m = 11, .d = 1, .want = 1793487600 },
// Without an end rule, daylight time ends on the first Sunday in
Expand Down Expand Up @@ -489,50 +480,13 @@ test "a rule string is read and applied as glibc reads and applies it" {
defer z.deinit();
try std.testing.expectEqual(r.want, z.fromLocal(daysFromCivil(r.y, r.m, r.d) * 86400));
}
const utc: Zone = .{};
try std.testing.expectEqual(@as(?i64, 0), utc.fromLocal(0));
try std.testing.expectError(error.Bad, parsePosix("X1"));
}

test "a local midnight inside a DST gap does not exist" {
// America/Santiago moves 00:00 to 01:00 on the first Sunday of September.
const z: Zone = .{ .footer = try parsePosix("<-04>4<-03>,M9.1.6/24,M4.1.6/24") };
try std.testing.expectEqual(@as(?i64, null), z.fromLocal(daysFromCivil(2026, 9, 6) * 86400));
}

test "an empty TZDIR is the default directory, as glibc takes it" {
const gpa = std.testing.allocator;
const nov1 = daysFromCivil(2026, 11, 1) * 86400;
var unset = load(gpa, "Asia/Tokyo", null);
defer unset.deinit();
// A system without the zone files has nothing to compare with.
if (unset.offsetAt(nov1) != 9 * 3600) return error.SkipZigTest;
var empty = load(gpa, "Asia/Tokyo", "");
defer empty.deinit();
// `TZDIR= TZ=Asia/Tokyo date -d 20261101 +%s` prints 1793458800.
try std.testing.expectEqual(@as(?i64, 1793458800), empty.fromLocal(nov1));
}

test "a rule string after a leading ':' is read, as glibc reads it" {
const gpa = std.testing.allocator;
var z = load(gpa, ":CET-1CEST,M3.5.0,M10.5.0/3", null);
defer z.deinit();
// `TZ=':CET-1CEST,M3.5.0,M10.5.0/3' date -d 20260701 +%s` prints
// 1782856800, and 1793487600 for 20261101.
try std.testing.expectEqual(@as(?i64, 1782856800), z.fromLocal(daysFromCivil(2026, 7, 1) * 86400));
try std.testing.expectEqual(@as(?i64, 1793487600), z.fromLocal(daysFromCivil(2026, 11, 1) * 86400));
var colon = load(gpa, ":", null);
defer colon.deinit();
// `TZ=: date -d 20261101 +%s` prints 1793491200, the UTC midnight.
try std.testing.expectEqual(@as(?i64, 1793491200), colon.fromLocal(daysFromCivil(2026, 11, 1) * 86400));
}

test "civilFromDays inverts daysFromCivil" {
const cases = [_]CivilDate{
.{ .year = 1970, .month = 1, .day = 1 },
.{ .year = 2024, .month = 2, .day = 29 },
.{ .year = 2026, .month = 11, .day = 17 },
.{ .year = 2027, .month = 12, .day = 31 },
};
for (cases) |c| {
const d = daysFromCivil(c.year, c.month, c.day);
try std.testing.expectEqual(c, civilFromDays(d));
}
}
56 changes: 30 additions & 26 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -711,11 +711,41 @@ fn emitFailedSyslog(
else
(a.print("eb-keep-logs failed: no EasyBuild log kept for {s} ({s})", .{ name, reason }) catch return);

if (c.getenv("EB_KEEP_LOGS_SYSLOG_CAPTURE")) |cap| {
captureSyslogFrame(a, t, 4, msgid, params.items, jfields.items, msg, cap);
return;
}
if (!syslog.emitFor(a, syslog_identity, t, 4, msgid, params.items, jfields.items, msg)) {
warn("cannot emit syslog event", .{});
}
}

/// Write the framed event `emitFor` would send to `path`, and do not open a
/// socket. TCP is octet-counted (`N SP payload`); every other scheme is one
/// datagram, the payload alone. Set by the syslog tests.
fn captureSyslogFrame(
a: Allocator,
target: syslog.Target,
severity: u8,
msgid: []const u8,
params: []const syslog.Param,
jfields: []const syslog.JournalField,
msg: []const u8,
path: []const u8,
) void {
const payload = switch (target.scheme) {
.journal_unix => syslog.formatJournaldFor(a, syslog_identity, severity, msgid, jfields, msg),
else => syslog.formatRfc5424For(a, syslog_identity, severity, msgid, params, msg, target.pen),
} catch return;
const framed = switch (target.scheme) {
.rfc5424_tcp => a.print("{d} {s}", .{ payload.len, payload }) catch return,
else => payload,
};
const fd = sys.openat(fsx.cwd, path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, 0o644) catch return;
defer sys.close(fd);
sys.writeAll(fd, framed) catch return;
}

fn emitRotateSyslog(
c: Ctx,
target: ?syslog.Target,
Expand Down Expand Up @@ -2470,14 +2500,6 @@ test "parseSeconds" {
try std.testing.expectEqual(max_lock_timeout, parseSeconds("123456789012345678901234567890"));
}

test "usage_text gives the lock timeout's default and the exit statuses" {
const fmt = std.fmt.comptimePrint;
try std.testing.expect(std.mem.indexOf(u8, usage_text, fmt("Default: {d};", .{default_lock_timeout})) != null);
try std.testing.expect(std.mem.indexOf(u8, usage_text, fmt("give up and exit {d}.", .{lock_busy_exit})) != null);
try std.testing.expect(std.mem.indexOf(u8, usage_text, fmt("\n {d} The invocation was refused", .{cli.usage_exit})) != null);
try std.testing.expect(std.mem.indexOf(u8, usage_text, fmt("\n {d} rotate waited --lock-timeout", .{lock_busy_exit})) != null);
}

test "listed" {
try std.testing.expect(listed("/l/a.log\n/l/b.log\n", "/l/b.log"));
try std.testing.expect(listed("/l/a.log", "/l/a.log"));
Expand All @@ -2486,24 +2508,6 @@ test "listed" {
try std.testing.expect(!listed("", "/l/a.log"));
}

test "joinable and plural" {
try std.testing.expectEqualStrings("logs", joinable("logs//"));
try std.testing.expectEqualStrings("/srv/logs", joinable("/srv/logs"));
try std.testing.expectEqualStrings("", joinable("/"));
try std.testing.expectEqualStrings("", plural(1));
try std.testing.expectEqualStrings("s", plural(0));
try std.testing.expectEqualStrings("s", plural(2));
}

test "below" {
try std.testing.expect(below("/srv/logs/run/easybuild-logs/Foo", "/srv/logs/run"));
try std.testing.expect(!below("/srv/logs/run", "/srv/logs/run"));
try std.testing.expect(!below("/srv/logs/run2/x", "/srv/logs/run"));
try std.testing.expect(!below("/srv/logs", "/srv/logs/run"));
try std.testing.expect(below("/x", "/"));
try std.testing.expect(!below("/", "/"));
}

test "runAgeDays" {
const z: localtime.Zone = .{};
const now = localtime.daysFromCivil(2026, 11, 13) * 86400;
Expand Down
Loading
Loading