package metrics_test import ( "net/http" "net/http/httptest" "strings" "testing" "time" "sneak.berlin/go/smallwebwaf/internal/alerts" "sneak.berlin/go/smallwebwaf/internal/bans" "sneak.berlin/go/smallwebwaf/internal/metrics" ) const ( listURL = "https://lists.example/block.txt" zone = "dnsbl.example" destination = "webhook" ) // Each stand-in answers with a number of its own, so that each metric // shows which part it was read from. type ledgerStandIn struct{} func (ledgerStandIn) Made(bans.Cause) int { return 1 } func (ledgerStandIn) Count(time.Time) (int, int) { return 2, 1 } type clientTableStandIn struct{} func (clientTableStandIn) Len() int { return 3 } type ruleFilesStandIn struct{} func (ruleFilesStandIn) Len() int { return 4 } type listsStandIn struct{} func (listsStandIn) URLs() []string { return []string{listURL} } func (listsStandIn) Failures(string) int { return 5 } func (listsStandIn) Fetched(string) time.Time { return time.Time{} } type dnsblStandIn struct{} func (dnsblStandIn) Zones() []string { return []string{zone} } func (dnsblStandIn) Queries(string) int { return 6 } func (dnsblStandIn) Failures(string) int { return 0 } type abuseIPDBStandIn struct{} func (abuseIPDBStandIn) Checked() int { return 0 } func (abuseIPDBStandIn) Failures() int { return 0 } func (abuseIPDBStandIn) BudgetLeft() int { return 7 } type alertQueueStandIn struct{} func (alertQueueStandIn) DestinationsSet() []alerts.DestinationName { return []alerts.DestinationName{destination} } func (alertQueueStandIn) Counts(alerts.DestinationName) alerts.Counts { return alerts.Counts{Sent: 8} } func (alertQueueStandIn) Suppressed() int64 { return 0 } type lookupDatabaseStandIn struct{} func (lookupDatabaseStandIn) LastRead() time.Time { return time.Time{} } func (lookupDatabaseStandIn) ReadFailures() int { return 9 } type remoteLogStandIn struct{} func (remoteLogStandIn) Sent() int64 { return 10 } func (remoteLogStandIn) Dropped() int64 { return 0 } func (remoteLogStandIn) Depth() int { return 0 } func TestMetricsAreReadFromEachPart(t *testing.T) { t.Parallel() m := metrics.New(metrics.Params{ BusiestCount: 1, InstanceName: "app", Now: func() time.Time { return time.Time{} }, Bans: ledgerStandIn{}, Clients: clientTableStandIn{}, Rules: ruleFilesStandIn{}, Lists: listsStandIn{}, DNSBL: dnsblStandIn{}, AbuseIPDB: abuseIPDBStandIn{}, Alerts: alertQueueStandIn{}, LookupFile: lookupDatabaseStandIn{}, RemoteLog: remoteLogStandIn{}, }) scraped := scrape(t, m) for _, line := range []string{ `sww_bans_made_total{cause="limit",instance="app"} 1`, `sww_active_bans{instance="app"} 2`, `sww_tracked_clients{instance="app"} 3`, `sww_rules_loaded{instance="app"} 4`, `sww_reputation_failures_total{instance="app",source="` + listURL + `"} 5`, `sww_reputation_queries_total{instance="app",source="` + zone + `"} 6`, `sww_reputation_daily_budget_remaining{instance="app",source="abuseipdb"} 7`, `sww_alerts_sent_total{destination="` + destination + `",instance="app"} 8`, `sww_lookup_database_read_failures_total{instance="app"} 9`, `sww_remote_log_lines_sent_total{instance="app"} 10`, } { if !strings.Contains(scraped, "\n"+line+"\n") { t.Errorf("metrics\n%s\nwant\n%s", scraped, line) } } } func TestPartThatIsOffAddsNoMetric(t *testing.T) { t.Parallel() scraped := scrape(t, metrics.New(metrics.Params{ BusiestCount: 1, InstanceName: "app", })) for _, name := range []string{ "sww_bans_made_total", "sww_tracked_clients", "sww_rules_loaded", "sww_reputation_", "sww_alert", "sww_lookup_database_", "sww_remote_log_", } { if strings.Contains(scraped, name) { t.Errorf("metrics\n%s\nshow %s", scraped, name) } } } // scrape returns the metrics m serves. func scrape(t *testing.T, m *metrics.Metrics) string { t.Helper() scraped := httptest.NewRecorder() m.ServeHTTP( scraped, httptest.NewRequestWithContext( t.Context(), http.MethodGet, "/", http.NoBody, ), ) return scraped.Body.String() }