oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build e2elive
2
3package e2elivetest
4
5import (
6 "encoding/json"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strconv"
11 "strings"
12 "sync"
13 "syscall"
14 "testing"
15 "time"
16
17 "amadan.net/oficina/equipo/internal/runner"
18)
19
20func liveEnvironment(t *testing.T, root string) map[string]string {
21 t.Helper()
22 home, err := os.UserHomeDir()
23 executionMust(t, err)
24 source := os.Getenv("EQUIPO_E2E_ENV")
25 if source == "" {
26 source = filepath.Join(home, ".local/share/equipo-runner/service.env")
27 }
28 raw, err := os.ReadFile(source)
29 executionMust(t, err)
30 env := map[string]string{}
31 for _, line := range strings.Split(string(raw), "\n") {
32 k, v, ok := strings.Cut(line, "=")
33 if !ok || !strings.HasPrefix(k, "EQUIPO_") {
34 continue
35 }
36 if strings.HasPrefix(v, `"`) {
37 v, err = strconv.Unquote(v)
38 executionMust(t, err)
39 }
40 env[k] = v
41 }
42 raw, err = os.ReadFile(env["EQUIPO_ACCOUNT_BINDINGS_PATH"])
43 executionMust(t, err)
44 var bindings []runner.AccountBinding
45 executionMust(t, json.Unmarshal(raw, &bindings))
46 var selected []runner.AccountBinding
47 for _, b := range bindings {
48 if b.LocalID == "claude-a" && b.Provider == "claude" && b.AccountID == "paul@tito.io" && b.ExpectedIdentity.ProviderVersion == "2.1.312" {
49 selected = append(selected, b)
50 }
51 }
52 if len(selected) != 1 {
53 t.Fatal("stage account selection: require dedicated claude-a / paul@tito.io / 2.1.312")
54 }
55 for key, name := range map[string]string{"EQUIPO_RUNNER_STATE_DIR": "state", "EQUIPO_WORKSPACE_ROOT": "work", "EQUIPO_SCRATCH_ROOT": "scratch"} {
56 env[key] = filepath.Join(root, name)
57 executionMust(t, os.Mkdir(env[key], 0700))
58 }
59 env["EQUIPO_ACCOUNT_BINDINGS_PATH"] = filepath.Join(root, "bindings.json")
60 raw, err = json.Marshal(selected)
61 executionMust(t, err)
62 executionMust(t, os.WriteFile(env["EQUIPO_ACCOUNT_BINDINGS_PATH"], raw, 0600))
63 // The coordinator can propose setup without any forge credential.
64 env["EQUIPO_AMADAN_CREDENTIAL_PATH"] = filepath.Join(root, "amadan.json")
65 executionMust(t, os.WriteFile(env["EQUIPO_AMADAN_CREDENTIAL_PATH"], []byte(`{"repositories":{"amadan.net/oficina/e2e-unknown-repo":""}}`), 0600))
66 return env
67}
68func liveBuild(t *testing.T, path, pkg string) {
69 t.Helper()
70 args := []string{"build", "-o", path, pkg}
71 cmd := exec.Command("go", args...)
72 cmd.Dir = "../.."
73 cmd.Env = append(os.Environ(), "CGO_ENABLED=0")
74 if raw, err := cmd.CombinedOutput(); err != nil {
75 t.Fatalf("stage build %s: %v\n%s", pkg, err, raw)
76 }
77}
78func liveStart(t *testing.T, root string, env map[string]string, bin string, args ...string) string {
79 log, _ := liveProcess(t, root, env, bin, args...)
80 return log
81}
82func liveProcess(t *testing.T, root string, env map[string]string, bin string, args ...string) (string, func()) {
83 t.Helper()
84 path := filepath.Join(root, filepath.Base(bin)+".log")
85 f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
86 executionMust(t, err)
87 cmd := exec.Command(bin, args...)
88 cmd.Dir = root
89 for _, entry := range os.Environ() {
90 k, _, _ := strings.Cut(entry, "=")
91 if !strings.HasPrefix(k, "EQUIPO_") && !strings.HasPrefix(k, "CARLOS_") && !strings.HasPrefix(k, "LISTEN_") {
92 cmd.Env = append(cmd.Env, entry)
93 }
94 }
95 for k, v := range env {
96 cmd.Env = append(cmd.Env, k+"="+v)
97 }
98 cmd.Stdout = f
99 cmd.Stderr = f
100 executionMust(t, cmd.Start())
101 var once sync.Once
102 stop := func() {
103 once.Do(func() {
104 cmd.Process.Signal(syscall.SIGTERM)
105 done := make(chan error, 1)
106 go func() { done <- cmd.Wait() }()
107 select {
108 case <-done:
109 case <-time.After(35 * time.Second):
110 cmd.Process.Kill()
111 <-done
112 }
113 f.Close()
114 })
115 }
116 t.Cleanup(stop)
117 return path, stop
118}
119func liveWait(t *testing.T, stage string, limit time.Duration, ready func() bool) {
120 t.Helper()
121 deadline := time.Now().Add(limit)
122 for time.Now().Before(deadline) {
123 if ready() {
124 return
125 }
126 time.Sleep(250 * time.Millisecond)
127 }
128 t.Fatalf("stage %s: timed out after %s", stage, limit)
129}
130