mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-11 13:57:36 -08:00
cmd/prometheus: Fix flakiness of QueryLogTest
Now we check that a rule execution has taken place. This also reduces the time to run the rules tests from 45s to 25s. Signed-off-by: Julien <roidelapluie@o11y.eu>
This commit is contained in:
parent
34c0f2d48e
commit
9d275c23de
|
@ -125,12 +125,61 @@ func (p *queryLogTest) query(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, 200, r.StatusCode)
|
require.Equal(t, 200, r.StatusCode)
|
||||||
case ruleOrigin:
|
case ruleOrigin:
|
||||||
time.Sleep(2 * time.Second)
|
// Poll the /api/v1/rules endpoint until a new rule evaluation is detected.
|
||||||
|
var lastEvalTime time.Time
|
||||||
|
for {
|
||||||
|
r, err := http.Get(fmt.Sprintf("http://%s:%d/api/v1/rules", p.host, p.port))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
rulesBody, err := io.ReadAll(r.Body)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
// Parse the rules response to find the last evaluation time.
|
||||||
|
newEvalTime := parseLastEvaluation(rulesBody)
|
||||||
|
if newEvalTime.After(lastEvalTime) {
|
||||||
|
if !lastEvalTime.IsZero() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
lastEvalTime = newEvalTime
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
panic("can't query this origin")
|
panic("can't query this origin")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseLastEvaluation extracts the last evaluation timestamp from the /api/v1/rules response.
|
||||||
|
func parseLastEvaluation(rulesBody []byte) time.Time {
|
||||||
|
var ruleResponse struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
Data struct {
|
||||||
|
Groups []struct {
|
||||||
|
Rules []struct {
|
||||||
|
LastEvaluation string `json:"lastEvaluation"`
|
||||||
|
} `json:"rules"`
|
||||||
|
} `json:"groups"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := json.Unmarshal(rulesBody, &ruleResponse)
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, group := range ruleResponse.Data.Groups {
|
||||||
|
for _, rule := range group.Rules {
|
||||||
|
if evalTime, err := time.Parse(time.RFC3339Nano, rule.LastEvaluation); err == nil {
|
||||||
|
return evalTime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
// queryString returns the expected queryString of a this test.
|
// queryString returns the expected queryString of a this test.
|
||||||
func (p *queryLogTest) queryString() string {
|
func (p *queryLogTest) queryString() string {
|
||||||
switch p.origin {
|
switch p.origin {
|
||||||
|
@ -322,7 +371,7 @@ func (p *queryLogTest) run(t *testing.T) {
|
||||||
if p.exactQueryCount() {
|
if p.exactQueryCount() {
|
||||||
require.Len(t, ql, qc)
|
require.Len(t, ql, qc)
|
||||||
} else {
|
} else {
|
||||||
require.Greater(t, len(ql), qc, "no queries logged")
|
require.GreaterOrEqual(t, len(ql), qc, "no queries logged")
|
||||||
}
|
}
|
||||||
p.validateLastQuery(t, ql)
|
p.validateLastQuery(t, ql)
|
||||||
qc = len(ql)
|
qc = len(ql)
|
||||||
|
@ -353,7 +402,7 @@ func (p *queryLogTest) run(t *testing.T) {
|
||||||
if p.exactQueryCount() {
|
if p.exactQueryCount() {
|
||||||
require.Len(t, ql, qc)
|
require.Len(t, ql, qc)
|
||||||
} else {
|
} else {
|
||||||
require.Greater(t, len(ql), qc, "no queries logged")
|
require.GreaterOrEqual(t, len(ql), qc, "no queries logged")
|
||||||
}
|
}
|
||||||
p.validateLastQuery(t, ql)
|
p.validateLastQuery(t, ql)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue