From 573b5f89e15a457fb91c1d6cf6903c155c8f4540 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Mon, 16 Nov 2020 10:10:02 -0500 Subject: [PATCH] Improve test coverage --- core/condition.go | 7 ++++--- core/condition_test.go | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/core/condition.go b/core/condition.go index cf13e87b..36cc9bf3 100644 --- a/core/condition.go +++ b/core/condition.go @@ -2,12 +2,13 @@ package core import ( "fmt" - "github.com/TwinProduction/gatus/jsonpath" - "github.com/TwinProduction/gatus/pattern" "log" "strconv" "strings" "time" + + "github.com/TwinProduction/gatus/jsonpath" + "github.com/TwinProduction/gatus/pattern" ) const ( @@ -186,7 +187,7 @@ func sanitizeAndResolveNumerical(list []string, result *Result) []int64 { var sanitizedNumbers []int64 sanitizedList := sanitizeAndResolve(list, result) for _, element := range sanitizedList { - if duration, err := time.ParseDuration(element); err == nil { + if duration, err := time.ParseDuration(element); duration != 0 && err == nil { sanitizedNumbers = append(sanitizedNumbers, duration.Milliseconds()) } else if number, err := strconv.ParseInt(element, 10, 64); err != nil { // Default to 0 if the string couldn't be converted to an integer diff --git a/core/condition_test.go b/core/condition_test.go index d4e929df..90890e58 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -320,7 +320,7 @@ func TestCondition_evaluateWithUnsetCertificateExpiration(t *testing.T) { } } -func TestCondition_evaluateWithCertificateExpirationGreaterThan(t *testing.T) { +func TestCondition_evaluateWithCertificateExpirationGreaterThanNumerical(t *testing.T) { acceptable := (time.Hour * 24 * 28).Milliseconds() condition := Condition("[CERTIFICATE_EXPIRATION] > " + strconv.FormatInt(acceptable, 10)) result := &Result{CertificateExpiration: time.Hour * 24 * 60} @@ -330,7 +330,7 @@ func TestCondition_evaluateWithCertificateExpirationGreaterThan(t *testing.T) { } } -func TestCondition_evaluateWithCertificateExpirationGreaterThanFailure(t *testing.T) { +func TestCondition_evaluateWithCertificateExpirationGreaterThanNumericalFailure(t *testing.T) { acceptable := (time.Hour * 24 * 28).Milliseconds() condition := Condition("[CERTIFICATE_EXPIRATION] > " + strconv.FormatInt(acceptable, 10)) result := &Result{CertificateExpiration: time.Hour * 24 * 14} @@ -339,3 +339,21 @@ func TestCondition_evaluateWithCertificateExpirationGreaterThanFailure(t *testin t.Errorf("Condition '%s' should have been a failure", condition) } } + +func TestCondition_evaluateWithCertificateExpirationGreaterThanDuration(t *testing.T) { + condition := Condition("[CERTIFICATE_EXPIRATION] > 12h") + result := &Result{CertificateExpiration: 24 * time.Hour} + condition.evaluate(result) + if !result.ConditionResults[0].Success { + t.Errorf("Condition '%s' should have been a success", condition) + } +} + +func TestCondition_evaluateWithCertificateExpirationGreaterThanDurationFailure(t *testing.T) { + condition := Condition("[CERTIFICATE_EXPIRATION] > 48h") + result := &Result{CertificateExpiration: 24 * time.Hour} + condition.evaluate(result) + if result.ConditionResults[0].Success { + t.Errorf("Condition '%s' should have been a failure", condition) + } +}