mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-02 19:43:10 +03:00
* feat(server): supervisor detection and server.restart_mode config key RunningUnderSupervisor detects systemd (INVOCATION_ID) and, best-effort, NSSM (NSSM_SERVICE_NAME — 2.24 does not set it, so NSSM deployments set the mode explicitly). server.restart_mode (auto|spawn|supervised, default auto, env OWNCORD_SERVER_RESTART_MODE) selects how a self-restart hands off after the server drains: exit for the supervisor to relaunch, or spawn the replacement directly. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ngzj2Rx9UGC35uLHAfErMp * fix(server): make the self-restart handoff drain fully before starting the successor The update/restore/wizard restart previously spawned the replacement while the old server was still serving, then SIGTERMed itself and hard-exited after 10s. That design failed in every documented deployment mode: under the shipped systemd unit the spawned child (same cgroup) was killed when the old main process exited and Restart=on-failure never relaunched a clean exit; on Windows the self-SIGTERM is unsupported and silently dropped, so graceful shutdown never ran — hub.GracefulStop (the only caller of LiveKitProcess.Stop) was skipped, orphaning livekit-server on TCP 7880/UDP 50000-60000 and dropping queued event/audit batches; and NSSM's relaunch raced the self-spawned replacement for the database lock. Admin handlers now perform only the on-disk swap and request a restart through an injected hook (admin.SetRestartHandoff). The main package's restart coordinator cancels the parent of run()'s signal.NotifyContext — the exact drain a SIGTERM triggers, on every platform — and after run() has fully torn down (listeners closed, hub and LiveKit stopped, queues flushed, DB closed and its lock released) main() performs the handoff: spawn the replacement in spawn mode, or exit 0 for the supervisor in supervised mode. A 90s backstop force-exits a wedged teardown; the DB-lock and bind retries demote to safety nets. A three-state guard (idle/busy/restart-pending) serializes update apply, backup restore, and setup-wizard restarts against each other: concurrent applies no longer race the same staged .new file or broadcast a spurious update_aborted, and conflicting requests get 409 UPDATE_IN_PROGRESS / RESTART_PENDING. The swap being free of process side effects also makes the apply success path unit-testable for the first time. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ngzj2Rx9UGC35uLHAfErMp * fix(server): errno-based bind-conflict detection, ACME bind retry, LiveKit Pdeathsig isAddrInUse now unwraps to the platform errno (EADDRINUSE; WSAEADDRINUSE 10048 on Windows) with the English strings kept only as fallback — the string-only match never fired on localized Windows, silently disabling the bind retry. The retry loop is extracted into serveWithBindRetry and now also covers the ACME :80 challenge server, which previously gave up on first conflict and stayed dead (breaking HTTP-01 renewals) until the next restart. The .old-binary boot cleanup retries briefly for the window where a spawn-mode predecessor has not fully exited. The companion livekit-server gets Pdeathsig SIGKILL on Linux so a parent killed without teardown (kill -9, OOM, backstop exit) cannot orphan it with the voice ports held. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ngzj2Rx9UGC35uLHAfErMp * docs(deploy): Restart=always unit and per-supervisor restart-mode guidance Restart=always is what lets the deliberate clean exit after a self-update/restore relaunch under systemd (systemctl stop is never auto-restarted; failure exits behave as before). Deployment docs gain the required NSSM AppEnvironmentExtra line, the Task Scheduler and Docker restart-policy notes, and the new drain-then-handoff update flow. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ngzj2Rx9UGC35uLHAfErMp --------- Co-authored-by: Claude <noreply@anthropic.com>
493 lines
20 KiB
Go
493 lines
20 KiB
Go
package admin_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/owncord/server/admin"
|
|
"github.com/owncord/server/auth"
|
|
"github.com/owncord/server/updater"
|
|
)
|
|
|
|
func TestAdminAPI_CheckUpdate_OK(t *testing.T) {
|
|
// Mock GitHub API
|
|
mockGH := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"tag_name": "v2.0.0",
|
|
"body": "New release",
|
|
"html_url": "https://github.com/J3vb/OwnCord/releases/tag/v2.0.0",
|
|
"assets": []map[string]any{
|
|
{"name": "chatserver.exe", "browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/chatserver.exe"},
|
|
{"name": "chatserver-linux-amd64.tar.gz", "browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/chatserver-linux-amd64.tar.gz"},
|
|
{"name": "checksums.sha256", "browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/checksums.sha256"},
|
|
{"name": "chatserver.exe.sig", "browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/chatserver.exe.sig"},
|
|
{"name": "server-update-manifest.json", "browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/server-update-manifest.json"},
|
|
{"name": "server-update-manifest.json.sig", "browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/server-update-manifest.json.sig"},
|
|
},
|
|
})
|
|
}))
|
|
defer mockGH.Close()
|
|
|
|
u := updater.NewUpdater("1.0.0", "", "J3vb", "OwnCord")
|
|
u.SetBaseURL(mockGH.URL)
|
|
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, u, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodGet, "/updates", token, nil)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200; body: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var info updater.UpdateInfo
|
|
_ = json.Unmarshal(w.Body.Bytes(), &info)
|
|
if !info.UpdateAvailable {
|
|
t.Error("expected update_available = true")
|
|
}
|
|
if info.Latest != "v2.0.0" {
|
|
t.Errorf("latest = %q, want v2.0.0", info.Latest)
|
|
}
|
|
if !info.RequiredAssetsPresent {
|
|
t.Error("expected required_assets_present = true")
|
|
}
|
|
}
|
|
|
|
func TestAdminAPI_CheckUpdate_IncompleteReleaseNotInstallable(t *testing.T) {
|
|
mockGH := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"tag_name": "v2.0.0",
|
|
"body": "Missing manifest",
|
|
"html_url": "https://github.com/J3vb/OwnCord/releases/tag/v2.0.0",
|
|
"assets": []map[string]any{
|
|
{"name": "chatserver.exe", "browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/chatserver.exe"},
|
|
{"name": "checksums.sha256", "browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/checksums.sha256"},
|
|
},
|
|
})
|
|
}))
|
|
defer mockGH.Close()
|
|
|
|
u := updater.NewUpdater("1.0.0", "", "J3vb", "OwnCord")
|
|
u.SetBaseURL(mockGH.URL)
|
|
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, u, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodGet, "/updates", token, nil)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200; body: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var info updater.UpdateInfo
|
|
_ = json.Unmarshal(w.Body.Bytes(), &info)
|
|
if info.UpdateAvailable {
|
|
t.Error("expected update_available = false for incomplete release")
|
|
}
|
|
if info.RequiredAssetsPresent {
|
|
t.Error("expected required_assets_present = false for incomplete release")
|
|
}
|
|
}
|
|
|
|
func TestAdminAPI_CheckUpdate_UpToDate(t *testing.T) {
|
|
mockGH := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"tag_name": "v1.0.0",
|
|
"body": "",
|
|
"html_url": "https://github.com/J3vb/OwnCord/releases/tag/v1.0.0",
|
|
"assets": []map[string]any{},
|
|
})
|
|
}))
|
|
defer mockGH.Close()
|
|
|
|
u := updater.NewUpdater("1.0.0", "", "J3vb", "OwnCord")
|
|
u.SetBaseURL(mockGH.URL)
|
|
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, u, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodGet, "/updates", token, nil)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200", w.Code)
|
|
}
|
|
|
|
var info updater.UpdateInfo
|
|
_ = json.Unmarshal(w.Body.Bytes(), &info)
|
|
if info.UpdateAvailable {
|
|
t.Error("expected update_available = false")
|
|
}
|
|
}
|
|
|
|
func TestAdminAPI_CheckUpdate_Unauthenticated(t *testing.T) {
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, nil, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
|
|
w := doRequest(t, handler, http.MethodGet, "/updates", "", nil)
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Errorf("status = %d, want 401", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminAPI_ApplyUpdate_RequiresOwner(t *testing.T) {
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, nil, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
|
|
// Create admin user (not owner - role 2)
|
|
adminUID, _ := database.CreateUser(context.Background(), "adminonly2", "hash", 2)
|
|
token := "admin-role-token"
|
|
_, _ = database.CreateSession(context.Background(), adminUID, auth.HashToken(token), "test", "127.0.0.1")
|
|
|
|
w := doRequest(t, handler, http.MethodPost, "/updates/apply", token, nil)
|
|
if w.Code != http.StatusForbidden {
|
|
t.Errorf("status = %d, want 403", w.Code)
|
|
}
|
|
}
|
|
|
|
// ─── handleApplyUpdate additional paths ──────────────────────────────────────
|
|
|
|
// TestAdminAPI_ApplyUpdate_NilUpdater verifies that POST /updates/apply returns
|
|
// 503 when no updater is configured.
|
|
func TestAdminAPI_ApplyUpdate_NilUpdater(t *testing.T) {
|
|
database := openAdminTestDB(t)
|
|
// nil updater — the endpoint should return 503
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, nil, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodPost, "/updates/apply", token, nil)
|
|
if w.Code != http.StatusServiceUnavailable {
|
|
t.Errorf("status = %d, want 503; body: %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
// TestAdminAPI_ApplyUpdate_NilUpdater_ErrorCode verifies the error code field
|
|
// in the 503 response.
|
|
func TestAdminAPI_ApplyUpdate_NilUpdater_ErrorCode(t *testing.T) {
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, nil, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodPost, "/updates/apply", token, nil)
|
|
|
|
var resp map[string]string
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("unmarshal response: %v", err)
|
|
}
|
|
if resp["error"] != "UPDATE_UNAVAILABLE" {
|
|
t.Errorf("error code = %q, want UPDATE_UNAVAILABLE", resp["error"])
|
|
}
|
|
}
|
|
|
|
// TestAdminAPI_ApplyUpdate_NoUpdateAvailable verifies that 409 Conflict is
|
|
// returned when the server is already up to date.
|
|
func TestAdminAPI_ApplyUpdate_NoUpdateAvailable(t *testing.T) {
|
|
// Mock GitHub API to return same version (no update available).
|
|
mockGH := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"tag_name": "v1.0.0",
|
|
"body": "",
|
|
"html_url": "https://github.com/J3vb/OwnCord/releases/tag/v1.0.0",
|
|
"assets": []map[string]any{},
|
|
})
|
|
}))
|
|
defer mockGH.Close()
|
|
|
|
u := updater.NewUpdater("1.0.0", "", "J3vb", "OwnCord")
|
|
u.SetBaseURL(mockGH.URL)
|
|
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, u, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodPost, "/updates/apply", token, nil)
|
|
if w.Code != http.StatusConflict {
|
|
t.Errorf("status = %d, want 409 (no update available); body: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var resp map[string]string
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("unmarshal response: %v", err)
|
|
}
|
|
if resp["error"] != "NO_UPDATE" {
|
|
t.Errorf("error = %q, want NO_UPDATE", resp["error"])
|
|
}
|
|
}
|
|
|
|
// TestAdminAPI_ApplyUpdate_CheckFails verifies that 502 Bad Gateway is returned
|
|
// when the update check request to GitHub fails.
|
|
func TestAdminAPI_ApplyUpdate_CheckFails(t *testing.T) {
|
|
// Server that immediately closes connections (simulates network error).
|
|
mockGH := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Return invalid JSON to trigger a parse error.
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
defer mockGH.Close()
|
|
|
|
u := updater.NewUpdater("1.0.0", "", "J3vb", "OwnCord")
|
|
u.SetBaseURL(mockGH.URL)
|
|
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, u, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodPost, "/updates/apply", token, nil)
|
|
// Expect 502 Bad Gateway when update check call fails.
|
|
if w.Code != http.StatusBadGateway {
|
|
t.Errorf("status = %d, want 502; body: %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
// TestAdminAPI_ApplyUpdate_MissingAssets verifies that 502 is returned when the
|
|
// release has no download URL, checksum URL, or detached signature URL.
|
|
func TestAdminAPI_ApplyUpdate_MissingAssets(t *testing.T) {
|
|
// Return a newer version but with no assets (empty download/checksum URLs).
|
|
mockGH := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"tag_name": "v2.0.0",
|
|
"body": "Release notes",
|
|
"html_url": "https://github.com/J3vb/OwnCord/releases/tag/v2.0.0",
|
|
"assets": []map[string]any{},
|
|
})
|
|
}))
|
|
defer mockGH.Close()
|
|
|
|
u := updater.NewUpdater("1.0.0", "", "J3vb", "OwnCord")
|
|
u.SetBaseURL(mockGH.URL)
|
|
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, u, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodPost, "/updates/apply", token, nil)
|
|
if w.Code != http.StatusBadGateway {
|
|
t.Errorf("status = %d, want 502 (missing assets); body: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var resp map[string]string
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("unmarshal response: %v", err)
|
|
}
|
|
if resp["error"] != "MISSING_ASSETS" {
|
|
t.Errorf("error = %q, want MISSING_ASSETS", resp["error"])
|
|
}
|
|
}
|
|
|
|
// TestAdminAPI_ApplyUpdate_Unauthenticated verifies that 401 is returned for
|
|
// unauthenticated requests to POST /updates/apply.
|
|
func TestAdminAPI_ApplyUpdate_Unauthenticated(t *testing.T) {
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, nil, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
|
|
w := doRequest(t, handler, http.MethodPost, "/updates/apply", "", nil)
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Errorf("status = %d, want 401", w.Code)
|
|
}
|
|
}
|
|
|
|
// TestAdminAPI_ApplyUpdate_DownloadFails verifies that 502 is returned when
|
|
// the binary download itself fails (bad URL, network error, etc.).
|
|
// We use a mock server that reports an available update with valid-format
|
|
// GitHub URLs, but those URLs point to a server that returns 404.
|
|
func TestAdminAPI_ApplyUpdate_DownloadFails(t *testing.T) {
|
|
// The mock server that serves the GitHub release info — it reports an
|
|
// update is available with GitHub-prefixed asset URLs.
|
|
// The actual download will fail because the URLs don't point to real files.
|
|
var mockGHURL string
|
|
mockGH := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// If this is the checksum/download request, return an error.
|
|
// The release API endpoint returns a release with asset URLs.
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"tag_name": "v2.0.0",
|
|
"body": "Release notes",
|
|
"html_url": "https://github.com/J3vb/OwnCord/releases/tag/v2.0.0",
|
|
"assets": []map[string]any{
|
|
{
|
|
"name": "chatserver.exe",
|
|
"browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/chatserver.exe",
|
|
},
|
|
{
|
|
"name": "chatserver-linux-amd64.tar.gz",
|
|
"browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/chatserver-linux-amd64.tar.gz",
|
|
},
|
|
{
|
|
"name": "checksums.sha256",
|
|
"browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/checksums.sha256",
|
|
},
|
|
{
|
|
"name": "chatserver.exe.sig",
|
|
"browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/chatserver.exe.sig",
|
|
},
|
|
{
|
|
"name": "server-update-manifest.json",
|
|
"browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/server-update-manifest.json",
|
|
},
|
|
{
|
|
"name": "server-update-manifest.json.sig",
|
|
"browser_download_url": "https://github.com/J3vb/OwnCord/releases/download/v2.0.0/server-update-manifest.json.sig",
|
|
},
|
|
},
|
|
})
|
|
_ = mockGHURL // suppress unused warning
|
|
}))
|
|
defer mockGH.Close()
|
|
mockGHURL = mockGH.URL
|
|
|
|
u := updater.NewUpdater("1.0.0", "", "J3vb", "OwnCord")
|
|
u.SetBaseURL(mockGH.URL)
|
|
// The download URLs are real GitHub URLs that will fail since we're not
|
|
// actually connected to GitHub in tests, or we can use the URL validation
|
|
// to force a failure. The URLs pass validation (they have the right prefix),
|
|
// but the actual HTTP fetch will fail (unreachable host).
|
|
// In CI environments without internet, this returns 502.
|
|
// We accept either 502 (download failed) or 200 (unexpectedly succeeded) —
|
|
// the important thing is that the code path is executed.
|
|
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, u, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodPost, "/updates/apply", token, nil)
|
|
// Either 502 (download failed as expected in isolated test environment)
|
|
// or 200 (succeeded in environment with GitHub access) is acceptable.
|
|
// What should NOT happen is 409 (no update) or 503 (nil updater).
|
|
if w.Code == http.StatusServiceUnavailable || w.Code == http.StatusConflict {
|
|
t.Errorf("status = %d; expected download attempt to proceed (got 503/409 instead)", w.Code)
|
|
}
|
|
}
|
|
|
|
// ─── handleApplyUpdate — container deployments ───────────────────────────────
|
|
|
|
// In a container the staged replacement dies with the container, so the
|
|
// endpoint refuses before any updater logic runs — the answer must not
|
|
// depend on whether an updater is configured (closes the long-standing
|
|
// "disable this endpoint in docker builds?" TODO).
|
|
func TestAdminAPI_ApplyUpdate_RefusedInContainer(t *testing.T) {
|
|
t.Setenv("OWNCORD_CONTAINER", "1")
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, nil, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodPost, "/updates/apply", token, nil)
|
|
if w.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status = %d, want 503; body: %s", w.Code, w.Body.String())
|
|
}
|
|
var resp map[string]string
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if resp["error"] != "CONTAINER_DEPLOYMENT" {
|
|
t.Errorf("error code = %q, want CONTAINER_DEPLOYMENT", resp["error"])
|
|
}
|
|
}
|
|
|
|
// The explicit opt-out keeps in-place update available for operators who
|
|
// bind-mount the binary and know what they are doing: with the variable set
|
|
// to 0, the container guard steps aside and the nil-updater 503 answers.
|
|
func TestAdminAPI_ApplyUpdate_ContainerOptOut(t *testing.T) {
|
|
t.Setenv("OWNCORD_CONTAINER", "0")
|
|
database := openAdminTestDB(t)
|
|
handler := admin.NewAdminAPI(database, "1.0.0", nil, nil, nil, nil, nil, newTestModService(database), newTestRoleService(database))
|
|
token := createAdminUser(t, database)
|
|
|
|
w := doRequest(t, handler, http.MethodPost, "/updates/apply", token, nil)
|
|
if w.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status = %d, want 503; body: %s", w.Code, w.Body.String())
|
|
}
|
|
var resp map[string]string
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if resp["error"] != "UPDATE_UNAVAILABLE" {
|
|
t.Errorf("error code = %q, want UPDATE_UNAVAILABLE (container guard must step aside)", resp["error"])
|
|
}
|
|
}
|
|
|
|
// ─── OC-0226: aborted apply must correct the earlier restart promise ────────
|
|
//
|
|
// handleApplyUpdate's background goroutine broadcasts "server restarting in
|
|
// 5s" as its very first action, before any of the on-disk swap actually
|
|
// happens. If the swap then fails, every connected client is left believing
|
|
// a restart is underway (ServerBanner counts down to a permanent
|
|
// "Reconnecting..." state) with no corrective signal ever sent. These tests
|
|
// call the swap logic directly — admin.ApplyStagedUpdate — with inputs
|
|
// engineered to fail at different points, and assert a corrective
|
|
// "update_aborted" broadcast follows. The success path is covered too, now
|
|
// that the swap has no process side effects (the restart happens through the
|
|
// coordinator hook, not an in-package spawn + os.Exit).
|
|
|
|
// TestApplyStagedUpdate_VerifyFails_BroadcastsAbort covers the earliest abort
|
|
// point: the staged binary re-verification (OpenVerifiedBinary) fails
|
|
// because the staged file was never written.
|
|
func TestApplyStagedUpdate_VerifyFails_BroadcastsAbort(t *testing.T) {
|
|
dir := t.TempDir()
|
|
exePath := filepath.Join(dir, "chatserver")
|
|
if err := os.WriteFile(exePath, []byte("old binary"), 0o755); err != nil {
|
|
t.Fatalf("writing fake exe: %v", err)
|
|
}
|
|
oldPath := exePath + ".old"
|
|
newPath := exePath + ".new" // deliberately never written
|
|
|
|
hub := &mockHub{}
|
|
admin.ApplyStagedUpdate(hub, exePath, oldPath, newPath, "0000000000000000000000000000000000000000000000000000000000000000")
|
|
|
|
if len(hub.restartCalls) != 1 {
|
|
t.Fatalf("restartCalls = %d, want 1 (corrective broadcast after abort); got %+v", len(hub.restartCalls), hub.restartCalls)
|
|
}
|
|
if hub.restartCalls[0].reason == "update" {
|
|
t.Fatalf("only broadcast was the original 'restarting' promise (%+v); no corrective broadcast was sent after the abort", hub.restartCalls[0])
|
|
}
|
|
|
|
// The original binary must be untouched: verification failed before any
|
|
// filesystem mutation.
|
|
got, err := os.ReadFile(exePath)
|
|
if err != nil || string(got) != "old binary" {
|
|
t.Errorf("exePath contents = %q, err=%v; want original binary untouched", got, err)
|
|
}
|
|
}
|
|
|
|
// TestApplyStagedUpdate_RenameToOldFails_BroadcastsAbort covers the second
|
|
// abort point: the staged binary verifies fine, but renaming the current
|
|
// executable to its .old backup fails (exePath does not exist).
|
|
func TestApplyStagedUpdate_RenameToOldFails_BroadcastsAbort(t *testing.T) {
|
|
dir := t.TempDir()
|
|
exePath := filepath.Join(dir, "chatserver") // deliberately never created
|
|
oldPath := exePath + ".old"
|
|
newPath := exePath + ".new"
|
|
|
|
content := []byte("verified staged bytes")
|
|
if err := os.WriteFile(newPath, content, 0o755); err != nil {
|
|
t.Fatalf("writing staged binary: %v", err)
|
|
}
|
|
sum := sha256.Sum256(content)
|
|
stagedHash := hex.EncodeToString(sum[:])
|
|
|
|
hub := &mockHub{}
|
|
admin.ApplyStagedUpdate(hub, exePath, oldPath, newPath, stagedHash)
|
|
|
|
if len(hub.restartCalls) != 1 {
|
|
t.Fatalf("restartCalls = %d, want 1 (corrective broadcast after abort); got %+v", len(hub.restartCalls), hub.restartCalls)
|
|
}
|
|
if hub.restartCalls[0].reason == "update" {
|
|
t.Fatalf("only broadcast was the original 'restarting' promise (%+v); no corrective broadcast was sent after the abort", hub.restartCalls[0])
|
|
}
|
|
}
|
|
|
|
// TestApplyStagedUpdate_NilHub_NoPanic verifies the corrective-broadcast
|
|
// guard does not dereference a nil hub (update checking with no ws.Hub is a
|
|
// supported configuration — see handleApplyUpdate's nil checks).
|
|
func TestApplyStagedUpdate_NilHub_NoPanic(t *testing.T) {
|
|
dir := t.TempDir()
|
|
exePath := filepath.Join(dir, "chatserver")
|
|
oldPath := exePath + ".old"
|
|
newPath := exePath + ".new" // never written -> verification fails
|
|
|
|
admin.ApplyStagedUpdate(nil, exePath, oldPath, newPath, "0000000000000000000000000000000000000000000000000000000000000000")
|
|
}
|