Improve test coverage

This commit is contained in:
TwinProduction 2020-11-16 10:10:02 -05:00
parent 121369d9c0
commit 573b5f89e1
2 changed files with 24 additions and 5 deletions

View File

@ -2,12 +2,13 @@ package core
import ( import (
"fmt" "fmt"
"github.com/TwinProduction/gatus/jsonpath"
"github.com/TwinProduction/gatus/pattern"
"log" "log"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/TwinProduction/gatus/jsonpath"
"github.com/TwinProduction/gatus/pattern"
) )
const ( const (
@ -186,7 +187,7 @@ func sanitizeAndResolveNumerical(list []string, result *Result) []int64 {
var sanitizedNumbers []int64 var sanitizedNumbers []int64
sanitizedList := sanitizeAndResolve(list, result) sanitizedList := sanitizeAndResolve(list, result)
for _, element := range sanitizedList { 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()) sanitizedNumbers = append(sanitizedNumbers, duration.Milliseconds())
} else if number, err := strconv.ParseInt(element, 10, 64); err != nil { } else if number, err := strconv.ParseInt(element, 10, 64); err != nil {
// Default to 0 if the string couldn't be converted to an integer // Default to 0 if the string couldn't be converted to an integer

View File

@ -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() acceptable := (time.Hour * 24 * 28).Milliseconds()
condition := Condition("[CERTIFICATE_EXPIRATION] > " + strconv.FormatInt(acceptable, 10)) condition := Condition("[CERTIFICATE_EXPIRATION] > " + strconv.FormatInt(acceptable, 10))
result := &Result{CertificateExpiration: time.Hour * 24 * 60} 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() acceptable := (time.Hour * 24 * 28).Milliseconds()
condition := Condition("[CERTIFICATE_EXPIRATION] > " + strconv.FormatInt(acceptable, 10)) condition := Condition("[CERTIFICATE_EXPIRATION] > " + strconv.FormatInt(acceptable, 10))
result := &Result{CertificateExpiration: time.Hour * 24 * 14} 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) 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)
}
}