quality / commitlint (push) Skipped
quality / changes (push) Successful in 9s
quality / docker-check (push) Skipped
quality / openapi (push) Successful in 46s
quality / web (push) Successful in 1m16s
quality / go (push) Successful in 2m42s
quality / bird2 (push) Successful in 16s
CD / quality (push) Successful in 5m19s
CD / publish (push) Successful in 7m19s
- Deleted unused components: `DashboardActivityTimeline`, `DashboardFramePanel`, `DashboardModulesGrid`, `DashboardRecentJobsGrid`, and `DashboardRecentRevisionsGrid` to streamline the dashboard. - Updated `DashboardKpiGrid` to improve KPI display logic, including progress indicators and enhanced badge functionality. - Refactored `DashboardNetworkHealth` to provide better status representation based on loading states and network conditions. - Introduced new properties for KPI cards to support progress tracking and improved visual feedback. This cleanup aims to enhance performance and maintainability of the dashboard while providing a better user experience.
259 lines
6.6 KiB
Go
259 lines
6.6 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"evobgp/internal/store"
|
|
)
|
|
|
|
func TestCollectModulePrefixRows_CDNSendsIfNoneMatch(t *testing.T) {
|
|
t.Setenv("EVOBGP_CDN_ALLOW_PRIVATE", "1")
|
|
m := store.NewMemory()
|
|
m.SeedDemo()
|
|
tenant, _, _, _, _ := m.DemoIDs()
|
|
|
|
mod, err := m.CreateModule(tenant, &store.Module{
|
|
Type: "CDN_CIDRS",
|
|
Name: "cdn-conditional",
|
|
Enabled: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var gotIfNoneMatch string
|
|
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotIfNoneMatch = strings.TrimSpace(r.Header.Get("If-None-Match"))
|
|
w.Header().Set("ETag", "etag-new")
|
|
_, _ = w.Write([]byte("198.51.100.0/24\n"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
etag := "etag-old"
|
|
if _, err := m.CreateCDNSource(tenant, mod.ID, &store.CDNSource{
|
|
SourceKind: "txt",
|
|
URL: srv.URL,
|
|
Etag: etag,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
collected, err := collectModulePrefixRows(context.Background(), m, srv.Client(), tenant, mod, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if gotIfNoneMatch != etag {
|
|
t.Fatalf("want If-None-Match %q, got %q", etag, gotIfNoneMatch)
|
|
}
|
|
if len(collected) != 1 || collected[0].Prefix != "198.51.100.0/24" {
|
|
t.Fatalf("unexpected collected rows: %+v", collected)
|
|
}
|
|
}
|
|
|
|
func TestCollectModulePrefixRows_CDN304UsesSnapshot(t *testing.T) {
|
|
t.Setenv("EVOBGP_CDN_ALLOW_PRIVATE", "1")
|
|
m := store.NewMemory()
|
|
m.SeedDemo()
|
|
tenant, _, _, _, _ := m.DemoIDs()
|
|
|
|
mod, err := m.CreateModule(tenant, &store.Module{
|
|
Type: "CDN_CIDRS",
|
|
Name: "cdn-304",
|
|
Enabled: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotModified)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
etag := "etag-stable"
|
|
src, err := m.CreateCDNSource(tenant, mod.ID, &store.CDNSource{
|
|
SourceKind: "txt",
|
|
URL: srv.URL,
|
|
Etag: etag,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prior := []store.PrefixRow{{
|
|
Prefix: "203.0.113.0/24",
|
|
Source: cdnSourceKey(src.ID),
|
|
}}
|
|
if err := mergeCDNSourceIntoModuleSnapshot(m, tenant, mod, src.ID, prior); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
collected, err := collectModulePrefixRows(context.Background(), m, srv.Client(), tenant, mod, prior)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(collected) != 1 || collected[0].Prefix != "203.0.113.0/24" {
|
|
t.Fatalf("want cached prefix on 304, got %+v", collected)
|
|
}
|
|
}
|
|
|
|
func TestRefreshModuleIngest_CDN304UsesStoredSnapshot(t *testing.T) {
|
|
t.Setenv("EVOBGP_CDN_ALLOW_PRIVATE", "1")
|
|
m := store.NewMemory()
|
|
m.SeedDemo()
|
|
tenant, _, _, _, _ := m.DemoIDs()
|
|
|
|
mod, err := m.CreateModule(tenant, &store.Module{
|
|
Type: "CDN_CIDRS",
|
|
Name: "cdn-304-ingest",
|
|
Enabled: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotModified)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
etag := "etag-stable"
|
|
src, err := m.CreateCDNSource(tenant, mod.ID, &store.CDNSource{
|
|
SourceKind: "txt",
|
|
URL: srv.URL,
|
|
Etag: etag,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prior := []store.PrefixRow{{
|
|
Prefix: "203.0.113.0/24",
|
|
Source: cdnSourceKey(src.ID),
|
|
}}
|
|
if err := mergeCDNSourceIntoModuleSnapshot(m, tenant, mod, src.ID, prior); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := RefreshModuleIngest(context.Background(), m, srv.Client(), tenant, mod.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestCollectModulePrefixRows_CDN304RetriesWithoutETag(t *testing.T) {
|
|
t.Setenv("EVOBGP_CDN_ALLOW_PRIVATE", "1")
|
|
m := store.NewMemory()
|
|
m.SeedDemo()
|
|
tenant, _, _, _, _ := m.DemoIDs()
|
|
|
|
mod, err := m.CreateModule(tenant, &store.Module{
|
|
Type: "CDN_CIDRS",
|
|
Name: "cdn-304-retry",
|
|
Enabled: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var calls int
|
|
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
calls++
|
|
if calls == 1 {
|
|
if got := strings.TrimSpace(r.Header.Get("If-None-Match")); got != "etag-stable" {
|
|
t.Fatalf("first request want If-None-Match etag-stable, got %q", got)
|
|
}
|
|
w.WriteHeader(http.StatusNotModified)
|
|
return
|
|
}
|
|
if got := strings.TrimSpace(r.Header.Get("If-None-Match")); got != "" {
|
|
t.Fatalf("retry must omit If-None-Match, got %q", got)
|
|
}
|
|
w.Header().Set("ETag", "etag-stable")
|
|
_, _ = w.Write([]byte("198.51.100.0/24\n"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
if _, err := m.CreateCDNSource(tenant, mod.ID, &store.CDNSource{
|
|
SourceKind: "txt",
|
|
URL: srv.URL,
|
|
Etag: "etag-stable",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
collected, err := collectModulePrefixRows(context.Background(), m, srv.Client(), tenant, mod, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if calls != 2 {
|
|
t.Fatalf("want 2 HTTP calls (304 then 200), got %d", calls)
|
|
}
|
|
if len(collected) != 1 || collected[0].Prefix != "198.51.100.0/24" {
|
|
t.Fatalf("unexpected collected rows: %+v", collected)
|
|
}
|
|
}
|
|
|
|
func TestPrefetchCDNSourceETags_SkipsFreshSources(t *testing.T) {
|
|
t.Setenv("EVOBGP_CDN_ALLOW_PRIVATE", "1")
|
|
m := store.NewMemory()
|
|
m.SeedDemo()
|
|
tenant, _, _, _, _ := m.DemoIDs()
|
|
|
|
mod, err := m.CreateModule(tenant, &store.Module{
|
|
Type: "CDN_CIDRS",
|
|
Name: "cdn-prefetch-due",
|
|
Enabled: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var hits atomic.Int32
|
|
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
hits.Add(1)
|
|
w.Header().Set("ETag", `"v1"`)
|
|
_, _ = w.Write([]byte("198.51.100.0/24\n"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
// Fresh source: refreshed 30s ago with a 3600s interval — prefetch must skip it.
|
|
freshAt := time.Now().UTC().Add(-30 * time.Second)
|
|
interval := 3600
|
|
if _, err := m.CreateCDNSource(tenant, mod.ID, &store.CDNSource{
|
|
SourceKind: "txt",
|
|
URL: srv.URL,
|
|
RefreshIntervalSec: &interval,
|
|
LastRefreshedAt: &freshAt,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := PrefetchCDNSourceETags(context.Background(), m, srv.Client()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hits.Load() != 0 {
|
|
t.Fatalf("fresh source must be skipped by prefetch, got %d HTTP hits", hits.Load())
|
|
}
|
|
|
|
// Stale source: last refresh older than its interval — prefetch must probe it.
|
|
staleAt := time.Now().UTC().Add(-7200 * time.Second)
|
|
if _, err := m.CreateCDNSource(tenant, mod.ID, &store.CDNSource{
|
|
SourceKind: "txt",
|
|
URL: srv.URL + "?stale",
|
|
RefreshIntervalSec: &interval,
|
|
LastRefreshedAt: &staleAt,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := PrefetchCDNSourceETags(context.Background(), m, srv.Client()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hits.Load() == 0 {
|
|
t.Fatal("stale source must be probed by prefetch")
|
|
}
|
|
}
|