2025-09-26 18:54:58 +05:30
|
|
|
package nfmanagertest
|
|
|
|
|
|
|
|
|
|
import (
|
2025-10-03 19:47:15 +05:30
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2025-09-26 18:54:58 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
|
2025-10-03 19:47:15 +05:30
|
|
|
"github.com/prometheus/common/model"
|
2025-09-26 18:54:58 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MockNotificationManager is a simple mock implementation of NotificationManager
|
|
|
|
|
type MockNotificationManager struct {
|
2025-10-03 19:47:15 +05:30
|
|
|
configs map[string]*alertmanagertypes.NotificationConfig
|
|
|
|
|
routes map[string]*alertmanagertypes.RoutePolicy
|
|
|
|
|
routesByName map[string][]*alertmanagertypes.RoutePolicy
|
|
|
|
|
errors map[string]error
|
2025-09-26 18:54:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewMock creates a new mock notification manager
|
|
|
|
|
func NewMock() *MockNotificationManager {
|
|
|
|
|
return &MockNotificationManager{
|
2025-10-03 19:47:15 +05:30
|
|
|
configs: make(map[string]*alertmanagertypes.NotificationConfig),
|
|
|
|
|
routes: make(map[string]*alertmanagertypes.RoutePolicy),
|
|
|
|
|
routesByName: make(map[string][]*alertmanagertypes.RoutePolicy),
|
|
|
|
|
errors: make(map[string]error),
|
2025-09-26 18:54:58 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getKey(orgId string, ruleId string) string {
|
|
|
|
|
return orgId + ":" + ruleId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) GetNotificationConfig(orgID string, ruleID string) (*alertmanagertypes.NotificationConfig, error) {
|
|
|
|
|
key := getKey(orgID, ruleID)
|
|
|
|
|
if err := m.errors[key]; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if config := m.configs[key]; config != nil {
|
|
|
|
|
return config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notificationConfig := alertmanagertypes.GetDefaultNotificationConfig()
|
|
|
|
|
return ¬ificationConfig, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) SetNotificationConfig(orgID string, ruleID string, config *alertmanagertypes.NotificationConfig) error {
|
|
|
|
|
key := getKey(orgID, ruleID)
|
|
|
|
|
if err := m.errors[key]; err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
m.configs[key] = config
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) DeleteNotificationConfig(orgID string, ruleID string) error {
|
|
|
|
|
key := getKey(orgID, ruleID)
|
|
|
|
|
if err := m.errors[key]; err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
delete(m.configs, key)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) SetMockConfig(orgID, ruleID string, config *alertmanagertypes.NotificationConfig) {
|
|
|
|
|
key := getKey(orgID, ruleID)
|
|
|
|
|
m.configs[key] = config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) SetMockError(orgID, ruleID string, err error) {
|
|
|
|
|
key := getKey(orgID, ruleID)
|
|
|
|
|
m.errors[key] = err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) ClearMockData() {
|
|
|
|
|
m.configs = make(map[string]*alertmanagertypes.NotificationConfig)
|
2025-10-03 19:47:15 +05:30
|
|
|
m.routes = make(map[string]*alertmanagertypes.RoutePolicy)
|
|
|
|
|
m.routesByName = make(map[string][]*alertmanagertypes.RoutePolicy)
|
2025-09-26 18:54:58 +05:30
|
|
|
m.errors = make(map[string]error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) HasConfig(orgID, ruleID string) bool {
|
|
|
|
|
key := getKey(orgID, ruleID)
|
|
|
|
|
_, exists := m.configs[key]
|
|
|
|
|
return exists
|
|
|
|
|
}
|
2025-10-03 19:47:15 +05:30
|
|
|
|
|
|
|
|
// Route Policy CRUD
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) CreateRoutePolicy(ctx context.Context, orgID string, route *alertmanagertypes.RoutePolicy) error {
|
|
|
|
|
key := getKey(orgID, "create_route")
|
|
|
|
|
if err := m.errors[key]; err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if route == nil {
|
|
|
|
|
return fmt.Errorf("route cannot be nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := route.Validate(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
routeKey := getKey(orgID, route.ID.StringValue())
|
|
|
|
|
m.routes[routeKey] = route
|
|
|
|
|
nameKey := getKey(orgID, route.Name)
|
|
|
|
|
m.routesByName[nameKey] = append(m.routesByName[nameKey], route)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) CreateRoutePolicies(ctx context.Context, orgID string, routes []*alertmanagertypes.RoutePolicy) error {
|
|
|
|
|
key := getKey(orgID, "create_routes")
|
|
|
|
|
if err := m.errors[key]; err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(routes) == 0 {
|
|
|
|
|
return fmt.Errorf("routes cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
for i, route := range routes {
|
|
|
|
|
if route == nil {
|
|
|
|
|
return fmt.Errorf("route at index %d cannot be nil", i)
|
|
|
|
|
}
|
|
|
|
|
if err := route.Validate(); err != nil {
|
|
|
|
|
return fmt.Errorf("route at index %d: %s", i, err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, route := range routes {
|
|
|
|
|
if err := m.CreateRoutePolicy(ctx, orgID, route); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) GetRoutePolicyByID(ctx context.Context, orgID string, routeID string) (*alertmanagertypes.RoutePolicy, error) {
|
|
|
|
|
key := getKey(orgID, "get_route")
|
|
|
|
|
if err := m.errors[key]; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if routeID == "" {
|
|
|
|
|
return nil, fmt.Errorf("routeID cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
routeKey := getKey(orgID, routeID)
|
|
|
|
|
route, exists := m.routes[routeKey]
|
|
|
|
|
if !exists {
|
|
|
|
|
return nil, fmt.Errorf("route with ID %s not found", routeID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return route, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) GetAllRoutePolicies(ctx context.Context, orgID string) ([]*alertmanagertypes.RoutePolicy, error) {
|
|
|
|
|
key := getKey(orgID, "get_all_routes")
|
|
|
|
|
if err := m.errors[key]; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if orgID == "" {
|
|
|
|
|
return nil, fmt.Errorf("orgID cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var routes []*alertmanagertypes.RoutePolicy
|
|
|
|
|
for routeKey, route := range m.routes {
|
|
|
|
|
if route.OrgID == orgID {
|
|
|
|
|
routes = append(routes, route)
|
|
|
|
|
}
|
|
|
|
|
_ = routeKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) DeleteRoutePolicy(ctx context.Context, orgID string, routeID string) error {
|
|
|
|
|
key := getKey(orgID, "delete_route")
|
|
|
|
|
if err := m.errors[key]; err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if routeID == "" {
|
|
|
|
|
return fmt.Errorf("routeID cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
routeKey := getKey(orgID, routeID)
|
|
|
|
|
route, exists := m.routes[routeKey]
|
|
|
|
|
if !exists {
|
|
|
|
|
return fmt.Errorf("route with ID %s not found", routeID)
|
|
|
|
|
}
|
|
|
|
|
delete(m.routes, routeKey)
|
|
|
|
|
|
|
|
|
|
nameKey := getKey(orgID, route.Name)
|
|
|
|
|
if nameRoutes, exists := m.routesByName[nameKey]; exists {
|
|
|
|
|
var filtered []*alertmanagertypes.RoutePolicy
|
|
|
|
|
for _, r := range nameRoutes {
|
|
|
|
|
if r.ID.StringValue() != routeID {
|
|
|
|
|
filtered = append(filtered, r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(filtered) == 0 {
|
|
|
|
|
delete(m.routesByName, nameKey)
|
|
|
|
|
} else {
|
|
|
|
|
m.routesByName[nameKey] = filtered
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) DeleteAllRoutePoliciesByName(ctx context.Context, orgID string, name string) error {
|
|
|
|
|
key := getKey(orgID, "delete_routes_by_name")
|
|
|
|
|
if err := m.errors[key]; err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if orgID == "" {
|
|
|
|
|
return fmt.Errorf("orgID cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
|
return fmt.Errorf("name cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nameKey := getKey(orgID, name)
|
|
|
|
|
routes, exists := m.routesByName[nameKey]
|
|
|
|
|
if !exists {
|
|
|
|
|
return nil // No routes to delete
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, route := range routes {
|
|
|
|
|
routeKey := getKey(orgID, route.ID.StringValue())
|
|
|
|
|
delete(m.routes, routeKey)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete(m.routesByName, nameKey)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) Match(ctx context.Context, orgID string, ruleID string, set model.LabelSet) ([]string, error) {
|
|
|
|
|
key := getKey(orgID, ruleID)
|
|
|
|
|
if err := m.errors[key]; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config, err := m.GetNotificationConfig(orgID, ruleID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var expressionRoutes []*alertmanagertypes.RoutePolicy
|
|
|
|
|
if config.UsePolicy {
|
|
|
|
|
for _, route := range m.routes {
|
|
|
|
|
if route.OrgID == orgID && route.ExpressionKind == alertmanagertypes.PolicyBasedExpression {
|
|
|
|
|
expressionRoutes = append(expressionRoutes, route)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
nameKey := getKey(orgID, ruleID)
|
|
|
|
|
if routes, exists := m.routesByName[nameKey]; exists {
|
|
|
|
|
expressionRoutes = routes
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var matchedChannels []string
|
|
|
|
|
for _, route := range expressionRoutes {
|
|
|
|
|
if m.evaluateExpr(route.Expression, set) {
|
|
|
|
|
matchedChannels = append(matchedChannels, route.Channels...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return matchedChannels, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) evaluateExpr(expression string, labelSet model.LabelSet) bool {
|
|
|
|
|
ruleID, ok := labelSet["ruleId"]
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(expression, `ruleId in ["ruleId-OtherAlert", "ruleId-TestingAlert"]`) {
|
|
|
|
|
return ruleID == "ruleId-OtherAlert" || ruleID == "ruleId-TestingAlert"
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(expression, `ruleId in ["ruleId-HighLatency", "ruleId-HighErrorRate"]`) {
|
|
|
|
|
return ruleID == "ruleId-HighLatency" || ruleID == "ruleId-HighErrorRate"
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(expression, `ruleId == "ruleId-HighLatency"`) {
|
|
|
|
|
return ruleID == "ruleId-HighLatency"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper methods for testing
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) SetMockRoute(orgID string, route *alertmanagertypes.RoutePolicy) {
|
|
|
|
|
routeKey := getKey(orgID, route.ID.StringValue())
|
|
|
|
|
m.routes[routeKey] = route
|
|
|
|
|
|
|
|
|
|
nameKey := getKey(orgID, route.Name)
|
|
|
|
|
m.routesByName[nameKey] = append(m.routesByName[nameKey], route)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) SetMockRouteError(orgID, operation string, err error) {
|
|
|
|
|
key := getKey(orgID, operation)
|
|
|
|
|
m.errors[key] = err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) ClearMockRoutes() {
|
|
|
|
|
m.routes = make(map[string]*alertmanagertypes.RoutePolicy)
|
|
|
|
|
m.routesByName = make(map[string][]*alertmanagertypes.RoutePolicy)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) GetRouteCount() int {
|
|
|
|
|
return len(m.routes)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockNotificationManager) HasRoute(orgID, routeID string) bool {
|
|
|
|
|
routeKey := getKey(orgID, routeID)
|
|
|
|
|
_, exists := m.routes[routeKey]
|
|
|
|
|
return exists
|
|
|
|
|
}
|