clean up of the codebase

This commit is contained in:
ari committed 2026-11-01 13:01:54 -05:00
1 parent 7822529a38
commit 0ac61b4ece
52 files changed
+2545 -2274

No files matched your search

@@ -0,0 +1,59 @@
package clustertest
import (
"context"
"path/filepath"
"testing"
"slackerbot/internal/config"
"slackerbot/internal/manager"
"slackerbot/internal/model"
"slackerbot/internal/state"
"slackerbot/internal/testutil"
)
const ClusterSecret = "cluster-secret-1234567890"
type Cluster struct {
Cfg *config.Config
Mgr *manager.Manager
Store *state.Store
Master, Slave *testutil.FakeNode
Ctx context.Context
}
func New(t testing.TB) *Cluster {
addr := testutil.FreePort(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
cfg := &config.Config{StateDir: t.TempDir(), Container: model.Size{MemoryMB: 1024, CPUs: 1}}
cfg.Cluster = config.ClusterConfig{Name: "master", Secret: ClusterSecret, Listen: addr}
store, err := state.Open(filepath.Join(cfg.StateDir, "state.json"))
if err != nil {
t.Fatal(err)
}
c := &Cluster{Cfg: cfg, Store: store, Master: testutil.NewFakeNode("master"), Slave: testutil.NewFakeNode("slave-1"), Ctx: ctx}
c.Mgr = manager.New(cfg, store, c.Master)
go c.Mgr.ServeCluster(ctx)
slaveCfg := &config.Config{StateDir: t.TempDir()}
slaveCfg.Cluster = config.ClusterConfig{Name: "slave-1", Secret: ClusterSecret, Master: "wss://" + addr}
go manager.RunSlave(ctx, slaveCfg, c.Slave)
testutil.WaitFor(t, "slave to come online", func() bool { return c.Mgr.State(&model.Owner{Node: "slave-1"}) != manager.StateOffline })
return c
}
func (c *Cluster) AddOwner(t testing.TB, data []byte, running bool) {
u := &model.Owner{ID: "C1", Kind: model.OwnerChannel, Machine: "c-c1", Secret: "s", Size: model.Size{MemoryMB: 1024, CPUs: 1},
Node: "master", Drive: "/d", StorageMinMB: 1024, StorageMaxMB: 2048}
if err := c.Store.PutOwner(u); err != nil {
t.Fatal(err)
}
c.Master.Mu.Lock()
defer c.Master.Mu.Unlock()
c.Master.Homes["/d/c-c1"] = data
if running {
c.Master.Runs["c-c1"] = true
}
}
+243
View File
@@ -0,0 +1,243 @@
package testutil
import (
"bytes"
"context"
"errors"
"net"
"strings"
"sync"
"testing"
"time"
"slackerbot/internal/model"
"slackerbot/internal/node"
"slackerbot/internal/util"
)
type FakeNode struct {
name string
Mu sync.Mutex
Runs map[string]bool
Last node.StartSpec
Homes map[string][]byte
Exports map[string]*fakeExport
Imports map[string]*fakeImport
Passes int
FailWrites bool
Usable model.Size
}
func NewFakeNode(name string) *FakeNode {
return &FakeNode{name: name, Runs: map[string]bool{}, Homes: map[string][]byte{},
Exports: map[string]*fakeExport{}, Imports: map[string]*fakeImport{}, Usable: model.Size{MemoryMB: 8192, CPUs: 4}}
}
func (f *FakeNode) Name() string { return f.name }
func (f *FakeNode) Info(ctx context.Context) (node.NodeInfo, error) {
running, _ := f.Running(ctx)
f.Mu.Lock()
defer f.Mu.Unlock()
homes := map[string]int64{}
for k, v := range f.Homes {
homes[strings.TrimPrefix(k, "/d/")] = int64(len(v)) >> 20
}
return node.NodeInfo{Name: f.name, Usable: f.Usable, Running: running,
Drives: []node.DriveInfo{{Path: "/d", FreeMB: 50_000, HomesMB: homes}}}, nil
}
func (f *FakeNode) Running(context.Context) ([]node.RunningMachine, error) {
f.Mu.Lock()
defer f.Mu.Unlock()
var out []node.RunningMachine
for m := range f.Runs {
out = append(out, node.RunningMachine{Name: m})
}
return out, nil
}
func (f *FakeNode) IsRunning(machine string) bool {
f.Mu.Lock()
defer f.Mu.Unlock()
return f.Runs[machine]
}
func (f *FakeNode) Start(_ context.Context, spec node.StartSpec) error {
f.Mu.Lock()
defer f.Mu.Unlock()
f.Last, f.Runs[spec.Machine] = spec, true
return nil
}
func (f *FakeNode) Poweroff(_ context.Context, machine string) error {
f.Mu.Lock()
defer f.Mu.Unlock()
delete(f.Runs, machine)
return nil
}
func (f *FakeNode) SetLimits(context.Context, string, model.Size) error { return nil }
func (f *FakeNode) SetQuota(context.Context, string, string, int64) error { return nil }
func (f *FakeNode) DialAgent(_ context.Context, machine, secret string) (node.MsgConn, error) {
if secret != "agent-secret" {
return nil, errors.New("bad agent secret")
}
return newEcho(), nil
}
func (f *FakeNode) WaitStopped(_ context.Context, machine string) error {
if f.IsRunning(machine) {
return errors.New("still running")
}
return nil
}
func (f *FakeNode) ExportHome(_ context.Context, drive, machine, parent string) (string, error) {
f.Mu.Lock()
defer f.Mu.Unlock()
data, ok := f.Homes[drive+"/"+machine]
if !ok {
return "", errors.New("no such home")
}
kind := "FULL:"
if parent != "" {
kind = "INCR:"
}
id := util.RandomHex(4)
f.Exports[id] = &fakeExport{data: append([]byte(kind), data...)}
return id, nil
}
func (f *FakeNode) ReadExport(_ context.Context, id string, max int) ([]byte, bool, error) {
f.Mu.Lock()
defer f.Mu.Unlock()
x := f.Exports[id]
if x == nil {
return nil, false, errors.New("no export")
}
n := min(max, len(x.data)-x.pos)
chunk := x.data[x.pos : x.pos+n]
x.pos += n
return chunk, x.pos == len(x.data), nil
}
func (f *FakeNode) CloseExport(_ context.Context, id string) error {
f.Mu.Lock()
defer f.Mu.Unlock()
delete(f.Exports, id)
return nil
}
func (f *FakeNode) ImportHome(_ context.Context, drive, machine string) (string, error) {
f.Mu.Lock()
defer f.Mu.Unlock()
if _, exists := f.Homes[drive+"/"+machine]; exists {
return "", errors.New("home already exists")
}
id := util.RandomHex(4)
f.Imports[id] = &fakeImport{key: drive + "/" + machine}
return id, nil
}
func (f *FakeNode) WriteImport(_ context.Context, id string, data []byte) error {
f.Mu.Lock()
defer f.Mu.Unlock()
if f.FailWrites {
return errors.New("disk on fire")
}
x := f.Imports[id]
if x == nil {
return errors.New("no import")
}
x.cur = append(x.cur, data...)
return nil
}
func (f *FakeNode) EndReceive(_ context.Context, id string) error {
f.Mu.Lock()
defer f.Mu.Unlock()
x := f.Imports[id]
if x == nil || len(x.cur) < 5 {
return errors.New("nothing received")
}
if string(x.cur[:5]) == "INCR:" && x.latest == nil {
return errors.New("incremental send without a parent")
}
x.latest, x.cur = x.cur[5:], nil
x.passes++
return nil
}
func (f *FakeNode) FinishImport(_ context.Context, id string, quotaMB int64) error {
f.Mu.Lock()
defer f.Mu.Unlock()
x := f.Imports[id]
delete(f.Imports, id)
f.Homes[x.key] = x.latest
f.Passes = x.passes
return nil
}
func (f *FakeNode) AbortImport(_ context.Context, id string) error {
f.Mu.Lock()
defer f.Mu.Unlock()
delete(f.Imports, id)
return nil
}
func (f *FakeNode) DeleteHome(_ context.Context, drive, machine string) error {
f.Mu.Lock()
defer f.Mu.Unlock()
delete(f.Homes, drive+"/"+machine)
return nil
}
type fakeExport struct {
data []byte
pos int
}
type fakeImport struct {
key string
cur, latest []byte
passes int
}
type echo struct {
ch chan []byte
closed chan struct{}
once sync.Once
}
func newEcho() *echo { return &echo{ch: make(chan []byte, 16), closed: make(chan struct{})} }
func (e *echo) Read(ctx context.Context) ([]byte, error) {
select {
case b := <-e.ch:
return b, nil
case <-e.closed:
return nil, errors.New("closed")
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (e *echo) Write(_ context.Context, b []byte) error {
e.ch <- append([]byte("echo: "), b...)
return nil
}
func (e *echo) Close() error { e.once.Do(func() { close(e.closed) }); return nil }
func FreePort(t testing.TB) string {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
return l.Addr().String()
}
func WaitFor(t testing.TB, what string, cond func() bool) {
deadline := time.Now().Add(10 * time.Second)
for !cond() {
if time.Now().After(deadline) {
t.Fatalf("timed out waiting for %s", what)
}
time.Sleep(20 * time.Millisecond)
}
}
func HomeData() []byte { return bytes.Repeat([]byte("0123456789abcdef"), 600_000) }