Adds PutApplyStatus/ListApplyStatus so each process can report which version of a control-plane resource it is running, and the UI can read it back. docs/prds/control-plane-apply-status.md is the approved design. It goes on the State Store contract rather than the control plane, whose service is for editing a resource: a status report is not an edit. The config-snapshot pair is the precedent, and the deny-by-default arm of authorizesTaskScopedCall already refuses both new RPCs to a task grant, which TestOnlyAdminReportsApplyStatus pins. The row is keyed on (process, kind) and replaced in place, because a process re-stamps on an interval and nothing reads a history of re-stamps. An error leaves applied_version alone, so a rejected edit never erases the fact that an older version is live. The read is a list and an empty one is not an error, unlike the config snapshot's found=false singleton. Contract PRD inventory and RPC count follow in the same series. Relates to archie-core-pskb.
93 lines
3.2 KiB
Go
93 lines
3.2 KiB
Go
package store
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestApplyStatusReplacesPerProcessAndKind pins the key: a process re-stamping
|
|
// its report must overwrite its own row, not append one, or the settings page
|
|
// accumulates a row per report and cannot say which is current.
|
|
func TestApplyStatusReplacesPerProcessAndKind(t *testing.T) {
|
|
s := openTest(t)
|
|
first := time.Date(2026, 9, 21, 10, 0, 0, 0, time.UTC)
|
|
|
|
for _, status := range []ApplyStatus{
|
|
{Process: "archied", Kind: "model-role-assignments", AppliedVersion: 3, ReportedAt: first},
|
|
{Process: "archied", Kind: "repository-policies", AppliedVersion: 5, ReportedAt: first},
|
|
{Process: "archie-gateway", Kind: "model-role-assignments", AppliedVersion: 2, ReportedAt: first},
|
|
} {
|
|
if err := s.PutApplyStatus(t.Context(), status); err != nil {
|
|
t.Fatalf("PutApplyStatus(%s/%s): %v", status.Process, status.Kind, err)
|
|
}
|
|
}
|
|
|
|
restamp := first.Add(30 * time.Second)
|
|
if err := s.PutApplyStatus(t.Context(), ApplyStatus{
|
|
Process: "archied", Kind: "model-role-assignments", AppliedVersion: 4, ReportedAt: restamp,
|
|
}); err != nil {
|
|
t.Fatalf("re-stamp: %v", err)
|
|
}
|
|
|
|
got, err := s.ListApplyStatus(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("ListApplyStatus: %v", err)
|
|
}
|
|
if len(got) != 3 {
|
|
t.Fatalf("ListApplyStatus returned %d records, want 3: %+v", len(got), got)
|
|
}
|
|
for _, record := range got {
|
|
if record.Process != "archied" || record.Kind != "model-role-assignments" {
|
|
continue
|
|
}
|
|
if record.AppliedVersion != 4 {
|
|
t.Errorf("AppliedVersion = %d, want the re-stamped 4", record.AppliedVersion)
|
|
}
|
|
if !record.ReportedAt.Equal(restamp) {
|
|
t.Errorf("ReportedAt = %v, want the re-stamped %v", record.ReportedAt, restamp)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestApplyStatusKeepsAppliedVersionWhenReportingAnError pins that a rejected
|
|
// edit does not erase the fact that an older version is still live.
|
|
func TestApplyStatusKeepsAppliedVersionWhenReportingAnError(t *testing.T) {
|
|
s := openTest(t)
|
|
at := time.Date(2026, 9, 21, 10, 0, 0, 0, time.UTC)
|
|
if err := s.PutApplyStatus(t.Context(), ApplyStatus{
|
|
Process: "archied", Kind: "tool-settings", AppliedVersion: 7, ReportedAt: at,
|
|
}); err != nil {
|
|
t.Fatalf("PutApplyStatus: %v", err)
|
|
}
|
|
if err := s.PutApplyStatus(t.Context(), ApplyStatus{
|
|
Process: "archied", Kind: "tool-settings", AppliedVersion: 7,
|
|
Error: "validate database settings: bad policy", ReportedAt: at.Add(time.Minute),
|
|
}); err != nil {
|
|
t.Fatalf("PutApplyStatus with error: %v", err)
|
|
}
|
|
|
|
got, err := s.ListApplyStatus(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("ListApplyStatus: %v", err)
|
|
}
|
|
if len(got) != 1 {
|
|
t.Fatalf("ListApplyStatus returned %d records, want 1", len(got))
|
|
}
|
|
if got[0].AppliedVersion != 7 || got[0].Error == "" {
|
|
t.Fatalf("record = %+v, want version 7 still live alongside the error", got[0])
|
|
}
|
|
}
|
|
|
|
// TestListApplyStatusEmptyIsNotAnError pins the list convention: nothing has
|
|
// reported yet is the normal state of a store whose processes have not booted,
|
|
// and it is an empty slice rather than an error or a found=false.
|
|
func TestListApplyStatusEmptyIsNotAnError(t *testing.T) {
|
|
got, err := openTest(t).ListApplyStatus(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("ListApplyStatus on an empty store: %v", err)
|
|
}
|
|
if len(got) != 0 {
|
|
t.Fatalf("ListApplyStatus returned %d records, want none", len(got))
|
|
}
|
|
}
|