Every install serves from Postgres, so the legacyread/legacyimport packages, the archie-state-store import command, the boot gate that refused to serve over unimported SQLite files, and db_path (which only located those files) go. Migration 0010 drops the import_completion table.
623 lines
24 KiB
Go
623 lines
24 KiB
Go
// Package main smoke-tests the standalone archie-state-store process by
|
|
// building the real binary, running it against a minimal config on the
|
|
// loopback interface, and driving it through the State Store gRPC contract
|
|
// exactly the way a consumer (archied, archie-gateway, archie-agent) does.
|
|
//
|
|
// This is the .4.7 end-to-end verification for
|
|
// docs/prds/state-store-contract.md (rev. 2c): it proves the process owns the
|
|
// task store, serves the combined task/event/capture
|
|
// surface over one gRPC service, preserves error-sentinel fidelity across the
|
|
// wire, honours a per-call deadline, survives a restart/recovery cycle, and
|
|
// is the sole owner of the store. See §5 (binary layout), §6 (handoff),
|
|
// §7 (error semantics), §11 (remote adapter) and §12 step 7/8 (single owner).
|
|
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/binding"
|
|
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
|
)
|
|
|
|
// repoRoot walks up from the test's source directory to find the module root
|
|
// (where go.mod lives). The test runs in the package directory
|
|
// (cmd/archie-state-store), so the module root is above it; walking keeps the
|
|
// result correct regardless of the harness working directory.
|
|
func repoRoot(t *testing.T) string {
|
|
t.Helper()
|
|
_, file, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
t.Fatal("cannot determine test source path")
|
|
}
|
|
dir := filepath.Dir(file)
|
|
for {
|
|
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
|
return dir
|
|
}
|
|
parent := filepath.Dir(dir)
|
|
if parent == dir {
|
|
t.Fatal("go.mod not found above test source")
|
|
}
|
|
dir = parent
|
|
}
|
|
}
|
|
|
|
// buildBinary compiles ./cmd/archie-state-store into dir and returns the path.
|
|
// This is the real binary the smoke test exercises, not a test double -- the
|
|
// point of .4.7 is to verify the actual process.
|
|
func buildBinary(t *testing.T, dir string) string {
|
|
t.Helper()
|
|
bin := filepath.Join(dir, "archie-state-store")
|
|
cmd := exec.CommandContext(t.Context(), "go", "build", "-o", bin, "./cmd/archie-state-store")
|
|
cmd.Dir = repoRoot(t)
|
|
cmd.Env = os.Environ()
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("go build archie-state-store: %v\n%s", err, out)
|
|
}
|
|
if _, err := os.Stat(bin); err != nil {
|
|
t.Fatalf("built binary not found: %v", err)
|
|
}
|
|
return bin
|
|
}
|
|
|
|
// writeMinimalConfig writes a config.toml that passes configuration.Validate
|
|
// while keeping the process minimal: the standalone binary takes its gRPC
|
|
// listen address from -listen or [services.state].listen, never from the
|
|
// target, so the config only needs the fields validation requires plus the
|
|
// database_url it owns. The
|
|
// forge token resolves from an env var, so no real credential is needed.
|
|
func writeMinimalConfig(t *testing.T, dir string) string {
|
|
t.Helper()
|
|
return writeConfigFor(t, dir, pgtest.URL(t))
|
|
}
|
|
|
|
// writeConfigFor is writeMinimalConfig naming the database at url.
|
|
func writeConfigFor(t *testing.T, dir, url string) string {
|
|
t.Helper()
|
|
cfg := filepath.Join(dir, "config.toml")
|
|
content := fmt.Sprintf(`bot_user = "archie-bot"
|
|
database_url = %q
|
|
[forge]
|
|
type = "github"
|
|
host = "https://github.example.com"
|
|
token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" }
|
|
[[repos]]
|
|
owner = "acme"
|
|
name = "widget"
|
|
`, url)
|
|
if err := os.WriteFile(cfg, []byte(content), 0o600); err != nil {
|
|
t.Fatalf("write config: %v", err)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
// writeConfigWithServiceListen is writeMinimalConfig plus the
|
|
// [services.state] block that lets a test supply the listen address through
|
|
// configuration instead of the -listen flag.
|
|
func writeConfigWithServiceListen(t *testing.T, dir string) string {
|
|
t.Helper()
|
|
cfg := writeMinimalConfig(t, dir)
|
|
f, err := os.OpenFile(cfg, os.O_APPEND|os.O_WRONLY, 0o600)
|
|
if err != nil {
|
|
t.Fatalf("open config to append: %v", err)
|
|
}
|
|
defer func() {
|
|
if err := f.Close(); err != nil {
|
|
t.Fatalf("close config: %v", err)
|
|
}
|
|
}()
|
|
if _, err := f.WriteString("\n[services.state]\nlisten = \"127.0.0.1:0\"\n"); err != nil {
|
|
t.Fatalf("append [services.state]: %v", err)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
// syncBuffer is a concurrency-safe buffer for a subprocess's stdout/stderr:
|
|
// the os/exec goroutine writes while the test goroutine reads (to parse the
|
|
// log and to report it on failure). A plain strings.Builder races under
|
|
// -race, so both paths take the mutex.
|
|
type syncBuffer struct {
|
|
mu sync.Mutex
|
|
b bytes.Buffer
|
|
}
|
|
|
|
func (s *syncBuffer) Write(p []byte) (int, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return s.b.Write(p)
|
|
}
|
|
|
|
func (s *syncBuffer) String() string {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return s.b.String()
|
|
}
|
|
|
|
// stateStoreProcess wraps a running archie-state-store subprocess plus the
|
|
// addresses it bound. addr is the gRPC listen address; readyAddr is the
|
|
// readiness HTTP surface. It is stopped gracefully on cleanup by SIGTERM, the
|
|
// signal the systemd unit ("Restart=on-failure") and the daemon use to drain
|
|
// the server.
|
|
type stateStoreProcess struct {
|
|
cmd *exec.Cmd
|
|
addr string
|
|
readyAddr string
|
|
log *syncBuffer
|
|
}
|
|
|
|
// startStateStoreProcess starts bin with config, waiting for the gRPC listener
|
|
// to come up and the readiness surface to answer /healthz. It parses the
|
|
// binary's stderr log for the bound addresses because --listen 127.0.0.1:0
|
|
// lets the process choose ephemeral ports, and the real address (not the
|
|
// configured one) is what a consumer must dial.
|
|
func startStateStoreProcess(t *testing.T, bin, cfg string) *stateStoreProcess {
|
|
t.Helper()
|
|
return startStateStore(t, bin, cfg, "127.0.0.1:0", "")
|
|
}
|
|
|
|
// startStateStore is startStateStoreProcess with the listener topology and
|
|
// bearer token exposed: the token-protected, non-loopback listener is the
|
|
// topology every container-mode deployment profile uses
|
|
// (docs/prds/state-store-contract.md §9), where the server installs the
|
|
// credential interceptors on both the unary and the streaming surface.
|
|
func startStateStore(t *testing.T, bin, cfg, listen, token string) *stateStoreProcess {
|
|
t.Helper()
|
|
args := []string{"-config", cfg, "-ready-addr", "127.0.0.1:0"}
|
|
if listen != "" {
|
|
// An empty listen means "pass no -listen at all". A test uses that to
|
|
// prove the bound address came from [services.state].listen.
|
|
args = append(args, "-listen", listen)
|
|
}
|
|
if token != "" {
|
|
// An explicit -token wins over [services.state].target_token and the
|
|
// STATE_STORE_TOKEN secret, so the process authenticates callers with
|
|
// exactly this value.
|
|
args = append(args, "-token", token)
|
|
}
|
|
cmd := exec.CommandContext(t.Context(), bin, args...)
|
|
cmd.Env = append(os.Environ(), "ARCHIE_GITHUB_TOKEN=test-token")
|
|
var log syncBuffer
|
|
cmd.Stderr = &log
|
|
cmd.Stdout = &log
|
|
if err := cmd.Start(); err != nil {
|
|
t.Fatalf("start state store: %v", err)
|
|
}
|
|
p := &stateStoreProcess{cmd: cmd, log: &log}
|
|
t.Cleanup(func() { p.stop(t) })
|
|
|
|
// Recover the ephemeral ports from the JSON log lines the binary emits.
|
|
deadline := time.Now().Add(15 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if p.parseAddrs() {
|
|
break
|
|
}
|
|
if cmd.ProcessState != nil {
|
|
t.Fatalf("state store exited early:\n%s", log.String())
|
|
}
|
|
time.Sleep(25 * time.Millisecond)
|
|
}
|
|
if p.addr == "" || p.readyAddr == "" {
|
|
t.Fatalf("did not recover bound addresses from log:\n%s", log.String())
|
|
}
|
|
|
|
// The readiness surface is the liveness signal the runbook uses; poll it
|
|
// until it answers (the gRPC listener may come up a tick ahead of it).
|
|
deadline = time.Now().Add(15 * time.Second)
|
|
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://"+p.readyAddr+"/healthz", nil)
|
|
if err != nil {
|
|
t.Fatalf("build readiness request: %v", err)
|
|
}
|
|
for time.Now().Before(deadline) {
|
|
if resp, err := http.DefaultClient.Do(req); err == nil {
|
|
_ = resp.Body.Close()
|
|
if resp.StatusCode == http.StatusOK {
|
|
return p
|
|
}
|
|
}
|
|
time.Sleep(25 * time.Millisecond)
|
|
}
|
|
t.Fatalf("readiness /healthz never became healthy:\n%s", log.String())
|
|
return nil
|
|
}
|
|
|
|
// parseAddrs scans the captured log for the bound gRPC and readiness
|
|
// addresses. The binary logs "archie-state-store running" with the gRPC addr
|
|
// and "state store readiness listening" with the HTTP addr.
|
|
func (p *stateStoreProcess) parseAddrs() bool {
|
|
if p.addr != "" && p.readyAddr != "" {
|
|
return true
|
|
}
|
|
scanner := bufio.NewScanner(strings.NewReader(p.log.String()))
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
var rec map[string]any
|
|
if err := json.Unmarshal([]byte(line), &rec); err != nil {
|
|
continue
|
|
}
|
|
if msg, _ := rec["msg"].(string); msg == "archie-state-store running" {
|
|
if addr, ok := rec["addr"].(string); ok && addr != "" && p.addr == "" {
|
|
p.addr = addr
|
|
}
|
|
}
|
|
if msg, _ := rec["msg"].(string); msg == "state store readiness listening" {
|
|
if addr, ok := rec["addr"].(string); ok && addr != "" && p.readyAddr == "" {
|
|
p.readyAddr = strings.TrimPrefix(addr, "http://")
|
|
}
|
|
}
|
|
}
|
|
return p.addr != "" && p.readyAddr != ""
|
|
}
|
|
|
|
// stop drains the process with SIGTERM and waits for it to exit. This is the
|
|
// same graceful-stop path serveStateStore's signal handling implements.
|
|
func (p *stateStoreProcess) stop(t *testing.T) {
|
|
t.Helper()
|
|
if p.cmd.Process == nil {
|
|
return
|
|
}
|
|
_ = p.cmd.Process.Signal(syscall.SIGTERM)
|
|
done := make(chan struct{})
|
|
go func() { _ = p.cmd.Wait(); close(done) }()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(10 * time.Second):
|
|
_ = p.cmd.Process.Kill()
|
|
<-done
|
|
}
|
|
}
|
|
|
|
// contract is the combined consumer surface a daemon/gateway/agent mixes: the
|
|
// task lifecycle + events surface (storecontract.TaskStore) and the capture surface.
|
|
// Mapping/binding/dispatch is exercised separately in
|
|
// TestStateStoreRealProcessRemoteSurfaces via the concrete Client, which
|
|
// asserts against each narrow interface at compile time.
|
|
type contract interface {
|
|
storecontract.TaskStore
|
|
storecontract.CaptureStore
|
|
}
|
|
|
|
// dial opens a gRPC connection to the state store process and wraps it in the
|
|
// remote contract adapter, exactly as the daemon/gateway/agent consumer does
|
|
// (internal/infrastructure/staterpc.Client).
|
|
func dial(t *testing.T, addr string) contract {
|
|
t.Helper()
|
|
conn, err := grpc.NewClient("passthrough:///"+addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
if err != nil {
|
|
t.Fatalf("dial %s: %v", addr, err)
|
|
}
|
|
t.Cleanup(func() { _ = conn.Close() })
|
|
return staterpc.NewClient(conn)
|
|
}
|
|
|
|
// capture builds a CapturedEvent without pulling the whole store field set
|
|
// into every call site.
|
|
func capture(source, body string) storecontract.CapturedEvent {
|
|
return storecontract.CapturedEvent{Source: source, Body: body, Authenticated: true}
|
|
}
|
|
|
|
// TestStateStoreRealProcessSmoke exercises the actual archie-state-store
|
|
// binary end-to-end: the combined task/event/capture view through one consumer
|
|
// adapter, error-sentinel fidelity across the wire, the not-found-as-(nil,nil)
|
|
// convention, and single-owner SQLite (the binary owns the one archie.db file
|
|
// and the consumer dials gRPC without opening any store file).
|
|
//
|
|
// It also takes its listen address from [services.state].listen rather than
|
|
// -listen, so the startup path every deployment uses is covered by the process
|
|
// that exercises the most: before that key existed the address came from a flag
|
|
// default with no config key, so a host whose port was already taken could
|
|
// only be retargeted by passing -listen and
|
|
// hand-writing an overlay whose target matched -- and the failure read as a
|
|
// code fault, "address already in use" in the store and a gRPC handshake error
|
|
// in every client.
|
|
func TestStateStoreRealProcessSmoke(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("real-process smoke test builds and execs the binary; skip under -short")
|
|
}
|
|
dir := t.TempDir()
|
|
bin := buildBinary(t, dir)
|
|
cfg := writeConfigWithServiceListen(t, dir)
|
|
st := startStateStore(t, bin, cfg, "", "")
|
|
if st.addr == "" {
|
|
t.Fatalf("no bound gRPC address in the log:\n%s", st.log.String())
|
|
}
|
|
if !strings.Contains(st.log.String(), `"listen":"127.0.0.1:0"`) {
|
|
t.Fatalf("the resolved listen was not the configured address:\n%s", st.log.String())
|
|
}
|
|
cl := dial(t, st.addr)
|
|
|
|
ctx := t.Context()
|
|
|
|
// Combined task/event/capture view: a task, an event on that task, and an
|
|
// unrelated capture written through one consumer, read back across all
|
|
// three surfaces in a single pass (the dashboard/observability read path).
|
|
task, err := cl.EnqueueChatTask(ctx, "acme", "widget", "verify state store", "body", "implement", "")
|
|
if err != nil || task == nil {
|
|
t.Fatalf("EnqueueChatTask = %+v, %v", task, err)
|
|
}
|
|
if err := cl.Transition(ctx, task.ID, task.Status, "running", "smoke started"); err != nil {
|
|
t.Fatalf("Transition: %v", err)
|
|
}
|
|
if _, err := cl.InsertEvent(ctx, events.Event{Kind: "stage_finish", TaskID: task.ID, Attempt: 1, Data: map[string]any{"duration_ms": 12.0}}); err != nil {
|
|
t.Fatalf("InsertEvent: %v", err)
|
|
}
|
|
// R4's document is stored as an event, so the real process must round-trip
|
|
// it with its nested structure and its attempt key intact: the standalone
|
|
// State Store owns the only copy, and a lost field here is a Config tab
|
|
// that reads a run it cannot describe. The key is the daemon's own
|
|
// (internal/daemon/daemon.go writes "document"), asserted by name below.
|
|
if _, err := cl.InsertEvent(ctx, events.Event{
|
|
Kind: events.KindConfigCaptured, TaskID: task.ID, Attempt: 1,
|
|
Data: map[string]any{
|
|
"schema": events.ConfigCapturedSchema,
|
|
"document": map[string]any{"bot_user": "archie", "models": map[string]any{"implement": "anthropic/claude"}},
|
|
},
|
|
}); err != nil {
|
|
t.Fatalf("InsertEvent config_captured: %v", err)
|
|
}
|
|
if capID, err := cl.InsertCapture(ctx, capture("sentry", `{"id":1}`), 0, 0); err != nil || capID == "" {
|
|
t.Fatalf("InsertCapture = (%q, %v)", capID, err)
|
|
}
|
|
|
|
got, err := cl.TaskByID(ctx, task.ID)
|
|
if err != nil || got == nil || got.ID != task.ID || got.Status != "running" {
|
|
t.Fatalf("TaskByID after transition = %+v, %v", got, err)
|
|
}
|
|
evs, err := cl.TaskEvents(ctx, task.ID)
|
|
if err != nil || len(evs) != 2 || evs[0].Kind != "stage_finish" {
|
|
t.Fatalf("TaskEvents = %+v, %v", evs, err)
|
|
}
|
|
if evs[0].Attempt != 1 {
|
|
t.Errorf("TaskEvents attempt = %d, want 1 across the real process", evs[0].Attempt)
|
|
}
|
|
if evs[1].Kind != events.KindConfigCaptured || evs[1].Attempt != 1 || evs[1].Data["schema"] != events.ConfigCapturedSchema {
|
|
t.Errorf("config_captured event = %+v, want its kind, attempt and schema preserved", evs[1])
|
|
}
|
|
if _, renamed := evs[1].Data["config"]; renamed {
|
|
t.Error(`the document crossed under "config"; the producer writes it under "document"`)
|
|
}
|
|
if len(evs[1].Data) != 2 {
|
|
t.Errorf("config_captured data keys = %v, want exactly schema and document", evs[1].Data)
|
|
}
|
|
if doc, ok := evs[1].Data["document"].(map[string]any); !ok || doc["bot_user"] != "archie" {
|
|
t.Errorf("config_captured payload = %#v, want the decoded document under its own key", evs[1].Data)
|
|
}
|
|
if caps, err := cl.ListCaptures(ctx, 10); err != nil || len(caps) != 1 || caps[0].Source != "sentry" {
|
|
t.Fatalf("ListCaptures = %+v, %v", caps, err)
|
|
}
|
|
if counts, err := cl.StatusCounts(ctx); err != nil || counts["running"] != 1 {
|
|
t.Fatalf("StatusCounts = %+v, %v", counts, err)
|
|
}
|
|
|
|
t.Run("error_sentinel_fidelity", func(t *testing.T) {
|
|
// A stale transition must rehydrate to storecontract.ErrStaleTransition across
|
|
// the wire (§7). Because the binary is up and we dialed it, a sentinel
|
|
// mismatch here would surface as a transport error, so a pass proves
|
|
// the gRPC mapError/unmapError round-trip, not just the in-process
|
|
// adapter.
|
|
err := cl.Transition(ctx, task.ID, "queued", "merged", "")
|
|
if !errors.Is(err, storecontract.ErrStaleTransition) {
|
|
t.Fatalf("stale Transition = %v, want storecontract.ErrStaleTransition", err)
|
|
}
|
|
})
|
|
|
|
t.Run("not_found_is_not_error", func(t *testing.T) {
|
|
// Reading a missing task returns (nil, nil), never a NotFound gRPC
|
|
// error (§7's found=false-not-error convention).
|
|
missing, err := cl.TaskByID(ctx, task.ID+999999)
|
|
if err != nil || missing != nil {
|
|
t.Fatalf("TaskByID missing = (%+v, %v), want (nil, nil)", missing, err)
|
|
}
|
|
})
|
|
|
|
t.Run("serves_from_postgres_only", func(t *testing.T) {
|
|
// The binary serves every store from database_url: no SQLite file
|
|
// appears in its directory.
|
|
for _, name := range []string{"archie.db", "archie.db-tasks.sqlite", "archie.db-eda.sqlite"} {
|
|
if _, err := os.Stat(filepath.Join(dir, name)); err == nil {
|
|
t.Fatalf("state store created %s; it serves from Postgres only", name)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestStateStoreRealProcessDeadline verifies the per-call deadline behaviour
|
|
// the state store contract requires (§6 StoreTimeout): a consumer context that
|
|
// is cancelled before the call completes must surface a deadline/cancel error
|
|
// rather than hang or silently succeed.
|
|
func TestStateStoreRealProcessDeadline(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("real-process smoke test builds and execs the binary; skip under -short")
|
|
}
|
|
dir := t.TempDir()
|
|
bin := buildBinary(t, dir)
|
|
cfg := writeMinimalConfig(t, dir)
|
|
st := startStateStoreProcess(t, bin, cfg)
|
|
cl := dial(t, st.addr)
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
cancel()
|
|
_, err := cl.StatusCounts(ctx)
|
|
if err == nil {
|
|
t.Fatal("StatusCounts on a cancelled context returned nil; expected a deadline/cancel error")
|
|
}
|
|
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("StatusCounts cancelled = %v, want context.Canceled or DeadlineExceeded", err)
|
|
}
|
|
}
|
|
|
|
// TestStateStoreRealProcessRestartRecovery starts the process, writes a task,
|
|
// stops it (SIGTERM), restarts it on the same config, and verifies the
|
|
// task and its events were recovered from the on-disk SQLite -- the
|
|
// restart/recovery path the runbook relies on when archie-state-store is
|
|
// restarted as a service.
|
|
func TestStateStoreRealProcessRestartRecovery(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("real-process smoke test builds and execs the binary; skip under -short")
|
|
}
|
|
dir := t.TempDir()
|
|
bin := buildBinary(t, dir)
|
|
cfg := writeMinimalConfig(t, dir)
|
|
|
|
first := startStateStoreProcess(t, bin, cfg)
|
|
s1 := dial(t, first.addr)
|
|
task, err := s1.EnqueueChatTask(t.Context(), "acme", "widget", "survive restart", "body", "implement", "")
|
|
if err != nil || task == nil {
|
|
t.Fatalf("first EnqueueChatTask = %+v, %v", task, err)
|
|
}
|
|
if _, err := s1.InsertEvent(t.Context(), events.Event{Kind: "run_started", TaskID: task.ID}); err != nil {
|
|
t.Fatalf("first InsertEvent: %v", err)
|
|
}
|
|
first.stop(t)
|
|
|
|
// Restart on the same config; the SQLite file persists on disk.
|
|
second := startStateStoreProcess(t, bin, cfg)
|
|
s2 := dial(t, second.addr)
|
|
|
|
got, err := s2.TaskByID(t.Context(), task.ID)
|
|
if err != nil || got == nil || got.Title != "survive restart" {
|
|
t.Fatalf("TaskByID after restart = %+v, %v", got, err)
|
|
}
|
|
if evs, err := s2.TaskEvents(t.Context(), task.ID); err != nil || len(evs) != 1 {
|
|
t.Fatalf("TaskEvents after restart = %+v, %v", evs, err)
|
|
}
|
|
// Stale-running work reopened on a fresh process is recovered by
|
|
// RecoverStale, which must return without error on the restarted store.
|
|
if _, err := s2.RecoverStale(t.Context()); err != nil {
|
|
t.Fatalf("RecoverStale after restart: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestStateStoreRealProcessTokenProtectedCaptureStream is the capture-batch
|
|
// regression against the real binary: the token-protected, non-loopback
|
|
// listener topology a container-mode deployment uses, read through the
|
|
// production client wiring (staterpc.Dial) the daemon and the dashboard both
|
|
// use. The reads the batch needs are server-streaming RPCs, whose metadata
|
|
// the unary-only client interceptor never saw -- so before the fix this
|
|
// failed with PermissionDenied, and no unary fallback existed to take. 20 x
|
|
// 256 KiB is 5 MiB, past gRPC's 4 MiB unary cap, so the batch also proves the
|
|
// streams still carry it.
|
|
func TestStateStoreRealProcessTokenProtectedCaptureStream(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("real-process smoke test builds and execs the binary; skip under -short")
|
|
}
|
|
const token = "operator-secret-token"
|
|
dir := t.TempDir()
|
|
bin := buildBinary(t, dir)
|
|
cfg := writeMinimalConfig(t, dir)
|
|
st := startStateStore(t, bin, cfg, "0.0.0.0:0", token)
|
|
|
|
// The listener advertises the wildcard address it bound; a consumer dials
|
|
// the host address it can reach, which is this host's loopback.
|
|
_, port, err := net.SplitHostPort(st.addr)
|
|
if err != nil {
|
|
t.Fatalf("parse bound address %q: %v", st.addr, err)
|
|
}
|
|
cl, cleanup, err := staterpc.Dial("127.0.0.1:"+port, token)
|
|
if err != nil {
|
|
t.Fatalf("dial token-protected state store: %v", err)
|
|
}
|
|
t.Cleanup(cleanup)
|
|
|
|
ctx := t.Context()
|
|
body := strings.Repeat("x", 256<<10)
|
|
for range 20 {
|
|
if _, err := cl.InsertCapture(ctx, capture("large", body), 0, 0); err != nil {
|
|
t.Fatalf("InsertCapture: %v", err)
|
|
}
|
|
}
|
|
captures, err := cl.ListCaptures(ctx, 20)
|
|
if err != nil {
|
|
t.Fatalf("ListCaptures over a token-protected listener: %v", err)
|
|
}
|
|
if len(captures) != 20 {
|
|
t.Fatalf("got %d captures, want 20", len(captures))
|
|
}
|
|
for _, c := range captures {
|
|
if c.Body != body {
|
|
t.Fatal("capture body truncated")
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestStateStoreRealProcessRemoteSurfaces exercises the capture/mapping/
|
|
// binding surfaces the standalone process fronts (the daemon/webui surfaces
|
|
// swapped to the adapter in .4.4/.4.5), proving one process serves the full
|
|
// combined contract a consumer mixes.
|
|
func TestStateStoreRealProcessRemoteSurfaces(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("real-process smoke test builds and execs the binary; skip under -short")
|
|
}
|
|
dir := t.TempDir()
|
|
bin := buildBinary(t, dir)
|
|
cfg := writeMinimalConfig(t, dir)
|
|
st := startStateStoreProcess(t, bin, cfg)
|
|
|
|
conn, err := grpc.NewClient("passthrough:///"+st.addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
if err != nil {
|
|
t.Fatalf("dial: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = conn.Close() })
|
|
cl := staterpc.NewClient(conn)
|
|
|
|
ctx := t.Context()
|
|
mappingID, err := cl.InsertMapping(ctx, mapping.Mapping{Name: "m1", Fields: []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}}})
|
|
if err != nil || mappingID == "" {
|
|
t.Fatalf("InsertMapping = (%q, %v)", mappingID, err)
|
|
}
|
|
if got, err := cl.GetMapping(ctx, mappingID); err != nil || got == nil || got.Name != "m1" {
|
|
t.Fatalf("GetMapping = %+v, %v", got, err)
|
|
}
|
|
bindingID, err := cl.InsertBinding(ctx, binding.Binding{
|
|
Name: "b1", Matcher: binding.Matcher{Source: "sentry"}, MappingID: mappingID,
|
|
Workflow: "implement",
|
|
})
|
|
if err != nil || bindingID == "" {
|
|
t.Fatalf("InsertBinding = (%q, %v)", bindingID, err)
|
|
}
|
|
// A binding starts as draft and must be edited (draft -> pending_approval)
|
|
// then approved (pending_approval -> armed) before it is armed for a
|
|
// source. Drive the state machine so ArmedBindingsForSource returns it.
|
|
if err := cl.UpdateBinding(ctx, binding.Binding{
|
|
ID: bindingID, Name: "b1", Matcher: binding.Matcher{Source: "sentry"},
|
|
MappingID: mappingID, Workflow: "implement", Owner: "acme", Repo: "widget",
|
|
}); err != nil {
|
|
t.Fatalf("UpdateBinding: %v", err)
|
|
}
|
|
if err := cl.ApproveBinding(ctx, bindingID); err != nil {
|
|
t.Fatalf("ApproveBinding: %v", err)
|
|
}
|
|
if armed, err := cl.ArmedBindingsForSource(ctx, "sentry"); err != nil || len(armed) != 1 || armed[0].ID != bindingID {
|
|
t.Fatalf("ArmedBindingsForSource = %+v, %v", armed, err)
|
|
}
|
|
// Remote surfaces share error-sentinel fidelity on the wire too.
|
|
missing := mapping.Mapping{ID: "rabsent00000000", Name: "x", Fields: []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}}}
|
|
if err := cl.UpdateMapping(ctx, missing); !errors.Is(err, storecontract.ErrMappingNotFound) {
|
|
t.Fatalf("UpdateMapping missing = %v, want ErrMappingNotFound", err)
|
|
}
|
|
}
|