When creating Grafana-managed alerts from Prometheus rule definitions with mimirtool or cortextool, the rules are marked as "provisioned" and are not editable in the Grafana UI. This PR allows changing this by providing an extra header: --extra-header="X-Disable-Provenance=true". When provenance is disabled, we do not keep the original rule definition in YAML, so it is impossible to read it back using the Prometheus conversion API (mimirtool/cortextool). This is intentional because if we did keep it and the rule was later changed in the UI, its Prometheus YAML definition would no longer reflect the latest version of the alert rule, as it would be unchanged.
112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
package validation
|
|
|
|
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.ProvenanceConvertedPrometheus,
|
|
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))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCanUpdateProvenanceInRuleGroup(t *testing.T) {
|
|
all := []models.Provenance{
|
|
models.ProvenanceNone,
|
|
models.ProvenanceAPI,
|
|
models.ProvenanceFile,
|
|
models.ProvenanceConvertedPrometheus,
|
|
models.Provenance(fmt.Sprintf("random-%s", util.GenerateShortUID())),
|
|
}
|
|
|
|
t.Run("same provenance transitions are allowed", func(t *testing.T) {
|
|
for _, provenance := range all {
|
|
assert.True(t, CanUpdateProvenanceInRuleGroup(provenance, provenance))
|
|
}
|
|
})
|
|
|
|
t.Run("all transitions from 'none' are allowed", func(t *testing.T) {
|
|
for _, provenance := range all {
|
|
assert.True(t, CanUpdateProvenanceInRuleGroup(models.ProvenanceNone, provenance))
|
|
}
|
|
})
|
|
|
|
t.Run("only specific provenances can transition to 'none'", func(t *testing.T) {
|
|
allowed := []models.Provenance{
|
|
models.ProvenanceAPI,
|
|
models.ProvenanceConvertedPrometheus,
|
|
}
|
|
|
|
for _, from := range allowed {
|
|
assert.True(t, CanUpdateProvenanceInRuleGroup(from, models.ProvenanceNone),
|
|
"transition %s -> 'none' should be allowed", from)
|
|
}
|
|
|
|
notAllowed := []models.Provenance{
|
|
models.ProvenanceFile,
|
|
models.Provenance(fmt.Sprintf("random-%s", util.GenerateShortUID())),
|
|
}
|
|
|
|
for _, from := range notAllowed {
|
|
assert.False(t, CanUpdateProvenanceInRuleGroup(from, models.ProvenanceNone),
|
|
"transition %s -> 'none' should not be allowed", from)
|
|
}
|
|
})
|
|
|
|
t.Run("transitions between different provenances are not allowed", func(t *testing.T) {
|
|
for _, from := range all {
|
|
if from == models.ProvenanceNone {
|
|
continue // always allowed
|
|
}
|
|
for _, to := range all {
|
|
if from == to || to == models.ProvenanceNone {
|
|
continue // always allowed
|
|
}
|
|
assert.False(t, CanUpdateProvenanceInRuleGroup(from, to),
|
|
"transition %s -> '%s' should not be allowed", from, to)
|
|
}
|
|
}
|
|
})
|
|
}
|