7c0c75457f
Purely additive; the controller path (reports/customer_configs/checkAuthCustomer/ existing checkers) is untouched. Cutover remains slice 10. - store: new hosts/guests/host_reports tables (full schema incl. columns INERT until slice 10, so no later ALTER); GetHostByAPIKey/GetHost/ListHosts/UpsertHost/ SaveHostReport/UpsertGuestFromReport (preserves inert cols)/GetHostStaleness/ GuestID; Prune also prunes host_reports. - api: checkAuthHost (sibling of checkAuthCustomer); POST /host-report (per-host Bearer, 4MiB, denorm + guest upsert, control envelope); POST /admin/hosts (PROVISIONAL global-key host mint); host_* event types registered. - monitor: HostStalenessChecker sibling over host_reports (host_stale/down/ recovered), wired on the existing 60s ticker; controller checkers unchanged. - tests (hermetic): store intent/inert-column preservation, auth, ingest (envelope+denorm, mismatch/unknown/blocked/oversize), admin mint round-trip, host staleness transitions. CHANGELOG v0.7.0. Contract matches the agent host-report spec field-for-field. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
123 lines
4.1 KiB
Go
123 lines
4.1 KiB
Go
package store
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func newTestStore(t *testing.T) *Store {
|
|
t.Helper()
|
|
s, err := New(filepath.Join(t.TempDir(), "test.db"), log.New(io.Discard, "", 0))
|
|
if err != nil {
|
|
t.Fatalf("store.New: %v", err)
|
|
}
|
|
t.Cleanup(func() { s.Close() })
|
|
return s
|
|
}
|
|
|
|
func TestGuestID(t *testing.T) {
|
|
if got := GuestID("demo-host-01", 100); got != "demo-host-01/100" {
|
|
t.Errorf("GuestID = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestUpsertHost_AndLookup(t *testing.T) {
|
|
s := newTestStore(t)
|
|
if err := s.UpsertHost(&Host{HostID: "h1", CustomerID: "c1", APIKey: "k1"}); err != nil {
|
|
t.Fatalf("UpsertHost: %v", err)
|
|
}
|
|
h, err := s.GetHost("h1")
|
|
if err != nil || h == nil {
|
|
t.Fatalf("GetHost: %v / %v", h, err)
|
|
}
|
|
if h.CustomerID != "c1" || h.APIKey != "k1" || h.DesiredJSON != "{}" || h.LastReportAt != nil {
|
|
t.Errorf("host = %+v", h)
|
|
}
|
|
byKey, err := s.GetHostByAPIKey("k1")
|
|
if err != nil || byKey == nil || byKey.HostID != "h1" {
|
|
t.Errorf("GetHostByAPIKey hit = %+v / %v", byKey, err)
|
|
}
|
|
miss, err := s.GetHostByAPIKey("nope")
|
|
if err != nil || miss != nil {
|
|
t.Errorf("GetHostByAPIKey miss = %+v / %v (want nil,nil)", miss, err)
|
|
}
|
|
}
|
|
|
|
func TestSaveHostReport_BumpsRealityPreservesIntent(t *testing.T) {
|
|
s := newTestStore(t)
|
|
if err := s.UpsertHost(&Host{HostID: "h1", CustomerID: "c1", APIKey: "k1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Operator-owned intent columns (inert this slice) set out-of-band.
|
|
if _, err := s.db.Exec(`UPDATE hosts SET desired_json='{"want":1}', desired_generation=7 WHERE host_id='h1'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
denorm := HostReportDenorm{AgentVersion: "0.3.0", CPUPercent: 3.2, MemoryPercent: 25, DiskPercent: 19, GuestTotal: 2, GuestRunning: 1, CloudflaredStatus: "active"}
|
|
if err := s.SaveHostReport("h1", "c1", []byte(`{"host_id":"h1"}`), denorm); err != nil {
|
|
t.Fatalf("SaveHostReport: %v", err)
|
|
}
|
|
|
|
h, _ := s.GetHost("h1")
|
|
if h.AgentVersion != "0.3.0" || h.LastReportAt == nil {
|
|
t.Errorf("reality not bumped: %+v", h)
|
|
}
|
|
if h.DesiredJSON != `{"want":1}` || h.DesiredGeneration != 7 {
|
|
t.Errorf("a report must NOT clobber intent columns: desired_json=%q gen=%d", h.DesiredJSON, h.DesiredGeneration)
|
|
}
|
|
var n int
|
|
s.db.QueryRow(`SELECT COUNT(*) FROM host_reports WHERE host_id='h1'`).Scan(&n)
|
|
if n != 1 {
|
|
t.Errorf("host_reports rows = %d, want 1", n)
|
|
}
|
|
}
|
|
|
|
func TestUpsertGuestFromReport_PreservesInertColumns(t *testing.T) {
|
|
s := newTestStore(t)
|
|
gid := GuestID("h1", 100)
|
|
if err := s.UpsertGuestFromReport(&Guest{GuestID: gid, CustomerID: "c1", HostID: "h1", VMID: 100, DisplayName: "acme", Status: "running"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Slice-10 columns set out-of-band; a report upsert must not touch them.
|
|
if _, err := s.db.Exec(`UPDATE guests SET api_key='controllerkey', desired_spec_json='{"cores":4}' WHERE guest_id=?`, gid); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// A later report changes reality (status/name).
|
|
if err := s.UpsertGuestFromReport(&Guest{GuestID: gid, CustomerID: "c1", HostID: "h1", VMID: 100, DisplayName: "acme-renamed", Status: "stopped"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var apiKey, desiredSpec, status, name string
|
|
err := s.db.QueryRow(`SELECT api_key, desired_spec_json, status, display_name FROM guests WHERE guest_id=?`, gid).
|
|
Scan(&apiKey, &desiredSpec, &status, &name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if apiKey != "controllerkey" || desiredSpec != `{"cores":4}` {
|
|
t.Errorf("inert columns clobbered: api_key=%q desired_spec_json=%q", apiKey, desiredSpec)
|
|
}
|
|
if status != "stopped" || name != "acme-renamed" {
|
|
t.Errorf("reality not updated: status=%q name=%q", status, name)
|
|
}
|
|
}
|
|
|
|
func TestGetHostStaleness_SkipsNeverReported(t *testing.T) {
|
|
s := newTestStore(t)
|
|
s.UpsertHost(&Host{HostID: "h1", CustomerID: "c1", APIKey: "k1"})
|
|
rows, err := s.GetHostStaleness()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(rows) != 0 {
|
|
t.Errorf("never-reported host should be skipped, got %d rows", len(rows))
|
|
}
|
|
s.SaveHostReport("h1", "c1", []byte(`{}`), HostReportDenorm{})
|
|
rows, _ = s.GetHostStaleness()
|
|
if len(rows) != 1 || rows[0].HostID != "h1" {
|
|
t.Errorf("after a report expected 1 row, got %+v", rows)
|
|
}
|
|
}
|