miaopanel
1package webserver
2
3import (
4 "context"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9)
10
11func TestEnsureHTTPIncludeOnFreshNginxConf(t *testing.T) {
12 cfg := testConfig(t)
13 conf := filepath.Join(cfg.PanelDir, "nginx.conf")
14 raw := "events {}\nhttp {\n include /etc/nginx/mime.types;\n}\n"
15 if err := os.WriteFile(conf, []byte(raw), 0o644); err != nil {
16 t.Fatal(err)
17 }
18 cfg.Nginx.Conf = conf
19 mgr := New(cfg, nil)
20 if err := mgr.EnsureHTTPInclude(context.Background()); err != nil {
21 t.Fatal(err)
22 }
23 data, err := os.ReadFile(conf)
24 if err != nil {
25 t.Fatal(err)
26 }
27 text := string(data)
28 for _, want := range []string{
29 "include " + cfg.Nginx.VhostDir + "/*.conf;",
30 "include " + cfg.Nginx.VhostDir + "/http/*.conf;",
31 includeMarker,
32 } {
33 if !strings.Contains(text, want) {
34 t.Fatalf("missing %q in:\n%s", want, text)
35 }
36 }
37 // A second run must be a no-op instead of inserting duplicates.
38 if err := mgr.EnsureHTTPInclude(context.Background()); err != nil {
39 t.Fatal(err)
40 }
41 again, _ := os.ReadFile(conf)
42 if strings.Count(string(again), includeMarker) != 1 {
43 t.Fatalf("include marker duplicated:\n%s", again)
44 }
45}
46
47// A leftover marker comment with no include line (hand edited file, panel
48// upgrade, changed vhost directory) used to be treated as "already patched",
49// which then broke every site with "unknown log format mp_main".
50func TestEnsureHTTPIncludeRepairsStaleMarker(t *testing.T) {
51 cfg := testConfig(t)
52 conf := filepath.Join(cfg.PanelDir, "nginx.conf")
53 raw := "events {}\nhttp {\n include /etc/nginx/mime.types;\n" +
54 " # " + includeMarker + "\n" +
55 " include " + cfg.Nginx.VhostDir + "/*.conf;\n}\n"
56 if err := os.WriteFile(conf, []byte(raw), 0o644); err != nil {
57 t.Fatal(err)
58 }
59 cfg.Nginx.Conf = conf
60 mgr := New(cfg, nil)
61 if err := mgr.EnsureHTTPInclude(context.Background()); err != nil {
62 t.Fatal(err)
63 }
64 data, _ := os.ReadFile(conf)
65 text := string(data)
66 httpInclude := "include " + cfg.Nginx.VhostDir + "/http/*.conf;"
67 siteInclude := "include " + cfg.Nginx.VhostDir + "/*.conf;"
68 if strings.Count(text, httpInclude) != 1 {
69 t.Fatalf("http include not repaired exactly once:\n%s", text)
70 }
71 if strings.Count(text, siteInclude) != 1 {
72 t.Fatalf("site include duplicated:\n%s", text)
73 }
74 if strings.Index(text, httpInclude) > strings.Index(text, siteInclude) {
75 t.Fatalf("http include must precede the site include:\n%s", text)
76 }
77}
78
79// A site include pointing at an old vhost directory must be replaced, not
80// shadowed by a second include pointing at the current one.
81func TestEnsureHTTPIncludeReplacesStaleVhostDir(t *testing.T) {
82 cfg := testConfig(t)
83 conf := filepath.Join(cfg.PanelDir, "nginx.conf")
84 raw := "events {}\nhttp {\n" +
85 " include /old/panel/vhost/nginx/http/*.conf;\n" +
86 " include /old/panel/vhost/nginx/*.conf;\n}\n"
87 if err := os.WriteFile(conf, []byte(raw), 0o644); err != nil {
88 t.Fatal(err)
89 }
90 cfg.Nginx.Conf = conf
91 mgr := New(cfg, nil)
92 patched, changed := mgr.mainConfPatch(raw)
93 if !changed {
94 t.Fatal("stale include not detected")
95 }
96 if strings.Contains(patched, "/old/panel/vhost") {
97 t.Fatalf("stale include kept:\n%s", patched)
98 }
99}