devbox/internal/daemon/daemon_test.go

180 lines
4.7 KiB
Go

package daemon
import (
"context"
"crypto/ed25519"
"crypto/rand"
"errors"
"net/http/httptest"
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/shoemoney/devbox/internal/config"
"github.com/shoemoney/devbox/internal/hub"
"github.com/shoemoney/devbox/internal/hub/blobstore"
"github.com/shoemoney/devbox/internal/hub/meta"
"github.com/shoemoney/devbox/internal/transport"
)
// startDaemons returns a shutdown function that must be deferred after the hub
// and database close defers, so Run finishes before its dependencies or t expire.
func startDaemons(t *testing.T, daemons ...*Daemon) func() {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
for _, d := range daemons {
wg.Add(1)
go func() {
defer wg.Done()
if err := d.Run(ctx); err != nil {
t.Errorf("daemon Run: %v", err)
}
}()
}
return func() {
cancel()
wg.Wait()
}
}
func join(t *testing.T, db *meta.DB, url, name string) string {
t.Helper()
tok := name + "-tok"
if err := db.CreateToken(hub.HashToken(tok), time.Now().Unix()+3600); err != nil {
t.Fatal(err)
}
c := transport.New(url)
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
t.Fatal(err)
}
if _, err := c.Join(tok, name, pub, priv); err != nil {
t.Fatalf("join %s: %v", name, err)
}
return c.Bearer()
}
func writeF(t *testing.T, root, rel, content string) {
t.Helper()
p := filepath.Join(root, filepath.FromSlash(rel))
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func waitFor(path, want string, d time.Duration) bool {
deadline := time.Now().Add(d)
for time.Now().Before(deadline) {
if b, err := os.ReadFile(path); err == nil && string(b) == want {
return true
}
time.Sleep(50 * time.Millisecond)
}
return false
}
// TestRecordErrSurfacedAndCleared verifies that recordErr surfaces the error in
// StateSnapshot and that a subsequent recordSync clears it.
func TestRecordErrSurfacedAndCleared(t *testing.T) {
d, err := New(t.TempDir(), config.Daemon{}, "h", t.Logf)
if err != nil {
t.Fatal(err)
}
m := config.Mount{Share: "s", Local: t.TempDir(), Hub: "http://hub"}
k := mountKey(m)
// Register so StateSnapshot emits this mount.
d.mu.Lock()
d.mounts[k] = m
d.mu.Unlock()
d.recordErr(m, errors.New("boom"))
snap := d.StateSnapshot()
var got string
for _, ms := range snap.Mounts {
if mountKey(config.Mount{Share: ms.Share, Subpath: ms.Subpath, Local: ms.Local}) == k {
got = ms.LastErr
}
}
if got != "boom" {
t.Fatalf("expected LastErr=boom, got %q", got)
}
d.recordSync(m)
snap = d.StateSnapshot()
for _, ms := range snap.Mounts {
if mountKey(config.Mount{Share: ms.Share, Subpath: ms.Subpath, Local: ms.Local}) == k {
if ms.LastErr != "" {
t.Fatalf("expected LastErr cleared after recordSync, got %q", ms.LastErr)
}
if ms.LastSyncUnix == 0 {
t.Fatal("expected LastSyncUnix != 0 after recordSync")
}
}
}
}
// TestLiveTwoWaySyncViaSSE runs two daemons against one hub and proves a change
// on one machine reaches the other automatically (push -> SSE event -> pull).
func TestLiveTwoWaySyncViaSSE(t *testing.T) {
db, err := meta.Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
store, err := blobstore.NewDisk(t.TempDir())
if err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(hub.NewServer(db, store).Handler())
defer srv.Close()
bearerA := join(t, db, srv.URL, "alice")
bearerB := join(t, db, srv.URL, "bob")
ca := transport.New(srv.URL)
ca.SetBearer(bearerA)
if err := ca.Publish("s"); err != nil {
t.Fatal(err)
}
rootA, rootB := t.TempDir(), t.TempDir()
mount := func(hubURL, bearer, root string) (string, config.Daemon) {
return t.TempDir(), config.Daemon{
Hub: hubURL, Bearer: bearer,
Mounts: []config.Mount{{Share: "s", Local: root, Hub: hubURL}},
}
}
dirA, cfgA := mount(srv.URL, bearerA, rootA)
dirB, cfgB := mount(srv.URL, bearerB, rootB)
dA, err := New(dirA, cfgA, "alice", t.Logf)
if err != nil {
t.Fatal(err)
}
dB, err := New(dirB, cfgB, "bob", t.Logf)
if err != nil {
t.Fatal(err)
}
stop := startDaemons(t, dA, dB)
defer stop()
time.Sleep(200 * time.Millisecond) // let both subscribe before the first edit
// Alice creates a file -> it should reach Bob automatically.
writeF(t, rootA, "hello.txt", "from alice\n")
if !waitFor(filepath.Join(rootB, "hello.txt"), "from alice\n", 10*time.Second) {
t.Fatal("alice's file did not reach bob via live sync")
}
// Bob creates a file -> it should reach Alice automatically.
writeF(t, rootB, "reply.txt", "from bob\n")
if !waitFor(filepath.Join(rootA, "reply.txt"), "from bob\n", 10*time.Second) {
t.Fatal("bob's file did not reach alice via live sync")
}
}