fix(alertmanager): fix tests for alertmanager (#7225)

This commit is contained in:
Vibhu Pandey 2025-03-05 21:24:54 +05:30 committed by GitHub
parent b1e3f03bb5
commit 4177b88a4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,12 +2,14 @@ package alertmanagertypes
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/url" "net/url"
"testing" "testing"
"github.com/prometheus/alertmanager/config" "github.com/prometheus/alertmanager/config"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
) )
func TestCreateRuleIDMatcher(t *testing.T) { func TestCreateRuleIDMatcher(t *testing.T) {
@ -111,12 +113,15 @@ func TestCreateRuleIDMatcher(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
routes, err := json.Marshal(cfg.alertmanagerConfig.Route.Routes) actualRoutes, err := json.Marshal(cfg.alertmanagerConfig.Route.Routes)
require.NoError(t, err) require.NoError(t, err)
var actualRoutes []map[string]any expectedRoutes, err := json.Marshal(tc.expectedRoutes)
err = json.Unmarshal(routes, &actualRoutes)
require.NoError(t, err) require.NoError(t, err)
assert.ElementsMatch(t, tc.expectedRoutes, actualRoutes)
for i := range len(tc.expectedRoutes) {
assert.Equal(t, gjson.GetBytes(expectedRoutes, fmt.Sprintf("$[%d].receiver", i)).String(), gjson.GetBytes(actualRoutes, fmt.Sprintf("$[%d].receiver", i)).String())
assert.ElementsMatch(t, gjson.GetBytes(expectedRoutes, fmt.Sprintf("$[%d].matchers", i)).Array(), gjson.GetBytes(actualRoutes, fmt.Sprintf("$[%d].matchers", i)).Array())
}
}) })
} }
} }