The groups plugin now owns the user's own routing groups at the router strategy point. The groups magpie finds (auto-<model>, one model several providers serve) stay core, as Smart does: a new install has groups off under Essentials, and taking those would silently drop cross-provider failover. planGroup and the walk over a group's members stay core too. - provider.GateGroups / ServeGroups (group_serve.go): the gateway holds the gate while it serves; the plugin holds ServeGroups while on. Gated with none served, groupsIn drops every group not Auto, so FindGroup, GroupFor, the catalog and /v1/models go without them. The CLI and TUI are never gated. Nothing derived from groups is cached past a call, so a flip is seen by the next lookup. - Gated and off, a lookup that finds a user group asks the gate's refresh (pluginrt.Refresh in the gateway) at most once a second, never re-entrantly, then looks again. Without this, `magpie group add` was 404 for up to 2s until KeepFresh turned the plugin on (seen in the sandbox). - plugins/groups and plugins/routing: InUse is provider.NamedGroups, the file's non-Auto, non-hidden groups read past the gate; an unreadable providers.json is an error, so the plugin stays on. Run before rebasing onto707fbb249: - go test -count=1 -timeout 40m ./... : all ok (59 packages) - gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean - sandbox, real nogui binary + `magpie serve` + two fake upstreams: before add: group/auto-m listed, group/mine 404; right after `magpie group add Mine models=pa/x,pb/m`: group/mine listed and 200; after `group rm`: 404, auto-m still served. After the rebase onto707fbb249: vets (3 OSes) and go test of internal/provider, internal/gateway, internal/pluginrt and plugins/... all ok. Broken by hand, each went red: - groupsIn filter removed: TestOwnGroupsWaitOnThePlugin, TestGroupsOnAndOff/off - NamedGroups swallowing the read error: TestNamedGroupsUnreadable, plugins/groups TestInUse - Apply not serving: TestOnServesOffDoesNot - refresh not called / not throttled / asked for Auto groups too: TestOwnGroupRefreshesTheGate
78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
package groups
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/pluginrt"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
func TestMain(m *testing.M) { testenv.Main(m) }
|
|
|
|
func manifest(t *testing.T) pluginrt.Manifest {
|
|
t.Helper()
|
|
for _, m := range pluginrt.Registered() {
|
|
if m.ID == "groups" {
|
|
return m
|
|
}
|
|
}
|
|
t.Fatal("groups not registered")
|
|
return pluginrt.Manifest{}
|
|
}
|
|
|
|
func sandbox(t *testing.T) {
|
|
t.Helper()
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, "cache"))
|
|
}
|
|
|
|
// Where the gateway is served, the user's own groups are served while the
|
|
// plugin is on, and not while it is off.
|
|
func TestOnServesOffDoesNot(t *testing.T) {
|
|
sandbox(t)
|
|
defer provider.GateGroups(nil)()
|
|
rt := pluginrt.New([]pluginrt.Manifest{manifest(t)})
|
|
defer rt.Stop()
|
|
served := func(want bool) {
|
|
t.Helper()
|
|
if provider.GroupsServed() != want {
|
|
t.Fatalf("served: %v, want %v (%+v)", !want, want, rt.Statuses())
|
|
}
|
|
}
|
|
served(false)
|
|
rt.Reconcile(map[string]bool{"groups": true}, nil)
|
|
served(true)
|
|
rt.Reconcile(map[string]bool{"groups": false}, nil)
|
|
served(false)
|
|
rt.Reconcile(map[string]bool{"groups": true}, nil)
|
|
served(true)
|
|
}
|
|
|
|
// It counts the user's own groups; a providers.json that can't be read
|
|
// keeps it on.
|
|
func TestInUse(t *testing.T) {
|
|
sandbox(t)
|
|
inUse := manifest(t).InUse
|
|
if n, err := inUse(); n != 0 || err != nil {
|
|
t.Fatalf("no file: %d, %v", n, err)
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{Name: "Mine", Members: []string{"a/x", "b/y"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, err := inUse(); n != 1 || err != nil {
|
|
t.Fatalf("one group: %d, %v", n, err)
|
|
}
|
|
if err := os.WriteFile(provider.Path(), []byte("{not json"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := inUse(); err == nil {
|
|
t.Fatal("no error for a providers.json that can't be read")
|
|
}
|
|
}
|