package activator import ( "context" "fmt" "io" "net" "net/http" "os" "path/filepath" "strings" "testing" "time" "github.com/carlosframework/platform/internal/registry" ) // serveOn binds an HTTP server at sock that answers body, the way a // generation's process would. It returns the server so a test can stop it. func serveOn(t *testing.T, sock, body string) *http.Server { t.Helper() ln, err := net.Listen("unix", sock) if err != nil { if strings.Contains(err.Error(), "operation not permitted") { t.Skipf("sandbox forbids unix sockets: %v", err) } t.Fatalf("listen %s: %v", sock, err) } srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { io.WriteString(w, body) })} go srv.Serve(ln) t.Cleanup(func() { srv.Close() }) return srv } // dialAsOutsider GETs / over a plain unix dial of path, the way a member of // the carlos-sockets group dials an instance: no unixdial checks, so the // kernel follows a symlink at path. func dialAsOutsider(t *testing.T, path string) string { t.Helper() c := &http.Client{Timeout: 2 * time.Second, Transport: &http.Transport{ DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { return (&net.Dialer{}).DialContext(ctx, "unix", path) }, }} resp, err := c.Get("http://carlos/") if err != nil { t.Fatalf("outside dial of %s: %v", path, err) } defer resp.Body.Close() b, _ := io.ReadAll(resp.Body) return string(b) } // servingProc is a fake child whose Stop also stops its server, so the // generation's socket goes away with it. type servingProc struct { Proc srv *http.Server } func (p *servingProc) Stop(ctx context.Context) error { p.srv.Close() return p.Proc.Stop(ctx) } // sockGenBackend is genFakeBackend whose generations really serve: perGen // binds /g.sock, otherwise rt.Socket, each answering "gen ". type sockGenBackend struct { *fakeBackend t *testing.T dir string } func (b *sockGenBackend) StartGen(ctx context.Context, rt registry.Route, n uint64, perGen bool) (Proc, Gen, error) { proc, ver, err := b.fakeBackend.Start(ctx, rt) if err != nil { return proc, Gen{}, err } sock := rt.Socket if perGen { sock = filepath.Join(b.dir, fmt.Sprintf("g%d.sock", n)) } srv := serveOn(b.t, sock, fmt.Sprintf("gen %d", n)) return &servingProc{Proc: proc, srv: srv}, Gen{N: n, Socket: sock, Control: sock + ".control", Build: ver}, nil } func (b *sockGenBackend) NextDueVia(ctx context.Context, rt registry.Route, _ string) (int64, error) { return b.fakeBackend.NextDue(ctx, rt) } // sockGenActivator is a Service over sockGenBackend, its sockets in a short // directory (run with TMPDIR=/tmp/k), and a route whose canonical socket is // /c.sock. func sockGenActivator(t *testing.T, on bool) (*Service, registry.Route) { t.Helper() withGenerations(t, on) dir := shortRunDir(t) reg, err := registry.Open(filepath.Join(dir, "r.db")) if err != nil { t.Fatal(err) } a := New(Config{Bucket: "test-bucket", Idle: time.Hour}, &sockGenBackend{fakeBackend: &fakeBackend{}, t: t, dir: dir}, reg) rt := testRoute("link.example.com") rt.Socket = filepath.Join(dir, "c.sock") return a, rt } func isSymlink(path string) bool { fi, err := os.Lstat(path) return err == nil && fi.Mode()&os.ModeSymlink != 0 } // The helper plan 3 calls at every switch: it replaces whatever is at the // canonical path, and leaves no temporary link behind. func TestSwapCanonicalLinkReplacesAtomically(t *testing.T) { dir := t.TempDir() canonical := filepath.Join(dir, "c.sock") if err := os.WriteFile(canonical, []byte("stale socket file"), 0o600); err != nil { t.Fatal(err) } for _, target := range []string{"/run/carlos/g/aaaaaaaaaaaa.1.sock", "/run/carlos/g/aaaaaaaaaaaa.2.sock"} { if err := swapCanonicalLink(canonical, target); err != nil { t.Fatalf("swapCanonicalLink(%s): %v", target, err) } if got, err := os.Readlink(canonical); err != nil || got != target { t.Fatalf("canonical names %q (%v), want %q", got, err, target) } } if _, err := os.Lstat(canonical + ".lnk"); !os.IsNotExist(err) { t.Fatalf("temporary link left behind (lstat: %v)", err) } } // A process dialling the canonical path follows the link to whichever // generation it names, before and after a switch. func TestSwapCanonicalLinkMovesOutsideDiallers(t *testing.T) { dir := shortRunDir(t) g1, g2 := filepath.Join(dir, "g1.sock"), filepath.Join(dir, "g2.sock") serveOn(t, g1, "one") serveOn(t, g2, "two") canonical := filepath.Join(dir, "c.sock") if err := swapCanonicalLink(canonical, g1); err != nil { t.Fatal(err) } if got := dialAsOutsider(t, canonical); got != "one" { t.Fatalf("before the switch: %q, want one", got) } if err := swapCanonicalLink(canonical, g2); err != nil { t.Fatal(err) } if got := dialAsOutsider(t, canonical); got != "two" { t.Fatalf("after the switch: %q, want two", got) } } func TestRemoveCanonicalLinkRemovesOnlyItsOwnLink(t *testing.T) { dir := t.TempDir() canonical := filepath.Join(dir, "c.sock") if err := swapCanonicalLink(canonical, "/a"); err != nil { t.Fatal(err) } if err := removeCanonicalLink(canonical, "/b"); err != nil || !isSymlink(canonical) { t.Fatalf("a link to another generation was removed (err %v)", err) } if err := removeCanonicalLink(canonical, "/a"); err != nil { t.Fatal(err) } if _, err := os.Lstat(canonical); !os.IsNotExist(err) { t.Fatalf("own link not removed (lstat: %v)", err) } if err := os.WriteFile(canonical, nil, 0o600); err != nil { t.Fatal(err) } if err := removeCanonicalLink(canonical, "/a"); err != nil || !isRegular(canonical) { t.Fatalf("a real file at the canonical path was removed (err %v)", err) } } // Spec ยง7.1 rev 15, end to end through the Service: the link appears when // the generation becomes current, an outside dialler reaches that // generation through it, and it goes when the entry stops. func TestCanonicalPathLinksToTheCurrentGeneration(t *testing.T) { requireUnixSockets(t) a, rt := sockGenActivator(t, true) arrangeAwake(t, a, rt) g, _ := a.ServingGen(rt.Host) if target, err := os.Readlink(rt.Socket); err != nil || target != g.Socket { t.Fatalf("canonical path names %q (%v), want the current generation %q", target, err, g.Socket) } if got, want := dialAsOutsider(t, rt.Socket), fmt.Sprintf("gen %d", g.N); got != want { t.Fatalf("outside dial of the canonical path got %q, want %q", got, want) } stopAndWait(t, a, rt.Host) if _, err := os.Lstat(rt.Socket); !os.IsNotExist(err) { t.Fatalf("canonical link outlived its entry (lstat: %v)", err) } } // Flag off: the canonical path is the app's own socket, never a link. func TestCanonicalPathIsARealSocketWithGenerationsOff(t *testing.T) { requireUnixSockets(t) a, rt := sockGenActivator(t, false) arrangeAwake(t, a, rt) if isSymlink(rt.Socket) || !isSocket(rt.Socket) { t.Fatalf("%s is not a real socket with generations off", rt.Socket) } g, _ := a.ServingGen(rt.Host) if got, want := dialAsOutsider(t, rt.Socket), fmt.Sprintf("gen %d", g.N); got != want { t.Fatalf("outside dial got %q, want %q", got, want) } } // A generation that IS on the canonical socket (a unit tenant, a backend // without generations) is never linked, even with the switch on: the link // would replace the real socket. func TestNoCanonicalLinkWhenTheGenerationOwnsTheCanonicalSocket(t *testing.T) { withGenerations(t, true) a, _, _, _ := testActivator(t, time.Hour) rt := genTestRoute(t, "plain-link.example.com") arrangeAwake(t, a, rt) if _, err := os.Lstat(rt.Socket); !os.IsNotExist(err) { t.Fatalf("something was created at the canonical path (lstat: %v)", err) } } // Flag off, after an agent that ran with generations on died without // stopping its entries: the leftover link is cleared so the app can bind. func TestStartGenWithGenerationsOffClearsALeftoverCanonicalLink(t *testing.T) { requireUnixSockets(t) b := testExecBackend(t) b.RunDirOverride = shortRunDir(t) host := "leftover.example.com" stageServingLive(t, b, host) rt := flatGenRoute(b, host) if err := os.Symlink(filepath.Join(b.RunDirOverride, "g", "gone.1.sock"), rt.Socket); err != nil { t.Fatal(err) } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() proc, _, err := b.StartGen(ctx, rt, 1, false) if err != nil { t.Fatalf("StartGen over a leftover link: %v", err) } defer proc.Stop(context.Background()) if !isSocket(rt.Socket) { t.Fatalf("%s is not the app's socket", rt.Socket) } }