SPB Git

Repository Public

cluster_helpers_test.go 7.3 KB 254 lines go

1package api_test23// Seeding helpers for the read-side and administration tests. Everything is4// written straight into the throwaway test database, the way telemetry-ingest5// would have stored it.67import (8	"context"9	"encoding/json"10	"fmt"11	"net/url"12	"strings"13	"testing"14	"time"1516	"clustr.spboucher.ai/mcc/internal/ids"17	"clustr.spboucher.ai/mcc/internal/protocol"18)1920func (h *harness) dbExec(sql string, args ...any) {21	h.t.Helper()22	if _, err := h.Pool.Exec(context.Background(), sql, args...); err != nil {23		h.t.Fatalf("exec %q: %v", sql, err)24	}25}2627func (h *harness) dbScan(dst []any, sql string, args ...any) {28	h.t.Helper()29	if err := h.Pool.QueryRow(context.Background(), sql, args...).Scan(dst...); err != nil {30		h.t.Fatalf("query %q: %v", sql, err)31	}32}3334type nodeSeed struct {35	Alias       string36	Kind        string37	Site, Role  string38	Tags        []string39	Cores       int40	Mem         int6441	Seen        bool          // false: the node never reported42	Age         time.Duration // age of the latest report43	Values      map[string]any44	Avail       map[string]string45	IP          string46	Manual      bool47	Inventory   map[string]any48	Maintenance bool49	Processes   []protocol.Process50}5152// seedNode inserts a node (and its latest state when Seen) and returns its id.53func (h *harness) seedNode(s nodeSeed) string {54	h.t.Helper()55	id := ids.New().String()56	def := func(v *string, d string) {57		if *v == "" {58			*v = d59		}60	}61	def(&s.Kind, "mac")62	def(&s.Site, "default")63	def(&s.Role, "worker")64	if s.Tags == nil {65		s.Tags = []string{}66	}67	if s.Manual {68		h.dbExec(`insert into nodes(id, alias, kind, site, role, tags, registered_via) values ($1,$2,$3::node_kind,$4,$5,$6,'manual')`,69			id, s.Alias, s.Kind, s.Site, s.Role, s.Tags)70		return id71	}72	inv := s.Inventory73	if inv == nil {74		inv = map[string]any{}75	}76	var ip, cores, mem any77	if s.IP != "" {78		ip = s.IP79	}80	if s.Cores > 0 {81		cores = s.Cores82	}83	if s.Mem > 0 {84		mem = s.Mem85	}86	h.dbExec(`insert into nodes(id, hardware_uuid, alias, kind, hostname, site, role, tags, model, chip, arch, cpu_cores, mem_bytes, os_name, os_version,87			os_build, primary_ip, registered_via, maintenance, inventory, inventory_at)88		values ($1,$2,$3,$4::node_kind,$5,$6,$7,$8,'Mac15,14','Apple M3 Ultra','arm64',$9,$10,'macOS','26.4','25E100',$11::inet,'agent',$12,$13, now())`,89		id, "HW-"+s.Alias, s.Alias, s.Kind, "host-"+strings.ToLower(s.Alias), s.Site, s.Role, s.Tags, cores, mem, ip, s.Maintenance, inv)90	if !s.Seen {91		return id92	}93	values, avail := s.Values, s.Avail94	if values == nil {95		values = map[string]any{}96	}97	if avail == nil {98		avail = map[string]string{}99	}100	var procs any101	if s.Processes != nil {102		b, _ := json.Marshal(s.Processes)103		procs = b104	}105	h.dbExec(`insert into node_state(node_id, ts, received_at, values, availability, processes, processes_at)106		values ($1, now() - make_interval(secs => $2), now() - make_interval(secs => $2), $3, $4, $5::jsonb, now() - make_interval(secs => $2))`,107		id, s.Age.Seconds(), values, avail, procs)108	h.dbExec(`update nodes set last_seen_at = now() - make_interval(secs => $2) where id = $1`, id, s.Age.Seconds())109	return id110}111112// seedAgent enrolls an agent for a node, last heard `age` ago.113func (h *harness) seedAgent(nodeID string, age time.Duration) string {114	h.t.Helper()115	id := ids.New().String()116	h.dbExec(`insert into agents(id, node_id, cert_serial, cert_fingerprint, cert_not_after, version, os, capabilities, last_seen_at, overhead)117		values ($1, $2, $3, decode('ab12', 'hex'), now() + interval '30 days', '1.0.0', 'darwin', '{inventory.refresh,service.restart}',118			now() - make_interval(secs => $4), '{"cpu": 0.4, "rss": 20971520}')`, id, nodeID, "serial-"+id, age.Seconds())119	return id120}121122// seedPartitions attaches one catch-all partition to each sample table (functional tests).123func (h *harness) seedPartitions() {124	h.t.Helper()125	for _, t := range []string{"samples_raw", "samples_1m", "samples_1h"} {126		h.dbExec(fmt.Sprintf(`create table %s_all partition of %s for values from (minvalue) to (maxvalue)`, t, t))127	}128}129130func seedMetricID(t *testing.T, name string) int16 {131	t.Helper()132	m, ok := protocol.MetricByName(name)133	if !ok {134		t.Fatalf("metric %s is not in the catalog", name)135	}136	return m.ID137}138139func (h *harness) seedService(nodeID, manager, name, state string) string {140	h.t.Helper()141	id := ids.New().String()142	h.dbExec(`insert into services(id, node_id, manager, name, state, pid, restarts, mem_bytes, cpu_pct, uptime_s, version, ports, meta, last_seen_at)143		values ($1, $2, $3::service_manager, $4, $5, 4242, 3, 104857600, 1.5, 3600, '1.2.0', '{8080,8443}',144			'{"raw_state":"online","api_token":"s3cr3t-value"}', now())`, id, nodeID, manager, name, state)145	return id146}147148func (r resp) jsonObj(t *testing.T) map[string]any {149	t.Helper()150	var m map[string]any151	r.json(t, &m)152	return m153}154155func (r resp) jsonItems(t *testing.T) []map[string]any {156	t.Helper()157	var l struct {158		Items []map[string]any `json:"items"`159	}160	r.json(t, &l)161	if l.Items == nil {162		t.Fatalf("response has no items array: %s", r.Raw)163	}164	return l.Items165}166167func (r resp) nextCursor(t *testing.T) string {168	t.Helper()169	var l struct {170		Next string `json:"next_cursor"`171	}172	r.json(t, &l)173	return l.Next174}175176// walkPages follows next_cursor until the list is exhausted and returns the value177// of `field` of every row, in order. It fails on a page larger than limit.178func (c *client) walkPages(path string, limit int, field string) []string {179	c.h.t.Helper()180	sep := "?"181	if strings.Contains(path, "?") {182		sep = "&"183	}184	var out []string185	cursor := ""186	for page := 0; page < 200; page++ {187		u := fmt.Sprintf("%s%slimit=%d", path, sep, limit)188		if cursor != "" {189			u += "&cursor=" + url.QueryEscape(cursor)190		}191		r := c.must("GET", u, nil, 200)192		rows := r.jsonItems(c.h.t)193		if len(rows) > limit {194			c.h.t.Fatalf("%s: page of %d rows, limit %d", u, len(rows), limit)195		}196		for _, row := range rows {197			out = append(out, fmt.Sprint(row[field]))198		}199		if cursor = r.nextCursor(c.h.t); cursor == "" {200			return out201		}202	}203	c.h.t.Fatalf("%s: pagination does not terminate", path)204	return nil205}206207func wantExactlyOnce(t *testing.T, what string, got []string, want ...string) {208	t.Helper()209	seen := map[string]int{}210	for _, g := range got {211		seen[g]++212	}213	for _, w := range want {214		if seen[w] != 1 {215			t.Fatalf("%s: %q returned %d times, want once (got %v)", what, w, seen[w], got)216		}217	}218	if len(got) != len(want) {219		t.Fatalf("%s: got %d rows %v, want %d %v", what, len(got), got, len(want), want)220	}221}222223func rowBy(rows []map[string]any, field, value string) map[string]any {224	for _, r := range rows {225		if fmt.Sprint(r[field]) == value {226			return r227		}228	}229	return nil230}231232// grant creates a policy grant through the API (which also refreshes the233// server's grant cache) and returns its id.234func (c *client) grant(subjectKind, subject, permission, resourceKind, resourceID, effect string) string {235	c.h.t.Helper()236	body := map[string]any{"subject_kind": subjectKind, "subject": subject, "permission": permission, "effect": effect}237	if resourceKind != "" {238		body["resource_kind"] = resourceKind239	}240	if resourceID != "" {241		body["resource_id"] = resourceID242	}243	var out struct{ ID string }244	c.must("POST", "/policy-grants", body, 201).json(c.h.t, &out)245	return out.ID246}247248func (h *harness) userIDOf(handle string) string {249	h.t.Helper()250	var id string251	h.dbScan([]any{&id}, `select id::text from users where handle = $1`, handle)252	return id253}254
Files main
Menu

Theme