96 lines
3.3 KiB
Go
96 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestReinstallPreservesDiskIdOnSecondDisk is a regression test for the bug where
|
|
// reinstalling a server that lives on a secondary disk dropped the __diskId from
|
|
// its rebuilt meta, causing the server to silently fall back to the primary disk
|
|
// on the next cold cache / restart and orphan its real files on the secondary
|
|
// disk. The reinstall handler rebuilds meta from scratch, so it must carry the
|
|
// disk assignment forward exactly like create persists it.
|
|
func TestReinstallPreservesDiskIdOnSecondDisk(t *testing.T) {
|
|
a, _ := newTestAgent(t)
|
|
mount := t.TempDir()
|
|
d := registerTestDisk(t, a, mount, 50)
|
|
a.setServerDiskMapping("beta", d.ID)
|
|
|
|
serverDir := a.serverDir("beta")
|
|
if err := os.MkdirAll(serverDir, 0o750); err != nil {
|
|
t.Fatalf("mkdir srv: %v", err)
|
|
}
|
|
|
|
// Pre-reinstall meta carries the secondary-disk assignment, as create writes it.
|
|
oldMeta := map[string]any{
|
|
"type": "custom",
|
|
"template": "custom",
|
|
"__diskId": d.ID,
|
|
"createdAt": float64(123),
|
|
}
|
|
if err := a.saveMeta(serverDir, oldMeta); err != nil {
|
|
t.Fatalf("saveMeta old: %v", err)
|
|
}
|
|
|
|
// Simulate the reinstall meta rebuild: a fresh meta map that must inherit
|
|
// __diskId from the old meta (mirrors handleReinstallServer).
|
|
rebuilt := map[string]any{
|
|
"type": "custom",
|
|
"template": "custom",
|
|
"hostPort": 8080,
|
|
}
|
|
if diskID, ok := oldMeta["__diskId"].(string); ok && strings.TrimSpace(diskID) != "" {
|
|
rebuilt["__diskId"] = strings.TrimSpace(diskID)
|
|
}
|
|
if err := a.saveMeta(serverDir, rebuilt); err != nil {
|
|
t.Fatalf("saveMeta rebuilt: %v", err)
|
|
}
|
|
|
|
// After a cold cache, the server must still resolve to the secondary disk.
|
|
a.invalidateServerDiskCache("beta")
|
|
if got := a.diskForServer("beta"); got.ID != d.ID {
|
|
t.Fatalf("server mis-routed after reinstall: got disk %q want %q", got.ID, d.ID)
|
|
}
|
|
if got := a.serverDir("beta"); got != serverDir {
|
|
t.Fatalf("serverDir after reinstall: got %q want %q", got, serverDir)
|
|
}
|
|
|
|
// Confirm the persisted meta on disk still records the disk assignment.
|
|
persisted := a.loadMeta(serverDir)
|
|
if id, _ := persisted["__diskId"].(string); id != d.ID {
|
|
t.Fatalf("__diskId not persisted after reinstall: got %q want %q", id, d.ID)
|
|
}
|
|
}
|
|
|
|
// TestReinstallPrimaryServerOmitsDiskId verifies the common single-disk case is
|
|
// unchanged: a primary-disk server with no __diskId stays on the primary disk
|
|
// after the reinstall meta rebuild, and we do not inject a spurious assignment.
|
|
func TestReinstallPrimaryServerOmitsDiskId(t *testing.T) {
|
|
a, vol := newTestAgent(t)
|
|
|
|
serverDir := filepath.Join(vol, "alpha")
|
|
if err := os.MkdirAll(serverDir, 0o750); err != nil {
|
|
t.Fatalf("mkdir srv: %v", err)
|
|
}
|
|
oldMeta := map[string]any{"type": "custom", "template": "custom"}
|
|
|
|
rebuilt := map[string]any{"type": "custom", "template": "custom", "hostPort": 8080}
|
|
if diskID, ok := oldMeta["__diskId"].(string); ok && strings.TrimSpace(diskID) != "" {
|
|
rebuilt["__diskId"] = strings.TrimSpace(diskID)
|
|
}
|
|
if _, ok := rebuilt["__diskId"]; ok {
|
|
t.Fatalf("primary-disk reinstall must not inject __diskId")
|
|
}
|
|
if err := a.saveMeta(serverDir, rebuilt); err != nil {
|
|
t.Fatalf("saveMeta: %v", err)
|
|
}
|
|
|
|
a.invalidateServerDiskCache("alpha")
|
|
if got := a.diskForServer("alpha"); !(got.Primary || got.ID == primaryDiskID) {
|
|
t.Fatalf("primary-disk server mis-routed after reinstall to %q", got.ID)
|
|
}
|
|
}
|