2021-09-13 00:39:09 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-06-21 03:25:14 +02:00
|
|
|
"github.com/TwiN/gatus/v4/config"
|
|
|
|
"github.com/TwiN/gatus/v4/core"
|
2022-08-11 03:05:34 +02:00
|
|
|
"github.com/TwiN/gatus/v4/core/ui"
|
2022-06-21 03:25:14 +02:00
|
|
|
"github.com/TwiN/gatus/v4/storage/store"
|
|
|
|
"github.com/TwiN/gatus/v4/watchdog"
|
2021-09-13 00:39:09 +02:00
|
|
|
)
|
|
|
|
|
2022-06-20 19:59:45 +02:00
|
|
|
func TestBadge(t *testing.T) {
|
2021-10-29 01:35:46 +02:00
|
|
|
defer store.Get().Clear()
|
2021-09-13 00:39:09 +02:00
|
|
|
defer cache.Clear()
|
|
|
|
cfg := &config.Config{
|
|
|
|
Metrics: true,
|
2021-10-23 22:47:12 +02:00
|
|
|
Endpoints: []*core.Endpoint{
|
2021-09-13 00:39:09 +02:00
|
|
|
{
|
|
|
|
Name: "frontend",
|
|
|
|
Group: "core",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "backend",
|
|
|
|
Group: "core",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-08-11 03:05:34 +02:00
|
|
|
|
|
|
|
testSuccessfulResult = core.Result{
|
|
|
|
Hostname: "example.org",
|
|
|
|
IP: "127.0.0.1",
|
|
|
|
HTTPStatus: 200,
|
|
|
|
Errors: nil,
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Endpoints[0].UIConfig = ui.GetDefaultConfig()
|
|
|
|
cfg.Endpoints[1].UIConfig = ui.GetDefaultConfig()
|
|
|
|
|
2022-06-20 19:59:45 +02:00
|
|
|
watchdog.UpdateEndpointStatuses(cfg.Endpoints[0], &core.Result{Success: true, Connected: true, Duration: time.Millisecond, Timestamp: time.Now()})
|
|
|
|
watchdog.UpdateEndpointStatuses(cfg.Endpoints[1], &core.Result{Success: false, Connected: false, Duration: time.Second, Timestamp: time.Now()})
|
2022-07-29 02:07:53 +02:00
|
|
|
router := CreateRouter("../../web/static", cfg)
|
2021-09-13 00:39:09 +02:00
|
|
|
type Scenario struct {
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
ExpectedCode int
|
|
|
|
Gzip bool
|
|
|
|
}
|
|
|
|
scenarios := []Scenario{
|
|
|
|
{
|
|
|
|
Name: "badge-uptime-1h",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/core_frontend/uptimes/1h/badge.svg",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-uptime-24h",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/core_backend/uptimes/24h/badge.svg",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-uptime-7d",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/core_frontend/uptimes/7d/badge.svg",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-uptime-with-invalid-duration",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/core_backend/uptimes/3d/badge.svg",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-uptime-for-invalid-key",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/invalid_key/uptimes/7d/badge.svg",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-response-time-1h",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/core_frontend/response-times/1h/badge.svg",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-response-time-24h",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/core_backend/response-times/24h/badge.svg",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-response-time-7d",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/core_frontend/response-times/7d/badge.svg",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-response-time-with-invalid-duration",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/core_backend/response-times/3d/badge.svg",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-response-time-for-invalid-key",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/invalid_key/response-times/7d/badge.svg",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusNotFound,
|
|
|
|
},
|
2022-06-20 19:59:45 +02:00
|
|
|
{
|
|
|
|
Name: "badge-health-up",
|
|
|
|
Path: "/api/v1/endpoints/core_frontend/health/badge.svg",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-health-down",
|
|
|
|
Path: "/api/v1/endpoints/core_backend/health/badge.svg",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-health-for-invalid-key",
|
|
|
|
Path: "/api/v1/endpoints/invalid_key/health/badge.svg",
|
|
|
|
ExpectedCode: http.StatusNotFound,
|
|
|
|
},
|
2021-09-13 00:39:09 +02:00
|
|
|
{
|
|
|
|
Name: "chart-response-time-24h",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/core_backend/response-times/24h/chart.svg",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
2022-08-31 02:00:04 +02:00
|
|
|
{
|
|
|
|
Name: "chart-response-time-7d",
|
|
|
|
Path: "/api/v1/endpoints/core_frontend/response-times/7d/chart.svg",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "chart-response-time-with-invalid-duration",
|
|
|
|
Path: "/api/v1/endpoints/core_backend/response-times/3d/chart.svg",
|
|
|
|
ExpectedCode: http.StatusBadRequest,
|
|
|
|
},
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run(scenario.Name, func(t *testing.T) {
|
2021-11-11 06:14:00 +01:00
|
|
|
request, _ := http.NewRequest("GET", scenario.Path, http.NoBody)
|
2021-09-13 00:39:09 +02:00
|
|
|
if scenario.Gzip {
|
|
|
|
request.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
}
|
|
|
|
responseRecorder := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(responseRecorder, request)
|
|
|
|
if responseRecorder.Code != scenario.ExpectedCode {
|
|
|
|
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, scenario.ExpectedCode, responseRecorder.Code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBadgeColorFromUptime(t *testing.T) {
|
|
|
|
scenarios := []struct {
|
|
|
|
Uptime float64
|
|
|
|
ExpectedColor string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Uptime: 1,
|
|
|
|
ExpectedColor: badgeColorHexAwesome,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uptime: 0.99,
|
|
|
|
ExpectedColor: badgeColorHexAwesome,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uptime: 0.97,
|
|
|
|
ExpectedColor: badgeColorHexGreat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uptime: 0.95,
|
|
|
|
ExpectedColor: badgeColorHexGreat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uptime: 0.93,
|
|
|
|
ExpectedColor: badgeColorHexGood,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uptime: 0.9,
|
|
|
|
ExpectedColor: badgeColorHexGood,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uptime: 0.85,
|
|
|
|
ExpectedColor: badgeColorHexPassable,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uptime: 0.7,
|
|
|
|
ExpectedColor: badgeColorHexBad,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uptime: 0.65,
|
|
|
|
ExpectedColor: badgeColorHexBad,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uptime: 0.6,
|
|
|
|
ExpectedColor: badgeColorHexVeryBad,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run("uptime-"+strconv.Itoa(int(scenario.Uptime*100)), func(t *testing.T) {
|
|
|
|
if getBadgeColorFromUptime(scenario.Uptime) != scenario.ExpectedColor {
|
|
|
|
t.Errorf("expected %s from %f, got %v", scenario.ExpectedColor, scenario.Uptime, getBadgeColorFromUptime(scenario.Uptime))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBadgeColorFromResponseTime(t *testing.T) {
|
2022-08-11 03:05:34 +02:00
|
|
|
defer store.Get().Clear()
|
|
|
|
defer cache.Clear()
|
|
|
|
|
2022-08-11 03:26:36 +02:00
|
|
|
var (
|
|
|
|
firstCondition = core.Condition("[STATUS] == 200")
|
|
|
|
secondCondition = core.Condition("[RESPONSE_TIME] < 500")
|
|
|
|
thirdCondition = core.Condition("[CERTIFICATE_EXPIRATION] < 72h")
|
|
|
|
)
|
|
|
|
|
|
|
|
firstTestEndpoint := core.Endpoint{
|
|
|
|
Name: "a",
|
2022-08-11 03:05:34 +02:00
|
|
|
URL: "https://example.org/what/ever",
|
|
|
|
Method: "GET",
|
|
|
|
Body: "body",
|
|
|
|
Interval: 30 * time.Second,
|
|
|
|
Conditions: []core.Condition{firstCondition, secondCondition, thirdCondition},
|
|
|
|
Alerts: nil,
|
|
|
|
NumberOfFailuresInARow: 0,
|
|
|
|
NumberOfSuccessesInARow: 0,
|
|
|
|
UIConfig: ui.GetDefaultConfig(),
|
|
|
|
}
|
2022-08-11 03:26:36 +02:00
|
|
|
secondTestEndpoint := core.Endpoint{
|
|
|
|
Name: "b",
|
|
|
|
URL: "https://example.org/what/ever",
|
|
|
|
Method: "GET",
|
|
|
|
Body: "body",
|
|
|
|
Interval: 30 * time.Second,
|
|
|
|
Conditions: []core.Condition{firstCondition, secondCondition, thirdCondition},
|
|
|
|
Alerts: nil,
|
|
|
|
NumberOfFailuresInARow: 0,
|
|
|
|
NumberOfSuccessesInARow: 0,
|
|
|
|
UIConfig: &ui.Config{
|
|
|
|
Badge: &ui.Badge{
|
|
|
|
ResponseTime: &ui.ResponseTime{
|
|
|
|
Thresholds: []int{100, 500, 1000, 2000, 3000},
|
|
|
|
},
|
2022-08-11 03:05:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cfg := &config.Config{
|
|
|
|
Metrics: true,
|
2022-08-11 03:26:36 +02:00
|
|
|
Endpoints: []*core.Endpoint{&firstTestEndpoint, &secondTestEndpoint},
|
2022-08-11 03:05:34 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 03:26:36 +02:00
|
|
|
store.Get().Insert(&firstTestEndpoint, &testSuccessfulResult)
|
|
|
|
store.Get().Insert(&secondTestEndpoint, &testSuccessfulResult)
|
2022-08-11 03:05:34 +02:00
|
|
|
|
2021-09-13 00:39:09 +02:00
|
|
|
scenarios := []struct {
|
2022-08-11 03:26:36 +02:00
|
|
|
Key string
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime int
|
|
|
|
ExpectedColor string
|
|
|
|
}{
|
|
|
|
{
|
2022-08-11 03:26:36 +02:00
|
|
|
Key: firstTestEndpoint.Key(),
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime: 10,
|
|
|
|
ExpectedColor: badgeColorHexAwesome,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 03:26:36 +02:00
|
|
|
Key: firstTestEndpoint.Key(),
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime: 50,
|
|
|
|
ExpectedColor: badgeColorHexAwesome,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 03:26:36 +02:00
|
|
|
Key: firstTestEndpoint.Key(),
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime: 75,
|
|
|
|
ExpectedColor: badgeColorHexGreat,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 03:26:36 +02:00
|
|
|
Key: firstTestEndpoint.Key(),
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime: 150,
|
|
|
|
ExpectedColor: badgeColorHexGreat,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 03:26:36 +02:00
|
|
|
Key: firstTestEndpoint.Key(),
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime: 201,
|
|
|
|
ExpectedColor: badgeColorHexGood,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 03:26:36 +02:00
|
|
|
Key: firstTestEndpoint.Key(),
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime: 300,
|
|
|
|
ExpectedColor: badgeColorHexGood,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 03:26:36 +02:00
|
|
|
Key: firstTestEndpoint.Key(),
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime: 301,
|
|
|
|
ExpectedColor: badgeColorHexPassable,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 03:26:36 +02:00
|
|
|
Key: firstTestEndpoint.Key(),
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime: 450,
|
|
|
|
ExpectedColor: badgeColorHexPassable,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 03:26:36 +02:00
|
|
|
Key: firstTestEndpoint.Key(),
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime: 700,
|
|
|
|
ExpectedColor: badgeColorHexBad,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 03:26:36 +02:00
|
|
|
Key: firstTestEndpoint.Key(),
|
2021-09-13 00:39:09 +02:00
|
|
|
ResponseTime: 1500,
|
|
|
|
ExpectedColor: badgeColorHexVeryBad,
|
|
|
|
},
|
2022-08-11 03:26:36 +02:00
|
|
|
{
|
|
|
|
Key: secondTestEndpoint.Key(),
|
|
|
|
ResponseTime: 50,
|
|
|
|
ExpectedColor: badgeColorHexAwesome,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: secondTestEndpoint.Key(),
|
|
|
|
ResponseTime: 1500,
|
|
|
|
ExpectedColor: badgeColorHexPassable,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: secondTestEndpoint.Key(),
|
|
|
|
ResponseTime: 2222,
|
|
|
|
ExpectedColor: badgeColorHexBad,
|
|
|
|
},
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
2022-08-11 03:26:36 +02:00
|
|
|
t.Run(scenario.Key+"-response-time-"+strconv.Itoa(scenario.ResponseTime), func(t *testing.T) {
|
|
|
|
if getBadgeColorFromResponseTime(scenario.ResponseTime, scenario.Key, cfg) != scenario.ExpectedColor {
|
|
|
|
t.Errorf("expected %s from %d, got %v", scenario.ExpectedColor, scenario.ResponseTime, getBadgeColorFromResponseTime(scenario.ResponseTime, scenario.Key, cfg))
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-20 19:59:45 +02:00
|
|
|
|
|
|
|
func TestGetBadgeColorFromHealth(t *testing.T) {
|
|
|
|
scenarios := []struct {
|
|
|
|
HealthStatus string
|
|
|
|
ExpectedColor string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
HealthStatus: HealthStatusUp,
|
|
|
|
ExpectedColor: badgeColorHexAwesome,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HealthStatus: HealthStatusDown,
|
|
|
|
ExpectedColor: badgeColorHexVeryBad,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HealthStatus: HealthStatusUnknown,
|
|
|
|
ExpectedColor: badgeColorHexPassable,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run("health-"+scenario.HealthStatus, func(t *testing.T) {
|
|
|
|
if getBadgeColorFromHealth(scenario.HealthStatus) != scenario.ExpectedColor {
|
|
|
|
t.Errorf("expected %s from %s, got %v", scenario.ExpectedColor, scenario.HealthStatus, getBadgeColorFromHealth(scenario.HealthStatus))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|