test(postgres): remove database-backed tests and the testcontainers harness
This commit is contained in:
1 parent
c2a649ded4
commit
ce5fc3e226
24 files changed
-2357
No files matched your search
@@ -1,226 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eventtype"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// A capture a binding evaluated without dispatching is recorded once and not
|
||||
// offered to that binding again; a later dispatch of the pair is refused. A
|
||||
// dispatch records the task it started, and the ledger lists by any end.
|
||||
func TestEvaluatedCaptureIsNotReoffered(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
eda := pgstore.EDA(t, nil)
|
||||
|
||||
eventTypeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: "forge", Name: "push"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: "push", EventTypeID: eventTypeID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bindingID, err := eda.InsertBinding(ctx, binding.Binding{
|
||||
Name: "on-push", Matcher: binding.Matcher{Source: "forge"}, MappingID: mappingID, Workflow: "tdd",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.ApproveBinding(ctx, bindingID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
captureID, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{
|
||||
Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(),
|
||||
}, time.Hour, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assertUndispatched(t, eda, 1)
|
||||
if err := eda.RecordDispatch(ctx, bindingID, 1, captureID, 0, "filtered"); err != nil {
|
||||
t.Fatalf("record filtered evaluation: %v", err)
|
||||
}
|
||||
assertUndispatched(t, eda, 0)
|
||||
if err := eda.RecordDispatch(ctx, bindingID, 1, captureID, 0, ""); !errors.Is(err, storecontract.ErrAlreadyDispatched) {
|
||||
t.Fatalf("dispatch after a filtered evaluation = %v, want ErrAlreadyDispatched", err)
|
||||
}
|
||||
|
||||
// A second capture dispatches; the ledger links it to the task it started.
|
||||
dispatched, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{
|
||||
Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(),
|
||||
}, time.Hour, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.RecordDispatch(ctx, bindingID, 1, dispatched, 0, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.SetDispatchTask(ctx, bindingID, dispatched, 42); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows, err := eda.ListDispatches(ctx, storecontract.DispatchFilter{TaskID: 42, Limit: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rows) != 1 || rows[0].CaptureID != dispatched || rows[0].BindingID != bindingID {
|
||||
t.Fatalf("dispatches for task 42 = %+v, want the dispatched capture", rows)
|
||||
}
|
||||
all, err := eda.ListDispatches(ctx, storecontract.DispatchFilter{BindingID: bindingID, Limit: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(all) != 2 {
|
||||
t.Fatalf("dispatches for the binding = %d, want 2", len(all))
|
||||
}
|
||||
}
|
||||
|
||||
// A sender's retry of one delivery is captured again but dispatches once,
|
||||
// even when both copies are offered before either is claimed.
|
||||
func TestRedeliveryDispatchesOnce(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
eda := pgstore.EDA(t, nil)
|
||||
eventTypeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: "forge", Name: "push"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: "push", EventTypeID: eventTypeID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bindingID, err := eda.InsertBinding(ctx, binding.Binding{
|
||||
Name: "on-push", Matcher: binding.Matcher{Source: "forge"}, MappingID: mappingID, Workflow: "tdd",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.ApproveBinding(ctx, bindingID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
capture := func(delivery string) string {
|
||||
id, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{
|
||||
Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(), Delivery: delivery,
|
||||
}, time.Hour, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
original, retry := capture("delivery-1"), capture("delivery-1")
|
||||
assertUndispatched(t, eda, 2)
|
||||
if err := eda.RecordDispatch(ctx, bindingID, 1, original, 0, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.RecordDispatch(ctx, bindingID, 1, retry, 0, ""); !errors.Is(err, storecontract.ErrAlreadyDispatched) {
|
||||
t.Fatalf("dispatch of the retry = %v, want ErrAlreadyDispatched", err)
|
||||
}
|
||||
assertUndispatched(t, eda, 0)
|
||||
|
||||
capture("delivery-2")
|
||||
capture("")
|
||||
assertUndispatched(t, eda, 2)
|
||||
captures, err := eda.ListCaptures(ctx, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(captures) != 4 {
|
||||
t.Fatalf("captures = %d, want 4: a retry is still recorded", len(captures))
|
||||
}
|
||||
}
|
||||
|
||||
func assertUndispatched(t *testing.T, eda *postgres.EDA, want int) {
|
||||
t.Helper()
|
||||
got, err := eda.ListUndispatchedCaptures(t.Context(), []string{"forge"}, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != want {
|
||||
t.Fatalf("undispatched captures = %d, want %d", len(got), want)
|
||||
}
|
||||
}
|
||||
|
||||
// A binding moves pending -> armed -> paused -> pending, and refuses any other
|
||||
// transition; a paused binding is offered no captures.
|
||||
func TestBindingLifecycle(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
eda := pgstore.EDA(t, nil)
|
||||
eventTypeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: "forge", Name: "push"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: "push", EventTypeID: eventTypeID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id, err := eda.InsertBinding(ctx, binding.Binding{
|
||||
Name: "on-push", Matcher: binding.Matcher{Source: "forge"}, MappingID: mappingID, Workflow: "tdd",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{
|
||||
Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(),
|
||||
}, time.Hour, 100); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
steps := []struct {
|
||||
name string
|
||||
apply func(context.Context, string) error
|
||||
wantErr error
|
||||
status binding.Status
|
||||
offered int
|
||||
}{
|
||||
{"a pending binding cannot pause", eda.PauseBinding, storecontract.ErrBindingTransition, binding.StatusPendingApproval, 0},
|
||||
{"approve arms it", eda.ApproveBinding, nil, binding.StatusArmed, 1},
|
||||
{"an armed binding cannot resume", eda.ResumeBinding, storecontract.ErrBindingTransition, binding.StatusArmed, 1},
|
||||
{"pause stops it", eda.PauseBinding, nil, binding.StatusPaused, 0},
|
||||
{"a paused binding cannot be approved", eda.ApproveBinding, storecontract.ErrBindingTransition, binding.StatusPaused, 0},
|
||||
{"resume returns it to approval", eda.ResumeBinding, nil, binding.StatusPendingApproval, 0},
|
||||
}
|
||||
for _, step := range steps {
|
||||
if err := step.apply(ctx, id); !errors.Is(err, step.wantErr) {
|
||||
t.Fatalf("%s: err = %v, want %v", step.name, err, step.wantErr)
|
||||
}
|
||||
b, err := eda.GetBinding(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b.Status != step.status {
|
||||
t.Fatalf("%s: status = %q, want %q", step.name, b.Status, step.status)
|
||||
}
|
||||
assertUndispatched(t, eda, step.offered)
|
||||
}
|
||||
if err := eda.PauseBinding(ctx, "missing"); !errors.Is(err, storecontract.ErrBindingNotFound) {
|
||||
t.Fatalf("pause of a missing binding = %v, want ErrBindingNotFound", err)
|
||||
}
|
||||
|
||||
// A source is not deleted while an armed binding fires on it.
|
||||
if err := eda.InsertSource(ctx, source.Source{Path: "forge", Signing: source.SigningSigned}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.ApproveBinding(ctx, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.DeleteSource(ctx, "forge"); !errors.Is(err, storecontract.ErrSourceInUse) {
|
||||
t.Fatalf("delete of a source an armed binding fires on = %v, want ErrSourceInUse", err)
|
||||
}
|
||||
if err := eda.PauseBinding(ctx, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.DeleteSource(ctx, "forge"); err != nil {
|
||||
t.Fatalf("delete of a source with no armed binding = %v", err)
|
||||
}
|
||||
if err := eda.DeleteSource(ctx, "forge"); !errors.Is(err, storecontract.ErrSourceNotFound) {
|
||||
t.Fatalf("second delete = %v, want ErrSourceNotFound", err)
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// ListCaptures returns only the acting org's captures; a caller with no org
|
||||
// in context keeps the default org's.
|
||||
func TestListCapturesIsOrgScoped(t *testing.T) {
|
||||
pool := pgstore.Pool(t)
|
||||
eda := postgres.NewEDA(pool, nil)
|
||||
ctx := t.Context()
|
||||
|
||||
insert := func(source string) string {
|
||||
id, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{Source: source, Body: "{}"}, time.Hour, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
sysID, acmeID := insert("sys-hook"), insert("acme-hook")
|
||||
// Captures take their org from the source, which current code always
|
||||
// writes to the default org, so reassign the fixture directly.
|
||||
if _, err := pool.Exec(ctx, "UPDATE captures SET org_id=$1 WHERE id=$2", "acme", acmeID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
orgID org.OrgID
|
||||
withOrg bool
|
||||
want string
|
||||
}{
|
||||
{"no org keeps the default org", "", false, sysID},
|
||||
{"default org lists only its own", org.DefaultOrgID, true, sysID},
|
||||
{"other org lists only its own", "acme", true, acmeID},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
listCtx := ctx
|
||||
if tc.withOrg {
|
||||
listCtx = org.WithOrg(ctx, tc.orgID)
|
||||
}
|
||||
got, err := eda.ListCaptures(listCtx, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 1 || got[0].ID != tc.want {
|
||||
t.Fatalf("captures = %#v, want only %s", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// Count retention keeps the newest N captures within the just-inserted
|
||||
// capture's org, so one org's volume never evicts another org's captures.
|
||||
func TestCaptureCountPruneStaysInOrg(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
eda := postgres.NewEDA(db.Pool, nil)
|
||||
for _, owned := range []struct{ path, org string }{{"prune-a", "org-a"}, {"prune-b", "org-b"}} {
|
||||
if err := eda.InsertSource(ctx, source.Source{Path: owned.path, Signing: source.SigningUnsigned}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Source ownership is set by another seam; write it directly.
|
||||
if _, err := db.Pool.Exec(ctx, "UPDATE sources SET org_id=$1 WHERE path=$2", owned.org, owned.path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
base := time.Now().UTC().Truncate(time.Second)
|
||||
insert := func(path string, at time.Time) string {
|
||||
id, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{Source: path, Body: "{}", ReceivedAt: at}, 0, 3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
// Quiet org first: its captures must survive the loud org's volume.
|
||||
for i := range 2 {
|
||||
insert("prune-b", base.Add(time.Duration(i)*time.Second))
|
||||
}
|
||||
var loud []string
|
||||
for i := range 5 {
|
||||
loud = append(loud, insert("prune-a", base.Add(time.Duration(10+i)*time.Second)))
|
||||
}
|
||||
count := func(owner string) int {
|
||||
var n int
|
||||
if err := db.Pool.QueryRow(ctx, "SELECT count(*) FROM captures WHERE org_id=$1", owner).Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
present := func(id string) bool {
|
||||
var n int
|
||||
if err := db.Pool.QueryRow(ctx, "SELECT count(*) FROM captures WHERE id=$1", id).Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return n == 1
|
||||
}
|
||||
if got := count("org-b"); got != 2 {
|
||||
t.Fatalf("quiet org kept %d captures, want 2", got)
|
||||
}
|
||||
if got := count("org-a"); got != 3 {
|
||||
t.Fatalf("loud org kept %d captures, want 3", got)
|
||||
}
|
||||
for _, id := range loud[:2] {
|
||||
if present(id) {
|
||||
t.Fatalf("loud org's oldest capture %s survived past the cap", id)
|
||||
}
|
||||
}
|
||||
for _, id := range loud[2:] {
|
||||
if !present(id) {
|
||||
t.Fatalf("loud org's newest capture %s missing", id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eventtype"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/captureintake"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
)
|
||||
|
||||
func TestPendingDispatchInCallerOrg(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
if err := db.BootstrapIdentities(ctx, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.UpgradeDefaultOrg(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, who := range []identity.Identity{{ID: "acme-owner", Kind: identity.KindUser, DisplayName: "Owner", Lifecycle: identity.LifecycleActive}, {ID: "acme-bot", Kind: identity.KindBot, DisplayName: "bot", Lifecycle: identity.LifecycleActive}} {
|
||||
if _, err := db.Create(ctx, who, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: string(who.ID)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := db.CreateOrg(ctx, org.Org{ID: "acme", Name: "Acme"}, "acme-owner"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AssignAgent(ctx, org.AgentAssignment{IdentityID: "acme-bot", OrgID: "acme"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
eda := postgres.NewEDA(db.Pool, nil)
|
||||
client := captureRPC(t, eda, db)
|
||||
receiver := &captureintake.Receiver{Captures: client, Sources: client, Retention: time.Hour, MaxEvents: 100}
|
||||
mux := http.NewServeMux()
|
||||
receiver.Register(mux)
|
||||
ids := map[org.OrgID]string{}
|
||||
bindings := map[org.OrgID]string{}
|
||||
paths := []string{}
|
||||
for _, owner := range []org.OrgID{org.DefaultOrgID, "acme"} {
|
||||
path := "webhook-" + string(owner)
|
||||
paths = append(paths, path)
|
||||
if err := eda.InsertSource(org.WithOrg(ctx, owner), source.Source{Path: path, Signing: source.SigningUnsigned}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Workspace selection is not yet a write path, so only the workspace
|
||||
// needs a fixture fixup; the source org comes from the insert itself.
|
||||
if _, err := db.Pool.Exec(ctx, "UPDATE sources SET workspace_id='incoming' WHERE path=$1", path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
typeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: path, Name: string(owner), Rule: eventtype.Rule{Headers: []eventtype.HeaderCondition{{Name: "X-Org", Value: string(owner)}}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: string(owner), EventTypeID: typeID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bindingID, err := eda.InsertBinding(ctx, binding.Binding{Name: string(owner), Matcher: binding.Matcher{Source: path}, MappingID: mappingID, Workflow: "deploy"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.ApproveBinding(ctx, bindingID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Catalogue writes still default to the system org; capture ownership
|
||||
// must come from the persisted source, never a caller-supplied org.
|
||||
for _, record := range []struct{ table, id string }{{"event_types", typeID}, {"mappings", mappingID}, {"bindings", bindingID}} {
|
||||
if _, err := db.Pool.Exec(ctx, "UPDATE "+record.table+" SET org_id=$1 WHERE id=$2", owner, record.id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
request := httptest.NewRequestWithContext(ctx, http.MethodPost, "/webhooks/capture/"+path, strings.NewReader("{}"))
|
||||
request.Header.Set("X-Org", string(owner))
|
||||
response := httptest.NewRecorder()
|
||||
mux.ServeHTTP(response, request)
|
||||
if response.Code != http.StatusAccepted {
|
||||
t.Fatalf("capture: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
var captureID, captureOrg, workspace string
|
||||
if err := db.Pool.QueryRow(ctx, "SELECT id, org_id, workspace_id FROM captures WHERE source=$1", path).Scan(&captureID, &captureOrg, &workspace); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if captureOrg != string(owner) || workspace != "incoming" {
|
||||
t.Fatalf("capture org %s workspace %s, want %s/incoming", captureOrg, workspace, owner)
|
||||
}
|
||||
ids[owner] = captureID
|
||||
bindings[owner] = bindingID
|
||||
}
|
||||
for _, owner := range []org.OrgID{org.DefaultOrgID, "acme"} {
|
||||
t.Run(string(owner), func(t *testing.T) {
|
||||
captures, err := eda.ListUndispatchedCaptures(org.WithOrg(t.Context(), owner), paths, 1)
|
||||
if err != nil || len(captures) != 1 || captures[0].ID != ids[owner] {
|
||||
t.Fatalf("org %s got captures %#v err %v", owner, captures, err)
|
||||
}
|
||||
bindingID := bindings[owner]
|
||||
if err := client.RecordDispatch(t.Context(), bindingID, 1, ids[owner], 0, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
taskIdentity := ""
|
||||
if owner == "acme" {
|
||||
taskIdentity = "acme-bot"
|
||||
}
|
||||
run, err := client.EnqueueBindingTask(t.Context(), "", "", "Org dispatch", "", "deploy", taskIdentity, bindingID, 1, nil)
|
||||
if err != nil || run.Org != owner {
|
||||
t.Fatalf("binding task %#v err %v", run, err)
|
||||
}
|
||||
if err := client.SetDispatchTask(t.Context(), bindingID, ids[owner], run.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pending, err := eda.ListUndispatchedCaptures(org.WithOrg(t.Context(), owner), paths, 1)
|
||||
if err != nil || len(pending) != 0 {
|
||||
t.Fatalf("dispatched capture still pending: %#v err %v", pending, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
t.Run("unknown source", func(t *testing.T) {
|
||||
id, err := client.InsertCapture(org.WithOrg(t.Context(), "acme"), storecontract.CapturedEvent{Source: "unknown", Body: "{}"}, time.Hour, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var owner, workspace string
|
||||
if err := db.Pool.QueryRow(t.Context(), "SELECT org_id,workspace_id FROM captures WHERE id=$1", id).Scan(&owner, &workspace); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if owner != string(org.DefaultOrgID) || workspace != string(org.DefaultWorkspaceID) {
|
||||
t.Fatalf("unknown source owner %s/%s", owner, workspace)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func captureRPC(t *testing.T, eda *postgres.EDA, db *pgstore.TaskDB) *staterpc.Client {
|
||||
t.Helper()
|
||||
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = listener.Close() })
|
||||
grants := &staterpc.TaskGrants{}
|
||||
srv := grpc.NewServer(grpc.ChainUnaryInterceptor(grants.UnaryInterceptor("test-token")), grpc.ChainStreamInterceptor(grants.StreamInterceptor("test-token")))
|
||||
staterpc.RegisterServer(srv, staterpc.Deps{Tasks: db, Captures: eda, Sources: eda, BindingDispatcher: eda, BindingTaskCreator: db})
|
||||
t.Cleanup(srv.Stop)
|
||||
go func() { _ = srv.Serve(listener) }()
|
||||
client, closeClient, err := staterpc.Dial(listener.Addr().String(), "archied", "test-token")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(closeClient)
|
||||
return client
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eventtype"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// An org's sources, event types, mappings and bindings are invisible to a
|
||||
// request scoped to another org, which can neither change nor delete them.
|
||||
// An unscoped internal service still sees them.
|
||||
func TestEDARecordsStayInTheirOrg(t *testing.T) {
|
||||
scoped := func(id org.OrgID) context.Context {
|
||||
return org.WithScope(org.WithOrg(t.Context(), id), id)
|
||||
}
|
||||
acme := scoped("acme")
|
||||
eda := postgres.NewEDA(pgstore.Pool(t), nil)
|
||||
if err := eda.InsertSource(acme, source.Source{Path: "acme-hook", Signing: source.SigningUnsigned}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
typeID, err := eda.InsertEventType(acme, eventtype.EventType{Source: "acme-hook", Name: "push"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mappingID, err := eda.InsertMapping(acme, mapping.Mapping{Name: "push", EventTypeID: typeID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bindingID, err := eda.InsertBinding(acme, binding.Binding{Name: "deploy", Matcher: binding.Matcher{Source: "acme-hook"}, MappingID: mappingID, Workflow: "deploy"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
ctx func() context.Context //nolint:containedctx // table input, not stored state
|
||||
sees bool
|
||||
}{
|
||||
{"the owning org", func() context.Context { return acme }, true},
|
||||
{"another org", func() context.Context { return scoped("other") }, false},
|
||||
{"an internal service", t.Context, true},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := tt.ctx()
|
||||
sources, err := eda.ListSources(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src, err := eda.GetSource(ctx, "acme-hook")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
types, err := eda.ListEventTypes(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mappings, err := eda.ListMappings(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m, err := eda.GetMapping(ctx, mappingID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bindings, err := eda.ListBindings(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := eda.GetBinding(ctx, bindingID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen := []bool{len(sources) == 1, src != nil, len(types) == 1, len(mappings) == 1, m != nil, len(bindings) == 1, b != nil}
|
||||
for i, s := range seen {
|
||||
if s != tt.sees {
|
||||
t.Fatalf("read %d visible = %v, want %v", i, s, tt.sees)
|
||||
}
|
||||
}
|
||||
if tt.sees {
|
||||
return
|
||||
}
|
||||
if err := eda.ApproveBinding(ctx, bindingID); err == nil {
|
||||
t.Fatal("approved another org's binding")
|
||||
}
|
||||
if err := eda.SetSourceSecret(ctx, "acme-hook", "stolen"); err == nil {
|
||||
t.Fatal("rotated another org's source secret")
|
||||
}
|
||||
if err := eda.DeleteBinding(ctx, bindingID); err == nil {
|
||||
t.Fatal("deleted another org's binding")
|
||||
}
|
||||
if err := eda.DeleteMapping(ctx, mappingID); err == nil {
|
||||
t.Fatal("deleted another org's mapping")
|
||||
}
|
||||
if err := eda.DeleteEventType(ctx, typeID); err == nil {
|
||||
t.Fatal("deleted another org's event type")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,234 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/pressly/goose/v3"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
)
|
||||
|
||||
// orgSysTables are the tables whose org_id defaulted to 'default' before the
|
||||
// rename; the migration points them at 'org-sys'.
|
||||
var orgSysTables = []string{
|
||||
"bindings", "captures", "event_types", "events", "mappings",
|
||||
"resource_history", "resources", "sources", "step_executions", "tasks",
|
||||
"tool_calls", "transitions",
|
||||
}
|
||||
|
||||
// TestOrgSysMigrationMovesExistingDefaultRows exercises the rename on the path
|
||||
// a real install takes: a database at the previous schema with rows already
|
||||
// under the old org id.
|
||||
func TestOrgSysMigrationMovesExistingDefaultRows(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
pool, err := postgres.Open(ctx, pgtest.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
db := stdlib.OpenDBFromPool(pool)
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
provider, err := goose.NewProvider(goose.DialectPostgres, db, postgres.Migrations())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := provider.UpTo(ctx, 32); err != nil {
|
||||
t.Fatalf("migrate to 32: %v", err)
|
||||
}
|
||||
seedDefaultOrg(t, pool)
|
||||
if _, err := provider.UpTo(ctx, 33); err != nil {
|
||||
t.Fatalf("migrate to 33: %v", err)
|
||||
}
|
||||
|
||||
if name := orgName(t, pool, "org-sys"); name != "System" {
|
||||
t.Fatalf("org-sys name = %q, want System", name)
|
||||
}
|
||||
assertNoOrg(t, pool, "default")
|
||||
|
||||
tables := orgIDTables(t, pool)
|
||||
for _, name := range tables {
|
||||
var left int
|
||||
if err := pool.QueryRow(ctx, fmt.Sprintf("SELECT count(*) FROM %s WHERE org_id = 'default'", name)).Scan(&left); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if left != 0 {
|
||||
t.Fatalf("%s left %d rows in the old org", name, left)
|
||||
}
|
||||
}
|
||||
// The seeded rows are on the new org, so the move was not a delete.
|
||||
for _, name := range []string{
|
||||
"workspaces", "tasks", "events", "resources", "step_executions", "access_policies",
|
||||
"installed_packages", "installed_package_requirements", "installed_package_contributions",
|
||||
} {
|
||||
var moved int
|
||||
if err := pool.QueryRow(ctx, fmt.Sprintf("SELECT count(*) FROM %s WHERE org_id = 'org-sys'", name)).Scan(&moved); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if moved == 0 {
|
||||
t.Fatalf("%s moved no rows to org-sys", name)
|
||||
}
|
||||
}
|
||||
// The new default is the renamed org, on exactly the tables that had one.
|
||||
for _, name := range tables {
|
||||
var def sql.NullString
|
||||
if err := pool.QueryRow(ctx, `SELECT column_default FROM information_schema.columns WHERE table_name = $1 AND column_name = 'org_id'`, name).Scan(&def); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := slices.Contains(orgSysTables, name)
|
||||
got := def.Valid && strings.Contains(def.String, "'org-sys'")
|
||||
if got != want {
|
||||
t.Fatalf("%s org_id default = %q, want org-sys = %v", name, def.String, want)
|
||||
}
|
||||
}
|
||||
assertForeignKey(t, pool, "installed_package_requirements", "installed_package_requirements_org_id_required_name_fkey")
|
||||
assertForeignKey(t, pool, "installed_package_contributions", "installed_package_contributions_org_id_package_name_fkey")
|
||||
|
||||
// The down migration is the exact reverse.
|
||||
if _, err := provider.DownTo(ctx, 32); err != nil {
|
||||
t.Fatalf("migrate down to 32: %v", err)
|
||||
}
|
||||
if name := orgName(t, pool, "default"); name != "Default" {
|
||||
t.Fatalf("default org name = %q, want Default", name)
|
||||
}
|
||||
assertNoOrg(t, pool, "org-sys")
|
||||
}
|
||||
|
||||
// TestOrgOperatorUpgrade pins the operator phase: a fresh install and an
|
||||
// existing one each end with exactly one person owner of org-sys, and running
|
||||
// the upgrade again adds no second.
|
||||
func TestOrgOperatorUpgrade(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
|
||||
if err := db.UpgradeDefaultOrg(ctx); err != nil {
|
||||
t.Fatalf("fresh upgrade: %v", err)
|
||||
}
|
||||
assertOneOperator(t, db.Pool)
|
||||
if err := db.UpgradeDefaultOrg(ctx); err != nil {
|
||||
t.Fatalf("second upgrade: %v", err)
|
||||
}
|
||||
assertOneOperator(t, db.Pool)
|
||||
|
||||
// An existing install that ran the first two phases but never the operator
|
||||
// phase: drop the phase's ledger row and what it wrote, then upgrade again.
|
||||
if _, err := db.Pool.Exec(ctx, `DELETE FROM org_upgrades WHERE phase = 'operator'`); err != nil {
|
||||
t.Fatalf("reset operator phase: %v", err)
|
||||
}
|
||||
for _, sql := range []string{
|
||||
`DELETE FROM memberships WHERE identity_id = $1`,
|
||||
`DELETE FROM identities WHERE id = $1`,
|
||||
} {
|
||||
if _, err := db.Pool.Exec(ctx, sql, string(identity.OperatorID())); err != nil {
|
||||
t.Fatalf("reset operator phase: %v", err)
|
||||
}
|
||||
}
|
||||
if err := db.UpgradeDefaultOrg(ctx); err != nil {
|
||||
t.Fatalf("upgrade on an existing install: %v", err)
|
||||
}
|
||||
assertOneOperator(t, db.Pool)
|
||||
}
|
||||
|
||||
func assertOneOperator(t *testing.T, pool *pgxpool.Pool) {
|
||||
t.Helper()
|
||||
var owners int
|
||||
if err := pool.QueryRow(t.Context(), `
|
||||
SELECT count(*) FROM memberships m
|
||||
JOIN identities i ON i.id = m.identity_id
|
||||
WHERE m.org_id = 'org-sys' AND m.workspace_id IS NULL AND m.role = 'owner' AND i.kind = 'user'
|
||||
`).Scan(&owners); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if owners != 1 {
|
||||
t.Fatalf("person owners of org-sys = %d, want 1", owners)
|
||||
}
|
||||
var kind string
|
||||
if err := pool.QueryRow(t.Context(), `SELECT kind FROM identities WHERE id = $1`, string(identity.OperatorID())).Scan(&kind); err != nil {
|
||||
t.Fatalf("operator identity: %v", err)
|
||||
}
|
||||
if kind != string(identity.KindUser) {
|
||||
t.Fatalf("operator kind = %q, want %q", kind, identity.KindUser)
|
||||
}
|
||||
}
|
||||
|
||||
func seedDefaultOrg(t *testing.T, pool *pgxpool.Pool) {
|
||||
t.Helper()
|
||||
ctx := t.Context()
|
||||
statements := []string{
|
||||
`INSERT INTO orgs (id, name) VALUES ('default', 'Default')`,
|
||||
`INSERT INTO workspaces (id, org_id, name) VALUES ('default', 'default', 'Default')`,
|
||||
`INSERT INTO tasks (owner, repo, issue_number, identity, org_id, workspace_id) VALUES ('o', 'r', 1, 'sam', 'default', 'default')`,
|
||||
`INSERT INTO events (at, kind, org_id, workspace_id) VALUES (now(), 'task_started', 'default', 'default')`,
|
||||
`INSERT INTO resources (org_id, kind, value, version, updated_at) VALUES ('default', 'workflows', '\x00', 1, now())`,
|
||||
`INSERT INTO step_executions (org_id, execution_id, attempt, kind, name) VALUES ('default', 1, 1, 'stage', 'build')`,
|
||||
`INSERT INTO access_policies (level, org_id, policy_id, text) VALUES ('org', 'default', 'p', 'allow')`,
|
||||
`INSERT INTO installed_packages (org_id, name, reference, digest, descriptor, layer) VALUES ('default', 'pkg', 'ref', 'sha256:' || repeat('a', 64), '{}', '\x00')`,
|
||||
`INSERT INTO installed_packages (org_id, name, reference, digest, descriptor, layer) VALUES ('default', 'dep', 'ref', 'sha256:' || repeat('b', 64), '{}', '\x00')`,
|
||||
`INSERT INTO installed_package_requirements (org_id, package_name, required_name, required_digest) VALUES ('default', 'pkg', 'dep', 'sha256:' || repeat('b', 64))`,
|
||||
`INSERT INTO installed_package_contributions (org_id, package_name, family, entry_id) VALUES ('default', 'pkg', 'fam', 'e1')`,
|
||||
}
|
||||
for _, sql := range statements {
|
||||
if _, err := pool.Exec(ctx, sql); err != nil {
|
||||
t.Fatalf("seed %q: %v", sql, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// orgIDTables lists every table that carries an org_id column.
|
||||
func orgIDTables(t *testing.T, pool *pgxpool.Pool) []string {
|
||||
t.Helper()
|
||||
rows, err := pool.Query(t.Context(), `SELECT table_name FROM information_schema.columns WHERE column_name = 'org_id' ORDER BY table_name`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var tables []string
|
||||
for rows.Next() {
|
||||
var name string
|
||||
if err := rows.Scan(&name); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tables = append(tables, name)
|
||||
}
|
||||
return tables
|
||||
}
|
||||
|
||||
func orgName(t *testing.T, pool *pgxpool.Pool, id string) string {
|
||||
t.Helper()
|
||||
var name string
|
||||
if err := pool.QueryRow(t.Context(), `SELECT name FROM orgs WHERE id = $1`, id).Scan(&name); err != nil {
|
||||
t.Fatalf("org %s: %v", id, err)
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func assertNoOrg(t *testing.T, pool *pgxpool.Pool, id string) {
|
||||
t.Helper()
|
||||
var n int
|
||||
if err := pool.QueryRow(t.Context(), `SELECT count(*) FROM orgs WHERE id = $1`, id).Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatalf("org %s still exists", id)
|
||||
}
|
||||
}
|
||||
|
||||
func assertForeignKey(t *testing.T, pool *pgxpool.Pool, table, name string) {
|
||||
t.Helper()
|
||||
var n int
|
||||
if err := pool.QueryRow(t.Context(), `SELECT count(*) FROM pg_constraint WHERE contype = 'f' AND conrelid = $1::regclass AND conname = $2`, table, name).Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Fatalf("foreign key %s on %s = %d, want 1", name, table, n)
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// TestOrgStoreMembers pins the org membership queries: a role change updates
|
||||
// the existing membership, the list joins the identity, removal deletes, agent
|
||||
// assignment lists, and an unknown role is refused naming it.
|
||||
func TestOrgStoreMembers(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
for _, id := range []string{"sam", "bot"} {
|
||||
kind := identity.KindUser
|
||||
name := "Sam"
|
||||
if id == "bot" {
|
||||
kind, name = identity.KindBot, "Bot"
|
||||
}
|
||||
if _, err := db.Create(ctx, identity.Identity{ID: identity.IdentityID(id), Kind: kind, DisplayName: name, Lifecycle: identity.LifecycleActive}, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: "id-" + id}); err != nil {
|
||||
t.Fatalf("create identity %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
if _, err := db.CreateOrg(ctx, org.Org{ID: "acme", Name: "Acme"}, "sam"); err != nil {
|
||||
t.Fatalf("create org: %v", err)
|
||||
}
|
||||
|
||||
if err := db.EnsureMembership(ctx, org.Membership{IdentityID: "sam", OrgID: "acme", Role: org.RoleDeveloper}); err != nil {
|
||||
t.Fatalf("ensure membership: %v", err)
|
||||
}
|
||||
if err := db.EnsureMembership(ctx, org.Membership{IdentityID: "sam", OrgID: "acme", Role: org.RoleAdmin}); err != nil {
|
||||
t.Fatalf("change membership: %v", err)
|
||||
}
|
||||
members, err := db.ListMembers(ctx, "acme")
|
||||
if err != nil {
|
||||
t.Fatalf("list members: %v", err)
|
||||
}
|
||||
if len(members) != 1 {
|
||||
t.Fatalf("members = %d, want 1", len(members))
|
||||
}
|
||||
got := members[0]
|
||||
if got.IdentityID != "sam" || got.Kind != identity.KindUser || got.DisplayName != "Sam" || got.Role != org.RoleAdmin {
|
||||
t.Fatalf("member = %#v", got)
|
||||
}
|
||||
|
||||
if err := db.RemoveMembership(ctx, org.Membership{IdentityID: "sam", OrgID: "acme"}); err != nil {
|
||||
t.Fatalf("remove membership: %v", err)
|
||||
}
|
||||
if members, err = db.ListMembers(ctx, "acme"); err != nil || len(members) != 0 {
|
||||
t.Fatalf("after remove: members = %d, err %v", len(members), err)
|
||||
}
|
||||
// Removing again is not an error: the target is simply gone.
|
||||
if err := db.RemoveMembership(ctx, org.Membership{IdentityID: "sam", OrgID: "acme"}); err != nil {
|
||||
t.Fatalf("second remove: %v", err)
|
||||
}
|
||||
|
||||
if err := db.AssignAgent(ctx, org.AgentAssignment{IdentityID: "bot", OrgID: "acme"}); err != nil {
|
||||
t.Fatalf("assign agent: %v", err)
|
||||
}
|
||||
agents, err := db.ListOrgAgents(ctx, "acme")
|
||||
if err != nil {
|
||||
t.Fatalf("list agents: %v", err)
|
||||
}
|
||||
if len(agents) != 1 || agents[0].IdentityID != "bot" {
|
||||
t.Fatalf("agents = %#v", agents)
|
||||
}
|
||||
|
||||
if err := db.EnsureMembership(ctx, org.Membership{IdentityID: "sam", OrgID: "acme", Role: "superuser"}); !errors.Is(err, org.ErrInvalidRole) {
|
||||
t.Fatalf("unknown role err = %v, want ErrInvalidRole", err)
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
// Package pgstore opens migrated PostgreSQL stores on a throwaway pgtest
|
||||
// database, for tests outside internal/infrastructure/postgres. A package
|
||||
// using it calls pgtest.Main from TestMain.
|
||||
package pgstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/bindingcipher"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
)
|
||||
|
||||
// TaskDB is the State Store's task database: the task, event, status and
|
||||
// identity surface plus the control-plane resources, over one pool.
|
||||
type TaskDB struct {
|
||||
*postgres.Store
|
||||
*postgres.Resources
|
||||
Pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
// Pool opens a pool on a fresh migrated database, closed when t ends.
|
||||
func Pool(t testing.TB) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
return PoolAt(t, pgtest.URL(t))
|
||||
}
|
||||
|
||||
// PoolAt opens a pool on url and migrates it, closed when t ends. Opening a
|
||||
// second pool on the same url is how a test models a restart.
|
||||
func PoolAt(t testing.TB, url string) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
pool, err := postgres.Open(t.Context(), url)
|
||||
if err != nil {
|
||||
t.Fatalf("pgstore: open: %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
if err := postgres.Migrate(t.Context(), pool, postgres.Migrations()); err != nil {
|
||||
t.Fatalf("pgstore: migrate: %v", err)
|
||||
}
|
||||
return pool
|
||||
}
|
||||
|
||||
// On returns the task database over pool.
|
||||
func On(pool *pgxpool.Pool) *TaskDB {
|
||||
return &TaskDB{Store: postgres.New(pool), Resources: postgres.NewResources(pool), Pool: pool}
|
||||
}
|
||||
|
||||
// Open returns the task database on a fresh migrated database.
|
||||
func Open(t testing.TB) *TaskDB {
|
||||
t.Helper()
|
||||
return On(Pool(t))
|
||||
}
|
||||
|
||||
// EDA returns the event-capture store on a fresh migrated database. A nil
|
||||
// cipher keeps binding secrets in plaintext.
|
||||
func EDA(t testing.TB, cipher bindingcipher.BindingCipher) *postgres.EDA {
|
||||
t.Helper()
|
||||
return postgres.NewEDA(Pool(t), cipher)
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
// Package pgtest runs a throwaway PostgreSQL 18 container for one test binary
|
||||
// and gives each test its own database on it.
|
||||
//
|
||||
// func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) }
|
||||
//
|
||||
// It needs a running Docker daemon.
|
||||
package pgtest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
)
|
||||
|
||||
// image is the server tests run against: the same major version archie
|
||||
// requires.
|
||||
const image = "postgres:18"
|
||||
|
||||
var (
|
||||
adminURL string
|
||||
nextDB atomic.Int64
|
||||
)
|
||||
|
||||
// Main starts the container, runs the tests and removes the container,
|
||||
// returning the exit code. A container that cannot start is a test failure,
|
||||
// not a skip.
|
||||
func Main(m *testing.M) int {
|
||||
ctx := context.Background()
|
||||
container, err := tcpostgres.Run(ctx, image,
|
||||
tcpostgres.WithDatabase("postgres"),
|
||||
tcpostgres.WithUsername("archie"),
|
||||
tcpostgres.WithPassword("archie"),
|
||||
tcpostgres.BasicWaitStrategies(),
|
||||
)
|
||||
defer func() { _ = testcontainers.TerminateContainer(container) }()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "pgtest: start %s (is Docker running?): %v\n", image, err)
|
||||
return 1
|
||||
}
|
||||
adminURL, err = container.ConnectionString(ctx, "sslmode=disable")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "pgtest: connection string: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
return m.Run()
|
||||
}
|
||||
|
||||
// URL creates an empty database for t, drops it when t ends, and returns a
|
||||
// connection URL for it.
|
||||
func URL(t testing.TB) string {
|
||||
t.Helper()
|
||||
if adminURL == "" {
|
||||
t.Fatal("pgtest: no container running; call pgtest.Main from TestMain")
|
||||
}
|
||||
name := fmt.Sprintf("t%d", nextDB.Add(1))
|
||||
exec(t, "CREATE DATABASE "+name)
|
||||
t.Cleanup(func() { exec(t, "DROP DATABASE "+name+" WITH (FORCE)") })
|
||||
|
||||
u, err := url.Parse(adminURL)
|
||||
if err != nil {
|
||||
t.Fatalf("pgtest: parse connection string: %v", err)
|
||||
}
|
||||
u.Path = "/" + name
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func exec(t testing.TB, sql string) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
conn, err := pgx.Connect(ctx, adminURL)
|
||||
if err != nil {
|
||||
t.Fatalf("pgtest: connect: %v", err)
|
||||
}
|
||||
defer func() { _ = conn.Close(ctx) }()
|
||||
if _, err := conn.Exec(ctx, sql); err != nil {
|
||||
t.Fatalf("pgtest: %s: %v", sql, err)
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
func TestPresenceReplacesDependencyChecks(t *testing.T) {
|
||||
store := postgres.New(pgstore.Pool(t))
|
||||
record := storecontract.Presence{Service: "gateway", InstanceID: "instance", StartedAt: time.Now(), ReportedAt: time.Now()}
|
||||
for _, checks := range [][]health.Component{
|
||||
{{Name: "nats", Status: health.StatusDegraded, Detail: "disconnected"}},
|
||||
{{Name: "nats", Status: health.StatusOK, Ready: true}, {Name: "state_store", Status: health.StatusOK, Ready: true}},
|
||||
{},
|
||||
} {
|
||||
record.Checks = checks
|
||||
if err := store.PutPresence(t.Context(), record); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows, err := store.ListPresence(t.Context())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rows) != 1 || !reflect.DeepEqual(rows[0].Checks, checks) {
|
||||
t.Fatalf("presence = %+v; want replacement checks %+v", rows, checks)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) }
|
||||
|
||||
// A retry that resumes starts from the results its After stage recorded in
|
||||
// its last run, and is refused when that run did not move past the stage.
|
||||
func TestRetryTaskResume(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
resume task.Resume
|
||||
want string
|
||||
wantErr error
|
||||
}{
|
||||
{name: "after a completed stage", resume: task.Resume{From: "check", After: "analyse"}, want: `{"analyse":{"summary":"found it"}}`},
|
||||
{name: "from the first stage", resume: task.Resume{From: "analyse"}, want: `{}`},
|
||||
{name: "after a stage that failed", resume: task.Resume{From: "publish", After: "check"}, wantErr: storecontract.ErrResumeIncomplete},
|
||||
{name: "after a stage that never ran", resume: task.Resume{From: "publish", After: "nowhere"}, wantErr: storecontract.ErrResumeIncomplete},
|
||||
{name: "plain retry", want: `{}`},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
if _, err := db.EnqueueChatTask(ctx, "", "", "resume", "", "tdd", "", "", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run, err := db.ClaimNext(ctx)
|
||||
if err != nil || run == nil {
|
||||
t.Fatalf("claim: %v", err)
|
||||
}
|
||||
finish := func(name string, to taskstate.StepStatus, results string) {
|
||||
id, _, err := db.StartStep(ctx, task.StepStart{ExecutionID: run.ID, Attempt: run.Attempt, Kind: task.StepKindStage, Name: name})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var raw []byte
|
||||
if results != "" {
|
||||
raw = []byte(results)
|
||||
}
|
||||
if _, err := db.FinishStep(ctx, task.StepFinish{StepID: id, ExecutionID: run.ID, From: taskstate.StepRunning, To: to, Results: raw}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
finish("analyse", taskstate.StepSucceeded, `{"analyse":{"summary":"found it"}}`)
|
||||
finish("check", taskstate.StepFailed, "")
|
||||
if err := db.ParkTask(ctx, run.ID, taskstate.Running, "stage check failed", taskstate.ParkNeedsHuman); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.RetryTask(ctx, run.ID, taskstate.Parked, "", string(taskstate.RetryRefreshOntoBase), tc.resume)
|
||||
if !errors.Is(err, tc.wantErr) {
|
||||
t.Fatalf("RetryTask error = %v, want %v", err, tc.wantErr)
|
||||
}
|
||||
got, err := db.TaskByID(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tc.wantErr != nil {
|
||||
if got.Status != taskstate.Parked {
|
||||
t.Fatalf("refused retry left the task %s, want parked", got.Status)
|
||||
}
|
||||
return
|
||||
}
|
||||
if got.ResumeFrom != tc.resume.From || !sameJSON(t, got.ResumeResults, tc.want) {
|
||||
t.Fatalf("resume = %q %s, want %q %s", got.ResumeFrom, got.ResumeResults, tc.resume.From, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func sameJSON(t *testing.T, got []byte, want string) bool {
|
||||
t.Helper()
|
||||
var a, b any
|
||||
if json.Unmarshal(got, &a) != nil || json.Unmarshal([]byte(want), &b) != nil {
|
||||
return false
|
||||
}
|
||||
ga, _ := json.Marshal(a)
|
||||
gb, _ := json.Marshal(b)
|
||||
return string(ga) == string(gb)
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
// A review that arrives while a remediation owns the task is queued behind it
|
||||
// and remediated when that run finishes, in arrival order. A re-delivered
|
||||
// review is absorbed rather than queued to run a second time.
|
||||
func TestSecondReviewDuringRemediationIsQueued(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
id := openTaskAtPROpen(t, db)
|
||||
|
||||
first, second := reviewUnit(t, 11), reviewUnit(t, 22)
|
||||
|
||||
if err := db.BeginRemediation(ctx, id, first); err != nil {
|
||||
t.Fatalf("begin first remediation: %v", err)
|
||||
}
|
||||
if err := db.BeginRemediation(ctx, id, second); err != nil {
|
||||
t.Fatalf("review arriving during remediation was refused, not queued: %v", err)
|
||||
}
|
||||
if err := db.BeginRemediation(ctx, id, first); err != nil {
|
||||
t.Fatalf("re-delivered review was refused: %v", err)
|
||||
}
|
||||
assertActiveReview(t, db, id, first)
|
||||
|
||||
// An inline comment on the waiting review is collected into its queued
|
||||
// unit rather than dropped, and travels with it when it is promoted.
|
||||
comment := workflow.ReviewUnitComment{CommentID: 5, Path: "main.go", Line: 3, Body: "rename this"}
|
||||
if err := db.UpdateReviewPayload(ctx, id, reviewUnitWith(t, 22, comment)); err != nil {
|
||||
t.Fatalf("comment on the queued review was refused: %v", err)
|
||||
}
|
||||
if err := db.UpdateReviewPayload(ctx, id, reviewUnitWith(t, 22, comment)); err != nil {
|
||||
t.Fatalf("re-delivered comment was refused: %v", err)
|
||||
}
|
||||
|
||||
// The first round runs and finishes; the second, still waiting, is
|
||||
// promoted behind it with its comment.
|
||||
runRemediationRound(t, db, id)
|
||||
assertActiveReview(t, db, id, withComment(t, second, comment))
|
||||
|
||||
// The second round runs and finds nothing left waiting.
|
||||
runRemediationRound(t, db, id)
|
||||
task, err := db.TaskByID(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if task.Status != taskstate.PROpen {
|
||||
t.Fatalf("status after both rounds = %q, want %q (a review was queued twice)", task.Status, taskstate.PROpen)
|
||||
}
|
||||
}
|
||||
|
||||
// openTaskAtPROpen enqueues a task, claims it and opens its pull request.
|
||||
func openTaskAtPROpen(t *testing.T, db *pgstore.TaskDB) int64 {
|
||||
t.Helper()
|
||||
ctx := t.Context()
|
||||
created, err := db.EnqueueChatTask(ctx, "acme", "widget", "a task", "", "tdd", "", "", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.ClaimNext(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Transition(ctx, created.ID, taskstate.Running, taskstate.PROpen, "opened the pull request"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return created.ID
|
||||
}
|
||||
|
||||
// runRemediationRound claims the queued task and finishes one round of it.
|
||||
func runRemediationRound(t *testing.T, db *pgstore.TaskDB, id int64) {
|
||||
t.Helper()
|
||||
ctx := t.Context()
|
||||
run, err := db.ClaimNext(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if run == nil || run.ID != id {
|
||||
t.Fatalf("claim: got %v, want task %d queued", run, id)
|
||||
}
|
||||
if err := db.Transition(ctx, id, taskstate.Running, taskstate.PROpen, "remediated a review round"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// assertActiveReview asserts the task is queued on payload, which is the
|
||||
// review a remediation would run next.
|
||||
func assertActiveReview(t *testing.T, db *pgstore.TaskDB, id int64, payload string) {
|
||||
t.Helper()
|
||||
task, err := db.TaskByID(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if task.Status != taskstate.Queued || task.Workflow != "remediate" {
|
||||
t.Fatalf("task is %q on workflow %q, want a queued remediation", task.Status, task.Workflow)
|
||||
}
|
||||
if task.ReviewPayload != payload {
|
||||
t.Fatalf("active review = %s, want %s", task.ReviewPayload, payload)
|
||||
}
|
||||
}
|
||||
|
||||
func reviewUnit(t *testing.T, reviewID int64) string {
|
||||
t.Helper()
|
||||
payload, err := workflow.EncodeReviewUnit(workflow.ReviewUnit{
|
||||
ReviewID: reviewID, State: "requested_changes", Body: "please change this",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func reviewUnitWith(t *testing.T, reviewID int64, comment workflow.ReviewUnitComment) string {
|
||||
t.Helper()
|
||||
payload, err := workflow.EncodeReviewUnit(workflow.ReviewUnit{ReviewID: reviewID, Comments: []workflow.ReviewUnitComment{comment}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func withComment(t *testing.T, payload string, comment workflow.ReviewUnitComment) string {
|
||||
t.Helper()
|
||||
unit, err := workflow.DecodeReviewUnit(payload)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
unit.Comments = append(unit.Comments, comment)
|
||||
encoded, err := workflow.EncodeReviewUnit(unit)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return encoded
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// One org's chat sessions and messages are invisible to another, which can
|
||||
// neither read nor overwrite them.
|
||||
func TestChatSessionsStayInTheirOrg(t *testing.T) {
|
||||
acme := org.WithOrg(t.Context(), "acme")
|
||||
other := org.WithOrg(t.Context(), "other")
|
||||
store := gateway.NewPostgresSessionStore(pgstore.Pool(t))
|
||||
now := time.Now().UTC()
|
||||
if err := store.Save(acme, gateway.SessionContext{SessionID: "s1", Title: "acme plans", CreatedAt: now, LastActiveAt: now}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.SaveMessage(acme, "s1", messaging.Message{ID: "m1", Role: messaging.RoleUser, Text: "secret"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
ctx func() context.Context //nolint:containedctx // table input, not stored state
|
||||
sees bool
|
||||
}{
|
||||
{"the owning org", func() context.Context { return acme }, true},
|
||||
{"another org", func() context.Context { return other }, false},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := tt.ctx()
|
||||
listed, err := store.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := store.Get(ctx, "s1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
messages, readErr := store.RecentMessages(ctx, "s1", 10)
|
||||
if sees := len(listed) == 1 && got != nil && readErr == nil && len(messages) == 1; sees != tt.sees {
|
||||
t.Fatalf("listed %d, got %v, messages %d (%v); want visible %v", len(listed), got != nil, len(messages), readErr, tt.sees)
|
||||
}
|
||||
if tt.sees {
|
||||
return
|
||||
}
|
||||
if err := store.Save(ctx, gateway.SessionContext{SessionID: "s1", Title: "taken"}); !errors.Is(err, gateway.ErrSessionNotFound) {
|
||||
t.Fatalf("overwrite err = %v, want ErrSessionNotFound", err)
|
||||
}
|
||||
if err := store.SaveMessage(ctx, "s1", messaging.Message{ID: "m2", Role: messaging.RoleUser, Text: "injected"}); !errors.Is(err, gateway.ErrSessionNotFound) {
|
||||
t.Fatalf("append err = %v, want ErrSessionNotFound", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// TestInsertSourceKeepsActingOrg pins that a source created while acting in a
|
||||
// non-default org is owned by that org, so its captures inherit the org.
|
||||
func TestInsertSourceKeepsActingOrg(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
eda := postgres.NewEDA(db.Pool, nil)
|
||||
if err := eda.InsertSource(org.WithOrg(ctx, "acme"), source.Source{Path: "acme-hook", Signing: source.SigningUnsigned}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var owner, workspace string
|
||||
if err := db.Pool.QueryRow(ctx, "SELECT org_id, workspace_id FROM sources WHERE path=$1", "acme-hook").Scan(&owner, &workspace); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if owner != "acme" || workspace != string(org.DefaultWorkspaceID) {
|
||||
t.Fatalf("source owner %s/%s, want acme/%s", owner, workspace, org.DefaultWorkspaceID)
|
||||
}
|
||||
// Lookup stays global: the capture receiver resolves by path alone.
|
||||
got, err := eda.GetSource(ctx, "acme-hook")
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("GetSource = %#v, err %v", got, err)
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// An org's tasks, their events and the stats built from them are invisible to
|
||||
// a request scoped to another org. An event a task emits belongs to the
|
||||
// task's org whatever org the writer acts in, and a caller cannot start work
|
||||
// under another org's identity.
|
||||
func TestTasksStayInTheirOrg(t *testing.T) {
|
||||
scoped := func(id org.OrgID) context.Context {
|
||||
return org.WithScope(org.WithOrg(t.Context(), id), id)
|
||||
}
|
||||
db := pgstore.Open(t)
|
||||
var taskID int64
|
||||
if err := db.Pool.QueryRow(t.Context(),
|
||||
`INSERT INTO tasks (owner, repo, issue_number, identity, workflow, status, tokens_used, org_id, workspace_id)
|
||||
VALUES ('o', 'r', 1, 'sam', 'deploy', 'running', 10, 'acme', 'default') RETURNING id`,
|
||||
).Scan(&taskID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.InsertEvent(t.Context(), events.Event{Kind: "stage_finish", TaskID: taskID, Workflow: "deploy", Stage: "plan"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
ctx func() context.Context //nolint:containedctx // table input, not stored state
|
||||
sees bool
|
||||
}{
|
||||
{"the owning org", func() context.Context { return scoped("acme") }, true},
|
||||
{"another org", func() context.Context { return scoped("other") }, false},
|
||||
{"an internal service", t.Context, true},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := tt.ctx()
|
||||
listed, err := db.Tasks(ctx, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := db.TaskByID(ctx, taskID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
counts, err := db.StatusCounts(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
timeline, err := db.TaskEvents(ctx, taskID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
feed, err := db.EventsSince(ctx, "", 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
workflows, err := db.WorkflowStats(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stages, err := db.StageStats(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen := []bool{len(listed) == 1, got != nil, counts["running"] == 1, len(timeline) == 1, len(feed) == 1, len(workflows) == 1, len(stages) == 1}
|
||||
for i, s := range seen {
|
||||
if s != tt.sees {
|
||||
t.Fatalf("read %d visible = %v, want %v", i, s, tt.sees)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
// An identity of the system org starts no work for a caller in acme.
|
||||
if _, err := db.EnqueueChatTask(scoped("acme"), "o", "r", "t", "b", "deploy", "operator", "", nil); err == nil {
|
||||
t.Fatal("acme started a task under a system-org identity")
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
// A conversation's /stop finds the queued and running tasks it created, and
|
||||
// only those: not another conversation's, not finished ones, not originless.
|
||||
func TestActiveTasksByOrigin(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
enqueue := func(origin string) int64 {
|
||||
created, err := db.EnqueueChatTask(ctx, "", "", "work", "", "tdd", "", origin, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return created.ID
|
||||
}
|
||||
running, queued, finished := enqueue("telegram:1"), enqueue("telegram:1"), enqueue("telegram:1")
|
||||
enqueue("telegram:2")
|
||||
enqueue("")
|
||||
if err := db.Transition(ctx, running, taskstate.Queued, taskstate.Running, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Transition(ctx, finished, taskstate.Queued, taskstate.Declined, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
origin string
|
||||
want []int64
|
||||
}{
|
||||
{"telegram:1", []int64{running, queued}},
|
||||
{"telegram:2", []int64{finished + 1}},
|
||||
{"", nil},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
tasks, err := db.ActiveTasksByOrigin(ctx, tc.origin)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got []int64
|
||||
for _, task := range tasks {
|
||||
got = append(got, task.ID)
|
||||
}
|
||||
if !slices.Equal(got, tc.want) {
|
||||
t.Errorf("origin %q: tasks %v, want %v", tc.origin, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/usage"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// A task's usage is billed to the task's org whatever org the caller names;
|
||||
// usage with no task keeps the caller's org.
|
||||
func TestRecordUsageBillsTheTaskOrg(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
var taskID int64
|
||||
if err := db.Pool.QueryRow(ctx,
|
||||
`INSERT INTO tasks (owner, repo, issue_number, identity, org_id, workspace_id) VALUES ('o', 'r', 1, 'sam', 'acme', 'default') RETURNING id`,
|
||||
).Scan(&taskID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
record usage.Record
|
||||
want string
|
||||
}{
|
||||
{"a task bills its own org", usage.Record{Org: "other", Source: usage.SourceTask, TaskID: taskID}, "acme"},
|
||||
{"chat keeps the caller's org", usage.Record{Org: "org-sys", Source: usage.SourceChat}, "org-sys"},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.record.Step, tt.record.At = tt.name, time.Now()
|
||||
tt.record = tt.record.WithRef("openai/gpt-6-luna")
|
||||
if err := db.RecordUsage(ctx, tt.record); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got string
|
||||
if err := db.Pool.QueryRow(ctx, "SELECT org_id FROM model_usage WHERE step = $1", tt.name).Scan(&got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Fatalf("org = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
// StartCall is idempotent per call key: the same caller and key return the
|
||||
// child already started, so a retry of a lost EnqueueCallTask reply, or of the
|
||||
// stage that made the call, does not start a second child and run its work
|
||||
// twice. Another call site -- the same declared path starting a different
|
||||
// workflow -- still gets its own child.
|
||||
func TestStartCallReturnsOneChildPerCallKey(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
if _, err := db.EnqueueChatTask(ctx, "", "", "caller", "", "tdd", "", "", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
caller, err := db.ClaimNext(ctx)
|
||||
if err != nil || caller == nil {
|
||||
t.Fatalf("claim: %v", err)
|
||||
}
|
||||
|
||||
first, err := db.StartCall(ctx, caller.ID, "check@callee-a", "callee-a", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
retry, err := db.StartCall(ctx, caller.ID, "check@callee-a", "callee-a", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if retry.ID != first.ID {
|
||||
t.Fatalf("the retry started task %d, want the child already started, %d", retry.ID, first.ID)
|
||||
}
|
||||
other, err := db.StartCall(ctx, caller.ID, "check@callee-b", "callee-b", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if other.ID == first.ID {
|
||||
t.Fatalf("the same path starting another workflow returned the first child, %d", first.ID)
|
||||
}
|
||||
|
||||
var rows int
|
||||
if err := db.Pool.QueryRow(ctx,
|
||||
`SELECT count(*) FROM tasks WHERE call_parent_task_id = $1`, caller.ID).Scan(&rows); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rows != 2 {
|
||||
t.Fatalf("the caller started %d children, want one per call key (2)", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// A child that reached a hard terminal failure can never do the work, so it
|
||||
// frees its key: it left no result the caller can use, and the caller's retry
|
||||
// starting a fresh child duplicates nothing. A parked child keeps the key --
|
||||
// it is recoverable by retrying the child, which is what the caller's own park
|
||||
// names.
|
||||
func TestStartCallFreesTheKeyOfAHardTerminalChild(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
// move takes the child out of queued the way its real ending does.
|
||||
move func(t *testing.T, db *pgstore.TaskDB, childID int64)
|
||||
wantNew bool
|
||||
}{
|
||||
{
|
||||
name: "a declined child frees its key",
|
||||
move: func(t *testing.T, db *pgstore.TaskDB, childID int64) {
|
||||
t.Helper()
|
||||
if err := db.Transition(t.Context(), childID, taskstate.Queued, taskstate.Declined, "the operator declined it"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
wantNew: true,
|
||||
},
|
||||
{
|
||||
name: "a parked child keeps its key",
|
||||
move: func(t *testing.T, db *pgstore.TaskDB, childID int64) {
|
||||
t.Helper()
|
||||
// Parked is reachable only from running, so the child is
|
||||
// claimed first, as a worker would claim it.
|
||||
claimed, err := db.ClaimNext(t.Context())
|
||||
if err != nil || claimed == nil || claimed.ID != childID {
|
||||
t.Fatalf("claim child %d: %v, got %v", childID, err, claimed)
|
||||
}
|
||||
if err := db.ParkTask(t.Context(), childID, taskstate.Running, "needs an operator", taskstate.ParkNeedsHuman); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
if _, err := db.EnqueueChatTask(ctx, "", "", "caller", "", "tdd", "", "", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
caller, err := db.ClaimNext(ctx)
|
||||
if err != nil || caller == nil {
|
||||
t.Fatalf("claim: %v", err)
|
||||
}
|
||||
first, err := db.StartCall(ctx, caller.ID, "check@callee", "callee", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tt.move(t, db, first.ID)
|
||||
|
||||
retry, err := db.StartCall(ctx, caller.ID, "check@callee", "callee", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tt.wantNew && retry.ID == first.ID {
|
||||
t.Fatalf("the retry returned the child that can never do the work, %d, want a fresh child", first.ID)
|
||||
}
|
||||
if !tt.wantNew && retry.ID != first.ID {
|
||||
t.Fatalf("the retry started task %d, want the parked child it can retry, %d", retry.ID, first.ID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user