Files
3x-ui-agent/internal/agent/telemetry_test.go
T

77 lines
2.4 KiB
Go

package agent
import (
"net"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/config"
)
func TestParseXrayVersion(t *testing.T) {
cases := []struct {
in string
want string
ok bool
}{
{"Xray 26.6.27 (Xray, Penetrates Everything.) 45cf289 (go1.26.4 linux/amd64)\nA unified platform for anti-censorship.", "26.6.27", true},
{"Xray 1.8.4", "1.8.4", true},
{"V2Ray 4.45.4 (V2Fly, a community-driven edition of V2Ray.)", "", false},
{"", "", false},
{"Xray", "", false},
{"not a version banner", "", false},
{"Xray (no version here)", "", false},
}
for _, c := range cases {
got, ok := parseXrayVersion(c.in)
if got != c.want || ok != c.ok {
t.Errorf("parseXrayVersion(%q) = %q,%v want %q,%v", c.in, got, ok, c.want, c.ok)
}
}
}
func TestChooseHostIPs(t *testing.T) {
mk := func(ss ...string) []net.IP {
var out []net.IP
for _, s := range ss {
out = append(out, net.ParseIP(s))
}
return out
}
cases := []struct {
name string
in []net.IP
v4 string
v6 string
}{
{"public wins over private", mk("10.0.0.5", "8.8.4.4", "2001:db8::1", "fd00::1"), "8.8.4.4", "2001:db8::1"},
{"private fallback", mk("192.168.1.2", "fd00::9"), "192.168.1.2", "fd00::9"},
{"loopback and link-local excluded", mk("127.0.0.1", "169.254.1.1", "fe80::1", "::1"), "", ""},
{"empty", nil, "", ""},
{"multicast excluded", mk("224.0.0.1", "ff02::1"), "", ""},
}
for _, c := range cases {
v4, v6 := chooseHostIPs(c.in)
if v4 != c.v4 || v6 != c.v6 {
t.Errorf("%s: chooseHostIPs = %q,%q want %q,%q", c.name, v4, v6, c.v4, c.v6)
}
}
}
// TestCollectTelemetryShape pins the always-on fields on any Linux host:
// the agent's own A-series display version (item 18) and a positive host
// uptime. Source-specific fields (xray version, IPs) are
// environment-dependent and not asserted.
func TestCollectTelemetryShape(t *testing.T) {
a := New(&Config{MasterURL: "http://127.0.0.1:1", Token: "t", NodeName: "n"})
tel := a.collectTelemetry()
if tel.AgentVersion != config.GetAgentDisplayVersion() || tel.AgentVersion == "" {
t.Errorf("AgentVersion = %q, want config.GetAgentDisplayVersion() %q", tel.AgentVersion, config.GetAgentDisplayVersion())
}
if tel.UptimeSeconds == nil || *tel.UptimeSeconds == 0 {
t.Errorf("UptimeSeconds = %v, want non-nil positive", tel.UptimeSeconds)
}
if tel.MemPercent == nil || *tel.MemPercent < 0 || *tel.MemPercent > 100 {
t.Errorf("MemPercent = %v, want within [0,100]", tel.MemPercent)
}
}