2021-02-01 07:37:56 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/TwinProduction/gatus/config"
|
|
|
|
"github.com/TwinProduction/gatus/core"
|
|
|
|
"github.com/TwinProduction/gatus/watchdog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateRouter(t *testing.T) {
|
|
|
|
staticFolder = "../web/static"
|
|
|
|
cfg := &config.Config{
|
|
|
|
Metrics: true,
|
|
|
|
Services: []*core.Service{
|
|
|
|
{
|
|
|
|
Name: "frontend",
|
|
|
|
Group: "core",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "backend",
|
|
|
|
Group: "core",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
watchdog.UpdateServiceStatuses(cfg.Services[0], &core.Result{Success: true, Duration: time.Millisecond, Timestamp: time.Now()})
|
|
|
|
watchdog.UpdateServiceStatuses(cfg.Services[1], &core.Result{Success: false, Duration: time.Second, Timestamp: time.Now()})
|
|
|
|
router := CreateRouter(cfg)
|
|
|
|
type Scenario struct {
|
2021-02-03 05:06:34 +01:00
|
|
|
Name string
|
2021-02-01 07:37:56 +01:00
|
|
|
Path string
|
|
|
|
ExpectedCode int
|
|
|
|
Gzip bool
|
|
|
|
}
|
|
|
|
scenarios := []Scenario{
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "health",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/health",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "metrics",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/metrics",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "badges-1h",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/api/v1/badges/uptime/1h/core_frontend.svg",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "badges-24h",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/api/v1/badges/uptime/24h/core_backend.svg",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "badges-7d",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/api/v1/badges/uptime/7d/core_frontend.svg",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "badges-with-invalid-duration",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/api/v1/badges/uptime/3d/core_backend.svg",
|
|
|
|
ExpectedCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "badges-for-invalid-key",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/api/v1/badges/uptime/7d/invalid_key.svg",
|
|
|
|
ExpectedCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "service-statuses",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/api/v1/statuses",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "service-statuses-gzip",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/api/v1/statuses",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
Gzip: true,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "service-status",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/api/v1/statuses/core_frontend",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "service-status-gzip",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/api/v1/statuses/core_frontend",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
Gzip: true,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "service-status-for-invalid-key",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/api/v1/statuses/invalid_key",
|
|
|
|
ExpectedCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "favicon",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/favicon.ico",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "frontend-home",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "frontend-assets",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/js/app.js",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:06:34 +01:00
|
|
|
Name: "frontend-service",
|
2021-02-01 07:37:56 +01:00
|
|
|
Path: "/services/core_frontend",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
2021-02-03 05:06:34 +01:00
|
|
|
t.Run(scenario.Name, func(t *testing.T) {
|
2021-02-01 07:37:56 +01:00
|
|
|
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 TestHandle(t *testing.T) {
|
|
|
|
cfg := &config.Config{
|
|
|
|
Web: &config.WebConfig{
|
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: rand.Intn(65534),
|
|
|
|
},
|
|
|
|
Services: []*core.Service{
|
|
|
|
{
|
|
|
|
Name: "frontend",
|
|
|
|
Group: "core",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "backend",
|
|
|
|
Group: "core",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
config.Set(cfg)
|
|
|
|
_ = os.Setenv("ROUTER_TEST", "true")
|
|
|
|
_ = os.Setenv("ENVIRONMENT", "dev")
|
|
|
|
Handle()
|
|
|
|
request, _ := http.NewRequest("GET", "/health", nil)
|
|
|
|
responseRecorder := httptest.NewRecorder()
|
|
|
|
server.Handler.ServeHTTP(responseRecorder, request)
|
|
|
|
if responseRecorder.Code != http.StatusOK {
|
|
|
|
t.Error("expected GET /health to return status code 200")
|
|
|
|
}
|
|
|
|
if server == nil {
|
|
|
|
t.Fatal("server should've been set (but because we set ROUTER_TEST, it shouldn't have been started)")
|
|
|
|
}
|
|
|
|
}
|