oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "context"
5 "crypto/x509"
6 "io"
7 "net/http"
8 "os"
9 "path/filepath"
10 "slices"
11 "strings"
12 "testing"
13 "time"
14
15 "amadan.net/oficina/equipo/internal/runnerproto"
16)
17
18func configFixture(t *testing.T) Config {
19 t.Helper()
20 root := t.TempDir()
21 amadan := filepath.Join(root, "amadan")
22 mustJournal(t, os.WriteFile(amadan, []byte("#!/bin/sh\nexit 0\n"), 0700))
23 return Config{BwrapPath: "/usr/bin/bwrap", ClaudePath: "/usr/bin/claude", CodexCodeModeHostPath: "/usr/bin/codex-code-mode-host", Origin: "https://equipo.example", StateDir: filepath.Join(root, "state"), AmadanCredentialPath: filepath.Join(root, "forge.json"), AccountBindingsPath: filepath.Join(root, "bindings.json"), CodexPath: "/usr/bin/codex", GoRoot: "/opt/go", GoBinary: "/opt/go/bin/go", WorkspaceRoot: filepath.Join(root, "work"), ScratchRoot: filepath.Join(root, "scratch"), GitPath: "/usr/bin/git", AmadanPath: amadan, Executable: "/usr/bin/equipo-runner", InstanceID: "instance"}
24}
25func TestRunnerConfig(t *testing.T) {
26 c := configFixture(t)
27 mustJournal(t, c.validate())
28 policy, err := bindingPolicy(c, nil, AccountBinding{})
29 if err != nil || c.securityPaths(nil).ForgeCredential != c.AmadanCredentialPath || !slices.Contains(policy.DeniedPaths, c.AmadanCredentialPath) {
30 t.Fatal("forge credentials absent from protected paths", err)
31 }
32 for _, origin := range []string{"http://example.com", "https://user:pass@example.com", "https://example.com/path", "https://example.com?x=y", "https://example.com/#x"} {
33 bad := c
34 bad.Origin = origin
35 if bad.validate() == nil {
36 t.Fatalf("accepted unsafe origin %s", origin)
37 }
38 }
39 bad := c
40 bad.StateDir = "relative"
41 if bad.validate() == nil {
42 t.Fatal("accepted relative state")
43 }
44 if _, err := LoadConfig(func(string) string { return "" }); err == nil {
45 t.Fatal("accepted missing config")
46 }
47}
48func TestRunnerServiceLifecycle(t *testing.T) {
49 c := configFixture(t)
50 lock, err := lockState(c.StateDir)
51 if err != nil {
52 t.Fatal(err)
53 }
54 if second, err := lockState(c.StateDir); err == nil {
55 second.Close()
56 t.Fatal("second process acquired lock")
57 }
58 st, err := os.Stat(c.StateDir)
59 if err != nil || st.Mode().Perm() != 0700 {
60 t.Fatal("state is not private", err)
61 }
62 lock.Close()
63 lock, err = lockState(c.StateDir)
64 if err != nil {
65 t.Fatal("lock not released", err)
66 }
67 lock.Close()
68 path := filepath.Join(c.StateDir, "credentials.json")
69 credential := runnerproto.CredentialRotation{Bearer: runnerproto.RunnerCredential{Version: 1, Token: "secret", ExpiresAt: time.Now().Add(time.Hour)}}
70 if err := runnerproto.JournalCredentials(path, credential); err != nil {
71 t.Fatal(err)
72 }
73 got, err := readCredentials(path)
74 if err != nil || got.Bearer.Token != "secret" {
75 t.Fatal("credentials not restored", err)
76 }
77 st, _ = os.Stat(path)
78 if st.Mode().Perm() != 0600 {
79 t.Fatal("credential mode")
80 }
81 if err := os.Chmod(path, 0644); err != nil {
82 t.Fatal(err)
83 }
84 if _, err = readCredentials(path); err == nil {
85 t.Fatal("accepted exposed credentials")
86 }
87}
88
89type transportFunc func(*http.Request) (*http.Response, error)
90
91func (f transportFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
92func TestRunnerHTTPClient(t *testing.T) {
93 j, err := OpenJournal(context.Background(), filepath.Join(t.TempDir(), "private", "j.db"))
94 if err != nil {
95 t.Fatal(err)
96 }
97 defer j.Close()
98 c, err := NewHTTPInstance("https://equipo.example/api/runner/v2", j, runnerproto.RunnerCredential{Token: "secret", ExpiresAt: time.Now().Add(time.Hour)})
99 if err != nil {
100 t.Fatal(err)
101 }
102 calls := 0
103 c.client.Transport = transportFunc(func(r *http.Request) (*http.Response, error) {
104 calls++
105 if r.URL.Host != "equipo.example" || r.Header.Get("Authorization") != "Bearer secret" {
106 t.Fatal("bearer escaped origin")
107 }
108 return &http.Response{StatusCode: 302, Header: http.Header{"Location": []string{"https://evil.example/"}}, Body: io.NopCloser(strings.NewReader("")), Request: r}, nil
109 })
110 if _, err = c.Poll(context.Background()); err == nil || calls != 1 {
111 t.Fatal("followed redirect", err, calls)
112 }
113 c.bearer.ExpiresAt = time.Now().Add(-time.Second)
114 if _, err = c.Poll(context.Background()); err == nil || calls != 1 {
115 t.Fatal("expired credential reused")
116 }
117}
118
119func TestRunnerHTTPClientTLSFailure(t *testing.T) {
120 j, err := OpenJournal(context.Background(), filepath.Join(t.TempDir(), "private/j.db"))
121 if err != nil {
122 t.Fatal(err)
123 }
124 defer j.Close()
125 c, err := NewHTTPInstance("https://equipo.example/api/runner/v2", j, runnerproto.RunnerCredential{Token: "secret", ExpiresAt: time.Now().Add(time.Hour)})
126 if err != nil {
127 t.Fatal(err)
128 }
129 calls := 0
130 c.client.Transport = transportFunc(func(*http.Request) (*http.Response, error) { calls++; return nil, x509.UnknownAuthorityError{} })
131 if _, err = c.Poll(context.Background()); err == nil || calls != 1 {
132 t.Fatal("TLS failure bypassed", err, calls)
133 }
134}
135
136func TestAccountHomeConfiguredRuntime(t *testing.T) {
137 c := configFixture(t)
138 b := AccountBinding{ProviderHome: filepath.Join(t.TempDir(), "dedicated")}
139 if _, e := c.DirectBinding(b); e != nil {
140 t.Fatal(e)
141 }
142 for _, path := range []string{"/", "relative", b.ProviderHome, filepath.Join(b.ProviderHome, "nested")} {
143 bad := c
144 bad.ScratchRoot = path
145 if _, e := bad.DirectBinding(b); e == nil {
146 t.Fatalf("unsafe runtime root accepted: %s", path)
147 }
148 }
149}
150
151func TestRunnerConfigAmadanPath(t *testing.T) {
152 c := configFixture(t)
153 command := filepath.Join(t.TempDir(), "amadan")
154 t.Chdir(filepath.Dir(command))
155 if err := os.WriteFile(command, []byte("#!/bin/sh\nexit 0\n"), 0700); err != nil {
156 t.Fatal(err)
157 }
158 env := map[string]string{
159 "EQUIPO_ORIGIN": c.Origin, "EQUIPO_INSTANCE_ID": c.InstanceID,
160 "EQUIPO_RUNNER_STATE_DIR": c.StateDir, "EQUIPO_AMADAN_CREDENTIAL_PATH": c.AmadanCredentialPath,
161 "EQUIPO_ACCOUNT_BINDINGS_PATH": c.AccountBindingsPath, "EQUIPO_CODEX_PATH": c.CodexPath,
162 "EQUIPO_GO_ROOT": c.GoRoot, "EQUIPO_GO_BINARY": c.GoBinary,
163 "EQUIPO_WORKSPACE_ROOT": c.WorkspaceRoot, "EQUIPO_SCRATCH_ROOT": c.ScratchRoot,
164 "EQUIPO_GIT_PATH": c.GitPath, "EQUIPO_RUNNER_EXECUTABLE": c.Executable,
165 "EQUIPO_AMADAN_PATH": command, "EQUIPO_BWRAP_PATH": c.BwrapPath, "EQUIPO_CLAUDE_PATH": c.ClaudePath, "EQUIPO_CODEX_CODE_MODE_HOST_PATH": c.CodexCodeModeHostPath,
166 }
167 if _, err := LoadConfig(func(k string) string { return env[k] }); err != nil {
168 t.Fatal(err)
169 }
170 for _, mode := range []string{"unset", "relative", "unclean", "missing", "directory", "not_executable"} {
171 t.Run(mode, func(t *testing.T) {
172 path := command
173 switch mode {
174 case "unset":
175 path = ""
176 case "relative":
177 path = "amadan"
178 case "unclean":
179 path = filepath.Dir(command) + "/./amadan"
180 case "missing":
181 path += "-missing"
182 case "directory":
183 path = filepath.Dir(command)
184 case "not_executable":
185 path += "-not-executable"
186 if err := os.WriteFile(path, []byte("exit 0"), 0600); err != nil {
187 t.Fatal(err)
188 }
189 }
190 env["EQUIPO_AMADAN_PATH"] = path
191 _, err := LoadConfig(func(k string) string { return env[k] })
192 _, variable := Diagnostic(err)
193 if err == nil || variable != "EQUIPO_AMADAN_PATH" {
194 t.Fatalf("invalid Amadan path accepted or misdiagnosed: %v (%s)", err, variable)
195 }
196 })
197 }
198}
199
200// A Mac worker needs its roots, git and itself, nothing of the Linux
201// runner's harnesses; the same configuration on Linux is refused.
202func TestMacWorkerConfig(t *testing.T) {
203 root := t.TempDir()
204 c := Config{Origin: "https://equipo.example", InstanceID: "instance", StateDir: filepath.Join(root, "state"), WorkspaceRoot: filepath.Join(root, "work"), ScratchRoot: filepath.Join(root, "scratch"), GitPath: "/usr/bin/git", Executable: "/Users/equipo-paul/.local/lib/equipo-runner/equipo-runner"}
205 mustJournal(t, c.validateFor("darwin"))
206 if c.validateFor("linux") == nil {
207 t.Fatal("a Linux runner without bubblewrap or harnesses accepted")
208 }
209 for name, bad := range map[string]func(*Config){
210 "origin": func(c *Config) { c.Origin = "http://example.com" },
211 "instance": func(c *Config) { c.InstanceID = "" },
212 "git": func(c *Config) { c.GitPath = "git" },
213 "scratch inside": func(c *Config) { c.ScratchRoot = filepath.Join(c.StateDir, "scratch") },
214 "state inside": func(c *Config) { c.StateDir = filepath.Join(c.WorkspaceRoot, "state") },
215 } {
216 b := c
217 bad(&b)
218 if b.validateFor("darwin") == nil {
219 t.Fatalf("%s accepted", name)
220 }
221 }
222}
223
224func TestPreviewHostDefaultsToPrivateDevbox(t *testing.T) {
225 for _, kind := range []string{"", "devbox", "carloku_temp", "off"} {
226 c, _ := LoadConfig(func(k string) string {
227 if k == "EQUIPO_PREVIEW_HOST" {
228 return kind
229 }
230 return ""
231 })
232 want := kind
233 if want == "" {
234 want = "devbox"
235 }
236 if c.PreviewHost != want {
237 t.Fatalf("host %q: got %q want %q", kind, c.PreviewHost, want)
238 }
239 }
240}
241