Skip to main content

promrule_test.go

promrule_test.go - Overview

This file contains unit tests for the Prometheus rule functionality, specifically focusing on the ShouldAlert method. It tests various scenarios based on different comparison operators, match types, and target values to determine if an alert should be triggered.

Detailed Documentation

TestPromRuleShouldAlert

  • Purpose: Tests the ShouldAlert method of a Prometheus rule with various comparison operators, match types, and target values.
  • Parameters:
    • t (*testing.T): Testing object for running the test.
  • Returns: None

cases (struct)

  • Purpose: Defines the structure for test cases, including input values, expected alert status, comparison operator, match type, target value, and expected alert sample.
    • values (pql.Series): Input time series data.
    • expectAlert (bool): Expected boolean indicating whether an alert should be triggered.
    • compareOp (string): String representation of the comparison operator.
    • matchType (string): String representation of the match type.
    • target (float64): Target value for comparison.
    • expectedAlertSample (v3.Point): The expected alert sample.

PostableRule (struct)

  • Purpose: Defines a struct to mock a rule, with the bare minimum amount of attributes to enable testing.
    • AlertName (string): Name of the alert.
    • AlertType (string): Type of the alert (e.g., metric).
    • RuleType (string): Type of the rule (e.g., Prometheus).
    • EvalWindow (Duration): Evaluation window duration.
    • Frequency (Duration): Frequency of rule evaluation.
    • RuleCondition (*RuleCondition): Rule condition containing the query and comparison parameters.

Code Examples

None

Getting Started Relevance

Include in Getting Started: NO