package appstore import ( "context" "strings" "testing" "github.com/miaopanel/miaopanel/internal/config" "github.com/miaopanel/miaopanel/internal/system" ) var testDistro = system.Distro{ID: "debian", Family: "debian", Version: "12"} var testRHEL = system.Distro{ID: "rocky", Family: "rhel", Version: "9.4"} func testService() *Service { cfg := config.Default() cfg.WebRoot = "/www/wwwroot" return &Service{cfg: cfg, tasks: newTaskManager("/tmp/mp-appstore-test")} } func TestCatalogIsConsistent(t *testing.T) { seen := map[string]bool{} for _, app := range catalog { if app.ID == "" || app.Name == "" || len(app.Versions) == 0 { t.Fatalf("invalid catalog entry: %+v", app) } if seen[app.ID] { t.Fatalf("duplicate app %q", app.ID) } switch app.Family { case "", "any", "debian", "rhel": default: t.Fatalf("%s has an unknown family %q", app.ID, app.Family) } seen[app.ID] = true for _, v := range app.Versions { if v.Version == "" || v.Label == "" { t.Fatalf("%s has an empty version", app.ID) } } } } func TestInstallScriptsRender(t *testing.T) { s := testService() ctx := context.Background() for _, app := range catalog { if app.ID == "python" { continue // resolved from the network at install time } script, err := s.installScript(ctx, app, app.Versions[0]) if err != nil { t.Fatalf("%s: %v", app.ID, err) } if strings.Contains(script, "%!") { t.Fatalf("%s script has a formatting error:\n%s", app.ID, script) } if !strings.Contains(script, "set -euo pipefail") { t.Fatalf("%s script misses the safety header", app.ID) } } } func TestPythonScriptHasNoFormattingError(t *testing.T) { script := pythonInstallScript(testDistro, "3.12.7", "https://example.com/python.tar.gz") if strings.Contains(script, "%!") { t.Fatalf("python script has a formatting error:\n%s", script) } for _, want := range []string{"/www/server/python/3.12.7", "python3 -V"} { if !strings.Contains(script, want) { t.Fatalf("python script missing %q", want) } } } func TestUninstallScriptsRejectInjection(t *testing.T) { s := testService() if _, err := s.uninstallScript(mustApp("go"), Version{Version: "1.2.3; rm -rf /"}); err == nil { t.Fatal("unsafe version accepted") } script, err := s.uninstallScript(mustApp("go"), Version{Version: "1.27.3"}) if err != nil { t.Fatal(err) } if !strings.Contains(script, "rm -rf \"$ROOT/$VER\"") { t.Fatalf("unexpected uninstall script:\n%s", script) } } func TestNginxScriptPinsVersion(t *testing.T) { script := nginxInstallScript(testDistro, "1.26.3") if !strings.Contains(script, `v="1.26.3"`) { t.Fatalf("nginx version not pinned:\n%s", script) } if strings.Contains(script, "1.26.3-1~") { t.Fatal("nginx suffix must be resolved from the repository") } } func TestNginxCatalogHasNewStableBranch(t *testing.T) { nginx := mustApp("nginx") if got := nginx.Versions[0].Version; got != "1.30.6" { t.Fatalf("newest nginx version is %s, want 1.30.6", got) } if _, ok := lookupVersion(nginx, "1.28.3"); !ok { t.Fatal("nginx 1.28.3 missing") } } func TestNginxScriptHandlesDowngradeAndModuleABI(t *testing.T) { script := nginxInstallScript(testDistro, "1.30.6") for _, want := range []string{ "--allow-downgrades", "dists/$CODENAME/Release", "modules-enabled.mp-disabled", "apt-cache madison nginx", } { if !strings.Contains(script, want) { t.Fatalf("nginx script missing %q:\n%s", want, script) } } } // TestInstallScriptsAvoidSigPipe guards the failure mode seen in the field: // a consumer that exits before draining stdin (grep -q, head, "awk ... exit") // makes the producer die of SIGPIPE, and `set -o pipefail` turns that into // "安装失败: exit status 141" with no hint about the real step. Ubuntu 24.04 + // nginx 1.30.6 reproduced it on `apt-cache madison nginx | awk ... exit`. func TestInstallScriptsAvoidSigPipe(t *testing.T) { s := testService() ctx := context.Background() check := func(name, script string) { for _, line := range strings.Split(script, "\n") { trimmed := strings.TrimSpace(line) if trimmed == "" || strings.HasPrefix(trimmed, "#") { continue } if strings.Contains(line, "| grep -q") || strings.Contains(line, "|grep -q") || strings.Contains(line, "| head") || strings.Contains(line, "|head ") { t.Fatalf("%s: consumer exits early on a pipe (%q); use `>/dev/null` after grep or write to a file first", name, trimmed) } // "END { exit !found }" still drains stdin, so only an exit in the // per-record action block is unsafe. if strings.Contains(line, "| awk") && strings.Contains(line, "exit") && !strings.Contains(line, "END {") { t.Fatalf("%s: awk exits early on a pipe (%q); the producer would take SIGPIPE", name, trimmed) } } } for _, app := range catalog { if app.ID == "python" { continue // resolved from the network at install time } for _, ver := range app.Versions { script, err := s.installScript(ctx, app, ver) if err != nil { t.Fatalf("%s: %v", app.ID, err) } check(app.ID+" install", script) uninstall, err := s.uninstallScript(app, ver) if err != nil { t.Fatalf("%s uninstall: %v", app.ID, err) } check(app.ID+" uninstall", uninstall) } } } func TestMySQLUninstallRemovesCorePackage(t *testing.T) { s := testService() script, err := s.uninstallScript(mustApp("mysql"), Version{Version: "8.4.13"}) if err != nil { t.Fatal(err) } for _, want := range []string{"mysql-community-server-core", "systemctl disable --now", "卸载未完成"} { if !strings.Contains(script, want) { t.Fatalf("mysql uninstall script missing %q:\n%s", want, script) } } } func TestMySQLAndMariaDBAreSeparate(t *testing.T) { mysql, ok := lookupApp("mysql") if !ok || mysql.Name != "MySQL" { t.Fatalf("mysql app missing: %+v", mysql) } mariadb, ok := lookupApp("mariadb") if !ok || mariadb.Name != "MariaDB" { t.Fatalf("mariadb app missing: %+v", mariadb) } if _, ok := lookupVersion(mysql, "8.4"); !ok { t.Fatal("mysql 8.4 missing") } if _, ok := lookupVersion(mariadb, "10.11"); !ok { t.Fatal("mariadb 10.11 missing") } } func TestMySQLScriptsForBothFamilies(t *testing.T) { debian := mysqlInstallScript(testDistro, "8.4") for _, want := range []string{"mysql-8.4-lts", "mysql-community-server", "repo.mysql.com/apt/debian"} { if !strings.Contains(debian, want) { t.Fatalf("debian mysql script missing %q:\n%s", want, debian) } } rhel := mysqlInstallScript(testRHEL, "8.4") for _, want := range []string{"mysql-8.4-community", "mysql-community-server", "repo.mysql.com/yum"} { if !strings.Contains(rhel, want) { t.Fatalf("rhel mysql script missing %q:\n%s", want, rhel) } } if strings.Contains(debian, "%!") || strings.Contains(rhel, "%!") { t.Fatal("mysql script has a formatting error") } } func TestMariaDBScriptForBothFamilies(t *testing.T) { for _, d := range []system.Distro{testDistro, testRHEL} { script := mariadbInstallScript(d, "10.11") if !strings.Contains(script, "mariadb_repo_setup") || !strings.Contains(script, "mariadb-server") { t.Fatalf("%s mariadb script incomplete:\n%s", d.Family, script) } } } func TestPHPCovers71To85(t *testing.T) { php := mustApp("php") for _, v := range []string{"7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2", "8.3", "8.4", "8.5"} { if _, ok := lookupVersion(php, v); !ok { t.Fatalf("php %s missing", v) } } script := phpInstallScript(testDistro, "7.1") if !strings.Contains(script, "php7.1-fpm") || strings.Contains(script, "%!") { t.Fatalf("bad php 7.1 script:\n%s", script) } } func TestLegacyMySQLIgnoresRemovedPackageConfig(t *testing.T) { script, err := mysqlLegacyInstallScript(testDistro, "5.6.51") if err != nil { t.Fatal(err) } if !strings.Contains(script, "Status: install ok installed") { t.Fatalf("legacy mysql must distinguish removed packages from installed ones:\n%s", script) } } func TestLegacyMySQLVersions(t *testing.T) { mysql := mustApp("mysql") for _, v := range []string{"5.5.62", "5.6.51", "5.7.44"} { if _, ok := lookupVersion(mysql, v); !ok { t.Fatalf("mysql %s missing", v) } } script, err := mysqlLegacyInstallScript(testDistro, "5.7.44") if err != nil { t.Fatal(err) } for _, want := range []string{ "mysql-5.7.44-linux-glibc2.12-x86_64", "/www/server/mysql", "--initialize-insecure", "/etc/systemd/system/mysql.service", } { if !strings.Contains(script, want) { t.Fatalf("legacy mysql script missing %q", want) } } if strings.Contains(script, "%!") { t.Fatal("legacy mysql script has a formatting error") } } func TestLegacyMySQLHardensDependenciesAndInit(t *testing.T) { script, err := mysqlLegacyInstallScript(testDistro, "5.7.44") if err != nil { t.Fatal(err) } for _, want := range []string{ "x86_64|amd64", "ensure_compat_link libaio.so.1 libaio.so.1t64", "libtinfo.so.5 libtinfo.so.6", "perl", "INIT_LOG", "tail -n 50 /www/wwwlogs/mysql/error.log", `rm -rf "$TMP"`, "TimeoutStartSec=300", } { if !strings.Contains(script, want) { t.Fatalf("legacy mysql script missing %q:\n%s", want, script) } } if strings.Contains(script, "ExecStop=") { t.Fatal("ExecStop must not call mysqladmin: a changed root password breaks every stop") } } func TestLegacyMySQL55UsesInstallDB(t *testing.T) { script, err := mysqlLegacyInstallScript(testDistro, "5.5.62") if err != nil { t.Fatal(err) } if !strings.Contains(script, `INIT_MODE="install_db"`) || !strings.Contains(script, "scripts/mysql_install_db") { t.Fatalf("mysql 5.5 must use the Perl initializer:\n%s", script) } latest, err := mysqlLegacyInstallScript(testDistro, "5.7.44") if err != nil { t.Fatal(err) } if !strings.Contains(latest, `INIT_MODE="initialize"`) { t.Fatalf("mysql 5.7 must use --initialize-insecure:\n%s", latest) } } func TestPhpMyAdminSessionDirIsWritableByFPM(t *testing.T) { s := testService() script := s.phpMyAdminInstallScript(testDistro, "5.2.2") for _, want := range []string{"chmod 1733", "www-data", "SessionSavePath"} { if !strings.Contains(script, want) { t.Fatalf("phpmyadmin script missing %q", want) } } } func TestTaskLogIsBounded(t *testing.T) { tk := &task{} for i := 0; i < maxTaskLogLines+50; i++ { tk.append("installer output line") } snap := tk.snapshot() if len(snap.Log) > maxTaskLogLines+1 { t.Fatalf("task log grew to %d lines", len(snap.Log)) } } func TestSupportedOnFamilies(t *testing.T) { if !familySupported("debian", "debian") || familySupported("debian", "rhel") { t.Fatal("debian-only app support mismatch") } if !familySupported("", "debian") || !familySupported("any", "rhel") { t.Fatal("generic app should support both families") } if familySupported("any", "unknown") { t.Fatal("unknown distro should be unsupported") } } func mustApp(id string) AppDef { app, ok := lookupApp(id) if !ok { panic("missing app " + id) } return app }