Alerting: Refactor of state manager tests (#72849)

* calculate cacheID instead of literals
   * use mocked clocks
   * advance clocks with the eval results
   * use clearer timestamp aliases
   * make expected state labels be more clear to read
Co-authored-by: Matthew Jacobson <matthew.jacobson@grafana.com>
This commit is contained in:
Yuri Tseretyan 2023-08-04 13:39:49 -04:00 committed by GitHub
parent 9328febbab
commit 0053b07885
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 787 additions and 1765 deletions

View File

@ -67,6 +67,13 @@ func WithState(state State) ResultMutator {
} }
} }
func WithError(err error) ResultMutator {
return func(r *Result) {
r.State = Error
r.Error = err
}
}
func WithLabels(labels data.Labels) ResultMutator { func WithLabels(labels data.Labels) ResultMutator {
return func(r *Result) { return func(r *Result) {
r.Instance = labels r.Instance = labels

View File

@ -179,6 +179,54 @@ func WithFor(duration time.Duration) AlertRuleMutator {
} }
} }
func WithForNTimes(timesOfInterval int64) AlertRuleMutator {
return func(rule *AlertRule) {
rule.For = time.Duration(rule.IntervalSeconds*timesOfInterval) * time.Second
}
}
func WithNoDataExecAs(nodata NoDataState) AlertRuleMutator {
return func(rule *AlertRule) {
rule.NoDataState = nodata
}
}
func WithErrorExecAs(err ExecutionErrorState) AlertRuleMutator {
return func(rule *AlertRule) {
rule.ExecErrState = err
}
}
func WithAnnotations(a data.Labels) AlertRuleMutator {
return func(rule *AlertRule) {
rule.Annotations = a
}
}
func WithAnnotation(key, value string) AlertRuleMutator {
return func(rule *AlertRule) {
if rule.Annotations == nil {
rule.Annotations = data.Labels{}
}
rule.Annotations[key] = value
}
}
func WithLabels(a data.Labels) AlertRuleMutator {
return func(rule *AlertRule) {
rule.Labels = a
}
}
func WithLabel(key, value string) AlertRuleMutator {
return func(rule *AlertRule) {
if rule.Labels == nil {
rule.Labels = data.Labels{}
}
rule.Labels[key] = value
}
}
func GenerateAlertLabels(count int, prefix string) data.Labels { func GenerateAlertLabels(count int, prefix string) data.Labels {
labels := make(data.Labels, count) labels := make(data.Labels, count)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {

File diff suppressed because it is too large Load Diff