Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,205 @@
|
||||
package main
|
||||
|
||||
// Tests for adpanel:restore-refusal-recovery: the restore handler mutates the
|
||||
// container (`docker update --restart=no` + `docker stop`) BEFORE the
|
||||
// live-wipe-guard verification and before every staging/swap/merge failure
|
||||
// exit, so every refused/failed exit must put the container back to its
|
||||
// pre-restore state — the captured restart policy re-applied, and the
|
||||
// container restarted when it was running pre-restore and is not running at
|
||||
// recovery time — while the refusal/failure response stays byte-identical.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// installRestoreRecoveryDockerStub shadows the docker CLI with a stub that
|
||||
// logs every invocation to the returned log path (one line per invocation,
|
||||
// $* joined), answers the restart-policy capture with "always", and answers
|
||||
// the State.Running inspects according to runningMode: "running" always
|
||||
// reports true; "then-stopped" reports true on the FIRST inspect (the
|
||||
// pre-restore capture) and false afterwards (the guard and the recovery-time
|
||||
// re-check), which is the stop/guard edge recovery exists for. Hermetic for
|
||||
// handler tests regardless of whether the host runs docker.
|
||||
func installRestoreRecoveryDockerStub(t *testing.T, runningMode string) string {
|
||||
t.Helper()
|
||||
binDir := t.TempDir()
|
||||
script := `#!/bin/sh
|
||||
[ -n "$ADP_TEST_LOG" ] && echo "$*" >> "$ADP_TEST_LOG"
|
||||
cmd="$1"; shift
|
||||
case "$cmd" in
|
||||
inspect)
|
||||
case "$2" in
|
||||
*RestartPolicy*)
|
||||
printf '%s\n' "${ADP_TEST_RESTART_POLICY:-always}"
|
||||
exit 0 ;;
|
||||
*State.Running*)
|
||||
if [ "$ADP_TEST_RUNNING_MODE" != "running" ]; then
|
||||
n=$(cat "$ADP_TEST_COUNTER" 2>/dev/null || echo 0)
|
||||
n=$((n+1))
|
||||
echo "$n" > "$ADP_TEST_COUNTER"
|
||||
if [ "$n" = "1" ]; then printf 'true\n'; exit 0; fi
|
||||
case "$ADP_TEST_RUNNING_MODE" in
|
||||
then-stopped) printf 'false\n' ;;
|
||||
then-invalid) printf '<no value>\n' ;;
|
||||
then-error) printf 'Docker context not found\n' >&2; exit 1 ;;
|
||||
esac
|
||||
else
|
||||
printf 'true\n'
|
||||
fi
|
||||
exit 0 ;;
|
||||
esac
|
||||
exit 0 ;;
|
||||
start|stop|update|kill|rm)
|
||||
exit 0 ;;
|
||||
*)
|
||||
exit 0 ;;
|
||||
esac
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(binDir, "docker"), []byte(script), 0o755); err != nil {
|
||||
t.Fatalf("write docker CLI stub: %v", err)
|
||||
}
|
||||
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
if out, err := exec.LookPath("docker"); err != nil || filepath.Dir(out) != binDir {
|
||||
t.Fatalf("docker CLI stub not in effect (looked up %q, err %v)", out, err)
|
||||
}
|
||||
logPath := filepath.Join(t.TempDir(), "docker.log")
|
||||
t.Setenv("ADP_TEST_LOG", logPath)
|
||||
t.Setenv("ADP_TEST_RUNNING_MODE", runningMode)
|
||||
t.Setenv("ADP_TEST_COUNTER", filepath.Join(t.TempDir(), "running.count"))
|
||||
// The engine API client bypasses PATH entirely (unix socket), so a host
|
||||
// with a live docker daemon could answer API-side calls for real. Both
|
||||
// stubs must be in effect.
|
||||
stubDockerEngineUnavailable(t)
|
||||
return logPath
|
||||
}
|
||||
|
||||
// The finding itself: the guard refuses a restore whose stop did not take,
|
||||
// and the refusal must not leave the container with its restart policy
|
||||
// mangled to "no". The container was (still) running at recovery time, so the
|
||||
// recovery re-applies the captured policy and does NOT start anything.
|
||||
func TestBackupsRestoreGuardRefusalRecoversRestartPolicy(t *testing.T) {
|
||||
const name = "alpha"
|
||||
logPath := installRestoreRecoveryDockerStub(t, "running")
|
||||
a, _ := newTestAgent(t)
|
||||
|
||||
backupId := "1700000000_stg0009"
|
||||
dataDir := filepath.Join(a.serverBackupsDir(name), backupId, "data")
|
||||
writeSeedFile(t, filepath.Join(dataDir, "world.txt"), "seed", 0o644)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/v1/servers/"+name+"/backups/"+backupId+"/restore",
|
||||
bytes.NewReader([]byte(`{"deleteOldFiles":true}`)))
|
||||
rec := httptest.NewRecorder()
|
||||
a.handleBackupsRestore(rec, req, name, backupId)
|
||||
|
||||
// Recovery must not mask the refusal: same 409, same body as before.
|
||||
if rec.Code != 409 {
|
||||
t.Fatalf("restore status %d body %s, want 409 from the live-wipe-guard", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "container is still running") {
|
||||
t.Fatalf("body must name the live-wipe-guard refusal: %s", rec.Body.String())
|
||||
}
|
||||
|
||||
log, err := os.ReadFile(logPath)
|
||||
if err != nil {
|
||||
t.Fatalf("read docker stub log: %v", err)
|
||||
}
|
||||
invocations := string(log)
|
||||
// The mutation ran...
|
||||
if !strings.Contains(invocations, "update --restart=no") {
|
||||
t.Fatalf("stub log must show the restore's --restart=no mutation: %s", invocations)
|
||||
}
|
||||
// ...and the recovery re-applied the CAPTURED pre-restore policy.
|
||||
if !strings.Contains(invocations, "update --restart=always") {
|
||||
t.Fatalf("stub log must show the recovery re-applying the captured policy: %s", invocations)
|
||||
}
|
||||
// The container was running at recovery time (the guard just said so), so
|
||||
// no start may be attempted.
|
||||
if strings.Contains(invocations, "start ") {
|
||||
t.Fatalf("recovery must not start a container the guard saw running: %s", invocations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupsRestoreRefusesUnknownPostStopState(t *testing.T) {
|
||||
for _, mode := range []string{"then-invalid", "then-error"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
installRestoreRecoveryDockerStub(t, mode)
|
||||
agent, _ := newTestAgent(t)
|
||||
const name = "alpha"
|
||||
const backupID = "1700000000_stg0011"
|
||||
serverFile := filepath.Join(agent.serverDir(name), "world.txt")
|
||||
writeSeedFile(t, serverFile, "current", 0o644)
|
||||
writeSeedFile(t, filepath.Join(agent.serverBackupsDir(name), backupID, "data", "world.txt"), "backup", 0o644)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/v1/servers/"+name+"/backups/"+backupID+"/restore",
|
||||
bytes.NewReader([]byte(`{"deleteOldFiles":true}`)))
|
||||
rec := httptest.NewRecorder()
|
||||
agent.handleBackupsRestore(rec, req, name, backupID)
|
||||
|
||||
if rec.Code != http.StatusConflict || !strings.Contains(rec.Body.String(), "could not confirm the container stopped") {
|
||||
t.Fatalf("restore status %d body %s, want an explicit stop verification refusal", rec.Code, rec.Body.String())
|
||||
}
|
||||
if content, err := os.ReadFile(serverFile); err != nil || string(content) != "current" {
|
||||
t.Fatalf("unconfirmed stop changed current server files: %q, %v", content, err)
|
||||
}
|
||||
assertNoStagingSiblings(t, agent.serverDir(name))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The other refused-exit class: the guard PASSES (stop took effect between
|
||||
// capture and the guard) and the restore then fails (here: the deterministic
|
||||
// same-device refusal of a wipe-restore whose server dir is missing). The
|
||||
// container is left stopped with its policy mangled to "no", so the recovery
|
||||
// must BOTH re-apply the captured policy AND start the container again.
|
||||
func TestBackupsRestoreStoppedExitRecoversPolicyAndStarts(t *testing.T) {
|
||||
const name = "alpha"
|
||||
logPath := installRestoreRecoveryDockerStub(t, "then-stopped")
|
||||
a, _ := newTestAgent(t)
|
||||
|
||||
// The server dir does not exist: the wipe-restore reaches the staging
|
||||
// phase and fails deterministically at the sameDevice check — AFTER the
|
||||
// stop and the live-wipe-guard, with the container stopped.
|
||||
backupId := "1700000000_stg0010"
|
||||
dataDir := filepath.Join(a.serverBackupsDir(name), backupId, "data")
|
||||
writeSeedFile(t, filepath.Join(dataDir, "world.txt"), "seed", 0o644)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/v1/servers/"+name+"/backups/"+backupId+"/restore",
|
||||
bytes.NewReader([]byte(`{"deleteOldFiles":true}`)))
|
||||
rec := httptest.NewRecorder()
|
||||
a.handleBackupsRestore(rec, req, name, backupId)
|
||||
|
||||
// Recovery must not mask the failure: same 500, same body as before.
|
||||
if rec.Code != 500 {
|
||||
t.Fatalf("restore status %d body %s, want 500 from the staging same-device check", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "staging directory is not on the server directory's filesystem") {
|
||||
t.Fatalf("body must name the same-device refusal: %s", rec.Body.String())
|
||||
}
|
||||
assertNoStagingSiblings(t, a.serverDir(name))
|
||||
|
||||
log, err := os.ReadFile(logPath)
|
||||
if err != nil {
|
||||
t.Fatalf("read docker stub log: %v", err)
|
||||
}
|
||||
invocations := string(log)
|
||||
if !strings.Contains(invocations, "update --restart=no") {
|
||||
t.Fatalf("stub log must show the restore's --restart=no mutation: %s", invocations)
|
||||
}
|
||||
if !strings.Contains(invocations, "update --restart=always") {
|
||||
t.Fatalf("stub log must show the recovery re-applying the captured policy: %s", invocations)
|
||||
}
|
||||
// The capture saw the container running and it is stopped at recovery
|
||||
// time: the recovery starts it again (docker start is NOT in
|
||||
// rewriteDockerCLIArgs' rewrite switch, so the handler passes the
|
||||
// dockerContainerName form explicitly).
|
||||
if !strings.Contains(invocations, "start "+dockerContainerName(name)) {
|
||||
t.Fatalf("stub log must show the recovery starting the stopped container: %s", invocations)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user