Files
Denozordec ea2ecb44d2
Publish telemt-api gateway Docker image / test (push) Successful in 9s
Publish telemt-api gateway Docker image / build-and-push (push) Successful in 1m53s
Implement radar-telemt-dcs aggregation endpoint and UI integration
- Added new API route `/api/agg/radar-telemt-dcs` to aggregate DC status data from multiple upstreams, including metrics like coverage percentage and RTT.
- Implemented handler logic in `handlers.go` and corresponding tests in `handlers_test.go` to ensure correct data retrieval and response formatting.
- Updated the frontend to fetch and display radar DC data, enhancing the user interface with a new section for Telemt ME snapshots.
- Enhanced documentation in `AGGREGATE.md` and `README.md` to reflect the new functionality and usage details.
2026-04-12 13:07:14 +07:00

264 lines
7.1 KiB
Go

package aggregate
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/telemt/telemt-api/internal/config"
)
func TestHandlerResolveAndFetch(t *testing.T) {
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/stats/users" {
http.NotFound(w, r)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{
"ok": true,
"data": []map[string]any{{"username": "u1", "total_octets": 42, "current_connections": 0}},
"revision": "abc",
})
}))
defer up.Close()
cfg := &config.Config{
Servers: []config.Server{
{Alias: "test", BaseURL: up.URL, PathPrefix: "/v1"},
},
}
if err := cfg.Validate(); err != nil {
t.Fatal(err)
}
parsed, err := cfg.Parse()
if err != nil {
t.Fatal(err)
}
h := NewHandler(parsed, up.Client(), nil, 0)
req := httptest.NewRequest(http.MethodGet, "/api/agg/summary?aliases=test", nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status %d body %s", rec.Code, rec.Body.String())
}
var env struct {
OK bool `json:"ok"`
Data SummaryData `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
t.Fatal(err)
}
if !env.OK || env.Data.FleetTotalMegabytes != octetsToMegabytes(42) {
t.Fatalf("data: %+v", env.Data)
}
}
func TestHandlerFleetStatus(t *testing.T) {
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/v1/health":
_ = json.NewEncoder(w).Encode(map[string]any{
"ok": true, "data": map[string]any{"status": "ok", "read_only": false}, "revision": "rh",
})
case "/v1/system/info":
_ = json.NewEncoder(w).Encode(map[string]any{
"ok": true,
"data": map[string]any{
"version": "1.0.0", "target_arch": "amd64", "target_os": "linux", "build_profile": "release",
"process_started_at_epoch_secs": 1, "uptime_seconds": 10.0, "config_path": "/x.toml",
"config_hash": "abc", "config_reload_count": uint64(0),
},
"revision": "rs",
})
default:
http.NotFound(w, r)
}
}))
defer up.Close()
cfg := &config.Config{
Servers: []config.Server{
{Alias: "test", BaseURL: up.URL, PathPrefix: "/v1"},
},
}
if err := cfg.Validate(); err != nil {
t.Fatal(err)
}
parsed, err := cfg.Parse()
if err != nil {
t.Fatal(err)
}
h := NewHandler(parsed, up.Client(), nil, 0)
req := httptest.NewRequest(http.MethodGet, "/api/agg/fleet-status?aliases=test", nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status %d body %s", rec.Code, rec.Body.String())
}
var env struct {
OK bool `json:"ok"`
Data FleetStatusData `json:"data"`
GeneratedAt string `json:"generated_at"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
t.Fatal(err)
}
if !env.OK || env.GeneratedAt == "" || len(env.Data.Servers) != 1 {
t.Fatalf("envelope: %+v", env)
}
s := env.Data.Servers[0]
if s.Alias != "test" || !s.OK || s.Health == nil || s.SystemInfo == nil {
t.Fatalf("server row: %+v", s)
}
}
func TestHandlerRadarTelemtDcs(t *testing.T) {
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/stats/dcs" {
http.NotFound(w, r)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{
"ok": true,
"data": map[string]any{
"middle_proxy_enabled": true,
"generated_at_epoch_secs": 1,
"dcs": []map[string]any{{
"dc": 1, "rtt_ms": 42.5, "coverage_pct": 100.0,
"alive_writers": 3, "required_writers": 3, "load": 0,
}},
},
"revision": "rd",
})
}))
defer up.Close()
cfg := &config.Config{
Servers: []config.Server{
{Alias: "test", BaseURL: up.URL, PathPrefix: "/v1"},
},
}
if err := cfg.Validate(); err != nil {
t.Fatal(err)
}
parsed, err := cfg.Parse()
if err != nil {
t.Fatal(err)
}
h := NewHandler(parsed, up.Client(), nil, 0)
req := httptest.NewRequest(http.MethodGet, "/api/agg/radar-telemt-dcs?aliases=test", nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status %d body %s", rec.Code, rec.Body.String())
}
var env struct {
OK bool `json:"ok"`
Data RadarTelemtDcsData `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
t.Fatal(err)
}
if !env.OK || len(env.Data.Servers) != 1 {
t.Fatalf("envelope: %+v", env)
}
row := env.Data.Servers[0]
if row.Alias != "test" || !row.OK || row.Data == nil || !row.Data.MiddleProxyEnabled {
t.Fatalf("row: %+v", row)
}
if len(row.Data.Dcs) != 1 || row.Data.Dcs[0].DC != 1 {
t.Fatalf("dcs: %+v", row.Data.Dcs)
}
}
func TestHandlerUserOne(t *testing.T) {
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/stats/users" {
http.NotFound(w, r)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{
"ok": true,
"data": []map[string]any{{
"username": "u1", "total_octets": 1048576, "current_connections": 2,
"active_unique_ips": 1, "recent_unique_ips": 1,
"max_tcp_conns": 10,
"data_quota_bytes": 1000,
}},
"revision": "abc",
})
}))
defer up.Close()
cfg := &config.Config{
Servers: []config.Server{
{Alias: "test", BaseURL: up.URL, PathPrefix: "/v1"},
},
}
if err := cfg.Validate(); err != nil {
t.Fatal(err)
}
parsed, err := cfg.Parse()
if err != nil {
t.Fatal(err)
}
h := NewHandler(parsed, up.Client(), nil, 0)
req := httptest.NewRequest(http.MethodGet, "/api/agg/user/u1?aliases=test", nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status %d body %s", rec.Code, rec.Body.String())
}
var env struct {
OK bool `json:"ok"`
Data UsersRow `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
t.Fatal(err)
}
if env.Data.Username != "u1" || env.Data.MaxTCPConns == nil || *env.Data.MaxTCPConns != 10 {
t.Fatalf("row: %+v", env.Data)
}
}
func TestHandlerUserOneInvalidName(t *testing.T) {
cfg := &config.Config{
Servers: []config.Server{{Alias: "x", BaseURL: "http://127.0.0.1:1", PathPrefix: "/v1"}},
}
if err := cfg.Validate(); err != nil {
t.Fatal(err)
}
parsed, err := cfg.Parse()
if err != nil {
t.Fatal(err)
}
h := NewHandler(parsed, http.DefaultClient, nil, 0)
req := httptest.NewRequest(http.MethodGet, "/api/agg/user/!!!", nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("got %d", rec.Code)
}
}
func TestHandlerMethodNotAllowed(t *testing.T) {
cfg := &config.Config{
Servers: []config.Server{{Alias: "x", BaseURL: "http://127.0.0.1:1", PathPrefix: "/v1"}},
}
if err := cfg.Validate(); err != nil {
t.Fatal(err)
}
parsed, err := cfg.Parse()
if err != nil {
t.Fatal(err)
}
h := NewHandler(parsed, http.DefaultClient, nil, 0)
req := httptest.NewRequest(http.MethodPost, "/api/agg/summary", nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusMethodNotAllowed {
t.Fatalf("got %d", rec.Code)
}
}