1
0
Fork 0
mirror of https://github.com/TwiN/gatus.git synced 2024-12-14 11:58:04 +00:00

test: Add several tests for numerical conditions

This commit is contained in:
TwiN 2023-01-19 01:37:21 -05:00
parent 0ffa03f42d
commit 0db92f46da
2 changed files with 36 additions and 0 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/TwiN/gatus/v5/pattern" "github.com/TwiN/gatus/v5/pattern"
) )
// Placeholders
const ( const (
// StatusPlaceholder is a placeholder for a HTTP status. // StatusPlaceholder is a placeholder for a HTTP status.
// //
@ -49,7 +50,10 @@ const (
// DomainExpirationPlaceholder is a placeholder for the duration before the domain expires, in milliseconds. // DomainExpirationPlaceholder is a placeholder for the duration before the domain expires, in milliseconds.
DomainExpirationPlaceholder = "[DOMAIN_EXPIRATION]" DomainExpirationPlaceholder = "[DOMAIN_EXPIRATION]"
)
// Functions
const (
// LengthFunctionPrefix is the prefix for the length function // LengthFunctionPrefix is the prefix for the length function
// //
// Usage: len([BODY].articles) == 10, len([BODY].name) > 5 // Usage: len([BODY].articles) == 10, len([BODY].name) > 5
@ -72,7 +76,10 @@ const (
// FunctionSuffix is the suffix for all functions // FunctionSuffix is the suffix for all functions
FunctionSuffix = ")" FunctionSuffix = ")"
)
// Other constants
const (
// InvalidConditionElementSuffix is the suffix that will be appended to an invalid condition // InvalidConditionElementSuffix is the suffix that will be appended to an invalid condition
InvalidConditionElementSuffix = "(INVALID)" InvalidConditionElementSuffix = "(INVALID)"

View file

@ -182,6 +182,34 @@ func TestCondition_evaluate(t *testing.T) {
ExpectedSuccess: true, ExpectedSuccess: true,
ExpectedOutput: "[BODY] == test", ExpectedOutput: "[BODY] == test",
}, },
{
Name: "body-numerical-equal",
Condition: Condition("[BODY] == 123"),
Result: &Result{body: []byte("123")},
ExpectedSuccess: true,
ExpectedOutput: "[BODY] == 123",
},
{
Name: "body-numerical-less-than",
Condition: Condition("[BODY] < 124"),
Result: &Result{body: []byte("123")},
ExpectedSuccess: true,
ExpectedOutput: "[BODY] < 124",
},
{
Name: "body-numerical-greater-than",
Condition: Condition("[BODY] > 122"),
Result: &Result{body: []byte("123")},
ExpectedSuccess: true,
ExpectedOutput: "[BODY] > 122",
},
{
Name: "body-numerical-greater-than-failure",
Condition: Condition("[BODY] > 123"),
Result: &Result{body: []byte("100")},
ExpectedSuccess: false,
ExpectedOutput: "[BODY] (100) > 123",
},
{ {
Name: "body-jsonpath", Name: "body-jsonpath",
Condition: Condition("[BODY].status == UP"), Condition: Condition("[BODY].status == UP"),
@ -594,6 +622,7 @@ func TestCondition_evaluate(t *testing.T) {
} }
for _, scenario := range scenarios { for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) { t.Run(scenario.Name, func(t *testing.T) {
t.Parallel()
scenario.Condition.evaluate(scenario.Result, scenario.DontResolveFailedConditions) scenario.Condition.evaluate(scenario.Result, scenario.DontResolveFailedConditions)
if scenario.Result.ConditionResults[0].Success != scenario.ExpectedSuccess { if scenario.Result.ConditionResults[0].Success != scenario.ExpectedSuccess {
t.Errorf("Condition '%s' should have been success=%v", scenario.Condition, scenario.ExpectedSuccess) t.Errorf("Condition '%s' should have been success=%v", scenario.Condition, scenario.ExpectedSuccess)