仰望星辰工作室

miaopanel

miaopanel/ internal/httpapi/sites_test.go 3.4 KB · 117 行 原始文件
1package httpapi
2
3import (
4 "net/http"
5 "os"
6 "path/filepath"
7 "runtime"
8 "testing"
9)
10
11func TestGoSiteDetectsExecutable(t *testing.T) {
12 if runtime.GOOS == "windows" {
13 t.Skip("executable bits are not meaningful on windows")
14 }
15 h := newHarness(t)
16 csrf := h.login(t)
17
18 dir := filepath.Join(h.cfg.WebRoot, "goapp")
19 if err := os.MkdirAll(dir, 0o755); err != nil {
20 t.Fatal(err)
21 }
22 // Minimal ELF header is enough for detection.
23 binary := append([]byte{0x7f, 'E', 'L', 'F'}, []byte("fake-binary")...)
24 if err := os.WriteFile(filepath.Join(dir, "app"), binary, 0o755); err != nil {
25 t.Fatal(err)
26 }
27 // A plain text file must not be offered as a candidate.
28 if err := os.WriteFile(filepath.Join(dir, "README.md"), []byte("docs"), 0o644); err != nil {
29 t.Fatal(err)
30 }
31
32 code, env := h.do(t, http.MethodGet, "/api/v1/sites/executables?dir=%2Fgoapp", nil, "")
33 if code != http.StatusOK {
34 t.Fatalf("executables status %d: %v", code, env)
35 }
36 list := env["data"].([]any)
37 if len(list) != 1 {
38 t.Fatalf("executables = %v", list)
39 }
40 first := list[0].(map[string]any)
41 if first["path"] != "./app" {
42 t.Fatalf("candidate path = %v", first["path"])
43 }
44
45 // Create without a start command: it must be filled from the binary.
46 code, env = h.do(t, http.MethodPost, "/api/v1/sites", map[string]any{
47 "kind": "go", "domains": []string{"go.test"}, "root": "/goapp",
48 }, csrf)
49 if code != http.StatusOK {
50 t.Fatalf("create status %d: %v", code, env)
51 }
52 site := env["data"].(map[string]any)
53 if site["start_cmd"] != "./app" {
54 t.Fatalf("start_cmd = %v, want ./app", site["start_cmd"])
55 }
56 if site["root"] != filepath.Join(h.cfg.WebRoot, "goapp") {
57 t.Fatalf("root = %v", site["root"])
58 }
59 if site["port"].(float64) < 8000 {
60 t.Fatalf("port not allocated: %v", site["port"])
61 }
62}
63
64func TestGoSiteChmodsUploadedBinary(t *testing.T) {
65 if runtime.GOOS == "windows" {
66 t.Skip("executable bits are not meaningful on windows")
67 }
68 h := newHarness(t)
69 csrf := h.login(t)
70
71 dir := filepath.Join(h.cfg.WebRoot, "uploaded")
72 if err := os.MkdirAll(dir, 0o755); err != nil {
73 t.Fatal(err)
74 }
75 bin := filepath.Join(dir, "server")
76 if err := os.WriteFile(bin, append([]byte{0x7f, 'E', 'L', 'F'}, []byte("x")...), 0o644); err != nil {
77 t.Fatal(err)
78 }
79
80 // Detection works even without the executable bit (uploads land at 0644).
81 code, env := h.do(t, http.MethodGet, "/api/v1/sites/executables?dir=%2Fuploaded", nil, "")
82 if code != http.StatusOK || len(env["data"].([]any)) != 1 {
83 t.Fatalf("executables = %d %v", code, env)
84 }
85
86 code, env = h.do(t, http.MethodPost, "/api/v1/sites", map[string]any{
87 "kind": "go", "domains": []string{"uploaded.test"}, "root": "/uploaded",
88 }, csrf)
89 if code != http.StatusOK {
90 t.Fatalf("create status %d: %v", code, env)
91 }
92 info, err := os.Stat(bin)
93 if err != nil {
94 t.Fatal(err)
95 }
96 if info.Mode().Perm()&0o111 == 0 {
97 t.Fatalf("binary was not made executable: %v", info.Mode())
98 }
99}
100
101func TestGoSiteWithoutExecutableFails(t *testing.T) {
102 if runtime.GOOS == "windows" {
103 t.Skip("executable bits are not meaningful on windows")
104 }
105 h := newHarness(t)
106 csrf := h.login(t)
107 dir := filepath.Join(h.cfg.WebRoot, "empty")
108 if err := os.MkdirAll(dir, 0o755); err != nil {
109 t.Fatal(err)
110 }
111 code, env := h.do(t, http.MethodPost, "/api/v1/sites", map[string]any{
112 "kind": "go", "domains": []string{"empty.test"}, "root": "/empty",
113 }, csrf)
114 if code != http.StatusBadRequest {
115 t.Fatalf("status %d: %v", code, env)
116 }
117}