feat(domain): 支持通配符域名及关联功能适配 (#63)

* feat(domain): 支持通配符域名及关联功能适配

* fix(domain): preserve legacy Unicode imports and address review findings

* fix(domain): canonicalize legacy IDNs and exclude empty host labels

* fix(domain): normalize equivalent Unicode IDN spellings

---------

Co-authored-by: Ryan <63696351+Rain-kl@users.noreply.github.com>
Co-authored-by: ryan <ryan@arctel.net>
This commit is contained in:
Deqiying
2026-11-09 10:31:03 +08:00
committed by GitHub
parent 8d8b3e1ea8
commit 9a51a68206
37 changed files with 1614 additions and 113 deletions
+11 -12
View File
@@ -4,16 +4,15 @@
package zone
const (
errZoneDomainRequired = "domain name must not be empty"
errZoneRootInvalid = "zone must be a valid registered root domain"
errDomainInvalid = "invalid domain name format"
errDomainWildcardUnsupported = "wildcard domain names are not supported"
errDomainOutsideZone = "domain does not belong to this zone"
errZoneNotFound = "Zone was not found"
errDomainNotFound = "Domain was not found"
errDomainExists = "domain already exists"
errCertificateNotFound = "selected certificate was not found"
errDomainBoundToRoute = "domain is bound to a proxy route; remove the binding first"
errZoneHasDomains = "root domain still contains domains; delete them all first"
errStatsRangeInvalid = "invalid time range; select 24h, 7d or 30d"
errZoneDomainRequired = "domain name must not be empty"
errZoneRootInvalid = "zone must be a valid registered root domain"
errDomainInvalid = "invalid domain name format"
errDomainOutsideZone = "domain does not belong to this zone"
errZoneNotFound = "Zone was not found"
errDomainNotFound = "Domain was not found"
errDomainExists = "domain already exists"
errCertificateNotFound = "selected certificate was not found"
errDomainBoundToRoute = "domain is bound to a proxy route; remove the binding first"
errZoneHasDomains = "root domain still contains domains; delete them all first"
errStatsRangeInvalid = "invalid time range; select 24h, 7d or 30d"
)
+27 -5
View File
@@ -13,6 +13,7 @@ import (
"strings"
"github.com/Rain-kl/Wavelet/internal/apps/openflare/routeidentity"
"golang.org/x/net/idna"
)
// ImportReport describes the idempotent legacy migration result.
@@ -82,7 +83,7 @@ func ImportLegacyTx(ctx context.Context, tx *sql.Tx, postgres bool) (report Impo
}
for _, item := range items {
domain, normErr := normalizeDomain(item.Domain)
domain, normErr := normalizeLegacyDomain(item.Domain)
if normErr != nil {
report.Conflicts = append(report.Conflicts, fmt.Sprintf("%s: %v", item.Domain, normErr))
continue
@@ -95,22 +96,28 @@ func ImportLegacyTx(ctx context.Context, tx *sql.Tx, postgres bool) (report Impo
var existingID uint
var existingZoneDomain string
var existingRouteID, existingCertID sql.Null[uint]
scanErr := tx.QueryRowContext(ctx, q(`
SELECT zd.id, z.domain
SELECT zd.id, z.domain, zd.proxy_route_id, zd.cert_id
FROM of_zone_domains zd
JOIN of_zones z ON z.id = zd.zone_id
WHERE zd.domain = ?
`), domain).Scan(&existingID, &existingZoneDomain)
`), domain).Scan(&existingID, &existingZoneDomain, &existingRouteID, &existingCertID)
if scanErr == nil {
if (item.ProxyRouteID != nil && existingRouteID.Valid && existingRouteID.V != *item.ProxyRouteID) ||
(item.CertID != nil && existingCertID.Valid && existingCertID.V != *item.CertID) {
report.Conflicts = append(report.Conflicts, domain+": canonical domain binding conflict")
continue
}
if existingZoneDomain != root {
report.Conflicts = append(report.Conflicts, domain+": global domain conflict")
} else if item.ProxyRouteID != nil {
} else if item.ProxyRouteID != nil || item.CertID != nil {
if _, bindErr := tx.ExecContext(ctx, q(`
UPDATE of_zone_domains
SET proxy_route_id = COALESCE(proxy_route_id, ?),
cert_id = COALESCE(cert_id, ?)
WHERE id = ?
`), *item.ProxyRouteID, nullableUint(item.CertID), existingID); bindErr != nil {
`), nullableUint(item.ProxyRouteID), nullableUint(item.CertID), existingID); bindErr != nil {
return report, bindErr
}
}
@@ -152,6 +159,21 @@ func ImportLegacyTx(ctx context.Context, tx *sql.Tx, postgres bool) (report Impo
return report, nil
}
// normalizeLegacyDomain canonicalizes historical IDNs for DNS identity and rendering.
// Certificate associations and route bindings remain attached to the imported row.
func normalizeLegacyDomain(raw string) (string, error) {
domain := strings.ToLower(strings.TrimSpace(raw))
host := strings.TrimPrefix(domain, "*.")
ascii, err := idna.Lookup.ToASCII(host)
if err != nil {
return "", errors.New(errDomainInvalid)
}
if host != domain {
ascii = "*." + ascii
}
return normalizeDomain(ascii)
}
func ensureZone(
ctx context.Context,
tx *sql.Tx,
@@ -148,3 +148,117 @@ func TestImportLegacyTxNoOpWithoutLegacyColumns(t *testing.T) {
assert.Equal(t, 0, report.Zones)
assert.Equal(t, 0, report.Domains)
}
// TestImportLegacyTxUnicodeDomains canonicalizes historical names and preserves bindings on upgrade.
func TestImportLegacyTxUnicodeDomains(t *testing.T) {
for _, source := range []string{"route", "managed"} {
t.Run(source, func(t *testing.T) {
sqlDB, cleanup := setupLegacyImportDB(t)
t.Cleanup(cleanup)
ctx := context.Background()
if _, err := sqlDB.Exec(`INSERT INTO of_tls_certificates (id, name) VALUES (7, 'cert')`); err != nil {
t.Fatal(err)
}
var err error
if source == "route" {
_, err = sqlDB.Exec(`INSERT INTO of_proxy_routes (id, domain, domains, domain_cert_ids) VALUES (3, 'api.bücher.de', '["api.bücher.de"]', '[7]')`)
} else {
_, err = sqlDB.Exec(`INSERT INTO of_managed_domains (domain, cert_id) VALUES ('api.bücher.de', 7)`)
}
if err != nil {
t.Fatal(err)
}
for attempt := range 2 {
tx, err := sqlDB.Begin()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = tx.Rollback() })
report, err := ImportLegacyTx(ctx, tx, false)
if err != nil {
t.Fatalf("ImportLegacyTx(%s, attempt=%d) = %+v, %v, want no conflicts", source, attempt, report, err)
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
want := 1 - attempt
if report.Zones != want || report.Domains != want {
t.Errorf("ImportLegacyTx(%s, attempt=%d) = %+v, want %d zones/domains", source, attempt, report, want)
}
}
var root, domain string
var certID uint
var routeID sql.NullInt64
if err := sqlDB.QueryRow(`SELECT z.domain, zd.domain, zd.cert_id, zd.proxy_route_id FROM of_zone_domains zd JOIN of_zones z ON z.id = zd.zone_id`).Scan(&root, &domain, &certID, &routeID); err != nil {
t.Fatal(err)
}
if root != "xn--bcher-kva.de" || domain != "api.xn--bcher-kva.de" || certID != 7 {
t.Errorf("imported %s = (%q, %q, cert=%d), want (xn--bcher-kva.de, api.xn--bcher-kva.de, cert=7)", source, root, domain, certID)
}
if (source == "route" && (!routeID.Valid || routeID.Int64 != 3)) || (source == "managed" && routeID.Valid) {
t.Errorf("imported %s route binding = %v", source, routeID)
}
var zoneID uint
if err := sqlDB.QueryRow(`SELECT id FROM of_zones`).Scan(&zoneID); err != nil {
t.Fatal(err)
}
if _, err := Create(ctx, Input{Domain: "xn--bcher-kva.de"}); err == nil {
t.Error("Create accepted an equivalent imported IDN zone")
}
if _, err := CreateDomain(ctx, zoneID, DomainInput{Domain: "api.xn--bcher-kva.de"}); err == nil {
t.Error("CreateDomain accepted an equivalent imported IDN host")
}
if _, err := CreateDomain(ctx, zoneID, DomainInput{Domain: "www.xn--bcher-kva.de"}); err != nil {
t.Errorf("CreateDomain in imported IDN zone: %v", err)
}
if _, err := normalizeDomain("api.bücher.de"); err == nil {
t.Error("normalizeDomain(api.bücher.de) accepted a new Unicode domain")
}
})
}
}
// TestImportLegacyTxIDNBindingConflict rejects equivalent names bound to different routes.
func TestImportLegacyTxIDNBindingConflict(t *testing.T) {
sqlDB, cleanup := setupLegacyImportDB(t)
t.Cleanup(cleanup)
if _, err := sqlDB.Exec(`INSERT INTO of_proxy_routes (id, domain, domains) VALUES (3, 'api.bücher.de', '["api.bücher.de"]'), (4, 'api.xn--bcher-kva.de', '["api.xn--bcher-kva.de"]')`); err != nil {
t.Fatal(err)
}
tx, err := sqlDB.Begin()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = tx.Rollback() })
report, err := ImportLegacyTx(context.Background(), tx, false)
if err == nil || len(report.Conflicts) != 1 {
t.Errorf("ImportLegacyTx(equivalent IDN with different bindings) = %+v, %v; want one conflict", report, err)
}
}
// TestNormalizeLegacyIDNRoot resolves Unicode public suffixes through their ASCII identity.
func TestNormalizeLegacyIDNRoot(t *testing.T) {
domain, err := normalizeLegacyDomain("api.example.公司.cn")
if err != nil {
t.Fatal(err)
}
root, err := zoneRoot(domain)
if err != nil || root != "example.xn--55qx5d.cn" {
t.Errorf("zoneRoot(%q) = %q, %v; want example.xn--55qx5d.cn", domain, root, err)
}
}
// TestNormalizeLegacyIDNCanonicalForms keeps Unicode normalization and wildcard identity consistent.
func TestNormalizeLegacyIDNCanonicalForms(t *testing.T) {
for _, raw := range []string{"api.bücher.de", "api.bu\u0308cher.de", "API.XN--BCHER-KVA.DE"} {
domain, err := normalizeLegacyDomain(raw)
if err != nil || domain != "api.xn--bcher-kva.de" {
t.Errorf("normalizeLegacyDomain(%q) = %q, %v; want api.xn--bcher-kva.de", raw, domain, err)
}
}
domain, err := normalizeLegacyDomain("*.bücher.de")
if err != nil || domain != "*.xn--bcher-kva.de" {
t.Errorf("normalizeLegacyDomain(*.bücher.de) = %q, %v; want *.xn--bcher-kva.de", domain, err)
}
}
+19 -7
View File
@@ -1,7 +1,7 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
// Package zone manages registered roots and their explicit hostnames.
// Package zone manages registered roots and their exact or wildcard hostnames.
package zone
import (
@@ -18,6 +18,8 @@ import (
"gorm.io/gorm"
)
const maxDomainLength = 253
// Input is the mutable Zone payload.
type Input struct {
Domain string `json:"domain"`
@@ -45,7 +47,8 @@ type ListItem struct {
}
func zoneRoot(domain string) (string, error) {
return publicsuffix.EffectiveTLDPlusOne(strings.ToLower(strings.TrimSpace(domain)))
domain = strings.ToLower(strings.TrimSpace(domain))
return publicsuffix.EffectiveTLDPlusOne(strings.TrimPrefix(domain, "*."))
}
func normalizeDomain(raw string) (string, error) {
@@ -53,12 +56,21 @@ func normalizeDomain(raw string) (string, error) {
if domain == "" {
return "", errors.New(errZoneDomainRequired)
}
if strings.Contains(domain, "*") {
return "", errors.New(errDomainWildcardUnsupported)
}
if strings.Contains(domain, "://") || strings.Contains(domain, "/") || strings.Contains(domain, "?") || strings.Contains(domain, "#") || strings.Contains(domain, "@") {
// 只允许最左侧完整标签为通配符;其余字符必须是 DNS 标签,不能进入配置语法。
host := strings.TrimPrefix(domain, "*.")
if len(domain) > maxDomainLength {
return "", errors.New(errDomainInvalid)
}
for _, label := range strings.Split(host, ".") {
if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
return "", errors.New(errDomainInvalid)
}
for _, char := range label {
if (char < 'a' || char > 'z') && (char < '0' || char > '9') && char != '-' {
return "", errors.New(errDomainInvalid)
}
}
}
if _, err := zoneRoot(domain); err != nil {
return "", errors.New(errDomainInvalid)
}
@@ -151,7 +163,7 @@ func GetOverview(ctx context.Context, id uint) (*Overview, error) {
return &Overview{Zone: *zone, Domains: domains}, nil
}
// CreateDomain adds a validated exact hostname to a Zone.
// CreateDomain adds a validated exact or wildcard hostname to a Zone.
func CreateDomain(ctx context.Context, zoneID uint, input DomainInput) (*model.ZoneDomain, error) {
zone, err := repository.GetZoneByID(ctx, zoneID)
if err != nil {
+90 -4
View File
@@ -5,6 +5,7 @@ package zone
import (
"context"
"strings"
"testing"
"time"
@@ -28,12 +29,97 @@ func setupZoneDB(t *testing.T) context.Context {
return context.Background()
}
func TestCreateZoneDomainRejectsWildcard(t *testing.T) {
func TestNormalizeDomainWildcard(t *testing.T) {
tests := []struct {
input string
want string
}{
{input: " *.Example.COM ", want: "*.example.com"},
{input: "*.s3.example.com", want: "*.s3.example.com"},
{input: "*.example.co.uk", want: "*.example.co.uk"},
{input: "API.Example.com", want: "api.example.com"},
{input: "example.com", want: "example.com"},
{input: "*"}, {input: "*.com"}, {input: "*.co.uk"},
{input: "foo*.example.com"}, {input: "a.*.example.com"},
{input: "*.*.example.com"}, {input: "**.example.com"},
{input: "*.example.com."}, {input: "*..example.com"},
{input: "*.-s3.example.com"}, {input: "*.s3-.example.com"},
{input: "*.s_3.example.com"}, {input: "*.example.com:443"},
{input: "*.example.com;"}, {input: "*.a b.example.com"},
{input: "*.a\nb.example.com"}, {input: "https://*.example.com"},
{input: "*.example.com/path"}, {input: "*.example.com?query"},
{input: "*.example.com#fragment"}, {input: "*@example.com"},
{input: "*." + strings.Repeat("a", 64) + ".example.com"},
{input: "*." + strings.Repeat(strings.Repeat("a", 63)+".", 4) + "com"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got, err := normalizeDomain(tt.input)
if got != tt.want || (err != nil) != (tt.want == "") {
t.Errorf("normalizeDomain(%q) = (%q, %v), want %q with error=%t", tt.input, got, err, tt.want, tt.want == "")
}
})
}
}
func TestCreateAndUpdateWildcardDomain(t *testing.T) {
ctx := setupZoneDB(t)
zone, err := Create(ctx, Input{Domain: "example.com"})
require.NoError(t, err)
_, err = CreateDomain(ctx, zone.ID, DomainInput{Domain: "*.example.com"})
require.EqualError(t, err, errDomainWildcardUnsupported)
if err != nil {
t.Fatal(err)
}
item, err := CreateDomain(ctx, zone.ID, DomainInput{Domain: " *.Example.COM "})
if err != nil {
t.Fatalf("CreateDomain(wildcard) error = %v", err)
}
if item.Domain != "*.example.com" {
t.Errorf("CreateDomain(wildcard).Domain = %q, want *.example.com", item.Domain)
}
if _, err := CreateDomain(ctx, zone.ID, DomainInput{Domain: "*.example.com"}); err == nil {
t.Error("CreateDomain(duplicate wildcard) error = nil, want duplicate error")
}
// 精确域名可以和通配符共存,路由匹配优先级交给 OpenResty。
if _, err := CreateDomain(ctx, zone.ID, DomainInput{Domain: "api.example.com"}); err != nil {
t.Fatalf("CreateDomain(exact alongside wildcard) error = %v", err)
}
for _, domain := range []string{"*.s3.example.com", "www.example.com", "*.example.com"} {
updated, err := UpdateDomain(ctx, zone.ID, item.ID, DomainInput{Domain: domain})
if err != nil {
t.Fatalf("UpdateDomain(%q) error = %v", domain, err)
}
if updated.Domain != domain {
t.Errorf("UpdateDomain(%q).Domain = %q", domain, updated.Domain)
}
}
for _, domain := range []string{"*.other.com", "*.example.com.evil.net", "a.*.example.com"} {
if _, err := CreateDomain(ctx, zone.ID, DomainInput{Domain: domain}); err == nil {
t.Errorf("CreateDomain(%q) error = nil, want invalid/outside-zone error", domain)
}
if _, err := UpdateDomain(ctx, zone.ID, item.ID, DomainInput{Domain: domain}); err == nil {
t.Errorf("UpdateDomain(%q) error = nil, want invalid/outside-zone error", domain)
}
}
stored, err := repository.GetZoneDomainByZoneAndID(ctx, zone.ID, item.ID)
if err != nil || stored.Domain != "*.example.com" {
t.Fatalf("stored domain after rejected updates = %+v, error=%v, want *.example.com", stored, err)
}
}
func TestZoneRootRejectsWildcard(t *testing.T) {
ctx := setupZoneDB(t)
if _, err := Create(ctx, Input{Domain: "*.example.com"}); err == nil {
t.Error("Create(wildcard zone) error = nil, want invalid root error")
}
zone, err := Create(ctx, Input{Domain: "example.co.uk"})
if err != nil {
t.Fatal(err)
}
if _, err := Update(ctx, zone.ID, Input{Domain: "*.example.co.uk"}); err == nil {
t.Error("Update(wildcard zone) error = nil, want invalid root error")
}
if _, err := CreateDomain(ctx, zone.ID, DomainInput{Domain: "*.s3.example.co.uk"}); err != nil {
t.Errorf("CreateDomain(wildcard under multi-label public suffix) error = %v", err)
}
}
func TestDeleteDomainRejectsBoundRoute(t *testing.T) {
+3 -1
View File
@@ -161,8 +161,9 @@ func DeleteHandler(c *gin.Context) {
c.JSON(http.StatusOK, response.OKNil())
}
// CreateDomainHandler creates an explicit FQDN under a Zone.
// CreateDomainHandler creates an exact or wildcard FQDN under a Zone.
// @Summary 创建 Zone 域名
// @Description 支持具体域名或最左侧完整标签为 * 的通配符域名,例如 *.s3.example.com;域名必须属于当前 Zone。
// @Tags openflare-zone
// @Accept json
// @Produce json
@@ -192,6 +193,7 @@ func CreateDomainHandler(c *gin.Context) {
// UpdateDomainHandler updates a Zone domain.
// @Summary 更新 Zone 域名
// @Description 支持具体域名与通配符域名互相修改;通配符仅允许出现在最左侧完整标签,且域名必须属于当前 Zone。
// @Tags openflare-zone
// @Accept json
// @Produce json
@@ -0,0 +1,105 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package zone
import (
"testing"
"time"
"github.com/Rain-kl/Wavelet/internal/model"
"github.com/Rain-kl/Wavelet/internal/repository"
"github.com/Rain-kl/Wavelet/internal/testhelper"
"github.com/google/go-cmp/cmp"
)
func TestGetStatsWildcardHosts(t *testing.T) {
type totals struct {
Requests int64
Visitors int64
Bytes int64
}
tests := []struct {
name string
domains []string
want totals
buckets map[int]totals
}{
{
name: "wildcard_matches_multiple_levels_without_root",
domains: []string{"*.example.com"}, want: totals{Requests: 5, Visitors: 3, Bytes: 150},
buckets: map[int]totals{0: {Requests: 2, Visitors: 1, Bytes: 30}, 1: {Requests: 3, Visitors: 2, Bytes: 120}},
},
{
name: "nested_wildcard_excludes_its_suffix_root",
domains: []string{"*.s3.example.com"}, want: totals{Requests: 2, Visitors: 1, Bytes: 70},
buckets: map[int]totals{1: {Requests: 2, Visitors: 1, Bytes: 70}},
},
{
name: "overlapping_exact_and_wildcards_are_not_summed_twice",
domains: []string{"api.example.com", "*.example.com", "*.s3.example.com"}, want: totals{Requests: 5, Visitors: 3, Bytes: 150},
buckets: map[int]totals{0: {Requests: 2, Visitors: 1, Bytes: 30}, 1: {Requests: 3, Visitors: 2, Bytes: 120}},
},
{
name: "exact_root_can_be_included_explicitly",
domains: []string{"example.com", "*.s3.example.com"}, want: totals{Requests: 3, Visitors: 2, Bytes: 130},
buckets: map[int]totals{0: {Requests: 1, Visitors: 1, Bytes: 60}, 1: {Requests: 2, Visitors: 1, Bytes: 70}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := setupZoneDB(t)
testhelper.SetupLogStoresForTest(t)
site, err := Create(ctx, Input{Domain: "example.com"})
if err != nil {
t.Fatalf("Create(example.com) error = %v, want nil", err)
}
// 直接登记域名,隔离流量统计与业务层域名校验的回归范围。
for _, domain := range tt.domains {
if err := repository.CreateZoneDomain(ctx, &model.ZoneDomain{ZoneID: site.ID, Domain: domain}); err != nil {
t.Fatalf("CreateZoneDomain(%q) error = %v, want nil", domain, err)
}
}
base := time.Now().UTC().Truncate(time.Hour).Add(-3 * time.Hour)
rows := []*model.OpenFlareAccessLog{
{Host: "a..example.com", LoggedAt: base.Add(time.Minute), RemoteAddr: "5.5.5.5", BytesSent: 1000},
{Host: ".a.example.com", LoggedAt: base.Add(time.Minute), RemoteAddr: "5.5.5.5", BytesSent: 1000},
{Host: "api.example.com", LoggedAt: base.Add(time.Minute), RemoteAddr: "1.1.1.1", BytesSent: 10},
{Host: " Deep.API.Example.COM ", LoggedAt: base.Add(2 * time.Minute), RemoteAddr: "1.1.1.1", BytesSent: 20},
{Host: "bucket.s3.example.com", LoggedAt: base.Add(time.Hour + time.Minute), RemoteAddr: "2.2.2.2", BytesSent: 30},
{Host: "deep.bucket.s3.example.com", LoggedAt: base.Add(time.Hour + 2*time.Minute), BytesSent: 40},
{Host: "s3.example.com", LoggedAt: base.Add(time.Hour + 3*time.Minute), RemoteAddr: "3.3.3.3", BytesSent: 50},
{Host: "example.com", LoggedAt: base.Add(3 * time.Minute), RemoteAddr: "1.1.1.1", BytesSent: 60},
{Host: "evil-example.com", LoggedAt: base.Add(time.Minute), RemoteAddr: "9.9.9.9", BytesSent: 70},
{Host: "bucket.s3.example.com.evil", LoggedAt: base.Add(time.Minute), RemoteAddr: "9.9.9.9", BytesSent: 80},
{Host: "bucket.s3.example.com", LoggedAt: base.Add(-48 * time.Hour), RemoteAddr: "9.9.9.9", BytesSent: 90},
}
if err := repository.InsertOpenFlareAccessLogsBatch(ctx, rows); err != nil {
t.Fatalf("InsertOpenFlareAccessLogsBatch(wildcard fixtures) error = %v, want nil", err)
}
stats, err := GetStats(ctx, site.ID, "24h")
if err != nil {
t.Fatalf("GetStats(%d, 24h) error = %v, want nil", site.ID, err)
}
if got := (totals{Requests: stats.RequestCount, Visitors: stats.UniqueVisitors, Bytes: stats.BytesSent}); got != tt.want {
t.Errorf("GetStats(domains=%v) totals = %+v, want %+v", tt.domains, got, tt.want)
}
if !stats.Available || stats.DomainCount != len(tt.domains) {
t.Errorf("GetStats(domains=%v) available/domain_count = %t/%d, want true/%d", tt.domains, stats.Available, stats.DomainCount, len(tt.domains))
}
wantBuckets := make(map[int64]totals)
for offset, total := range tt.buckets {
wantBuckets[base.Add(time.Duration(offset)*time.Hour).Unix()] = total
}
gotBuckets := make(map[int64]totals)
for _, point := range stats.Series {
if point.RequestCount != 0 || point.UniqueVisitors != 0 || point.BytesSent != 0 {
gotBuckets[point.BucketStartedAt.Unix()] = totals{Requests: point.RequestCount, Visitors: point.UniqueVisitors, Bytes: point.BytesSent}
}
}
if diff := cmp.Diff(wantBuckets, gotBuckets); diff != "" {
t.Errorf("GetStats(domains=%v) nonempty series mismatch (-want +got):\n%s", tt.domains, diff)
}
})
}
}