仰望星辰工作室

miaopanel

miaopanel/ internal/appstore/appstore_test.go 10.7 KB · 356 行 原始文件
1package appstore
2
3import (
4 "context"
5 "strings"
6 "testing"
7
8 "github.com/miaopanel/miaopanel/internal/config"
9 "github.com/miaopanel/miaopanel/internal/system"
10)
11
12var testDistro = system.Distro{ID: "debian", Family: "debian", Version: "12"}
13var testRHEL = system.Distro{ID: "rocky", Family: "rhel", Version: "9.4"}
14
15func testService() *Service {
16 cfg := config.Default()
17 cfg.WebRoot = "/www/wwwroot"
18 return &Service{cfg: cfg, tasks: newTaskManager("/tmp/mp-appstore-test")}
19}
20
21func TestCatalogIsConsistent(t *testing.T) {
22 seen := map[string]bool{}
23 for _, app := range catalog {
24 if app.ID == "" || app.Name == "" || len(app.Versions) == 0 {
25 t.Fatalf("invalid catalog entry: %+v", app)
26 }
27 if seen[app.ID] {
28 t.Fatalf("duplicate app %q", app.ID)
29 }
30 switch app.Family {
31 case "", "any", "debian", "rhel":
32 default:
33 t.Fatalf("%s has an unknown family %q", app.ID, app.Family)
34 }
35 seen[app.ID] = true
36 for _, v := range app.Versions {
37 if v.Version == "" || v.Label == "" {
38 t.Fatalf("%s has an empty version", app.ID)
39 }
40 }
41 }
42}
43
44func TestInstallScriptsRender(t *testing.T) {
45 s := testService()
46 ctx := context.Background()
47 for _, app := range catalog {
48 if app.ID == "python" {
49 continue // resolved from the network at install time
50 }
51 script, err := s.installScript(ctx, app, app.Versions[0])
52 if err != nil {
53 t.Fatalf("%s: %v", app.ID, err)
54 }
55 if strings.Contains(script, "%!") {
56 t.Fatalf("%s script has a formatting error:\n%s", app.ID, script)
57 }
58 if !strings.Contains(script, "set -euo pipefail") {
59 t.Fatalf("%s script misses the safety header", app.ID)
60 }
61 }
62}
63
64func TestPythonScriptHasNoFormattingError(t *testing.T) {
65 script := pythonInstallScript(testDistro, "3.12.7", "https://example.com/python.tar.gz")
66 if strings.Contains(script, "%!") {
67 t.Fatalf("python script has a formatting error:\n%s", script)
68 }
69 for _, want := range []string{"/www/server/python/3.12.7", "python3 -V"} {
70 if !strings.Contains(script, want) {
71 t.Fatalf("python script missing %q", want)
72 }
73 }
74}
75
76func TestUninstallScriptsRejectInjection(t *testing.T) {
77 s := testService()
78 if _, err := s.uninstallScript(mustApp("go"), Version{Version: "1.2.3; rm -rf /"}); err == nil {
79 t.Fatal("unsafe version accepted")
80 }
81 script, err := s.uninstallScript(mustApp("go"), Version{Version: "1.27.3"})
82 if err != nil {
83 t.Fatal(err)
84 }
85 if !strings.Contains(script, "rm -rf \"$ROOT/$VER\"") {
86 t.Fatalf("unexpected uninstall script:\n%s", script)
87 }
88}
89
90func TestNginxScriptPinsVersion(t *testing.T) {
91 script := nginxInstallScript(testDistro, "1.26.3")
92 if !strings.Contains(script, `v="1.26.3"`) {
93 t.Fatalf("nginx version not pinned:\n%s", script)
94 }
95 if strings.Contains(script, "1.26.3-1~") {
96 t.Fatal("nginx suffix must be resolved from the repository")
97 }
98}
99
100func TestNginxCatalogHasNewStableBranch(t *testing.T) {
101 nginx := mustApp("nginx")
102 if got := nginx.Versions[0].Version; got != "1.30.6" {
103 t.Fatalf("newest nginx version is %s, want 1.30.6", got)
104 }
105 if _, ok := lookupVersion(nginx, "1.28.3"); !ok {
106 t.Fatal("nginx 1.28.3 missing")
107 }
108}
109
110func TestNginxScriptHandlesDowngradeAndModuleABI(t *testing.T) {
111 script := nginxInstallScript(testDistro, "1.30.6")
112 for _, want := range []string{
113 "--allow-downgrades",
114 "dists/$CODENAME/Release",
115 "modules-enabled.mp-disabled",
116 "apt-cache madison nginx",
117 } {
118 if !strings.Contains(script, want) {
119 t.Fatalf("nginx script missing %q:\n%s", want, script)
120 }
121 }
122}
123
124// TestInstallScriptsAvoidSigPipe guards the failure mode seen in the field:
125// a consumer that exits before draining stdin (grep -q, head, "awk ... exit")
126// makes the producer die of SIGPIPE, and `set -o pipefail` turns that into
127// "安装失败: exit status 141" with no hint about the real step. Ubuntu 24.04 +
128// nginx 1.30.6 reproduced it on `apt-cache madison nginx | awk ... exit`.
129func TestInstallScriptsAvoidSigPipe(t *testing.T) {
130 s := testService()
131 ctx := context.Background()
132 check := func(name, script string) {
133 for _, line := range strings.Split(script, "\n") {
134 trimmed := strings.TrimSpace(line)
135 if trimmed == "" || strings.HasPrefix(trimmed, "#") {
136 continue
137 }
138 if strings.Contains(line, "| grep -q") || strings.Contains(line, "|grep -q") ||
139 strings.Contains(line, "| head") || strings.Contains(line, "|head ") {
140 t.Fatalf("%s: consumer exits early on a pipe (%q); use `>/dev/null` after grep or write to a file first", name, trimmed)
141 }
142 // "END { exit !found }" still drains stdin, so only an exit in the
143 // per-record action block is unsafe.
144 if strings.Contains(line, "| awk") && strings.Contains(line, "exit") &&
145 !strings.Contains(line, "END {") {
146 t.Fatalf("%s: awk exits early on a pipe (%q); the producer would take SIGPIPE", name, trimmed)
147 }
148 }
149 }
150 for _, app := range catalog {
151 if app.ID == "python" {
152 continue // resolved from the network at install time
153 }
154 for _, ver := range app.Versions {
155 script, err := s.installScript(ctx, app, ver)
156 if err != nil {
157 t.Fatalf("%s: %v", app.ID, err)
158 }
159 check(app.ID+" install", script)
160 uninstall, err := s.uninstallScript(app, ver)
161 if err != nil {
162 t.Fatalf("%s uninstall: %v", app.ID, err)
163 }
164 check(app.ID+" uninstall", uninstall)
165 }
166 }
167}
168
169func TestMySQLUninstallRemovesCorePackage(t *testing.T) {
170 s := testService()
171 script, err := s.uninstallScript(mustApp("mysql"), Version{Version: "8.4.13"})
172 if err != nil {
173 t.Fatal(err)
174 }
175 for _, want := range []string{"mysql-community-server-core", "systemctl disable --now", "卸载未完成"} {
176 if !strings.Contains(script, want) {
177 t.Fatalf("mysql uninstall script missing %q:\n%s", want, script)
178 }
179 }
180}
181
182func TestMySQLAndMariaDBAreSeparate(t *testing.T) {
183 mysql, ok := lookupApp("mysql")
184 if !ok || mysql.Name != "MySQL" {
185 t.Fatalf("mysql app missing: %+v", mysql)
186 }
187 mariadb, ok := lookupApp("mariadb")
188 if !ok || mariadb.Name != "MariaDB" {
189 t.Fatalf("mariadb app missing: %+v", mariadb)
190 }
191 if _, ok := lookupVersion(mysql, "8.4"); !ok {
192 t.Fatal("mysql 8.4 missing")
193 }
194 if _, ok := lookupVersion(mariadb, "10.11"); !ok {
195 t.Fatal("mariadb 10.11 missing")
196 }
197}
198
199func TestMySQLScriptsForBothFamilies(t *testing.T) {
200 debian := mysqlInstallScript(testDistro, "8.4")
201 for _, want := range []string{"mysql-8.4-lts", "mysql-community-server", "repo.mysql.com/apt/debian"} {
202 if !strings.Contains(debian, want) {
203 t.Fatalf("debian mysql script missing %q:\n%s", want, debian)
204 }
205 }
206 rhel := mysqlInstallScript(testRHEL, "8.4")
207 for _, want := range []string{"mysql-8.4-community", "mysql-community-server", "repo.mysql.com/yum"} {
208 if !strings.Contains(rhel, want) {
209 t.Fatalf("rhel mysql script missing %q:\n%s", want, rhel)
210 }
211 }
212 if strings.Contains(debian, "%!") || strings.Contains(rhel, "%!") {
213 t.Fatal("mysql script has a formatting error")
214 }
215}
216
217func TestMariaDBScriptForBothFamilies(t *testing.T) {
218 for _, d := range []system.Distro{testDistro, testRHEL} {
219 script := mariadbInstallScript(d, "10.11")
220 if !strings.Contains(script, "mariadb_repo_setup") || !strings.Contains(script, "mariadb-server") {
221 t.Fatalf("%s mariadb script incomplete:\n%s", d.Family, script)
222 }
223 }
224}
225
226func TestPHPCovers71To85(t *testing.T) {
227 php := mustApp("php")
228 for _, v := range []string{"7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2", "8.3", "8.4", "8.5"} {
229 if _, ok := lookupVersion(php, v); !ok {
230 t.Fatalf("php %s missing", v)
231 }
232 }
233 script := phpInstallScript(testDistro, "7.1")
234 if !strings.Contains(script, "php7.1-fpm") || strings.Contains(script, "%!") {
235 t.Fatalf("bad php 7.1 script:\n%s", script)
236 }
237}
238
239func TestLegacyMySQLIgnoresRemovedPackageConfig(t *testing.T) {
240 script, err := mysqlLegacyInstallScript(testDistro, "5.6.51")
241 if err != nil {
242 t.Fatal(err)
243 }
244 if !strings.Contains(script, "Status: install ok installed") {
245 t.Fatalf("legacy mysql must distinguish removed packages from installed ones:\n%s", script)
246 }
247}
248
249func TestLegacyMySQLVersions(t *testing.T) {
250 mysql := mustApp("mysql")
251 for _, v := range []string{"5.5.62", "5.6.51", "5.7.44"} {
252 if _, ok := lookupVersion(mysql, v); !ok {
253 t.Fatalf("mysql %s missing", v)
254 }
255 }
256 script, err := mysqlLegacyInstallScript(testDistro, "5.7.44")
257 if err != nil {
258 t.Fatal(err)
259 }
260 for _, want := range []string{
261 "mysql-5.7.44-linux-glibc2.12-x86_64",
262 "/www/server/mysql",
263 "--initialize-insecure",
264 "/etc/systemd/system/mysql.service",
265 } {
266 if !strings.Contains(script, want) {
267 t.Fatalf("legacy mysql script missing %q", want)
268 }
269 }
270 if strings.Contains(script, "%!") {
271 t.Fatal("legacy mysql script has a formatting error")
272 }
273}
274
275func TestLegacyMySQLHardensDependenciesAndInit(t *testing.T) {
276 script, err := mysqlLegacyInstallScript(testDistro, "5.7.44")
277 if err != nil {
278 t.Fatal(err)
279 }
280 for _, want := range []string{
281 "x86_64|amd64",
282 "ensure_compat_link libaio.so.1 libaio.so.1t64",
283 "libtinfo.so.5 libtinfo.so.6",
284 "perl",
285 "INIT_LOG",
286 "tail -n 50 /www/wwwlogs/mysql/error.log",
287 `rm -rf "$TMP"`,
288 "TimeoutStartSec=300",
289 } {
290 if !strings.Contains(script, want) {
291 t.Fatalf("legacy mysql script missing %q:\n%s", want, script)
292 }
293 }
294 if strings.Contains(script, "ExecStop=") {
295 t.Fatal("ExecStop must not call mysqladmin: a changed root password breaks every stop")
296 }
297}
298
299func TestLegacyMySQL55UsesInstallDB(t *testing.T) {
300 script, err := mysqlLegacyInstallScript(testDistro, "5.5.62")
301 if err != nil {
302 t.Fatal(err)
303 }
304 if !strings.Contains(script, `INIT_MODE="install_db"`) ||
305 !strings.Contains(script, "scripts/mysql_install_db") {
306 t.Fatalf("mysql 5.5 must use the Perl initializer:\n%s", script)
307 }
308 latest, err := mysqlLegacyInstallScript(testDistro, "5.7.44")
309 if err != nil {
310 t.Fatal(err)
311 }
312 if !strings.Contains(latest, `INIT_MODE="initialize"`) {
313 t.Fatalf("mysql 5.7 must use --initialize-insecure:\n%s", latest)
314 }
315}
316
317func TestPhpMyAdminSessionDirIsWritableByFPM(t *testing.T) {
318 s := testService()
319 script := s.phpMyAdminInstallScript(testDistro, "5.2.2")
320 for _, want := range []string{"chmod 1733", "www-data", "SessionSavePath"} {
321 if !strings.Contains(script, want) {
322 t.Fatalf("phpmyadmin script missing %q", want)
323 }
324 }
325}
326
327func TestTaskLogIsBounded(t *testing.T) {
328 tk := &task{}
329 for i := 0; i < maxTaskLogLines+50; i++ {
330 tk.append("installer output line")
331 }
332 snap := tk.snapshot()
333 if len(snap.Log) > maxTaskLogLines+1 {
334 t.Fatalf("task log grew to %d lines", len(snap.Log))
335 }
336}
337
338func TestSupportedOnFamilies(t *testing.T) {
339 if !familySupported("debian", "debian") || familySupported("debian", "rhel") {
340 t.Fatal("debian-only app support mismatch")
341 }
342 if !familySupported("", "debian") || !familySupported("any", "rhel") {
343 t.Fatal("generic app should support both families")
344 }
345 if familySupported("any", "unknown") {
346 t.Fatal("unknown distro should be unsupported")
347 }
348}
349
350func mustApp(id string) AppDef {
351 app, ok := lookupApp(id)
352 if !ok {
353 panic("missing app " + id)
354 }
355 return app
356}