An install that stayed in installed_packages left every contribution unprojected: nothing made a package's workflow a usable org resource, and nothing stopped it on removal. This lands projection for the workflow family, the one kind whose org resource, validation and live read path all exist: - storepkg.Service gains Projections (family -> projector): install resolves each projected family's contributed names against the fetched layer (FilesFromLayer), applies them, and on failure withdraws and removes the half installation; remove withdraws before the record goes, so a failed withdrawal keeps the package installed and usable. - controlplane.WorkflowProjector merges the package's entries into the org's workflow-definitions document — the same document the daemon's WorkflowDefinitions client reads per dispatch, so a projected workflow is usable at the next dispatch and a withdrawn one stops dispatching, without restarts. A contributed id displacing an operator's own workflow is refused (storepkg.ErrContributionCollision), never silently replaced. - installed_package_contributions (migration 0023) records the entries a package projected, so removal takes exactly its own contributions out. - stateStoreDeps wires the projector set on the State Store's pool; the composition is pinned by the live round-trip test over a real gRPC connection. Prompts, skills, MCP server definitions and default settings remain unprojected: their contribution formats are not settled by docs/prds/store.md and are left for their own slices rather than half-wired here.
240 lines
8.3 KiB
Go
240 lines
8.3 KiB
Go
package storepkg
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type fakeRegistry struct {
|
|
descriptor Descriptor
|
|
err error
|
|
calls int
|
|
}
|
|
|
|
func (r *fakeRegistry) Fetch(context.Context, string, string) (Descriptor, []byte, error) {
|
|
r.calls++
|
|
return r.descriptor, nil, r.err
|
|
}
|
|
|
|
type fakeInstalledStore struct {
|
|
records map[string]Installed
|
|
err error
|
|
}
|
|
|
|
func (s *fakeInstalledStore) Install(_ context.Context, p Installed) error {
|
|
if s.err != nil {
|
|
return s.err
|
|
}
|
|
s.records[p.OrgID+"/"+p.Name] = p
|
|
return nil
|
|
}
|
|
|
|
func (s *fakeInstalledStore) Get(_ context.Context, org, name string) (Installed, error) {
|
|
p, ok := s.records[org+"/"+name]
|
|
if !ok {
|
|
return Installed{}, ErrNotFound
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func (s *fakeInstalledStore) List(_ context.Context, org string) ([]Installed, error) {
|
|
var out []Installed
|
|
for _, p := range s.records {
|
|
if p.OrgID == org {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s *fakeInstalledStore) Remove(_ context.Context, org, name string) error {
|
|
if _, ok := s.records[org+"/"+name]; !ok {
|
|
return ErrNotFound
|
|
}
|
|
delete(s.records, org+"/"+name)
|
|
return nil
|
|
}
|
|
|
|
func (s *fakeInstalledStore) Accept(_ context.Context, org, name string, authority Authority) error {
|
|
record, ok := s.records[org+"/"+name]
|
|
if !ok {
|
|
return ErrNotFound
|
|
}
|
|
accepted := authority
|
|
record.AcceptedAuthority = &accepted
|
|
s.records[org+"/"+name] = record
|
|
return nil
|
|
}
|
|
|
|
func TestServiceInstall(t *testing.T) {
|
|
ctx := context.Background()
|
|
valid, err := Decode(strings.NewReader(validDescriptor))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
valid.Requires = nil
|
|
const digest = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
cases := []struct {
|
|
name, org, ref, pin string
|
|
descriptor Descriptor
|
|
registryErr error
|
|
wantErr bool
|
|
}{
|
|
{name: "pinned install", org: "org-a", ref: "localhost:5000/review", pin: digest, descriptor: valid},
|
|
{name: "tag refused", org: "org-a", ref: "localhost:5000/review", pin: "latest", descriptor: valid, wantErr: true},
|
|
{name: "missing org", ref: "localhost:5000/review", pin: digest, descriptor: valid, wantErr: true},
|
|
{name: "registry failure", org: "org-a", ref: "localhost:5000/review", pin: digest, descriptor: valid, registryErr: errors.New("missing"), wantErr: true},
|
|
{name: "invalid descriptor", org: "org-a", ref: "localhost:5000/review", pin: digest, descriptor: Descriptor{}, wantErr: true},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
registry := &fakeRegistry{descriptor: tc.descriptor, err: tc.registryErr}
|
|
store := &fakeInstalledStore{records: map[string]Installed{}}
|
|
got, err := (Service{Registry: registry, Store: store}).Install(ctx, tc.org, "review", tc.ref, tc.pin)
|
|
if (err != nil) != tc.wantErr {
|
|
t.Fatalf("Install error = %v", err)
|
|
}
|
|
if tc.wantErr {
|
|
if len(store.records) != 0 {
|
|
t.Fatal("failed install persisted a record")
|
|
}
|
|
return
|
|
}
|
|
if got.Digest != digest || got.OrgID != tc.org || got.UpdatePolicy != "manual" {
|
|
t.Fatalf("installed = %#v", got)
|
|
}
|
|
if _, err := store.Get(ctx, "org-b", "review"); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("other org sees package: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestServiceAcceptPackageAuthority(t *testing.T) {
|
|
ctx := context.Background()
|
|
declared, err := Decode(strings.NewReader(validDescriptor))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
declared.Authority = Authority{ForgePermissions: []string{"read", "comment"}, Tools: []string{"shell"}}
|
|
declared.Requires = nil
|
|
install := func(t *testing.T) (Service, *fakeInstalledStore) {
|
|
t.Helper()
|
|
store := &fakeInstalledStore{records: map[string]Installed{}}
|
|
svc := Service{Registry: &fakeRegistry{descriptor: declared}, Store: store}
|
|
if _, err := svc.Install(ctx, "org-a", "review", "localhost:5000/review", "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return svc, store
|
|
}
|
|
t.Run("records the accepted authority against the pin", func(t *testing.T) {
|
|
svc, store := install(t)
|
|
accepted := Authority{ForgePermissions: []string{"read"}}
|
|
record, err := svc.AcceptPackageAuthority(ctx, "org-a", "review", accepted)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if record.AcceptedAuthority == nil || !record.AcceptedAuthority.Covers(accepted) {
|
|
t.Fatalf("accepted record = %#v", record.AcceptedAuthority)
|
|
}
|
|
stored, err := store.Get(ctx, "org-a", "review")
|
|
if err != nil || stored.AcceptedAuthority == nil || !stored.AcceptedAuthority.Covers(accepted) {
|
|
t.Fatalf("stored record = %#v, %v", stored.AcceptedAuthority, err)
|
|
}
|
|
// The acceptance record is the bound the package is later checked
|
|
// against: the operator accepted a narrowed set, so the package's full
|
|
// declaration now exceeds the accepted authority.
|
|
if record.AcceptedAuthority.Covers(declared.Authority) {
|
|
t.Fatal("accepted record covers authority the operator did not accept")
|
|
}
|
|
})
|
|
t.Run("refuses grants the package does not declare", func(t *testing.T) {
|
|
svc, store := install(t)
|
|
_, err := svc.AcceptPackageAuthority(ctx, "org-a", "review", Authority{EgressHosts: []string{"internal.invalid"}})
|
|
if !errors.Is(err, ErrAuthorityNotDeclared) {
|
|
t.Fatalf("undeclared grant error = %v", err)
|
|
}
|
|
stored, err := store.Get(ctx, "org-a", "review")
|
|
if err != nil || stored.AcceptedAuthority != nil {
|
|
t.Fatalf("refused acceptance changed the record: %#v, %v", stored.AcceptedAuthority, err)
|
|
}
|
|
})
|
|
t.Run("refuses an acceptance nothing installed", func(t *testing.T) {
|
|
svc, _ := install(t)
|
|
if _, err := svc.AcceptPackageAuthority(ctx, "org-a", "missing", Authority{}); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("unknown package error = %v", err)
|
|
}
|
|
})
|
|
t.Run("rejects an invalid acceptance", func(t *testing.T) {
|
|
svc, store := install(t)
|
|
if _, err := svc.AcceptPackageAuthority(ctx, "org-a", "review", Authority{ForgePermissions: []string{"admin"}}); err == nil {
|
|
t.Fatal("invalid forge permission accepted")
|
|
}
|
|
stored, err := store.Get(ctx, "org-a", "review")
|
|
if err != nil || stored.AcceptedAuthority != nil {
|
|
t.Fatalf("rejected acceptance changed the record: %#v, %v", stored.AcceptedAuthority, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestServiceInstallRequiresPinnedDependency(t *testing.T) {
|
|
ctx := context.Background()
|
|
valid, err := Decode(strings.NewReader(validDescriptor))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const digest = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
cases := []struct {
|
|
name string
|
|
installed map[string]Installed
|
|
wantErr bool
|
|
}{
|
|
{name: "missing", installed: map[string]Installed{}, wantErr: true},
|
|
{name: "wrong org", installed: map[string]Installed{"other/review-kit": {OrgID: "other", Name: "review-kit", Digest: digest}}, wantErr: true},
|
|
{name: "wrong digest", installed: map[string]Installed{"org/review-kit": {OrgID: "org", Name: "review-kit", Digest: "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}}, wantErr: true},
|
|
{name: "matching", installed: map[string]Installed{"org/review-kit": {OrgID: "org", Name: "review-kit", Digest: digest}}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
store := &fakeInstalledStore{records: tc.installed}
|
|
_, err := (Service{Registry: &fakeRegistry{descriptor: valid}, Store: store}).Install(ctx, "org", "review", "localhost:5000/review", digest)
|
|
if (err != nil) != tc.wantErr {
|
|
t.Fatalf("Install error = %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestServiceRemove(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := &fakeInstalledStore{records: map[string]Installed{
|
|
"org-a/review": {OrgID: "org-a", Name: "review"},
|
|
"org-b/review": {OrgID: "org-b", Name: "review"},
|
|
}}
|
|
svc := Service{Store: store}
|
|
if err := svc.Remove(ctx, "org-a", "review"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := store.Get(ctx, "org-a", "review"); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("removed package remains: %v", err)
|
|
}
|
|
if _, err := store.Get(ctx, "org-b", "review"); err != nil {
|
|
t.Fatalf("other org package removed: %v", err)
|
|
}
|
|
}
|
|
|
|
// fakeLayeredRegistry is a registry returning a descriptor with its layer
|
|
// bytes, the way a fetched package carries both.
|
|
type fakeLayeredRegistry struct {
|
|
descriptor Descriptor
|
|
layer []byte
|
|
}
|
|
|
|
func (r *fakeLayeredRegistry) Fetch(context.Context, string, string) (Descriptor, []byte, error) {
|
|
return r.descriptor, r.layer, nil
|
|
}
|
|
|
|
var _ Registry = (*fakeLayeredRegistry)(nil)
|