Files
l-ui/internal/agent/singbox_apply_test.go
T
Muse 6a5ee8f77b rename: 3x-ui/x-ui -> L-UI/l-ui/lui-agent (paths, services, env, assets)
Display L-UI; panel binary/process l-ui, agent lui-agent.
Dirs /usr/local/l-ui, /etc/l-ui, /var/log/l-ui; DB /etc/l-ui/l-ui.db
(name file drives it via GetName), probe.db beside it, cert
/etc/l-ui/cert, bin /usr/local/l-ui/bin. Services l-ui.service /
lui-agent.service, OpenRC lui-agent, l-ui.sh, cmd/lui-agent.
Env prefix XUI_ -> LUI_ (code reads new prefix only). Release
assets l-ui-linux-* / lui-agent-linux-*; Gitea repo rogue/l-ui is
a placeholder pending user confirmation (see selfupdate.go note).
Go module path github.com/mhsanaei/3x-ui/v3 intentionally kept.
Versions unchanged: panel V2.6.0 / agent A2.5.0.
2026-10-24 12:00:55 +00:00

212 lines
5.5 KiB
Go

package agent
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
// fakeSingbox installs a shell-script sing-box stand-in into a temp
// bin folder: `check` rejects configs containing BADSCHEMA, `run`
// dies instantly on RUNTIMEFAIL and otherwise sleeps (standing in for
// the long-running core). The real binary's behavior is covered by
// the env-gated E2E; these tests pin the agent's activation
// discipline: check gate, no-change no-restart, rollback.
const fakeSingboxScript = `#!/bin/sh
case "$1" in
version)
echo "sing-box version 9.9.9"
exit 0
;;
check)
if grep -q BADSCHEMA "$3" 2>/dev/null; then
echo "check: invalid config" >&2
exit 1
fi
exit 0
;;
run)
if grep -q RUNTIMEFAIL "$3" 2>/dev/null; then
echo "fatal: cannot start" >&2
exit 1
fi
exec sleep 3600
;;
esac
exit 2
`
func setupFakeSingbox(t *testing.T) (binDir string) {
t.Helper()
binDir = t.TempDir()
t.Setenv("LUI_BIN_FOLDER", binDir)
path := filepath.Join(binDir, singboxBinaryName())
if err := os.WriteFile(path, []byte(fakeSingboxScript), 0o755); err != nil {
t.Fatal(err)
}
return binDir
}
func newApplyTestAgent() *Agent {
return New(&Config{MasterURL: "http://127.0.0.1:1", Token: "tok", NodeName: "n"})
}
const (
sbCfgA = `{"log":{"level":"info"},"inbounds":[],"outbounds":[{"type":"direct","tag":"direct"}]}`
sbCfgB = `{"log":{"level":"debug"},"inbounds":[],"outbounds":[{"type":"direct","tag":"direct"}]}`
)
func appliedConfig(t *testing.T) string {
t.Helper()
data, err := os.ReadFile(singboxConfigPath())
if err != nil {
t.Fatalf("read applied config: %v", err)
}
return string(data)
}
func TestApplySingboxStartsProcess(t *testing.T) {
setupFakeSingbox(t)
a := newApplyTestAgent()
t.Cleanup(a.stopSingbox)
if err := a.applySingboxConfig([]byte(sbCfgA)); err != nil {
t.Fatalf("apply: %v", err)
}
a.sbMu.Lock()
running := a.sbProc.isRunning()
pid := a.sbProc.pid()
a.sbMu.Unlock()
if !running || pid <= 0 {
t.Fatal("sing-box process not running after apply")
}
if got := appliedConfig(t); got != sbCfgA {
t.Fatalf("applied config = %q, want cfgA", got)
}
if _, err := os.Stat(singboxCandidatePath()); !os.IsNotExist(err) {
t.Fatal("candidate file must not linger after a successful apply")
}
}
func TestApplySingboxCheckGateKeepsRunningCore(t *testing.T) {
setupFakeSingbox(t)
a := newApplyTestAgent()
t.Cleanup(a.stopSingbox)
if err := a.applySingboxConfig([]byte(sbCfgA)); err != nil {
t.Fatalf("apply A: %v", err)
}
a.sbMu.Lock()
pidBefore := a.sbProc.pid()
a.sbMu.Unlock()
bad := `{"log":{"level":"info"},"inbounds":[{"type":"BADSCHEMA"}]}`
err := a.applySingboxConfig([]byte(bad))
if err == nil || !strings.Contains(err.Error(), "check") {
t.Fatalf("apply bad config err = %v, want a check failure", err)
}
a.sbMu.Lock()
pidAfter := a.sbProc.pid()
running := a.sbProc.isRunning()
a.sbMu.Unlock()
if !running || pidAfter != pidBefore {
t.Fatalf("check failure disturbed the running core: pid %d -> %d running=%v", pidBefore, pidAfter, running)
}
if got := appliedConfig(t); got != sbCfgA {
t.Fatalf("active config changed after failed check: %q", got)
}
}
func TestApplySingboxUnchangedNoRestart(t *testing.T) {
setupFakeSingbox(t)
a := newApplyTestAgent()
t.Cleanup(a.stopSingbox)
if err := a.applySingboxConfig([]byte(sbCfgA)); err != nil {
t.Fatalf("apply A: %v", err)
}
a.sbMu.Lock()
pidBefore := a.sbProc.pid()
a.sbMu.Unlock()
if err := a.applySingboxConfig([]byte(sbCfgA)); err != nil {
t.Fatalf("re-apply A: %v", err)
}
a.sbMu.Lock()
pidAfter := a.sbProc.pid()
a.sbMu.Unlock()
if pidAfter != pidBefore {
t.Fatalf("unchanged config restarted the core: pid %d -> %d", pidBefore, pidAfter)
}
}
func TestApplySingboxRuntimeFailureRollsBack(t *testing.T) {
setupFakeSingbox(t)
a := newApplyTestAgent()
t.Cleanup(a.stopSingbox)
if err := a.applySingboxConfig([]byte(sbCfgA)); err != nil {
t.Fatalf("apply A: %v", err)
}
bad := `{"log":{"level":"info"},"note":"RUNTIMEFAIL","inbounds":[]}`
if err := a.applySingboxConfig([]byte(bad)); err == nil {
t.Fatal("apply of a config that dies at startup must fail")
}
a.sbMu.Lock()
running := a.sbProc.isRunning()
a.sbMu.Unlock()
if !running {
t.Fatal("rollback must leave a running core")
}
if got := appliedConfig(t); got != sbCfgA {
t.Fatalf("active config after rollback = %q, want cfgA", got)
}
}
func TestApplySingboxMissingBinary(t *testing.T) {
binDir := t.TempDir()
t.Setenv("LUI_BIN_FOLDER", binDir)
a := newApplyTestAgent()
if err := a.applySingboxConfig([]byte(sbCfgA)); !errors.Is(err, errSingboxNotInstalled) {
t.Fatalf("err = %v, want errSingboxNotInstalled", err)
}
}
func TestApplySingboxInvalidJSON(t *testing.T) {
setupFakeSingbox(t)
a := newApplyTestAgent()
t.Cleanup(a.stopSingbox)
if err := a.applySingboxConfig([]byte("{not json")); err == nil {
t.Fatal("invalid JSON must fail")
}
a.sbMu.Lock()
proc := a.sbProc
a.sbMu.Unlock()
if proc != nil && proc.isRunning() {
t.Fatal("no process may start from invalid JSON")
}
}
func TestParseSingboxVersion(t *testing.T) {
cases := []struct {
out string
want string
ok bool
}{
{"sing-box version 1.13.21\n\nEnvironment: go1.26.9 linux/amd64", "1.13.21", true},
{"sing-box version unknown\n", "unknown", true},
{"sing-box version \n", "", false},
{"xray version 1.0\n", "", false},
{"", "", false},
}
for _, tc := range cases {
got, ok := parseSingboxVersion(tc.out)
if got != tc.want || ok != tc.ok {
t.Errorf("parseSingboxVersion(%q) = %q,%v want %q,%v", tc.out, got, ok, tc.want, tc.ok)
}
}
}