| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func policyInputsFixture(t *testing.T) (provider.Binding, provider.PolicyInputs) { |
| 13 | t.Helper() |
| 14 | root := t.TempDir() |
| 15 | home, work, scratch := filepath.Join(root, "account"), filepath.Join(root, "work"), filepath.Join(root, "scratch") |
| 16 | for _, p := range []string{home, work, scratch} { |
| 17 | mustJournal(t, os.Mkdir(p, 0700)) |
| 18 | } |
| 19 | id := provider.Identity{AccountID: "fixture", HomeID: "fixture", ProviderVersion: "0.165.0"} |
| 20 | mustJournal(t, os.WriteFile(filepath.Join(home, provider.IdentityFile), marshal(id), 0600)) |
| 21 | mustJournal(t, os.WriteFile(filepath.Join(home, "auth.json"), []byte(`{"fixture":true}`), 0600)) |
| 22 | l, err := provider.OpenAccount(context.Background(), provider.Binding{ID: "fixture", Provider: "codex", AccountID: id.AccountID, Expected: id, Home: home, RuntimeRoot: filepath.Join(root, "homes")}) |
| 23 | mustJournal(t, err) |
| 24 | t.Cleanup(func() { l.Close() }) |
| 25 | in := provider.PolicyInputs{Workspace: work, Scratch: scratch, ProviderVersion: id.ProviderVersion, LauncherVersion: "direct-v2", BwrapVersion: "0.11.0", Argv: []string{"/opt/codex"}, EgressHosts: []string{"chatgpt.com"}} |
| 26 | for _, name := range []string{"codex", "equipo-tool-proxy"} { |
| 27 | p := filepath.Join(root, name) |
| 28 | mustJournal(t, os.WriteFile(p, []byte(name), 0700)) |
| 29 | in.RuntimeFiles = append(in.RuntimeFiles, provider.Mount{Source: p, Target: "/opt/" + name, ReadOnly: true}) |
| 30 | } |
| 31 | return l.Binding(), in |
| 32 | } |
| 33 | func TestRuntimeMountsStandaloneToolProxy(t *testing.T) { |
| 34 | b, in := policyInputsFixture(t) |
| 35 | lease, err := b.AccountLease() |
| 36 | mustJournal(t, err) |
| 37 | mustJournal(t, lease.Close()) |
| 38 | c := configFixture(t) |
| 39 | c.GoRoot, c.ModuleCache = t.TempDir(), t.TempDir() |
| 40 | c.CodexPath = in.RuntimeFiles[0].Source |
| 41 | c.ToolProxyPath = in.RuntimeFiles[1].Source |
| 42 | c.CodexCodeModeHostPath = filepath.Join(filepath.Dir(c.ToolProxyPath), "code-mode-host") |
| 43 | c.Executable = filepath.Join(filepath.Dir(c.ToolProxyPath), "runner") |
| 44 | for _, path := range []string{c.CodexCodeModeHostPath, c.Executable} { |
| 45 | mustJournal(t, os.WriteFile(path, []byte(path), 0700)) |
| 46 | } |
| 47 | for _, path := range []string{c.WorkspaceRoot, c.ScratchRoot} { |
| 48 | mustJournal(t, os.Mkdir(path, 0700)) |
| 49 | } |
| 50 | binding := AccountBinding{ID: b.ID, LocalID: b.ID, Provider: b.Provider, Family: "gpt", Model: "model", AccountID: b.AccountID, ProviderHome: b.CredentialHome, ExpectedIdentity: b.Expected, Kind: "subscription"} |
| 51 | mustJournal(t, os.WriteFile(c.AccountBindingsPath, marshal([]AccountBinding{binding}), 0600)) |
| 52 | r := Runtime{cfg: c} |
| 53 | build := func(t *testing.T) provider.Policy { |
| 54 | r.cfg = c |
| 55 | source, err := c.DirectBinding(binding) |
| 56 | mustJournal(t, err) |
| 57 | account, err := provider.OpenAccount(context.Background(), source) |
| 58 | mustJournal(t, err) |
| 59 | l, cleanup, err := r.constructDirectLease(context.Background(), provider.Selection{Provider: b.Provider, Family: "gpt", Model: "model", AccountID: b.AccountID}, c.WorkspaceRoot, "implement", account) |
| 60 | if cleanup != nil { |
| 61 | defer cleanup() |
| 62 | } |
| 63 | mustJournal(t, err) |
| 64 | for _, m := range l.Policy.Mounts { |
| 65 | if m.Target == "/opt/equipo-tool-proxy" && m.Source != c.ToolProxyPath { |
| 66 | t.Fatal("runner executable entered provider policy") |
| 67 | } |
| 68 | } |
| 69 | var concrete struct{ Scratch string } |
| 70 | mustJournal(t, json.Unmarshal(l.Policy.Manifest, &concrete)) |
| 71 | inputs, err := PolicyInputs(context.Background(), c, l.Selection, l.Binding, c.WorkspaceRoot, "implement", concrete.Scratch) |
| 72 | mustJournal(t, err) |
| 73 | rebuilt, err := provider.BuildPolicy(l.Binding, inputs) |
| 74 | mustJournal(t, err) |
| 75 | if rebuilt.Hash != l.Policy.Hash { |
| 76 | t.Fatal("the policy built from PolicyInputs differs from the runtime policy") |
| 77 | } |
| 78 | return l.Policy |
| 79 | } |
| 80 | for _, project := range []string{"go", "non-go"} { |
| 81 | t.Run("missing-modules-"+project, func(t *testing.T) { |
| 82 | saved := c.ModuleCache |
| 83 | c.ModuleCache = filepath.Join(t.TempDir(), "absent") |
| 84 | defer func() { c.ModuleCache = saved }() |
| 85 | if project == "go" { |
| 86 | mustJournal(t, os.WriteFile(filepath.Join(c.WorkspaceRoot, "go.mod"), []byte("module fixture\n\ngo 1.26\n"), 0600)) |
| 87 | defer os.Remove(filepath.Join(c.WorkspaceRoot, "go.mod")) |
| 88 | } |
| 89 | for _, m := range build(t).Mounts { |
| 90 | if m.Target == "/opt/go-mod" { |
| 91 | t.Fatal("missing cache mounted") |
| 92 | } |
| 93 | } |
| 94 | }) |
| 95 | } |
| 96 | first := build(t) |
| 97 | mustJournal(t, os.WriteFile(c.Executable, []byte("another runner build"), 0700)) |
| 98 | if build(t).Hash != first.Hash { |
| 99 | t.Fatal("the runner executable changed the provider policy hash") |
| 100 | } |
| 101 | mustJournal(t, os.WriteFile(c.ToolProxyPath, []byte("changed helper"), 0700)) |
| 102 | if build(t).Hash == first.Hash { |
| 103 | t.Fatal("helper digest not bound to policy") |
| 104 | } |
| 105 | } |
| 106 | |