Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Code

527 commit(s) on main

Put job execution behind an interface Keenan Nemetz · 2h ago de3eec16
history blame raw 2.8 KiB
1package server_test
2
3// TestServeAcceptsAnInheritedListener is the accept test for socket activation:
4// a listener opened here, marked inheritable, is handed to a real `vole serve`
5// subprocess the way launchd or systemd would, and the process serves the forge
6// on it. No launchd is involved, so the test exercises the fd handoff directly.
7
8import (
9 "fmt"
10 "io"
11 "net"
12 "net/http"
13 "os"
14 "os/exec"
15 "path/filepath"
16 "strings"
17 "syscall"
18 "testing"
19 "time"
20)
21
22func TestServeAcceptsAnInheritedListener(t *testing.T) {
23 if binPath == "" {
24 t.Skip("the serve binary was not built by TestMain")
25 }
26 if fixtureTemplate == "" {
27 t.Fatal("the fixture template was not built by TestMain")
28 }
29
30 // A throwaway instance: the template carries every schema, so serve has a
31 // valid empty database to open.
32 dir := t.TempDir()
33 copyTree(t, fixtureTemplate, dir)
34 if err := os.MkdirAll(filepath.Join(dir, "repos"), 0o755); err != nil {
35 t.Fatal(err)
36 }
37 cfgPath := filepath.Join(dir, "vole.toml")
38 // http_addr/ssh_addr are deliberately loopback: the inherited fd wins, and
39 // these are only the fallback. ssh_listen = none avoids opening an SSH
40 // listener the test does not need.
41 cfg := fmt.Sprintf("data_dir = %q\nhttp_addr = %q\nssh_addr = %q\nssh_listen = \"none\"\n",
42 dir, "127.0.0.1:1", "127.0.0.1:1")
43 if err := os.WriteFile(cfgPath, []byte(cfg), 0o600); err != nil {
44 t.Fatal(err)
45 }
46
47 // Open the listener the manager would own and hand over.
48 ln, err := net.Listen("tcp", "127.0.0.1:0")
49 if err != nil {
50 t.Fatal(err)
51 }
52 addr := ln.Addr().String()
53 file, err := ln.(*net.TCPListener).File()
54 if err != nil {
55 t.Fatal(err)
56 }
57 defer file.Close()
58 ln.Close()
59
60 cmd := exec.Command(binPath, "serve", "--config", cfgPath)
61 // ExtraFiles becomes file descriptor 3 in the child, the first inherited
62 // descriptor. LISTEN_FDS declares one; no LISTEN_PID means the child accepts
63 // it, the same as launchd's socket handoff.
64 cmd.ExtraFiles = []*os.File{file}
65 cmd.Env = append(os.Environ(), "LISTEN_FDS=1")
66 if err := cmd.Start(); err != nil {
67 t.Fatal(err)
68 }
69 defer func() {
70 _ = cmd.Process.Signal(syscall.SIGTERM)
71 _ = cmd.Wait()
72 }()
73
74 // The child serves the forge on the inherited descriptor. Poll briefly for
75 // startup, then ask the unauthenticated health route.
76 var resp *http.Response
77 deadline := time.Now().Add(10 * time.Second)
78 for time.Now().Before(deadline) {
79 client := &http.Client{Timeout: 2 * time.Second}
80 resp, err = client.Get("http://" + addr + "/-/healthz")
81 if err == nil {
82 break
83 }
84 time.Sleep(100 * time.Millisecond)
85 }
86 if err != nil {
87 t.Fatalf("health request to inherited listener: %v", err)
88 }
89 defer resp.Body.Close()
90 body, err := io.ReadAll(resp.Body)
91 if err != nil {
92 t.Fatal(err)
93 }
94 if resp.StatusCode != http.StatusOK || strings.TrimSpace(string(body)) != "ok" {
95 t.Fatalf("health = %d %q, want 200 ok", resp.StatusCode, body)
96 }
97}
98