package webserver import ( "context" "os" "path/filepath" "strings" "testing" ) func TestEnsureHTTPIncludeOnFreshNginxConf(t *testing.T) { cfg := testConfig(t) conf := filepath.Join(cfg.PanelDir, "nginx.conf") raw := "events {}\nhttp {\n include /etc/nginx/mime.types;\n}\n" if err := os.WriteFile(conf, []byte(raw), 0o644); err != nil { t.Fatal(err) } cfg.Nginx.Conf = conf mgr := New(cfg, nil) if err := mgr.EnsureHTTPInclude(context.Background()); err != nil { t.Fatal(err) } data, err := os.ReadFile(conf) if err != nil { t.Fatal(err) } text := string(data) for _, want := range []string{ "include " + cfg.Nginx.VhostDir + "/*.conf;", "include " + cfg.Nginx.VhostDir + "/http/*.conf;", includeMarker, } { if !strings.Contains(text, want) { t.Fatalf("missing %q in:\n%s", want, text) } } // A second run must be a no-op instead of inserting duplicates. if err := mgr.EnsureHTTPInclude(context.Background()); err != nil { t.Fatal(err) } again, _ := os.ReadFile(conf) if strings.Count(string(again), includeMarker) != 1 { t.Fatalf("include marker duplicated:\n%s", again) } } // A leftover marker comment with no include line (hand edited file, panel // upgrade, changed vhost directory) used to be treated as "already patched", // which then broke every site with "unknown log format mp_main". func TestEnsureHTTPIncludeRepairsStaleMarker(t *testing.T) { cfg := testConfig(t) conf := filepath.Join(cfg.PanelDir, "nginx.conf") raw := "events {}\nhttp {\n include /etc/nginx/mime.types;\n" + " # " + includeMarker + "\n" + " include " + cfg.Nginx.VhostDir + "/*.conf;\n}\n" if err := os.WriteFile(conf, []byte(raw), 0o644); err != nil { t.Fatal(err) } cfg.Nginx.Conf = conf mgr := New(cfg, nil) if err := mgr.EnsureHTTPInclude(context.Background()); err != nil { t.Fatal(err) } data, _ := os.ReadFile(conf) text := string(data) httpInclude := "include " + cfg.Nginx.VhostDir + "/http/*.conf;" siteInclude := "include " + cfg.Nginx.VhostDir + "/*.conf;" if strings.Count(text, httpInclude) != 1 { t.Fatalf("http include not repaired exactly once:\n%s", text) } if strings.Count(text, siteInclude) != 1 { t.Fatalf("site include duplicated:\n%s", text) } if strings.Index(text, httpInclude) > strings.Index(text, siteInclude) { t.Fatalf("http include must precede the site include:\n%s", text) } } // A site include pointing at an old vhost directory must be replaced, not // shadowed by a second include pointing at the current one. func TestEnsureHTTPIncludeReplacesStaleVhostDir(t *testing.T) { cfg := testConfig(t) conf := filepath.Join(cfg.PanelDir, "nginx.conf") raw := "events {}\nhttp {\n" + " include /old/panel/vhost/nginx/http/*.conf;\n" + " include /old/panel/vhost/nginx/*.conf;\n}\n" if err := os.WriteFile(conf, []byte(raw), 0o644); err != nil { t.Fatal(err) } cfg.Nginx.Conf = conf mgr := New(cfg, nil) patched, changed := mgr.mainConfPatch(raw) if !changed { t.Fatal("stale include not detected") } if strings.Contains(patched, "/old/panel/vhost") { t.Fatalf("stale include kept:\n%s", patched) } }