2021-02-03 05:06:34 +01:00
|
|
|
package memory
|
2020-12-30 11:57:17 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/TwinProduction/gatus/core"
|
2021-07-15 04:26:51 +02:00
|
|
|
"github.com/TwinProduction/gatus/storage/store/paging"
|
2020-12-30 11:57:17 +01:00
|
|
|
)
|
|
|
|
|
2021-01-09 04:41:57 +01:00
|
|
|
var (
|
|
|
|
firstCondition = core.Condition("[STATUS] == 200")
|
|
|
|
secondCondition = core.Condition("[RESPONSE_TIME] < 500")
|
|
|
|
thirdCondition = core.Condition("[CERTIFICATE_EXPIRATION] < 72h")
|
|
|
|
|
2021-01-09 07:17:16 +01:00
|
|
|
timestamp = time.Now()
|
2021-01-09 04:41:57 +01:00
|
|
|
|
|
|
|
testService = core.Service{
|
|
|
|
Name: "name",
|
|
|
|
Group: "group",
|
|
|
|
URL: "https://example.org/what/ever",
|
|
|
|
Method: "GET",
|
|
|
|
Body: "body",
|
|
|
|
Interval: 30 * time.Second,
|
|
|
|
Conditions: []*core.Condition{&firstCondition, &secondCondition, &thirdCondition},
|
|
|
|
Alerts: nil,
|
|
|
|
Insecure: false,
|
|
|
|
NumberOfFailuresInARow: 0,
|
|
|
|
NumberOfSuccessesInARow: 0,
|
2020-12-30 11:57:17 +01:00
|
|
|
}
|
2021-01-09 04:41:57 +01:00
|
|
|
testSuccessfulResult = core.Result{
|
|
|
|
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,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Duration: 150 * time.Millisecond,
|
|
|
|
CertificateExpiration: 10 * time.Hour,
|
|
|
|
ConditionResults: []*core.ConditionResult{
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2021-01-09 04:41:57 +01:00
|
|
|
testUnsuccessfulResult = core.Result{
|
|
|
|
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-01-09 04:41:57 +01:00
|
|
|
Timestamp: timestamp,
|
|
|
|
Duration: 750 * time.Millisecond,
|
|
|
|
CertificateExpiration: 10 * time.Hour,
|
|
|
|
ConditionResults: []*core.ConditionResult{
|
|
|
|
{
|
|
|
|
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) {
|
2021-02-03 05:06:34 +01:00
|
|
|
store, _ := NewStore("")
|
2021-01-09 04:41:57 +01:00
|
|
|
store.Insert(&testService, &testSuccessfulResult)
|
2021-07-19 05:02:27 +02:00
|
|
|
if numberOfServiceStatuses := len(store.GetAllServiceStatuses(paging.NewServiceStatusParams())); numberOfServiceStatuses != 1 {
|
|
|
|
t.Fatalf("expected 1 ServiceStatus, got %d", numberOfServiceStatuses)
|
|
|
|
}
|
2021-01-09 04:41:57 +01:00
|
|
|
store.Insert(&testService, &testUnsuccessfulResult)
|
2021-07-19 05:02:27 +02:00
|
|
|
// Both results inserted are for the same service, therefore, the count shouldn't have increased
|
|
|
|
if numberOfServiceStatuses := len(store.GetAllServiceStatuses(paging.NewServiceStatusParams())); numberOfServiceStatuses != 1 {
|
|
|
|
t.Fatalf("expected 1 ServiceStatus, got %d", numberOfServiceStatuses)
|
2020-12-30 11:57:17 +01:00
|
|
|
}
|
2021-07-19 05:13:19 +02:00
|
|
|
ss := store.GetServiceStatus(testService.Group, testService.Name, paging.NewServiceStatusParams().WithResults(1, 20).WithEvents(1, 20))
|
2021-07-19 05:02:27 +02:00
|
|
|
if ss == nil {
|
2021-07-17 01:12:14 +02:00
|
|
|
t.Fatalf("Store should've had key '%s', but didn't", testService.Key())
|
2020-12-30 11:57:17 +01:00
|
|
|
}
|
2021-07-19 05:02:27 +02:00
|
|
|
if len(ss.Events) != 3 {
|
2021-07-19 05:13:19 +02:00
|
|
|
t.Errorf("Service '%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-07-19 05:13:19 +02:00
|
|
|
t.Errorf("Service '%s' should've had 2 results, got %d", ss.Name, len(ss.Results))
|
|
|
|
}
|
|
|
|
if deleted := store.DeleteAllServiceStatusesNotInKeys([]string{}); deleted != 1 {
|
|
|
|
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) {
|
|
|
|
files := []string{
|
|
|
|
"",
|
|
|
|
t.TempDir() + "/test.db",
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
t.Run(file, func(t *testing.T) {
|
|
|
|
store, err := NewStore(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected no error, got", err.Error())
|
|
|
|
}
|
|
|
|
err = store.Save()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected no error, got", err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|