2023-07-09 02:37:41 +02:00
|
|
|
package api
|
2021-09-13 00:39:09 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/config"
|
2024-05-10 04:56:16 +02:00
|
|
|
"github.com/TwiN/gatus/v5/config/endpoint"
|
|
|
|
"github.com/TwiN/gatus/v5/config/endpoint/ui"
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/storage/store"
|
|
|
|
"github.com/TwiN/gatus/v5/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,
|
2024-05-10 04:56:16 +02:00
|
|
|
Endpoints: []*endpoint.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
|
|
|
|
|
|
|
cfg.Endpoints[0].UIConfig = ui.GetDefaultConfig()
|
|
|
|
cfg.Endpoints[1].UIConfig = ui.GetDefaultConfig()
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
watchdog.UpdateEndpointStatuses(cfg.Endpoints[0], &endpoint.Result{Success: true, Connected: true, Duration: time.Millisecond, Timestamp: time.Now()})
|
|
|
|
watchdog.UpdateEndpointStatuses(cfg.Endpoints[1], &endpoint.Result{Success: false, Connected: false, Duration: time.Second, Timestamp: time.Now()})
|
2023-07-09 02:37:41 +02:00
|
|
|
api := New(cfg)
|
|
|
|
router := api.Router()
|
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,
|
|
|
|
},
|
2024-02-01 06:06:08 +01:00
|
|
|
{
|
|
|
|
Name: "badge-shields-health-up",
|
|
|
|
Path: "/api/v1/endpoints/core_frontend/health/badge.shields",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-shields-health-down",
|
|
|
|
Path: "/api/v1/endpoints/core_backend/health/badge.shields",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "badge-shields-health-for-invalid-key",
|
|
|
|
Path: "/api/v1/endpoints/invalid_key/health/badge.shields",
|
|
|
|
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) {
|
2023-07-09 02:37:41 +02:00
|
|
|
request := httptest.NewRequest("GET", scenario.Path, http.NoBody)
|
2021-09-13 00:39:09 +02:00
|
|
|
if scenario.Gzip {
|
|
|
|
request.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
}
|
2023-07-09 02:37:41 +02:00
|
|
|
response, err := router.Test(request)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if response.StatusCode != scenario.ExpectedCode {
|
|
|
|
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, scenario.ExpectedCode, response.StatusCode)
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
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")
|
2022-08-11 03:26:36 +02:00
|
|
|
)
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
firstTestEndpoint := endpoint.Endpoint{
|
2022-08-11 03:26:36 +02:00
|
|
|
Name: "a",
|
2022-08-11 03:05:34 +02:00
|
|
|
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},
|
2022-08-11 03:05:34 +02:00
|
|
|
Alerts: nil,
|
|
|
|
NumberOfFailuresInARow: 0,
|
|
|
|
NumberOfSuccessesInARow: 0,
|
|
|
|
UIConfig: ui.GetDefaultConfig(),
|
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
secondTestEndpoint := endpoint.Endpoint{
|
2022-08-11 03:26:36 +02:00
|
|
|
Name: "b",
|
|
|
|
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},
|
2022-08-11 03:26:36 +02:00
|
|
|
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,
|
2024-05-10 04:56:16 +02:00
|
|
|
Endpoints: []*endpoint.Endpoint{&firstTestEndpoint, &secondTestEndpoint},
|
2022-08-11 03:05:34 +02:00
|
|
|
}
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
testSuccessfulResult := endpoint.Result{
|
2023-07-09 02:37:41 +02:00
|
|
|
Hostname: "example.org",
|
|
|
|
IP: "127.0.0.1",
|
|
|
|
HTTPStatus: 200,
|
|
|
|
Errors: nil,
|
|
|
|
Connected: true,
|
|
|
|
Success: true,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Duration: 150 * time.Millisecond,
|
|
|
|
CertificateExpiration: 10 * time.Hour,
|
2024-05-10 04:56:16 +02:00
|
|
|
ConditionResults: []*endpoint.ConditionResult{
|
2023-07-09 02:37:41 +02:00
|
|
|
{
|
|
|
|
Condition: "[STATUS] == 200",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Condition: "[RESPONSE_TIME] < 500",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Condition: "[CERTIFICATE_EXPIRATION] < 72h",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|