2021-02-03 05:06:34 +01:00
|
|
|
package memory
|
2020-12-30 11:57:17 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
"github.com/TwiN/gatus/v5/config/endpoint"
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/storage/store/common/paging"
|
2020-12-30 11:57:17 +01:00
|
|
|
)
|
|
|
|
|
2021-01-09 04:41:57 +01:00
|
|
|
var (
|
2024-05-10 04:56:16 +02:00
|
|
|
firstCondition = endpoint.Condition("[STATUS] == 200")
|
|
|
|
secondCondition = endpoint.Condition("[RESPONSE_TIME] < 500")
|
|
|
|
thirdCondition = endpoint.Condition("[CERTIFICATE_EXPIRATION] < 72h")
|
2021-01-09 04:41:57 +01:00
|
|
|
|
2021-08-20 05:07:21 +02:00
|
|
|
now = time.Now()
|
2021-01-09 04:41:57 +01:00
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
testEndpoint = endpoint.Endpoint{
|
2021-01-09 04:41:57 +01:00
|
|
|
Name: "name",
|
|
|
|
Group: "group",
|
|
|
|
URL: "https://example.org/what/ever",
|
|
|
|
Method: "GET",
|
|
|
|
Body: "body",
|
|
|
|
Interval: 30 * time.Second,
|
2024-05-10 04:56:16 +02:00
|
|
|
Conditions: []endpoint.Condition{firstCondition, secondCondition, thirdCondition},
|
2021-01-09 04:41:57 +01:00
|
|
|
Alerts: nil,
|
|
|
|
NumberOfFailuresInARow: 0,
|
|
|
|
NumberOfSuccessesInARow: 0,
|
2020-12-30 11:57:17 +01:00
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
testSuccessfulResult = endpoint.Result{
|
2021-01-09 04:41:57 +01:00
|
|
|
Hostname: "example.org",
|
|
|
|
IP: "127.0.0.1",
|
2020-12-30 11:57:17 +01:00
|
|
|
HTTPStatus: 200,
|
|
|
|
Errors: nil,
|
2021-01-09 04:41:57 +01:00
|
|
|
Connected: true,
|
|
|
|
Success: true,
|
2021-08-20 05:07:21 +02:00
|
|
|
Timestamp: now,
|
2021-01-09 04:41:57 +01:00
|
|
|
Duration: 150 * time.Millisecond,
|
|
|
|
CertificateExpiration: 10 * time.Hour,
|
2024-05-10 04:56:16 +02:00
|
|
|
ConditionResults: []*endpoint.ConditionResult{
|
2021-01-09 04:41:57 +01:00
|
|
|
{
|
|
|
|
Condition: "[STATUS] == 200",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Condition: "[RESPONSE_TIME] < 500",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Condition: "[CERTIFICATE_EXPIRATION] < 72h",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
2020-12-30 11:57:17 +01:00
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
testUnsuccessfulResult = endpoint.Result{
|
2021-01-09 04:41:57 +01:00
|
|
|
Hostname: "example.org",
|
|
|
|
IP: "127.0.0.1",
|
2020-12-30 11:57:17 +01:00
|
|
|
HTTPStatus: 200,
|
2021-01-09 04:41:57 +01:00
|
|
|
Errors: []string{"error-1", "error-2"},
|
2020-12-30 11:57:17 +01:00
|
|
|
Connected: true,
|
2021-01-08 23:43:45 +01:00
|
|
|
Success: false,
|
2021-08-20 05:07:21 +02:00
|
|
|
Timestamp: now,
|
2021-01-09 04:41:57 +01:00
|
|
|
Duration: 750 * time.Millisecond,
|
|
|
|
CertificateExpiration: 10 * time.Hour,
|
2024-05-10 04:56:16 +02:00
|
|
|
ConditionResults: []*endpoint.ConditionResult{
|
2021-01-09 04:41:57 +01:00
|
|
|
{
|
|
|
|
Condition: "[STATUS] == 200",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Condition: "[RESPONSE_TIME] < 500",
|
|
|
|
Success: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Condition: "[CERTIFICATE_EXPIRATION] < 72h",
|
|
|
|
Success: false,
|
|
|
|
},
|
|
|
|
},
|
2020-12-30 11:57:17 +01:00
|
|
|
}
|
2021-01-09 04:41:57 +01:00
|
|
|
)
|
2020-12-30 11:57:17 +01:00
|
|
|
|
2021-07-19 05:13:19 +02:00
|
|
|
// Note that are much more extensive tests in /storage/store/store_test.go.
|
2021-07-19 05:02:27 +02:00
|
|
|
// This test is simply an extra sanity check
|
2021-07-19 05:13:19 +02:00
|
|
|
func TestStore_SanityCheck(t *testing.T) {
|
2022-08-12 02:42:56 +02:00
|
|
|
store, _ := NewStore()
|
2021-08-20 05:07:21 +02:00
|
|
|
defer store.Close()
|
2021-10-23 22:47:12 +02:00
|
|
|
store.Insert(&testEndpoint, &testSuccessfulResult)
|
|
|
|
endpointStatuses, _ := store.GetAllEndpointStatuses(paging.NewEndpointStatusParams())
|
|
|
|
if numberOfEndpointStatuses := len(endpointStatuses); numberOfEndpointStatuses != 1 {
|
|
|
|
t.Fatalf("expected 1 EndpointStatus, got %d", numberOfEndpointStatuses)
|
2021-07-19 05:02:27 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
store.Insert(&testEndpoint, &testUnsuccessfulResult)
|
|
|
|
// Both results inserted are for the same endpoint, therefore, the count shouldn't have increased
|
|
|
|
endpointStatuses, _ = store.GetAllEndpointStatuses(paging.NewEndpointStatusParams())
|
|
|
|
if numberOfEndpointStatuses := len(endpointStatuses); numberOfEndpointStatuses != 1 {
|
|
|
|
t.Fatalf("expected 1 EndpointStatus, got %d", numberOfEndpointStatuses)
|
2020-12-30 11:57:17 +01:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if hourlyAverageResponseTime, err := store.GetHourlyAverageResponseTimeByKey(testEndpoint.Key(), time.Now().Add(-24*time.Hour), time.Now()); err != nil {
|
2021-08-20 05:07:21 +02:00
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
} else if len(hourlyAverageResponseTime) != 1 {
|
|
|
|
t.Errorf("expected 1 hour to have had a result in the past 24 hours, got %d", len(hourlyAverageResponseTime))
|
2021-08-21 17:17:39 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if uptime, _ := store.GetUptimeByKey(testEndpoint.Key(), time.Now().Add(-24*time.Hour), time.Now()); uptime != 0.5 {
|
2021-08-21 17:17:39 +02:00
|
|
|
t.Errorf("expected uptime of last 24h to be 0.5, got %f", uptime)
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if averageResponseTime, _ := store.GetAverageResponseTimeByKey(testEndpoint.Key(), time.Now().Add(-24*time.Hour), time.Now()); averageResponseTime != 450 {
|
2021-08-21 17:17:39 +02:00
|
|
|
t.Errorf("expected average response time of last 24h to be 450, got %d", averageResponseTime)
|
2021-08-20 05:07:21 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
ss, _ := store.GetEndpointStatus(testEndpoint.Group, testEndpoint.Name, paging.NewEndpointStatusParams().WithResults(1, 20).WithEvents(1, 20))
|
2021-07-19 05:02:27 +02:00
|
|
|
if ss == nil {
|
2021-10-23 22:47:12 +02:00
|
|
|
t.Fatalf("Store should've had key '%s', but didn't", testEndpoint.Key())
|
2020-12-30 11:57:17 +01:00
|
|
|
}
|
2021-07-19 05:02:27 +02:00
|
|
|
if len(ss.Events) != 3 {
|
2021-10-23 22:47:12 +02:00
|
|
|
t.Errorf("Endpoint '%s' should've had 3 events, got %d", ss.Name, len(ss.Events))
|
2020-12-30 11:57:17 +01:00
|
|
|
}
|
2021-07-19 05:02:27 +02:00
|
|
|
if len(ss.Results) != 2 {
|
2021-10-23 22:47:12 +02:00
|
|
|
t.Errorf("Endpoint '%s' should've had 2 results, got %d", ss.Name, len(ss.Results))
|
2021-07-19 05:13:19 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if deleted := store.DeleteAllEndpointStatusesNotInKeys([]string{}); deleted != 1 {
|
2021-07-19 05:13:19 +02:00
|
|
|
t.Errorf("%d entries should've been deleted, got %d", 1, deleted)
|
2020-12-30 11:57:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 02:45:28 +01:00
|
|
|
func TestStore_Save(t *testing.T) {
|
2022-08-12 02:42:56 +02:00
|
|
|
store, err := NewStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected no error, got", err.Error())
|
2021-02-06 02:45:28 +01:00
|
|
|
}
|
2022-08-12 02:42:56 +02:00
|
|
|
err = store.Save()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected no error, got", err.Error())
|
2021-02-06 02:45:28 +01:00
|
|
|
}
|
2022-08-12 02:42:56 +02:00
|
|
|
store.Clear()
|
|
|
|
store.Close()
|
2021-02-06 02:45:28 +01:00
|
|
|
}
|