Alerting: Update provisioning to validate user-defined UID on create (#73793)

* add ValidateUID to util
* provisioning to validate UID on rule creation

---------

Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>
Co-authored-by: Alexander Weaver <weaver.alex.d@gmail.com>
This commit is contained in:
Yuri Tseretyan
2023-09-08 15:09:35 -04:00
committed by GitHub
parent 9c50296a07
commit 99fd7b8141
14 changed files with 161 additions and 24 deletions

View File

@@ -4,6 +4,7 @@ import (
"sync"
"testing"
"cuelang.org/go/pkg/strings"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/util/validation"
@@ -84,3 +85,47 @@ func TestIsShortUIDTooLong(t *testing.T) {
})
}
}
func TestValidateUID(t *testing.T) {
var tests = []struct {
name string
uid string
expected error
}{
{
name: "no error when string is of correct length",
uid: "f8cc010c-ee72-4681-89d2-d46e1bd47d33",
expected: nil,
},
{
name: "error when string is empty",
uid: "",
expected: ErrUIDEmpty,
},
{
name: "error when string is too long",
uid: strings.Repeat("1", MaxUIDLength+1),
expected: ErrUIDTooLong,
},
{
name: "error when string has invalid characters",
uid: "f8cc010c.ee72.4681;89d2+d46e1bd47d33",
expected: ErrUIDFormatInvalid,
},
{
name: "error when string has only whitespaces",
uid: " ",
expected: ErrUIDFormatInvalid,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateUID(tt.uid)
if tt.expected == nil {
require.NoError(t, err)
} else {
require.ErrorIs(t, err, tt.expected)
}
})
}
}