Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
+113
-19
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -136,13 +138,15 @@ func TestCheckDiskCapacity(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPathAllowedForDisk(t *testing.T) {
|
||||
bad := []string{"", "relative/path", "/", "/etc", "/var/lib/node", "/var/lib/node/servers", "/proc/1", "/mnt/disk1/", "/mnt/./disk1"}
|
||||
bad := []string{"", "relative/path", "/", "/etc", "/var/lib", "/var/lib/node", "/var/lib/node/servers", "/var/log", "/var/run", "/run", "/srv", "/opt", "/tmp", "/home", "/proc/1", "/mnt/disk1/", "/mnt/./disk1"}
|
||||
for _, p := range bad {
|
||||
if _, err := pathAllowedForDisk(p); err == nil {
|
||||
t.Fatalf("expected reject for %q", p)
|
||||
}
|
||||
}
|
||||
good := []string{"/mnt/disk1", "/srv/storage/extra", "/data2"}
|
||||
// "/srv/storage/extra" used to be here but /srv is now a denied prefix —
|
||||
// replaced with an equally non-system path that must stay allowed.
|
||||
good := []string{"/mnt/disk1", "/data/disks/disk2", "/data2"}
|
||||
for _, p := range good {
|
||||
if c, err := pathAllowedForDisk(p); err != nil || c != p {
|
||||
t.Fatalf("expected allow for %q, got (%q,%v)", p, c, err)
|
||||
@@ -186,6 +190,55 @@ func TestEnumerateServerNamesAcrossDisks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// adpanel:restore-staging — a leftover wipe-restore staging sibling
|
||||
// (<base>.restore-staging-<UnixMilli>, left in place by a mid-swap failure or
|
||||
// crash for operator recovery) must not surface as a phantom server entry in
|
||||
// enumerateServerNames, while ordinary dot-bearing server names keep listing.
|
||||
func TestEnumerateServerNamesSkipsRestoreStagingSiblings(t *testing.T) {
|
||||
a, vol := newTestAgent(t)
|
||||
for _, d := range []string{"alpha", "alpha.restore-staging-1730000000000", "beta"} {
|
||||
if err := os.MkdirAll(filepath.Join(vol, d), 0o750); err != nil {
|
||||
t.Fatalf("mkdir %s: %v", d, err)
|
||||
}
|
||||
}
|
||||
found := map[string]bool{}
|
||||
for _, n := range a.enumerateServerNames() {
|
||||
found[n] = true
|
||||
}
|
||||
if !found["alpha"] || !found["beta"] {
|
||||
t.Fatalf("legit servers must still enumerate, got %v", found)
|
||||
}
|
||||
if found["alpha.restore-staging-1730000000000"] {
|
||||
t.Fatalf("staging sibling must be filtered from enumeration")
|
||||
}
|
||||
}
|
||||
|
||||
// The filter matches the EXACT staging suffix format: "<anything>.restore-
|
||||
// staging-" + a non-empty decimal run parseable as int64 (what
|
||||
// time.Now().UnixMilli() formats). Anything else — wrong marker, non-digit or
|
||||
// empty tail, empty base, int64 overflow — is a name that lists.
|
||||
func TestIsRestoreStagingName(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
want bool
|
||||
}{
|
||||
{"alpha.restore-staging-1730000000000", true}, // exact staging format
|
||||
{"my_server.restore-staging-9", true}, // any int64 digit run (accepted reservation, see main.go)
|
||||
{"alpha.restore-staging-", false}, // empty suffix
|
||||
{"alpha.restore-staging-1730abc", false}, // non-digit tail
|
||||
{"alpha.restore.staging-1730000000000", false}, // wrong marker
|
||||
{"alpharestaging-1730000000000", false}, // no marker
|
||||
{".restore-staging-1730000000000", false}, // empty base (dot-entries are filtered anyway)
|
||||
{"alpha", false},
|
||||
{"alpha.restore-staging-99999999999999999999999", false}, // overflows int64, never a UnixMilli
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := isRestoreStagingName(c.name); got != c.want {
|
||||
t.Fatalf("isRestoreStagingName(%q) = %v, want %v", c.name, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecondDiskDeleteRoutesToOwningDisk(t *testing.T) {
|
||||
a, _ := newTestAgent(t)
|
||||
mount := t.TempDir()
|
||||
@@ -223,7 +276,7 @@ func TestSecondDiskDeleteRoutesToOwningDisk(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDiskMonitorEnforcesSecondDiskQuota(t *testing.T) {
|
||||
a, _ := newTestAgent(t)
|
||||
a := newReinstallRaceTestAgent(t)
|
||||
mount := t.TempDir()
|
||||
d := registerTestDisk(t, a, mount, 50)
|
||||
a.setServerDiskMapping("beta", d.ID)
|
||||
@@ -231,7 +284,7 @@ func TestDiskMonitorEnforcesSecondDiskQuota(t *testing.T) {
|
||||
if err := os.MkdirAll(srvDir, 0o750); err != nil {
|
||||
t.Fatalf("mkdir srv: %v", err)
|
||||
}
|
||||
meta := map[string]any{"resources": map[string]any{"storageMb": float64(20)}}
|
||||
meta := map[string]any{"type": "custom", "resources": map[string]any{"storageMb": float64(2)}}
|
||||
if err := a.saveMeta(srvDir, meta); err != nil {
|
||||
t.Fatalf("saveMeta: %v", err)
|
||||
}
|
||||
@@ -239,25 +292,47 @@ func TestDiskMonitorEnforcesSecondDiskQuota(t *testing.T) {
|
||||
if !contains(names, "beta") {
|
||||
t.Fatalf("disk monitor would not see secondary-disk server: %v", names)
|
||||
}
|
||||
if got := a.getServerDiskLimitBytes(srvDir); got != 20*1024*1024 {
|
||||
t.Fatalf("limit bytes: got %d want %d", got, 20*1024*1024)
|
||||
if got := a.getServerDiskLimitBytes(srvDir); got != 2*1024*1024 {
|
||||
t.Fatalf("limit bytes: got %d want %d", got, 2*1024*1024)
|
||||
}
|
||||
big := filepath.Join(srvDir, "server.jar")
|
||||
f, err := os.Create(big)
|
||||
if err != nil {
|
||||
t.Fatalf("create big: %v", err)
|
||||
backupsDir := a.serverBackupsDir("beta")
|
||||
if err := os.MkdirAll(backupsDir, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.Truncate(50 * 1024 * 1024); err != nil {
|
||||
f.Close()
|
||||
t.Skipf("sparse truncate unsupported: %v", err)
|
||||
for _, dir := range []string{srvDir, backupsDir} {
|
||||
if err := os.WriteFile(filepath.Join(dir, "data.bin"), make([]byte, 1536*1024), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
f.Close()
|
||||
used, err := getDirSizeBytes(srvDir)
|
||||
if err != nil {
|
||||
t.Fatalf("size: %v", err)
|
||||
limit := a.getServerDiskLimitBytes(srvDir)
|
||||
if a.getServerDiskUsage(srvDir) > limit || a.getServerTotalUsage(srvDir) <= limit {
|
||||
t.Fatal("fixture must exceed the limit only when backups are counted")
|
||||
}
|
||||
if used <= a.getServerDiskLimitBytes(srvDir) {
|
||||
t.Fatalf("expected usage %d to exceed limit %d", used, a.getServerDiskLimitBytes(srvDir))
|
||||
binDir := t.TempDir()
|
||||
stopLog := filepath.Join(binDir, "stop-target")
|
||||
if err := os.WriteFile(filepath.Join(binDir, "docker"), []byte(`#!/bin/sh
|
||||
[ "$1" = stop ] || exit 1
|
||||
for target do :; done
|
||||
printf '%s' "$target" > "$ADPANEL_TEST_DISK_STOP"
|
||||
`), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
t.Setenv("ADPANEL_TEST_DISK_STOP", stopLog)
|
||||
dockerEngineHTTPClient = &http.Client{Transport: reinstallDockerTransportFunc(func(request *http.Request) (*http.Response, error) {
|
||||
status, body := http.StatusNotFound, `{"message":"fixture not found"}`
|
||||
switch {
|
||||
case request.Method == http.MethodGet && strings.HasSuffix(request.URL.Path, "/containers/beta/json"):
|
||||
status = http.StatusOK
|
||||
body = `{"Id":"` + reinstallFixtureContainerID + `","Name":"/beta","State":{"Running":true,"Status":"running"}}`
|
||||
case request.Method == http.MethodPost && strings.HasSuffix(request.URL.Path, "/update"):
|
||||
status, body = http.StatusOK, `{}`
|
||||
}
|
||||
return &http.Response{StatusCode: status, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}, nil
|
||||
})}
|
||||
newDiskMonitor(a, 0, false).checkAllServers()
|
||||
if target, err := os.ReadFile(stopLog); err != nil || string(target) != reinstallFixtureContainerID {
|
||||
t.Fatalf("expected combined quota to stop the inspected container, got %q (%v)", target, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -292,6 +367,25 @@ func TestResolveStructuredBindsKeepsSecondDiskBind(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveStructuredBindsKeepsMountsInServerScope(t *testing.T) {
|
||||
agent, _ := newTestAgent(t)
|
||||
serverDir := agent.serverDir("alpha")
|
||||
siblingDir := agent.serverDir("beta")
|
||||
for _, dir := range []string{filepath.Join(serverDir, "data"), siblingDir} {
|
||||
if err := os.MkdirAll(dir, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.Symlink(siblingDir, filepath.Join(serverDir, "linked")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtimeConfig := map[string]any{"volumes": []string{siblingDir + ":/sibling", "{SERVER_DIR}/linked:/linked", "{SERVER_DIR}/data:/owned"}}
|
||||
binds, dataDir := agent.resolveStructuredBinds("custom", runtimeConfig, serverDir)
|
||||
if len(binds) != 1 || binds[0] != filepath.Join(serverDir, "data")+":/owned" || dataDir != "/owned" {
|
||||
t.Fatalf("only the server-owned directory may be bound: %v, %q", binds, dataDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestColdCacheDeleteRemovesSecondDiskFiles(t *testing.T) {
|
||||
a, _ := newTestAgent(t)
|
||||
mount := t.TempDir()
|
||||
|
||||
Reference in new issue
Block a user