grafana/pkg/services/ngalert/provisioning/provenance_test.go
Yuri Tseretyan a63ef42816
Alerting: Mute Timing service to prevent changing provenance status to none (#88462)
* use relaxed validation to not introduce breaking changes for now but to be able to use the service
in non-provisioning APIs.
2024-06-04 08:54:33 -04:00

52 lines
1.4 KiB
Go

package provisioning
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/util"
)
func TestValidateProvenanceRelaxed(t *testing.T) {
all := []models.Provenance{
models.ProvenanceNone,
models.ProvenanceAPI,
models.ProvenanceFile,
models.Provenance(fmt.Sprintf("random-%s", util.GenerateShortUID())),
}
t.Run("all transitions from 'none' are allowed", func(t *testing.T) {
for _, provenance := range all {
assert.NoError(t, ValidateProvenanceRelaxed(models.ProvenanceNone, provenance))
}
})
t.Run("noop transitions are allowed", func(t *testing.T) {
for _, provenance := range all {
assert.NoError(t, ValidateProvenanceRelaxed(provenance, provenance))
}
})
t.Run("no transitions to 'none' are allowed", func(t *testing.T) {
for _, from := range all {
if from == models.ProvenanceNone {
continue
}
assert.ErrorIsf(t, ErrProvenanceChangeNotAllowed.Base, ValidateProvenanceRelaxed(from, models.ProvenanceNone), "transition %s -> 'none' is allowed but should not", from)
}
})
t.Run("transitions between others are are allowed", func(t *testing.T) {
for _, from := range all {
if from == models.ProvenanceNone {
continue
}
for _, to := range all {
if to == models.ProvenanceNone || from == to {
continue
}
assert.NoError(t, ValidateProvenanceRelaxed(from, to))
}
}
})
}