Alerting: Fix flaky test (#55551)

The length of the identifier from the underlying library is 9 or more characters depending on the rate at which the identifiers are generated. See https://pkg.go.dev/github.com/teris-io/shortid

The test previously made the assumption that the length will always be 10, which would intermittently fail.
This commit is contained in:
Emil Tullstedt 2022-09-21 14:17:25 +02:00 committed by GitHub
parent 7b4cea8151
commit 647997cc4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -110,8 +110,10 @@ func TestMakeAlertRule(t *testing.T) {
require.NoError(t, err)
require.Len(t, ar.Title, DefaultFieldMaxLength)
uniq := ar.Title[len(ar.Title)-11:]
require.Regexp(t, "^_.{10}$", uniq)
parts := strings.SplitN(ar.Title, "_", 2)
require.Len(t, parts, 2)
require.Greater(t, len(parts[1]), 8, "unique identifier should be longer than 9 characters")
require.Equal(t, DefaultFieldMaxLength-1, len(parts[0])+len(parts[1]), "truncated name + underscore + unique identifier should together be DefaultFieldMaxLength")
require.Equal(t, ar.Title, ar.RuleGroup)
})
})