Track lastErr per mount (mirroring lastSync pattern), cleared on
success so a recovered mount shows clean. Errors from LoadIgnoreWith,
Pull, and Sync all land in the map and surface through StateSnapshot
into 'devbox status' (human ⚠️ warning) and --json (last_err field).
160 lines
4.3 KiB
Go
160 lines
4.3 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"errors"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"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"
|
|
)
|
|
|
|
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)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go dA.Run(ctx)
|
|
go dB.Run(ctx)
|
|
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")
|
|
}
|
|
}
|