This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package workerruntime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/itworx/pulse/internal/alert"
|
||||
"github.com/itworx/pulse/internal/metriccatalog"
|
||||
"github.com/itworx/pulse/internal/metricquery"
|
||||
"github.com/itworx/pulse/internal/prometheus"
|
||||
"github.com/itworx/pulse/internal/queryplan"
|
||||
)
|
||||
|
||||
type workerPrometheusSource struct{ queries int }
|
||||
|
||||
func (source *workerPrometheusSource) Query(context.Context, string, *time.Time) (prometheus.QueryResult, error) {
|
||||
source.queries++
|
||||
return prometheus.QueryResult{Status: "success", Data: []byte(`{"resultType":"vector","result":[{"metric":{"instance":"smoke-host"},"value":[1770000000,"95"]}]}`)}, nil
|
||||
}
|
||||
func (*workerPrometheusSource) QueryRange(context.Context, string, time.Time, time.Time, time.Duration) (prometheus.QueryResult, error) {
|
||||
return prometheus.QueryResult{}, nil
|
||||
}
|
||||
|
||||
func TestPrometheusMetricSourceUsesReadOnlyWorkerPrincipal(t *testing.T) {
|
||||
registry, err := metriccatalog.DefaultRegistry()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
upstream := &workerPrometheusSource{}
|
||||
service := metricquery.NewService(queryplan.NewPlanner(registry, queryplan.Limits{}), upstream, nil)
|
||||
source := PrometheusMetricSource{Service: service}
|
||||
value, err := source.Value(context.Background(), alert.Rule{Document: alert.Document{
|
||||
Condition: alert.Condition{InputType: "metric", Metric: "host.cpu.utilization", Aggregation: "avg"},
|
||||
Scope: map[string]any{"serverId": "smoke-host"},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !value.Known || value.Value != 95 || upstream.queries != 1 {
|
||||
t.Fatalf("value=%+v queries=%d", value, upstream.queries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrometheusMetricSourceDoesNotForwardAlertTargetMetadata(t *testing.T) {
|
||||
registry, err := metriccatalog.DefaultRegistry()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
upstream := &workerPrometheusSource{}
|
||||
service := metricquery.NewService(queryplan.NewPlanner(registry, queryplan.Limits{}), upstream, nil)
|
||||
source := PrometheusMetricSource{Service: service}
|
||||
rules := []alert.Rule{
|
||||
{Document: alert.Document{Condition: alert.Condition{InputType: "metric", Metric: "storage.disk.temperature.maximum", Aggregation: "max"}, Scope: map[string]any{"entityType": "disk"}}},
|
||||
{Document: alert.Document{Condition: alert.Condition{InputType: "metric", Metric: "service.availability.minimum", Aggregation: "min"}, Scope: map[string]any{"entityType": "service", "critical": true}}},
|
||||
}
|
||||
for _, rule := range rules {
|
||||
value, valueErr := source.Value(context.Background(), rule)
|
||||
if valueErr != nil || !value.Known {
|
||||
t.Fatalf("metric=%s value=%+v err=%v", rule.Condition.Metric, value, valueErr)
|
||||
}
|
||||
}
|
||||
if upstream.queries != len(rules) {
|
||||
t.Fatalf("queries=%d want=%d", upstream.queries, len(rules))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user