mirror of
https://github.com/coreos/prometheus-operator.git
synced 2026-02-05 06:45:27 +01:00
test: fix testThanosRulerStateless
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
@@ -327,7 +327,7 @@ func testAllNSThanosRuler(t *testing.T) {
|
||||
"ThanosRulerQueryConfig": testTRQueryConfig,
|
||||
"ThanosRulerCheckStorageClass": testTRCheckStorageClass,
|
||||
"ThanosRulerServiceName": testThanosRulerServiceName,
|
||||
"ThanosRulerStateless": ThanosRulerStateless,
|
||||
"ThanosRulerStateless": testThanosRulerStateless,
|
||||
}
|
||||
for name, f := range testFuncs {
|
||||
t.Run(name, f)
|
||||
|
||||
@@ -1536,9 +1536,13 @@ func testPromMultiplePrometheusRulesDifferentNS(t *testing.T) {
|
||||
for _, file := range ruleFiles {
|
||||
var loopError error
|
||||
err = wait.PollUntilContextTimeout(context.Background(), time.Second, 5*framework.DefaultTimeout, false, func(ctx context.Context) (bool, error) {
|
||||
var firing bool
|
||||
firing, loopError = framework.CheckPrometheusFiringAlert(ctx, file.ns, pSVC.Name, file.alertName)
|
||||
return !firing, nil
|
||||
var alerts []map[string]string
|
||||
alerts, loopError = framework.GetPrometheusFiringAlerts(ctx, file.ns, pSVC.Name, file.alertName)
|
||||
if len(alerts) > 0 {
|
||||
loopError = fmt.Errorf("%s: got %d alerts", file.alertName, len(alerts))
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -2905,14 +2909,9 @@ func testOperatorNSScope(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
firing, err := framework.CheckPrometheusFiringAlert(context.Background(), p.Namespace, pSVC.Name, secondAlertName)
|
||||
if err != nil && !strings.Contains(err.Error(), "expected 1 query result but got 0") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if firing {
|
||||
t.Fatalf("expected alert %q not to fire", secondAlertName)
|
||||
}
|
||||
alerts, err := framework.GetPrometheusFiringAlerts(context.Background(), p.Namespace, pSVC.Name, secondAlertName)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, alerts, 0)
|
||||
})
|
||||
|
||||
t.Run("MultiNS", func(t *testing.T) {
|
||||
@@ -2976,14 +2975,9 @@ func testOperatorNSScope(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
firing, err := framework.CheckPrometheusFiringAlert(context.Background(), p.Namespace, pSVC.Name, secondAlertName)
|
||||
if err != nil && !strings.Contains(err.Error(), "expected 1 query result but got 0") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if firing {
|
||||
t.Fatalf("expected alert %q not to fire", secondAlertName)
|
||||
}
|
||||
alerts, err := framework.GetPrometheusFiringAlerts(context.Background(), p.Namespace, pSVC.Name, secondAlertName)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, alerts, 0)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -560,7 +560,7 @@ func testThanosRulerServiceName(t *testing.T) {
|
||||
require.Equal(t, svcList.Items[0].Name, svc.Name)
|
||||
}
|
||||
|
||||
func ThanosRulerStateless(t *testing.T) {
|
||||
func testThanosRulerStateless(t *testing.T) {
|
||||
const (
|
||||
name = "test"
|
||||
group = "thanos-ruler-query-config"
|
||||
@@ -638,19 +638,6 @@ func ThanosRulerStateless(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Check that the ALERTS metric is present in Prometheus.
|
||||
var (
|
||||
pollErr error
|
||||
firing bool
|
||||
)
|
||||
pollErr = wait.PollUntilContextTimeout(ctx, time.Second, framework.DefaultTimeout, true, func(ctx context.Context) (bool, error) {
|
||||
firing, err = framework.CheckPrometheusFiringAlert(ctx, ns, promSVC.Name, testAlert)
|
||||
if err != nil {
|
||||
pollErr = fmt.Errorf("failed to check firing alert: %w", err)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return firing, nil
|
||||
})
|
||||
require.NoError(t, pollErr)
|
||||
require.True(t, firing)
|
||||
err = framework.WaitForPrometheusFiringAlert(context.Background(), ns, promSVC.Name, testAlert)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -760,22 +760,46 @@ func (f *Framework) GetHealthyTargets(ctx context.Context, ns, svcName string) (
|
||||
return healthyTargets, nil
|
||||
}
|
||||
|
||||
func (f *Framework) CheckPrometheusFiringAlert(ctx context.Context, ns, svcName, alertName string) (bool, error) {
|
||||
response, err := f.PrometheusSVCGetRequest(ctx, ns, svcName, "http", "/api/v1/query", map[string]string{"query": fmt.Sprintf(`ALERTS{alertname="%v",alertstate="firing"}`, alertName)})
|
||||
// GetPrometheusFiringAlerts returns a slice of alert labels matching the given alert name.
|
||||
func (f *Framework) GetPrometheusFiringAlerts(ctx context.Context, ns, svcName, alertName string) ([]map[string]string, error) {
|
||||
response, err := f.PrometheusSVCGetRequest(
|
||||
ctx,
|
||||
ns,
|
||||
svcName,
|
||||
"http",
|
||||
"/api/v1/query",
|
||||
map[string]string{
|
||||
"query": fmt.Sprintf(`ALERTS{alertname="%v",alertstate="firing"}`, alertName),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q := PrometheusQueryAPIResponse{}
|
||||
if err := json.NewDecoder(bytes.NewBuffer(response)).Decode(&q); err != nil {
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(q.Data.Result) != 1 {
|
||||
return false, fmt.Errorf("expected 1 query result but got %v", len(q.Data.Result))
|
||||
alerts := make([]map[string]string, len(q.Data.Result))
|
||||
for i, res := range q.Data.Result {
|
||||
alerts[i] = res.Metric
|
||||
}
|
||||
|
||||
return true, nil
|
||||
return alerts, nil
|
||||
}
|
||||
|
||||
func (f *Framework) CheckPrometheusFiringAlert(ctx context.Context, ns, svcName, alertName string) error {
|
||||
alerts, err := f.GetPrometheusFiringAlerts(ctx, ns, svcName, alertName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(alerts) != 1 {
|
||||
return fmt.Errorf("expected 1 query result but got %v", len(alerts))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Framework) PrometheusQuery(ns, svcName, scheme, query string) ([]PrometheusQueryResult, error) {
|
||||
@@ -799,10 +823,13 @@ func (f *Framework) PrometheusQuery(ns, svcName, scheme, query string) ([]Promet
|
||||
func (f *Framework) WaitForPrometheusFiringAlert(ctx context.Context, ns, svcName, alertName string) error {
|
||||
var loopError error
|
||||
|
||||
err := wait.PollUntilContextTimeout(ctx, time.Second, 5*f.DefaultTimeout, false, func(ctx context.Context) (bool, error) {
|
||||
var firing bool
|
||||
firing, loopError = f.CheckPrometheusFiringAlert(ctx, ns, svcName, alertName)
|
||||
return firing, nil
|
||||
err := wait.PollUntilContextTimeout(ctx, time.Second, 5*f.DefaultTimeout, true, func(_ context.Context) (bool, error) {
|
||||
loopError = f.CheckPrometheusFiringAlert(context.Background(), ns, svcName, alertName)
|
||||
if loopError != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user