Files
Denozordec d021e4b1d7
Publish telemt-api gateway Docker image / test (push) Successful in 23s
Publish telemt-api gateway Docker image / build-and-push (push) Successful in 2m9s
Add Radar DC Telegram functionality and configuration
- Introduced new `RadarConfig` structure in `config.go` to manage radar settings, including `statuses_url`, `http_timeout_ms`, and `ping_from`.
- Implemented validation for radar configuration in `config_test.go` to ensure correct URL schemes and timeout limits.
- Added new API routes for radar statuses and ping functionality in the gateway, enhancing the service's capabilities.
- Updated documentation in `GATEWAY_RUN.md` to include details about the new radar features and their usage.
- Enhanced the user interface to include navigation and display options for the Radar DC section in the sidebar and page titles.
- Added client-side API functions for fetching radar statuses and ping responses, improving integration with the frontend.
2026-04-12 12:53:20 +07:00

141 lines
2.9 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
)
func TestLoadExample(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "cfg.yaml")
if err := os.WriteFile(p, []byte(`
listen: ":0"
allow_all: true
servers:
- alias: main_srv
base_url: http://127.0.0.1:9091
`), 0644); err != nil {
t.Fatal(err)
}
c, err := Load(p)
if err != nil {
t.Fatal(err)
}
if c.Listen != ":0" {
t.Fatalf("listen: %q", c.Listen)
}
_, err = c.Parse()
if err != nil {
t.Fatal(err)
}
}
func TestParseCIDRorIP(t *testing.T) {
for _, tc := range []struct {
in string
want string
}{
{"87.103.241.8", "87.103.241.8/32"},
{"87.103.241.8/32", "87.103.241.8/32"},
{"10.0.0.0/8", "10.0.0.0/8"},
{"2001:db8::1", "2001:db8::1/128"},
} {
p, err := parseCIDROrIP(tc.in)
if err != nil {
t.Fatalf("%q: %v", tc.in, err)
}
if p.String() != tc.want {
t.Fatalf("%q: got %s want %s", tc.in, p, tc.want)
}
}
}
func TestValidateDuplicateAlias(t *testing.T) {
c := &Config{
Servers: []Server{
{Alias: "a", BaseURL: "http://x:1"},
{Alias: "a", BaseURL: "http://y:2"},
},
}
if err := c.Validate(); err == nil {
t.Fatal("expected error")
}
}
func TestValidateReservedAggAlias(t *testing.T) {
c := &Config{
Servers: []Server{
{Alias: "agg", BaseURL: "http://x:1"},
},
}
if err := c.Validate(); err == nil {
t.Fatal("expected error for reserved alias agg")
}
}
func TestValidateAggregateIncludeAliases(t *testing.T) {
c := &Config{
Servers: []Server{
{Alias: "a", BaseURL: "http://x:1"},
},
Aggregate: &AggregateConfig{
IncludeAliases: []string{"a"},
},
}
if err := c.Validate(); err != nil {
t.Fatal(err)
}
c2 := &Config{
Servers: []Server{
{Alias: "a", BaseURL: "http://x:1"},
},
Aggregate: &AggregateConfig{
IncludeAliases: []string{"nope"},
},
}
if err := c2.Validate(); err == nil {
t.Fatal("expected error for unknown include alias")
}
}
func TestValidateAggregateCacheTTL(t *testing.T) {
c := &Config{
Servers: []Server{
{Alias: "a", BaseURL: "http://x:1"},
},
Aggregate: &AggregateConfig{CacheTTLMs: 60001},
}
if err := c.Validate(); err == nil {
t.Fatal("expected error for cache_ttl_ms > 60000")
}
c.Aggregate.CacheTTLMs = 1000
if err := c.Validate(); err != nil {
t.Fatal(err)
}
}
func TestValidateRadarStatusesURL(t *testing.T) {
c := &Config{
Servers: []Server{{Alias: "a", BaseURL: "http://x:1"}},
Radar: &RadarConfig{StatusesURL: "ftp://bad"},
}
if err := c.Validate(); err == nil {
t.Fatal("expected error for invalid radar.statuses_url scheme")
}
c.Radar.StatusesURL = "https://radar.example/api"
if err := c.Validate(); err != nil {
t.Fatal(err)
}
}
func TestValidateRadarHTTPTimeoutMs(t *testing.T) {
c := &Config{
Servers: []Server{{Alias: "a", BaseURL: "http://x:1"}},
Radar: &RadarConfig{HTTPTimeoutMs: 600001},
}
if err := c.Validate(); err == nil {
t.Fatal("expected error for radar.http_timeout_ms > 600000")
}
}