Initial upload
This commit is contained in:
1 parent
b0bf3b7167
commit
a022f29e34
65 files changed
+46484
-4339
No files matched your search
@@ -0,0 +1,268 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsTransientDockerPullError(t *testing.T) {
|
||||
transient := []string{
|
||||
`failed to pull image eclipse-temurin:17-jre: failed to copy: httpReadSeeker: failed open: failed to do request: local error: tls: bad record MAC`,
|
||||
`unexpected EOF`,
|
||||
`Get "https://registry-1.docker.io/v2/": net/http: TLS handshake timeout`,
|
||||
`received unexpected HTTP status: 503 Service Unavailable`,
|
||||
}
|
||||
for _, msg := range transient {
|
||||
if !isTransientDockerPullError(errors.New(msg)) {
|
||||
t.Fatalf("expected transient pull error for %q", msg)
|
||||
}
|
||||
}
|
||||
|
||||
permanent := []string{
|
||||
`manifest unknown`,
|
||||
`pull access denied, repository does not exist or may require authorization`,
|
||||
`unauthorized: authentication required`,
|
||||
`invalid reference format`,
|
||||
}
|
||||
for _, msg := range permanent {
|
||||
if isTransientDockerPullError(errors.New(msg)) {
|
||||
t.Fatalf("expected permanent pull error for %q", msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferDockerAPIConsoleTransportByDefault(t *testing.T) {
|
||||
t.Setenv("ADPANEL_DOCKER_STREAM_TRANSPORT", "")
|
||||
if preferDockerCLIConsoleTransport() {
|
||||
t.Fatalf("expected Docker API attach transport by default")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferDockerCLIConsoleTransportOverride(t *testing.T) {
|
||||
t.Setenv("ADPANEL_DOCKER_STREAM_TRANSPORT", "cli")
|
||||
if !preferDockerCLIConsoleTransport() {
|
||||
t.Fatalf("expected Docker CLI transport override to be honored")
|
||||
}
|
||||
t.Setenv("ADPANEL_DOCKER_STREAM_TRANSPORT", "docker-api")
|
||||
if preferDockerCLIConsoleTransport() {
|
||||
t.Fatalf("expected Docker API transport override to be honored")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnterpriseLinuxOSReleaseDetection(t *testing.T) {
|
||||
if !isEnterpriseLinuxOSRelease("ID=rocky\nID_LIKE=\"rhel centos fedora\"\n") {
|
||||
t.Fatalf("expected Rocky Linux to prefer enterprise Linux stdin fallback")
|
||||
}
|
||||
if !isEnterpriseLinuxOSRelease("ID=almalinux\nID_LIKE=\"rhel centos fedora\"\n") {
|
||||
t.Fatalf("expected AlmaLinux to prefer enterprise Linux stdin fallback")
|
||||
}
|
||||
if isEnterpriseLinuxOSRelease("ID=ubuntu\nID_LIKE=debian\n") {
|
||||
t.Fatalf("did not expect Ubuntu to prefer enterprise Linux stdin fallback")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferDockerCLIPTYStdinTransportOverride(t *testing.T) {
|
||||
t.Setenv("ADPANEL_DOCKER_STDIN_TRANSPORT", "pty")
|
||||
if !preferDockerCLIPTYStdinTransport() {
|
||||
t.Fatalf("expected Docker CLI PTY stdin override to be honored")
|
||||
}
|
||||
t.Setenv("ADPANEL_DOCKER_STDIN_TRANSPORT", "docker-api")
|
||||
if preferDockerCLIPTYStdinTransport() {
|
||||
t.Fatalf("expected Docker API stdin override to be honored")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDockerLogSnapshotEntriesKeepsTimestamps(t *testing.T) {
|
||||
raw := "2026-05-14T19:20:01.123456789Z server starting\n" +
|
||||
"2026-05-14T19:20:02Z Done (3.2s)! For help, type \"help\"\n"
|
||||
|
||||
entries := parseDockerLogSnapshotEntries(raw, 10)
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("expected 2 entries, got %#v", entries)
|
||||
}
|
||||
if entries[0].line != "server starting" {
|
||||
t.Fatalf("expected timestamp prefix to be removed, got %q", entries[0].line)
|
||||
}
|
||||
if entries[0].ts != 1778786401123 {
|
||||
t.Fatalf("expected nanosecond timestamp to be parsed as ms, got %d", entries[0].ts)
|
||||
}
|
||||
if entries[1].line != `Done (3.2s)! For help, type "help"` {
|
||||
t.Fatalf("unexpected second line: %q", entries[1].line)
|
||||
}
|
||||
if entries[1].ts != 1778786402000 {
|
||||
t.Fatalf("expected whole-second timestamp to be parsed as ms, got %d", entries[1].ts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPterodactylRustKeepsHomeContainerVolume(t *testing.T) {
|
||||
cfg := imageConfig{
|
||||
Entrypoint: []string{"/bin/bash", "/entrypoint.sh"},
|
||||
Workdir: "/home/container",
|
||||
Env: map[string]string{},
|
||||
}
|
||||
binds, dataDir := sanitizeCustomImageDefaultBinds(
|
||||
"ghcr.io/pterodactyl/games:rust",
|
||||
cfg,
|
||||
[]string{"/srv/adpanel/rust:/home/container"},
|
||||
"/home/container",
|
||||
)
|
||||
if len(binds) != 1 || binds[0] != "/srv/adpanel/rust:/home/container" {
|
||||
t.Fatalf("expected /home/container bind to be preserved, got %#v", binds)
|
||||
}
|
||||
if dataDir != "/home/container" {
|
||||
t.Fatalf("expected dataDir /home/container, got %q", dataDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageWorkdirBindIsKeptWhenItIsKnownGameDataPath(t *testing.T) {
|
||||
cfg := imageConfig{
|
||||
Entrypoint: []string{"/entrypoint.sh"},
|
||||
Workdir: "/steamcmd/rust",
|
||||
Env: map[string]string{},
|
||||
}
|
||||
binds, dataDir := sanitizeCustomImageDefaultBinds(
|
||||
"didstopia/rust-server:full",
|
||||
cfg,
|
||||
[]string{"/srv/adpanel/rust:/steamcmd/rust"},
|
||||
"/steamcmd/rust",
|
||||
)
|
||||
if len(binds) != 1 || binds[0] != "/srv/adpanel/rust:/steamcmd/rust" {
|
||||
t.Fatalf("expected known data-path bind to be preserved, got %#v", binds)
|
||||
}
|
||||
if dataDir != "/steamcmd/rust" {
|
||||
t.Fatalf("expected dataDir /steamcmd/rust, got %q", dataDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRustRestartRecreatesContainer(t *testing.T) {
|
||||
if !shouldRecreateContainerOnRestart(map[string]any{"type": "rust"}) {
|
||||
t.Fatalf("expected rust template restart to recreate the container")
|
||||
}
|
||||
if !shouldRecreateContainerOnRestart(map[string]any{
|
||||
"type": "custom",
|
||||
"runtime": map[string]any{
|
||||
"image": "ghcr.io/pterodactyl/games",
|
||||
"tag": "rust",
|
||||
},
|
||||
}) {
|
||||
t.Fatalf("expected pterodactyl rust image restart to recreate the container")
|
||||
}
|
||||
if shouldRecreateContainerOnRestart(map[string]any{"type": "minecraft"}) {
|
||||
t.Fatalf("did not expect minecraft restart to use rust recreate path")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRustGetsDefaultHighFileLimit(t *testing.T) {
|
||||
resources := withRuntimeDefaultFileLimit(nil, "rust", "ghcr.io/pterodactyl/games:rust")
|
||||
if resources == nil || resources.FileLimit == nil || *resources.FileLimit != maxFileLimit {
|
||||
t.Fatalf("expected rust to default to max file limit, got %#v", resources)
|
||||
}
|
||||
|
||||
customLimit := 65535
|
||||
resources = withRuntimeDefaultFileLimit(&resourceLimits{FileLimit: &customLimit}, "rust", "ghcr.io/pterodactyl/games:rust")
|
||||
if resources == nil || resources.FileLimit == nil || *resources.FileLimit != customLimit {
|
||||
t.Fatalf("expected explicit file limit to be preserved, got %#v", resources)
|
||||
}
|
||||
|
||||
if got := withRuntimeDefaultFileLimit(nil, "minecraft", "eclipse-temurin:21-jre"); got != nil {
|
||||
t.Fatalf("did not expect non-rust runtime defaults, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCfxRuntimeLayoutStaysInsideMountedServerDir(t *testing.T) {
|
||||
serverDir := t.TempDir()
|
||||
if err := ensureDir(filepath.Join(serverDir, "serverfiles")); err != nil {
|
||||
t.Fatalf("failed to create nested legacy dir: %v", err)
|
||||
}
|
||||
if err := writeFileNoFollow(filepath.Join(serverDir, "serverfiles", "server.cfg"), []byte("sv_maxclients 32\n"), 0o644); err != nil {
|
||||
t.Fatalf("failed to create legacy cfg: %v", err)
|
||||
}
|
||||
|
||||
runtimeObj := map[string]any{
|
||||
"volumes": []any{"{BOT_DIR}:/serverdata"},
|
||||
"workdir": "/serverdata",
|
||||
"install": map[string]any{
|
||||
"runtimePath": "/serverdata",
|
||||
},
|
||||
"env": map[string]any{
|
||||
"DATA_DIR": "/serverdata",
|
||||
"SERVER_DIR": "/serverdata/serverfiles",
|
||||
"DATA_PERM": "770",
|
||||
"SERVER_KEY": "keep-me",
|
||||
"START_VARS": "+set gamename rdr3",
|
||||
"GAME_CONFIG": "old.cfg",
|
||||
},
|
||||
}
|
||||
|
||||
if !normalizeCfxRuntimeLayout("redm", runtimeObj, serverDir) {
|
||||
t.Fatalf("expected legacy CFX runtime to be normalized")
|
||||
}
|
||||
|
||||
volumes := runtimeObj["volumes"].([]any)
|
||||
if len(volumes) != 1 || volumes[0] != "{BOT_DIR}:/serverdata/serverfiles" {
|
||||
t.Fatalf("expected server root to be mounted directly to CFX server dir, got %#v", volumes)
|
||||
}
|
||||
if runtimeObj["workdir"] != "/serverdata/serverfiles" {
|
||||
t.Fatalf("expected CFX workdir inside mounted server dir, got %#v", runtimeObj["workdir"])
|
||||
}
|
||||
if runtimeObj["runAsRoot"] != true {
|
||||
t.Fatalf("expected CFX entrypoint to keep image root bootstrap enabled, got %#v", runtimeObj["runAsRoot"])
|
||||
}
|
||||
|
||||
env := runtimeObj["env"].(map[string]string)
|
||||
for key, want := range map[string]string{
|
||||
"DATA_DIR": "/serverdata/serverfiles",
|
||||
"SERVER_DIR": "/serverdata/serverfiles",
|
||||
"HOME": "/serverdata/serverfiles",
|
||||
"DATA_PERM": "777",
|
||||
"GAME_CONFIG": "server.cfg",
|
||||
} {
|
||||
if got := env[key]; got != want {
|
||||
t.Fatalf("expected env %s=%q, got %q", key, want, got)
|
||||
}
|
||||
}
|
||||
if env["SERVER_KEY"] != "keep-me" || env["START_VARS"] != "+set gamename rdr3" {
|
||||
t.Fatalf("expected CFX-specific user env to be preserved, got %#v", env)
|
||||
}
|
||||
install := runtimeObj["install"].(map[string]any)
|
||||
if install["runtimePath"] != "/serverdata/serverfiles" || install["mountPath"] != "/mnt/server" {
|
||||
t.Fatalf("expected CFX install paths to stay aligned to mounted dir, got %#v", install)
|
||||
}
|
||||
if !pathExists(filepath.Join(serverDir, "server.cfg")) {
|
||||
t.Fatalf("expected nested legacy serverfiles content to be migrated into server root")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyRustRuntimeMigratesToPterodactyl(t *testing.T) {
|
||||
meta := map[string]any{
|
||||
"type": "rust",
|
||||
"template": "rust",
|
||||
"runtime": map[string]any{
|
||||
"image": "didstopia/rust-server",
|
||||
"tag": "full",
|
||||
"volumes": []any{"{BOT_DIR}:/steamcmd/rust"},
|
||||
"workdir": "/",
|
||||
"env": map[string]any{
|
||||
"RUST_SERVER_NAME": "Legacy Rust",
|
||||
"RUST_RCON_PASSWORD": "secret",
|
||||
"RUST_SERVER_QUERYPORT": "28016",
|
||||
},
|
||||
},
|
||||
}
|
||||
if !normalizeRustRuntimeForPterodactyl(meta) {
|
||||
t.Fatalf("expected legacy rust metadata to be migrated")
|
||||
}
|
||||
runtimeObj := meta["runtime"].(map[string]any)
|
||||
if runtimeObj["image"] != "ghcr.io/pterodactyl/games" || runtimeObj["tag"] != "rust" {
|
||||
t.Fatalf("expected pterodactyl rust runtime, got %#v:%#v", runtimeObj["image"], runtimeObj["tag"])
|
||||
}
|
||||
env := runtimeObj["env"].(map[string]string)
|
||||
if env["SERVER_NAME"] != "Legacy Rust" || env["RCON_PASS"] != "secret" {
|
||||
t.Fatalf("expected legacy env values to be preserved, got %#v", env)
|
||||
}
|
||||
volumes := runtimeObj["volumes"].([]any)
|
||||
if len(volumes) != 1 || volumes[0] != "{BOT_DIR}:/home/container" {
|
||||
t.Fatalf("expected home/container volume, got %#v", volumes)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user