test: add CDN prefetch tests for 304 responses and snapshot handling
CI / changes (push) Successful in 8s
CI / openapi (push) Has been skipped
CI / go (push) Successful in 29s
CI / docker-web (push) Has been skipped
CI / docker-bird (push) Has been skipped
CI / bird2 (push) Successful in 15s
CI / docker-go (push) Successful in 8m32s
CI / changes (push) Successful in 8s
CI / openapi (push) Has been skipped
CI / go (push) Successful in 29s
CI / docker-web (push) Has been skipped
CI / docker-bird (push) Has been skipped
CI / bird2 (push) Successful in 15s
CI / docker-go (push) Successful in 8m32s
- Introduced `TestRefreshModuleIngest_CDN304UsesStoredSnapshot` to verify that the module ingest correctly uses stored snapshots when receiving a 304 Not Modified response. - Added `TestCollectModulePrefixRows_CDN304RetriesWithoutETag` to ensure that the system retries without the If-None-Match header after a 304 response, and correctly collects prefixes on subsequent successful requests. - Updated `cachedCDNPrefixRows` function to improve handling of cached prefixes during CDN source processing. - Modified `RefreshModuleIngest` to utilize prior snapshots when collecting module prefix rows.
This commit is contained in:
@@ -97,3 +97,96 @@ func TestCollectModulePrefixRows_CDN304UsesSnapshot(t *testing.T) {
|
||||
t.Fatalf("want cached prefix on 304, got %+v", collected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshModuleIngest_CDN304UsesStoredSnapshot(t *testing.T) {
|
||||
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.NewServer(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) {
|
||||
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.NewServer(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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user