Add resumable checked migrations and explicit schema administration commands. Port ML budgets, namespaces, identity and group persistence with three-backend contracts. Keep MySQL unavailable as the full application backend.
91 lines
2.9 KiB
Go
91 lines
2.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"pageswiki/internal/machinelearning"
|
|
)
|
|
|
|
func TestMachineLearningPolicyPersistence(t *testing.T) {
|
|
for _, backend := range []storeSQLBackend{storeSQLite, storePostgreSQL, storeMySQL} {
|
|
t.Run(fmt.Sprint(backend), func(t *testing.T) { testMachineLearningPolicyPersistence(t, backend) })
|
|
}
|
|
}
|
|
|
|
func testMachineLearningPolicyPersistence(t *testing.T, backend storeSQLBackend) {
|
|
s, domain := mlTestStore(t, backend)
|
|
ctx := context.Background()
|
|
empty, err := s.MachineLearningPolicy(ctx, domain.UUID)
|
|
if err != nil || empty.Enabled || empty.Generation != "" {
|
|
t.Fatal("not disabled by default", err)
|
|
}
|
|
p := machinelearning.Policy{Enabled: true, ScopeUUID: uuid.NewString(), Provider: "doc2vec", Model: strings.Repeat("a", 64), Dimensions: 8, Prefixes: []string{"/lab"}, MaxBatchBytes: 6000}
|
|
saved, err := s.SaveMachineLearningPolicy(ctx, domain.UUID, "", p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if saved.Generation == "" || saved.Generation == p.Generation {
|
|
t.Fatal("generation not assigned")
|
|
}
|
|
if _, err = s.SaveMachineLearningPolicy(ctx, domain.UUID, "", p); !errors.Is(err, ErrMachineLearningConflict) {
|
|
t.Fatal("lost update", err)
|
|
}
|
|
loaded, err := s.MachineLearningPolicy(ctx, domain.UUID)
|
|
if err != nil || loaded.Generation != saved.Generation {
|
|
t.Fatal("roundtrip", err)
|
|
}
|
|
other, err := s.MachineLearningPolicy(ctx, uuid.NewString())
|
|
if err != nil || other.Enabled {
|
|
t.Fatal("cross domain leak", err)
|
|
}
|
|
p.Enabled = false
|
|
disabled, err := s.SaveMachineLearningPolicy(ctx, domain.UUID, saved.Generation, p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if disabled.Generation == saved.Generation || disabled.Allows("/lab", saved.Generation) {
|
|
t.Fatal("disable did not invalidate")
|
|
}
|
|
p.Enabled = true
|
|
reenabled, err := s.SaveMachineLearningPolicy(ctx, domain.UUID, disabled.Generation, p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if reenabled.Allows("/lab", saved.Generation) {
|
|
t.Fatal("old consent resurrected")
|
|
}
|
|
start := make(chan struct{})
|
|
results := make(chan error, 2)
|
|
for i := 0; i < 2; i++ {
|
|
go func() {
|
|
<-start
|
|
_, err := s.SaveMachineLearningPolicy(ctx, domain.UUID, reenabled.Generation, p)
|
|
results <- err
|
|
}()
|
|
}
|
|
close(start)
|
|
winners := 0
|
|
for i := 0; i < 2; i++ {
|
|
err := <-results
|
|
if err == nil {
|
|
winners++
|
|
} else if !errors.Is(err, ErrMachineLearningConflict) {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if winners != 1 {
|
|
t.Fatal("concurrent configuration lost update", winners)
|
|
}
|
|
if _, err = s.DB.ExecContext(ctx, `UPDATE config SET value=`+s.sqlParam(1)+` WHERE domain_uuid=`+s.sqlParam(2)+` AND `+s.sqlConfigKey()+`=`+s.sqlParam(3), `{"enabled":true}`, domain.UUID, machineLearningConfigKey); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.MachineLearningPolicy(ctx, domain.UUID); !errors.Is(err, machinelearning.ErrPolicy) {
|
|
t.Fatal("corrupt policy did not fail closed", err)
|
|
}
|
|
}
|