- Introduced AggregateConfig to manage aggregation settings in the gateway configuration. - Added validation for reserved alias 'agg' and included tests for aggregate alias handling. - Updated config.example.yaml to demonstrate aggregate configuration options. - Enhanced README.md to include information about the new aggregation endpoint and its usage. - Modified gateway.go to integrate the new aggregate handler for processing aggregation requests.
123 lines
3.0 KiB
Go
123 lines
3.0 KiB
Go
package aggregate
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildTraffic(t *testing.T) {
|
|
results := []ServerFetchResult{
|
|
{
|
|
Alias: "a", OK: true, Revision: "r1",
|
|
Users: []UserInfo{
|
|
{Username: "u1", TotalOctets: 100, CurrentConnections: 2},
|
|
{Username: "u2", TotalOctets: 50, CurrentConnections: 0},
|
|
},
|
|
},
|
|
{
|
|
Alias: "b", OK: true, Revision: "r2",
|
|
Users: []UserInfo{
|
|
{Username: "u1", TotalOctets: 30, CurrentConnections: 1},
|
|
},
|
|
},
|
|
}
|
|
rows := BuildTraffic(results)
|
|
if len(rows) != 2 {
|
|
t.Fatalf("len rows: %d", len(rows))
|
|
}
|
|
var u1 *TrafficRow
|
|
for i := range rows {
|
|
if rows[i].Username == "u1" {
|
|
u1 = &rows[i]
|
|
break
|
|
}
|
|
}
|
|
if u1 == nil {
|
|
t.Fatal("missing u1")
|
|
}
|
|
if u1.Servers["a"].TotalOctets != 100 || u1.Servers["b"].TotalOctets != 30 {
|
|
t.Fatalf("u1 servers: %+v", u1.Servers)
|
|
}
|
|
}
|
|
|
|
func TestBuildUniqueIPs(t *testing.T) {
|
|
results := []ServerFetchResult{
|
|
{
|
|
Alias: "s1", OK: true,
|
|
Users: []UserInfo{
|
|
{
|
|
Username: "alice",
|
|
ActiveUniqueIPsList: []string{"10.0.0.1"},
|
|
RecentUniqueIPsList: []string{"10.0.0.2"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Alias: "s2", OK: true,
|
|
Users: []UserInfo{
|
|
{
|
|
Username: "alice",
|
|
ActiveUniqueIPsList: []string{"10.0.0.1", "10.0.0.3"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
rows := BuildUniqueIPs(results)
|
|
if len(rows) != 1 || rows[0].Username != "alice" {
|
|
t.Fatalf("rows: %+v", rows)
|
|
}
|
|
var ip1 *IPAssignments
|
|
for i := range rows[0].IPs {
|
|
if rows[0].IPs[i].IP == "10.0.0.1" {
|
|
ip1 = &rows[0].IPs[i]
|
|
break
|
|
}
|
|
}
|
|
if ip1 == nil {
|
|
t.Fatal("missing 10.0.0.1")
|
|
}
|
|
if !reflect.DeepEqual(ip1.ActiveOnServers, []string{"s1", "s2"}) {
|
|
t.Fatalf("active: %v", ip1.ActiveOnServers)
|
|
}
|
|
if ip1.PrimaryServer != nil {
|
|
t.Fatalf("expected no primary, got %v", *ip1.PrimaryServer)
|
|
}
|
|
}
|
|
|
|
func TestBuildUniqueIPsPrimary(t *testing.T) {
|
|
results := []ServerFetchResult{
|
|
{Alias: "only", OK: true, Users: []UserInfo{
|
|
{Username: "bob", ActiveUniqueIPsList: []string{"192.168.1.1"}},
|
|
}},
|
|
}
|
|
rows := BuildUniqueIPs(results)
|
|
if len(rows[0].IPs) != 1 || rows[0].IPs[0].PrimaryServer == nil || *rows[0].IPs[0].PrimaryServer != "only" {
|
|
t.Fatalf("primary: %+v", rows[0].IPs)
|
|
}
|
|
}
|
|
|
|
func TestBuildSummary(t *testing.T) {
|
|
results := []ServerFetchResult{
|
|
{Alias: "a", OK: true, Users: []UserInfo{{Username: "x", TotalOctets: 100, CurrentConnections: 2}}},
|
|
{Alias: "b", OK: false, Error: "down"},
|
|
}
|
|
s := BuildSummary(results, 5)
|
|
if s.ServersOK != 1 || s.ServersFailed != 1 || s.FleetTotalOctets != 100 || s.FleetTotalConnections != 2 {
|
|
t.Fatalf("summary: %+v", s)
|
|
}
|
|
if len(s.TopUsers) != 1 || s.TopUsers[0].Username != "x" {
|
|
t.Fatalf("top: %+v", s.TopUsers)
|
|
}
|
|
}
|
|
|
|
func TestBuildUsersMinOctets(t *testing.T) {
|
|
results := []ServerFetchResult{
|
|
{Alias: "a", OK: true, Users: []UserInfo{{Username: "low", TotalOctets: 5}}},
|
|
{Alias: "b", OK: true, Users: []UserInfo{{Username: "high", TotalOctets: 100}}},
|
|
}
|
|
rows := BuildUsers(results, false, 50)
|
|
if len(rows) != 1 || rows[0].Username != "high" {
|
|
t.Fatalf("rows: %+v", rows)
|
|
}
|
|
}
|