Files
Charon/backend/internal/metrics/metrics_test.go
T
GitHub Actions 25082778c9 feat(cerberus): integrate Cerberus security features (WAF, ACLs, rate limiting, CrowdSec)
- Implement GeoIPService for IP-to-country lookups with comprehensive error handling.
- Add tests for GeoIPService covering various scenarios including invalid IPs and database loading.
- Extend AccessListService to handle GeoIP service integration, including graceful degradation when GeoIP service is unavailable.
- Introduce new tests for AccessListService to validate geo ACL behavior and country code parsing.
- Update SecurityService to include new fields for WAF configuration and enhance decision logging functionality.
- Add extensive tests for SecurityService covering rule set management and decision logging.
- Create a detailed Security Coverage QA Plan to ensure 100% code coverage for security-related functionality.
2025-12-12 17:56:30 +00:00

86 lines
1.8 KiB
Go

package metrics
import (
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
)
func TestMetrics_Register(t *testing.T) {
// Create a new registry for testing
reg := prometheus.NewRegistry()
// Register metrics - should not panic
assert.NotPanics(t, func() {
Register(reg)
})
// Increment each metric at least once so they appear in Gather()
IncWAFRequest()
IncWAFBlocked()
IncWAFMonitored()
IncCrowdSecRequest()
IncCrowdSecBlocked()
// Verify metrics are registered by gathering them
metrics, err := reg.Gather()
assert.NoError(t, err)
assert.GreaterOrEqual(t, len(metrics), 5)
// Check that our WAF and CrowdSec metrics exist
expectedMetrics := map[string]bool{
"charon_waf_requests_total": false,
"charon_waf_blocked_total": false,
"charon_waf_monitored_total": false,
"charon_crowdsec_requests_total": false,
"charon_crowdsec_blocked_total": false,
}
for _, m := range metrics {
name := m.GetName()
if _, ok := expectedMetrics[name]; ok {
expectedMetrics[name] = true
}
}
for name, found := range expectedMetrics {
assert.True(t, found, "Metric %s should be registered", name)
}
}
func TestMetrics_Increment(t *testing.T) {
// Test that increment functions don't panic
assert.NotPanics(t, func() {
IncWAFRequest()
})
assert.NotPanics(t, func() {
IncWAFBlocked()
})
assert.NotPanics(t, func() {
IncWAFMonitored()
})
assert.NotPanics(t, func() {
IncCrowdSecRequest()
})
assert.NotPanics(t, func() {
IncCrowdSecBlocked()
})
// Multiple increments should also not panic
assert.NotPanics(t, func() {
IncWAFRequest()
IncWAFRequest()
IncWAFBlocked()
IncWAFMonitored()
IncWAFMonitored()
IncWAFMonitored()
IncCrowdSecRequest()
IncCrowdSecBlocked()
})
}