From 0bf2271a7379e87812b723c0bef51755754cd936 Mon Sep 17 00:00:00 2001 From: TwiN Date: Tue, 15 Mar 2022 20:52:46 -0400 Subject: [PATCH] test: Improve coverage for endpoint health evaluation edge cases (#262) --- core/endpoint_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/core/endpoint_test.go b/core/endpoint_test.go index 77b90040..ddf5c552 100644 --- a/core/endpoint_test.go +++ b/core/endpoint_test.go @@ -8,6 +8,7 @@ import ( "github.com/TwiN/gatus/v3/alerting/alert" "github.com/TwiN/gatus/v3/client" + "github.com/TwiN/gatus/v3/core/ui" ) func TestEndpoint_IsEnabled(t *testing.T) { @@ -270,6 +271,9 @@ func TestIntegrationEvaluateHealth(t *testing.T) { if !result.Success { t.Error("Because all conditions passed, this should have been a success") } + if result.Hostname != "twin.sh" { + t.Error("result.Hostname should've been twin.sh, but was", result.Hostname) + } } func TestIntegrationEvaluateHealthWithFailure(t *testing.T) { @@ -288,7 +292,53 @@ func TestIntegrationEvaluateHealthWithFailure(t *testing.T) { t.Error("Because the connection has been established, result.Connected should've been true") } if result.Success { - t.Error("Because one of the conditions failed, success should have been false") + t.Error("Because one of the conditions failed, result.Success should have been false") + } +} + +func TestIntegrationEvaluateHealthWithInvalidCondition(t *testing.T) { + condition := Condition("[STATUS] invalid 200") + endpoint := Endpoint{ + Name: "invalid-condition", + URL: "https://twin.sh/health", + Conditions: []*Condition{&condition}, + } + if err := endpoint.ValidateAndSetDefaults(); err != nil { + // XXX: Should this really not return an error? After all, the condition is not valid and conditions are part of the endpoint... + t.Error("endpoint validation should've been successful, but wasn't") + } + result := endpoint.EvaluateHealth() + if result.Success { + t.Error("Because one of the conditions was invalid, result.Success should have been false") + } + if len(result.Errors) == 0 { + t.Error("There should've been an error") + } +} + +func TestIntegrationEvaluateHealthWithError(t *testing.T) { + condition := Condition("[STATUS] == 200") + endpoint := Endpoint{ + Name: "invalid-host", + URL: "http://invalid/health", + Conditions: []*Condition{&condition}, + UIConfig: &ui.Config{ + HideHostname: true, + }, + } + endpoint.ValidateAndSetDefaults() + result := endpoint.EvaluateHealth() + if result.Success { + t.Error("Because one of the conditions was invalid, result.Success should have been false") + } + if len(result.Errors) == 0 { + t.Error("There should've been an error") + } + if !strings.Contains(result.Errors[0], "") { + t.Error("result.Errors[0] should've had the hostname redacted because ui.hide-hostname is set to true") + } + if result.Hostname != "" { + t.Error("result.Hostname should've been empty because ui.hide-hostname is set to true") } }