package equipotest import ( "context" "net/http/httptest" "os" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/equipo" ) func TestExecutionStartupClosed(t *testing.T) { d, path := executionDB(t) executionMust(t, os.Chmod(filepath.Dir(path), 0700)) s := equipo.NewStore(d) cfg := testConfig(testOrigin) marker := filepath.Join(t.TempDir(), "external", "witness") cfg.ExecutionMarkerPath = marker x, err := equipo.OpenExecution(context.Background(), d, path, cfg, s) if err != nil || x != nil { t.Fatal("disabled execution opened", err) } if _, err = os.Stat(filepath.Dir(marker)); !os.IsNotExist(err) { t.Fatal("disabled execution touched witness") } cfg.ExecutionEnabled = true cfg.ExecutionMarkerPath = "" if x, err = equipo.OpenExecution(context.Background(), d, path, cfg, s); err == nil || x != nil { t.Fatal("enabled without external witness") } cfg.ExecutionMarkerPath = filepath.Join(filepath.Dir(path), "witness") if x, err = equipo.OpenExecution(context.Background(), d, path, cfg, s); err == nil || x != nil { t.Fatal("witness shares database history directory") } cfg.ExecutionMarkerPath = marker x, err = equipo.OpenExecution(context.Background(), d, path, cfg, s) executionMust(t, err) defer x.Close() st, err := x.Core.Status(context.Background()) executionMust(t, err) if st.AdmissionPaused || st.Quarantine { t.Fatalf("fresh enabled startup did not resume: %+v", st) } if _, err = equipo.AppWithExecution(d, cfg, &capturingMailer{}, nil, x); err != nil { t.Fatal(err) } if err = x.Close(); err != nil { t.Fatal(err) } if scalar(t, d, "SELECT admission_paused FROM service WHERE id=1") != 1 { t.Fatal("core not closed before database") } if err = d.Writer().Ping(); err != nil { t.Fatal("core closed shared database", err) } } func TestExecutionHibernateWitness(t *testing.T) { d, path := executionDB(t) cfg := testConfig(testOrigin) cfg.ExecutionEnabled = true cfg.ExecutionMarkerPath = filepath.Join(t.TempDir(), "external", "witness") open := func() *equipo.Execution { x, err := equipo.OpenExecution(context.Background(), d, path, cfg, equipo.NewStore(d)) executionMust(t, err) return x } x := open() executionMust(t, x.Close()) oldWitness, err := os.ReadFile(cfg.ExecutionMarkerPath) executionMust(t, err) x = open() st, err := x.Core.Status(context.Background()) executionMust(t, err) if st.Quarantine { t.Fatal("preserved witness quarantined") } executionMust(t, x.Close()) executionMust(t, os.WriteFile(cfg.ExecutionMarkerPath, oldWitness, 0600)) x = open() st, err = x.Core.Status(context.Background()) executionMust(t, err) if !st.Quarantine || !st.AdmissionPaused { t.Fatal("mismatched witness admitted dispatch") } executionMust(t, x.Close()) executionMust(t, os.Remove(cfg.ExecutionMarkerPath)) x = open() defer x.Close() st, err = x.Core.Status(context.Background()) executionMust(t, err) if !st.Quarantine || !st.AdmissionPaused { t.Fatal("lost witness admitted dispatch") } } func TestDirectIdentityChangesFenceCreateAndTurn(t *testing.T) { for _, change := range []string{"removed", "provider", "launcher", "configuration"} { t.Run(change, func(t *testing.T) { f := runnerFixtureNew(t, true) q := f.request() q.RequestID = "before-create" if _, err := f.x.Mutate(connectionContext, f.principal, "validate", q); err != nil { t.Fatal(err) } switch change { case "removed": mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", ownerAddress) case "demoted": mustSQL(t, f.d, "UPDATE members SET role='member' WHERE address=?", ownerAddress) default: reports := append([]equipo.ConnectionReport(nil), f.reports...) switch change { case "provider": reports[1].ProviderVersion = "next" case "launcher": reports[1].LauncherVersion = "next" case "configuration": reports[1].IsolationConfigHash = strings.Repeat("c", 64) } executionMust(t, f.s.ReportConnections(connectionContext, f.principal, reports)) } for _, phase := range []string{"before-create", "before-turn"} { q.RequestID = phase if _, err := f.x.Mutate(connectionContext, f.principal, "validate", q); err == nil { t.Fatal("stale identity authorized", phase) } } }) } } func TestExecutionStartupReconciliation(t *testing.T) { d, path := executionDB(t) s := equipo.NewStore(d) cfg := testConfig(testOrigin) cfg.ExecutionEnabled = true cfg.ExecutionMarkerPath = filepath.Join(t.TempDir(), "external/witness") x, err := equipo.OpenExecution(context.Background(), d, path, cfg, s) executionMust(t, err) defer x.Close() executionMust(t, x.Core.Pause(context.Background(), "test recovery")) executionMust(t, x.Reconcile(context.Background())) status, err := x.Core.Status(context.Background()) executionMust(t, err) if !status.Quarantine { t.Fatal("reconciler released quarantine") } _, err = x.Core.Resume(context.Background()) executionMust(t, err) snapshot, err := x.Core.RecoverySnapshot(context.Background()) executionMust(t, err) executionMust(t, s.ReleaseExecution(context.Background(), ownerActor, snapshot.Revision, "reviewed", snapshot.Hash)) executionMust(t, x.Reconcile(context.Background())) status, err = x.Core.Status(context.Background()) executionMust(t, err) if status.AdmissionPaused || status.Quarantine { t.Fatal("owner release was not revalidated") } } func TestExecutionCarlosMarkerLayout(t *testing.T) { d, path := executionDB(t) marker := filepath.Join(filepath.Dir(path), "execution", "witness") values := map[string]string{ "EQUIPO_ORIGIN": "https://equipo.bds.oncarlos.com", "EQUIPO_INSTANCE_KEY": "fixture-instance-key", "EQUIPO_OWNERS": ownerAddress, "CARLOS_SMTP_HOST": "smtp.example.test", "CARLOS_SMTP_FROM": "equipo@example.test", "EQUIPO_EXECUTION_ENABLED": "1", "EQUIPO_EXECUTION_MARKER_PATH": marker, } cfg, err := equipo.LoadConfig(func(k string) string { return values[k] }) executionMust(t, err) if !cfg.ExecutionEnabled || cfg.ExecutionMarkerPath != marker || cfg.RunnerReleaseDir != "" { t.Fatal("CARLOS execution configuration", cfg.ExecutionEnabled) } for range 2 { x, err := equipo.OpenExecution(context.Background(), d, path, cfg, equipo.NewStore(d)) executionMust(t, err) status, err := x.Core.Status(context.Background()) executionMust(t, err) if status.Quarantine || status.AdmissionPaused { t.Fatal("persistent marker did not resume") } app, err := equipo.AppWithExecution(d, cfg, &capturingMailer{}, nil, x) executionMust(t, err) w := httptest.NewRecorder() equipo.Mux(app).ServeHTTP(w, httptest.NewRequest("POST", "/api/enrolments", strings.NewReader("{"))) if w.Code != 422 { t.Fatal("enrolment route requires always-on", w.Code) } executionMust(t, x.Close()) } info, err := os.Stat(filepath.Dir(marker)) executionMust(t, err) if info.Mode().Perm() != 0700 { t.Fatal("marker directory permissions", info.Mode()) } }