2021-09-13 00:39:09 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-10-08 03:28:04 +02:00
|
|
|
"github.com/TwiN/gatus/v3/config"
|
|
|
|
"github.com/TwiN/gatus/v3/core"
|
|
|
|
"github.com/TwiN/gatus/v3/storage"
|
|
|
|
"github.com/TwiN/gatus/v3/watchdog"
|
2021-09-13 00:39:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUptimeBadge(t *testing.T) {
|
|
|
|
defer storage.Get().Clear()
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
watchdog.UpdateEndpointStatuses(cfg.Endpoints[0], &core.Result{Success: true, Duration: time.Millisecond, Timestamp: time.Now()})
|
|
|
|
watchdog.UpdateEndpointStatuses(cfg.Endpoints[1], &core.Result{Success: false, Duration: time.Second, Timestamp: time.Now()})
|
2021-09-13 00:39:09 +02:00
|
|
|
router := CreateRouter("../../web/static", cfg.Security, nil, cfg.Metrics)
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
{ // XXX: Remove this in v4.0.0
|
|
|
|
Name: "backward-compatible-services-badge-uptime-1h",
|
|
|
|
Path: "/api/v1/services/core_frontend/uptimes/1h/badge.svg",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{ // XXX: Remove this in v4.0.0
|
|
|
|
Name: "backward-compatible-services-chart-response-time-24h",
|
2021-09-13 00:39:09 +02:00
|
|
|
Path: "/api/v1/services/core_backend/response-times/24h/chart.svg",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run(scenario.Name, func(t *testing.T) {
|
|
|
|
request, _ := http.NewRequest("GET", scenario.Path, nil)
|
|
|
|
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) {
|
|
|
|
scenarios := []struct {
|
|
|
|
ResponseTime int
|
|
|
|
ExpectedColor string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
ResponseTime: 10,
|
|
|
|
ExpectedColor: badgeColorHexAwesome,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResponseTime: 50,
|
|
|
|
ExpectedColor: badgeColorHexAwesome,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResponseTime: 75,
|
|
|
|
ExpectedColor: badgeColorHexGreat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResponseTime: 150,
|
|
|
|
ExpectedColor: badgeColorHexGreat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResponseTime: 201,
|
|
|
|
ExpectedColor: badgeColorHexGood,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResponseTime: 300,
|
|
|
|
ExpectedColor: badgeColorHexGood,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResponseTime: 301,
|
|
|
|
ExpectedColor: badgeColorHexPassable,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResponseTime: 450,
|
|
|
|
ExpectedColor: badgeColorHexPassable,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResponseTime: 700,
|
|
|
|
ExpectedColor: badgeColorHexBad,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ResponseTime: 1500,
|
|
|
|
ExpectedColor: badgeColorHexVeryBad,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run("response-time-"+strconv.Itoa(scenario.ResponseTime), func(t *testing.T) {
|
|
|
|
if getBadgeColorFromResponseTime(scenario.ResponseTime) != scenario.ExpectedColor {
|
|
|
|
t.Errorf("expected %s from %d, got %v", scenario.ExpectedColor, scenario.ResponseTime, getBadgeColorFromResponseTime(scenario.ResponseTime))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|