package httpapi import ( "net/http" "os" "path/filepath" "runtime" "testing" ) func TestGoSiteDetectsExecutable(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("executable bits are not meaningful on windows") } h := newHarness(t) csrf := h.login(t) dir := filepath.Join(h.cfg.WebRoot, "goapp") if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } // Minimal ELF header is enough for detection. binary := append([]byte{0x7f, 'E', 'L', 'F'}, []byte("fake-binary")...) if err := os.WriteFile(filepath.Join(dir, "app"), binary, 0o755); err != nil { t.Fatal(err) } // A plain text file must not be offered as a candidate. if err := os.WriteFile(filepath.Join(dir, "README.md"), []byte("docs"), 0o644); err != nil { t.Fatal(err) } code, env := h.do(t, http.MethodGet, "/api/v1/sites/executables?dir=%2Fgoapp", nil, "") if code != http.StatusOK { t.Fatalf("executables status %d: %v", code, env) } list := env["data"].([]any) if len(list) != 1 { t.Fatalf("executables = %v", list) } first := list[0].(map[string]any) if first["path"] != "./app" { t.Fatalf("candidate path = %v", first["path"]) } // Create without a start command: it must be filled from the binary. code, env = h.do(t, http.MethodPost, "/api/v1/sites", map[string]any{ "kind": "go", "domains": []string{"go.test"}, "root": "/goapp", }, csrf) if code != http.StatusOK { t.Fatalf("create status %d: %v", code, env) } site := env["data"].(map[string]any) if site["start_cmd"] != "./app" { t.Fatalf("start_cmd = %v, want ./app", site["start_cmd"]) } if site["root"] != filepath.Join(h.cfg.WebRoot, "goapp") { t.Fatalf("root = %v", site["root"]) } if site["port"].(float64) < 8000 { t.Fatalf("port not allocated: %v", site["port"]) } } func TestGoSiteChmodsUploadedBinary(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("executable bits are not meaningful on windows") } h := newHarness(t) csrf := h.login(t) dir := filepath.Join(h.cfg.WebRoot, "uploaded") if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } bin := filepath.Join(dir, "server") if err := os.WriteFile(bin, append([]byte{0x7f, 'E', 'L', 'F'}, []byte("x")...), 0o644); err != nil { t.Fatal(err) } // Detection works even without the executable bit (uploads land at 0644). code, env := h.do(t, http.MethodGet, "/api/v1/sites/executables?dir=%2Fuploaded", nil, "") if code != http.StatusOK || len(env["data"].([]any)) != 1 { t.Fatalf("executables = %d %v", code, env) } code, env = h.do(t, http.MethodPost, "/api/v1/sites", map[string]any{ "kind": "go", "domains": []string{"uploaded.test"}, "root": "/uploaded", }, csrf) if code != http.StatusOK { t.Fatalf("create status %d: %v", code, env) } info, err := os.Stat(bin) if err != nil { t.Fatal(err) } if info.Mode().Perm()&0o111 == 0 { t.Fatalf("binary was not made executable: %v", info.Mode()) } } func TestGoSiteWithoutExecutableFails(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("executable bits are not meaningful on windows") } h := newHarness(t) csrf := h.login(t) dir := filepath.Join(h.cfg.WebRoot, "empty") if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } code, env := h.do(t, http.MethodPost, "/api/v1/sites", map[string]any{ "kind": "go", "domains": []string{"empty.test"}, "root": "/empty", }, csrf) if code != http.StatusBadRequest { t.Fatalf("status %d: %v", code, env) } }