Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Command Center (MCC)8 * File : services/internal/orchestrator/housekeeping_test.go9 * Module : services10 * Repository : https://git.spboucher.ai/maclustr-command-center11 * Project Website : https://clustr.spboucher.ai12 * Created : 2026-11-1813 * Last Modified : 2026-11-1814 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142package orchestrator_test4344import (45 "testing"46 "time"4748 "clustr.spboucher.ai/mcc/internal/orchestrator"49 "clustr.spboucher.ai/mcc/internal/orchestrator/fakeagent"50 "clustr.spboucher.ai/mcc/internal/protocol"51)5253func TestHousekeepingPrunesOldEventsOnly(t *testing.T) {54 e := newEnv(t)55 old := e.enqueue("inventory.refresh", "node", e.node, nil, nil)56 e.step(e.orch)57 e.agent.Handle = func(*protocol.Command) fakeagent.Behavior {58 return fakeagent.Behavior{Output: []protocol.CommandOutput{{Stream: "stdout", Data: "x"}}, Result: &protocol.CommandResult{Status: "succeeded"}}59 }60 e.agent.Run(bg)61 e.step(e.orch)62 e.want(old, "succeeded")63 running := e.enqueue("inventory.refresh", "node", e.node, nil, nil) // still open64 e.step(e.orch)65 audits := e.count(`select count(*) from audit_log`)66 now := e.clock.Now()67 // Nothing is old enough yet.68 if ev, _, err := orchestrator.Housekeep(bg, e.pool, now.Add(89*24*time.Hour)); err != nil || ev != 0 {69 t.Fatalf("pruned %d events before the retention delay (%v)", ev, err)70 }71 ev, _, err := orchestrator.Housekeep(bg, e.pool, now.Add(91*24*time.Hour))72 if err != nil || ev != 4 { // queued, dispatched, output, succeeded73 t.Fatalf("pruned %d events, want 4 (%v)", ev, err)74 }75 if e.count(`select count(*) from action_events where action_id=$1`, running) == 0 {76 t.Fatal("events of an unfinished action were pruned")77 }78 if e.count(`select count(*) from actions`) != 2 || e.count(`select count(*) from audit_log`) != audits {79 t.Fatal("housekeeping removed actions or audit entries")80 }81 // The retention setting is honoured.82 e.clock.Advance(91 * time.Second)83 e.step(e.orch)84 e.want(running, "failed")85 e.pool.Exec(bg, `insert into settings(key,value) values ('retention.action_events_days','7')`)86 if ev, _, _ := orchestrator.Housekeep(bg, e.pool, now.Add(6*24*time.Hour)); ev != 0 {87 t.Fatalf("pruned %d events before 7 days", ev)88 }89 if ev, _, _ := orchestrator.Housekeep(bg, e.pool, now.Add(8*24*time.Hour)); ev == 0 {90 t.Fatal("retention setting ignored")91 }92}93