275 lines
6.9 KiB
Go
275 lines
6.9 KiB
Go
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
|
|
Outdated map[string]bool // machines that booted an older system; cleared on Start
|
|
Last node.StartSpec
|
|
Homes map[string][]byte
|
|
Files map[string][]byte
|
|
Exports map[string]*fakeExport
|
|
Imports map[string]*fakeImport
|
|
Passes int
|
|
FailWrites bool
|
|
Usable model.Size
|
|
Version string
|
|
// Answers agent connections when set. Otherwise they echo.
|
|
Goose *FakeGoose
|
|
}
|
|
|
|
func NewFakeNode(name string) *FakeNode {
|
|
return &FakeNode{name: name, Runs: map[string]bool{}, Outdated: map[string]bool{}, Homes: map[string][]byte{}, Files: 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, Version: f.Version, 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, Outdated: f.Outdated[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
|
|
delete(f.Outdated, spec.Machine)
|
|
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")
|
|
}
|
|
if f.Goose != nil {
|
|
return f.Goose.Conn(), nil
|
|
}
|
|
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
|
|
}
|
|
|
|
func (f *FakeNode) ReadFile(_ context.Context, drive, machine, path string, offset int64, limit int) ([]byte, int64, error) {
|
|
f.Mu.Lock()
|
|
defer f.Mu.Unlock()
|
|
data, ok := f.Files[drive+"/"+machine+"/"+path]
|
|
if !ok {
|
|
return nil, 0, errors.New("there's no file " + path)
|
|
}
|
|
end := min(int64(len(data)), offset+int64(limit))
|
|
return data[offset:end], int64(len(data)), nil
|
|
}
|
|
|
|
func (f *FakeNode) WriteFile(_ context.Context, drive, machine, path string, offset int64, data []byte) error {
|
|
f.Mu.Lock()
|
|
defer f.Mu.Unlock()
|
|
key := drive + "/" + machine + "/" + path
|
|
if offset == 0 {
|
|
f.Files[key] = nil
|
|
}
|
|
f.Files[key] = append(f.Files[key], data...)
|
|
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) }
|