oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipotest
2
3import (
4 "context"
5 "net/http/httptest"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "amadan.net/oficina/equipo/internal/equipo"
12)
13
14func TestExecutionStartupClosed(t *testing.T) {
15 d, path := executionDB(t)
16 executionMust(t, os.Chmod(filepath.Dir(path), 0700))
17 s := equipo.NewStore(d)
18 cfg := testConfig(testOrigin)
19 marker := filepath.Join(t.TempDir(), "external", "witness")
20 cfg.ExecutionMarkerPath = marker
21 x, err := equipo.OpenExecution(context.Background(), d, path, cfg, s)
22 if err != nil || x != nil {
23 t.Fatal("disabled execution opened", err)
24 }
25 if _, err = os.Stat(filepath.Dir(marker)); !os.IsNotExist(err) {
26 t.Fatal("disabled execution touched witness")
27 }
28 cfg.ExecutionEnabled = true
29 cfg.ExecutionMarkerPath = ""
30 if x, err = equipo.OpenExecution(context.Background(), d, path, cfg, s); err == nil || x != nil {
31 t.Fatal("enabled without external witness")
32 }
33 cfg.ExecutionMarkerPath = filepath.Join(filepath.Dir(path), "witness")
34 if x, err = equipo.OpenExecution(context.Background(), d, path, cfg, s); err == nil || x != nil {
35 t.Fatal("witness shares database history directory")
36 }
37 cfg.ExecutionMarkerPath = marker
38 x, err = equipo.OpenExecution(context.Background(), d, path, cfg, s)
39 executionMust(t, err)
40 defer x.Close()
41 st, err := x.Core.Status(context.Background())
42 executionMust(t, err)
43 if st.AdmissionPaused || st.Quarantine {
44 t.Fatalf("fresh enabled startup did not resume: %+v", st)
45 }
46 if _, err = equipo.AppWithExecution(d, cfg, &capturingMailer{}, nil, x); err != nil {
47 t.Fatal(err)
48 }
49 if err = x.Close(); err != nil {
50 t.Fatal(err)
51 }
52 if scalar(t, d, "SELECT admission_paused FROM service WHERE id=1") != 1 {
53 t.Fatal("core not closed before database")
54 }
55 if err = d.Writer().Ping(); err != nil {
56 t.Fatal("core closed shared database", err)
57 }
58}
59func TestExecutionHibernateWitness(t *testing.T) {
60 d, path := executionDB(t)
61 cfg := testConfig(testOrigin)
62 cfg.ExecutionEnabled = true
63 cfg.ExecutionMarkerPath = filepath.Join(t.TempDir(), "external", "witness")
64 open := func() *equipo.Execution {
65 x, err := equipo.OpenExecution(context.Background(), d, path, cfg, equipo.NewStore(d))
66 executionMust(t, err)
67 return x
68 }
69 x := open()
70 executionMust(t, x.Close())
71 oldWitness, err := os.ReadFile(cfg.ExecutionMarkerPath)
72 executionMust(t, err)
73 x = open()
74 st, err := x.Core.Status(context.Background())
75 executionMust(t, err)
76 if st.Quarantine {
77 t.Fatal("preserved witness quarantined")
78 }
79 executionMust(t, x.Close())
80 executionMust(t, os.WriteFile(cfg.ExecutionMarkerPath, oldWitness, 0600))
81 x = open()
82 st, err = x.Core.Status(context.Background())
83 executionMust(t, err)
84 if !st.Quarantine || !st.AdmissionPaused {
85 t.Fatal("mismatched witness admitted dispatch")
86 }
87 executionMust(t, x.Close())
88 executionMust(t, os.Remove(cfg.ExecutionMarkerPath))
89 x = open()
90 defer x.Close()
91 st, err = x.Core.Status(context.Background())
92 executionMust(t, err)
93 if !st.Quarantine || !st.AdmissionPaused {
94 t.Fatal("lost witness admitted dispatch")
95 }
96}
97
98func TestDirectIdentityChangesFenceCreateAndTurn(t *testing.T) {
99 for _, change := range []string{"removed", "provider", "launcher", "configuration"} {
100 t.Run(change, func(t *testing.T) {
101 f := runnerFixtureNew(t, true)
102 q := f.request()
103 q.RequestID = "before-create"
104 if _, err := f.x.Mutate(connectionContext, f.principal, "validate", q); err != nil {
105 t.Fatal(err)
106 }
107 switch change {
108 case "removed":
109 mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", ownerAddress)
110 case "demoted":
111 mustSQL(t, f.d, "UPDATE members SET role='member' WHERE address=?", ownerAddress)
112 default:
113 reports := append([]equipo.ConnectionReport(nil), f.reports...)
114 switch change {
115 case "provider":
116 reports[1].ProviderVersion = "next"
117 case "launcher":
118 reports[1].LauncherVersion = "next"
119 case "configuration":
120 reports[1].IsolationConfigHash = strings.Repeat("c", 64)
121 }
122 executionMust(t, f.s.ReportConnections(connectionContext, f.principal, reports))
123 }
124 for _, phase := range []string{"before-create", "before-turn"} {
125 q.RequestID = phase
126 if _, err := f.x.Mutate(connectionContext, f.principal, "validate", q); err == nil {
127 t.Fatal("stale identity authorized", phase)
128 }
129 }
130 })
131 }
132}
133
134func TestExecutionStartupReconciliation(t *testing.T) {
135 d, path := executionDB(t)
136 s := equipo.NewStore(d)
137 cfg := testConfig(testOrigin)
138 cfg.ExecutionEnabled = true
139 cfg.ExecutionMarkerPath = filepath.Join(t.TempDir(), "external/witness")
140 x, err := equipo.OpenExecution(context.Background(), d, path, cfg, s)
141 executionMust(t, err)
142 defer x.Close()
143 executionMust(t, x.Core.Pause(context.Background(), "test recovery"))
144 executionMust(t, x.Reconcile(context.Background()))
145 status, err := x.Core.Status(context.Background())
146 executionMust(t, err)
147 if !status.Quarantine {
148 t.Fatal("reconciler released quarantine")
149 }
150 _, err = x.Core.Resume(context.Background())
151 executionMust(t, err)
152 snapshot, err := x.Core.RecoverySnapshot(context.Background())
153 executionMust(t, err)
154 executionMust(t, s.ReleaseExecution(context.Background(), ownerActor, snapshot.Revision, "reviewed", snapshot.Hash))
155 executionMust(t, x.Reconcile(context.Background()))
156 status, err = x.Core.Status(context.Background())
157 executionMust(t, err)
158 if status.AdmissionPaused || status.Quarantine {
159 t.Fatal("owner release was not revalidated")
160 }
161}
162
163func TestExecutionCarlosMarkerLayout(t *testing.T) {
164 d, path := executionDB(t)
165 marker := filepath.Join(filepath.Dir(path), "execution", "witness")
166 values := map[string]string{
167 "EQUIPO_ORIGIN": "https://equipo.bds.oncarlos.com",
168 "EQUIPO_INSTANCE_KEY": "fixture-instance-key",
169 "EQUIPO_OWNERS": ownerAddress,
170 "CARLOS_SMTP_HOST": "smtp.example.test",
171 "CARLOS_SMTP_FROM": "equipo@example.test",
172 "EQUIPO_EXECUTION_ENABLED": "1",
173 "EQUIPO_EXECUTION_MARKER_PATH": marker,
174 }
175 cfg, err := equipo.LoadConfig(func(k string) string { return values[k] })
176 executionMust(t, err)
177 if !cfg.ExecutionEnabled || cfg.ExecutionMarkerPath != marker || cfg.RunnerReleaseDir != "" {
178 t.Fatal("CARLOS execution configuration", cfg.ExecutionEnabled)
179 }
180 for range 2 {
181 x, err := equipo.OpenExecution(context.Background(), d, path, cfg, equipo.NewStore(d))
182 executionMust(t, err)
183 status, err := x.Core.Status(context.Background())
184 executionMust(t, err)
185 if status.Quarantine || status.AdmissionPaused {
186 t.Fatal("persistent marker did not resume")
187 }
188 app, err := equipo.AppWithExecution(d, cfg, &capturingMailer{}, nil, x)
189 executionMust(t, err)
190 w := httptest.NewRecorder()
191 equipo.Mux(app).ServeHTTP(w, httptest.NewRequest("POST", "/api/enrolments", strings.NewReader("{")))
192 if w.Code != 422 {
193 t.Fatal("enrolment route requires always-on", w.Code)
194 }
195 executionMust(t, x.Close())
196 }
197 info, err := os.Stat(filepath.Dir(marker))
198 executionMust(t, err)
199 if info.Mode().Perm() != 0700 {
200 t.Fatal("marker directory permissions", info.Mode())
201 }
202}
203