Files
restic-manager/internal/agent/scheduler/scheduler_test.go
T
steve d000fe7ec1 P2R-01: REST + WS rewire against the slim shape
Schedules CRUD now takes {cron, enabled, source_group_ids[]} with cron
parsed via robfig/cron/v3 and group membership scoped to the host.
New source-groups CRUD lives at /api/hosts/{id}/source-groups; delete
refuses with 409 if any schedule still references the group, returning
the schedule list so the UI can prompt 'remove from these schedules
first.' Repo-maintenance GET/PUT manages forget/prune/check cadences
on host_repo_maintenance — no version bump, the server-side ticker
(P2R-06) drives execution.

Per-source-group Run-now (POST /hosts/{id}/source-groups/{gid}/run)
resolves the group's includes/excludes/retention/tag and dispatches a
backup command.run with the new structured CommandRunPayload fields
(Includes/Excludes/Tag). Old per-host /hosts/{id}/run-backup and
/hosts/{id}/init-repo return 410 Gone with a redirect message.

schedule_push.go is rebuilt: buildScheduleSetPayload assembles the
slim wire shape, pushScheduleSetOnConn ships it during the on-hello
window, pushScheduleSetAsync fires after every CRUD mutation, and
dispatchScheduledJob handles agent schedule.fire by iterating the
schedule's source groups and dispatching one backup per group with
actor_kind=schedule and scheduled_id pointing at the schedule.

Auto-init at first WS connect: when the host has repo creds bound and
no init job in its history, server dispatches restic init. Restic's
'config file already exists' soft-success means re-runs against an
existing repo no-op; we don't auto-retry on failure (operator triggers
re-init manually via the danger zone in P2R-09).

api.Schedule drops Kind/Paths/Excludes/Tags/RetentionPolicy/Manual etc.
in favour of {id, cron, enabled, source_groups: [...]}. The agent
scheduler stops checking sch.Manual; cmd/agent's backup dispatch reads
Includes/Excludes/Tag instead of Args.

Tests cover the new HTTP surface end-to-end: source-groups CRUD with
in-use refusal, schedule validation (bad cron / missing groups /
foreign group), repo-maintenance auto-seed and validation, the 410
route, and buildScheduleSetPayload's wire-shape correctness. Full
suite passes; smoke env exercises auto-init dispatch on hello,
async push after schedule create, and per-source-group Run-now
landing the right paths/excludes/tag at the agent.
2026-05-03 10:56:40 +01:00

160 lines
3.5 KiB
Go

package scheduler
import (
"sync"
"testing"
"time"
"gitea.dcglab.co.uk/steve/restic-manager/internal/api"
)
// recSender is a Sender that records every envelope it gets. Tests
// inspect it after a tick to assert the right messages were emitted.
type recSender struct {
mu sync.Mutex
envs []api.Envelope
}
func (r *recSender) Send(env api.Envelope) error {
r.mu.Lock()
defer r.mu.Unlock()
r.envs = append(r.envs, env)
return nil
}
func (r *recSender) snapshot() []api.Envelope {
r.mu.Lock()
defer r.mu.Unlock()
out := make([]api.Envelope, len(r.envs))
copy(out, r.envs)
return out
}
func TestApplyEmitsAck(t *testing.T) {
t.Parallel()
tx := &recSender{}
s := New()
defer s.Stop()
s.Apply(api.ScheduleSetPayload{
Version: 7,
Schedules: []api.Schedule{
{ID: "s1", CronExpr: "@hourly", Enabled: true},
},
}, tx)
if got := s.Version(); got != 7 {
t.Fatalf("Version: got %d, want 7", got)
}
envs := tx.snapshot()
if len(envs) != 1 {
t.Fatalf("expected 1 envelope (ack), got %d", len(envs))
}
if envs[0].Type != api.MsgScheduleAck {
t.Fatalf("envelope type: got %s, want %s", envs[0].Type, api.MsgScheduleAck)
}
var ack api.ScheduleAckPayload
_ = envs[0].UnmarshalPayload(&ack)
if ack.Version != 7 {
t.Fatalf("ack version: got %d", ack.Version)
}
}
func TestApplyTickFiresScheduleFire(t *testing.T) {
t.Parallel()
tx := &recSender{}
s := New()
defer s.Stop()
// Cron expression that fires roughly every second; close enough
// to be reliable in CI without making the test slow.
s.Apply(api.ScheduleSetPayload{
Version: 1,
Schedules: []api.Schedule{
{ID: "every-second", CronExpr: "@every 1s", Enabled: true},
},
}, tx)
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
envs := tx.snapshot()
for _, e := range envs {
if e.Type == api.MsgScheduleFire {
var p api.ScheduleFirePayload
_ = e.UnmarshalPayload(&p)
if p.ScheduleID == "every-second" {
return
}
}
}
time.Sleep(50 * time.Millisecond)
}
t.Fatal("schedule.fire did not arrive within 3s")
}
func TestApplyDisabledEntriesSkipped(t *testing.T) {
t.Parallel()
tx := &recSender{}
s := New()
defer s.Stop()
s.Apply(api.ScheduleSetPayload{
Version: 1,
Schedules: []api.Schedule{
{ID: "off", CronExpr: "@every 1s", Enabled: false},
},
}, tx)
// A disabled schedule must never fire — give the cron a couple
// of ticks to confirm it's silent.
time.Sleep(2200 * time.Millisecond)
for _, e := range tx.snapshot() {
if e.Type == api.MsgScheduleFire {
t.Fatalf("disabled schedule fired: %+v", e)
}
}
}
func TestApplyReplacesPriorState(t *testing.T) {
t.Parallel()
tx := &recSender{}
s := New()
defer s.Stop()
s.Apply(api.ScheduleSetPayload{
Version: 1,
Schedules: []api.Schedule{
{ID: "old", CronExpr: "@every 1s", Enabled: true},
},
}, tx)
// Wait long enough for the first version to fire at least once.
time.Sleep(1500 * time.Millisecond)
// Now replace with version 2 that doesn't include "old".
s.Apply(api.ScheduleSetPayload{
Version: 2,
Schedules: []api.Schedule{},
}, tx)
// Snapshot count *after* the replacement.
before := 0
for _, e := range tx.snapshot() {
if e.Type == api.MsgScheduleFire {
before++
}
}
time.Sleep(2 * time.Second)
after := 0
for _, e := range tx.snapshot() {
if e.Type == api.MsgScheduleFire {
after++
}
}
if after != before {
t.Fatalf("schedule.fire count grew after replacement (before=%d after=%d) — old cron still firing",
before, after)
}
}