mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-21 15:33:17 +01:00
9d151fcdb4
* refactor: Partially break core package into dns, result and ssh packages * refactor: Move core package to config/endpoint * refactor: Fix warning about overlapping imported package name with endpoint variable * refactor: Rename EndpointStatus to Status * refactor: Merge result pkg back into endpoint pkg, because it makes more sense * refactor: Rename parameter r to result in Condition.evaluate * refactor: Rename parameter r to result * refactor: Revert accidental change to endpoint.TypeDNS * refactor: Rename parameter r to result * refactor: Merge util package into endpoint package * refactor: Rename parameter r to result
103 lines
2.6 KiB
Go
103 lines
2.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"math/rand"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/TwiN/gatus/v5/config"
|
|
"github.com/TwiN/gatus/v5/config/endpoint"
|
|
"github.com/TwiN/gatus/v5/config/web"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func TestHandle(t *testing.T) {
|
|
cfg := &config.Config{
|
|
Web: &web.Config{
|
|
Address: "0.0.0.0",
|
|
Port: rand.Intn(65534),
|
|
},
|
|
Endpoints: []*endpoint.Endpoint{
|
|
{
|
|
Name: "frontend",
|
|
Group: "core",
|
|
},
|
|
{
|
|
Name: "backend",
|
|
Group: "core",
|
|
},
|
|
},
|
|
}
|
|
_ = os.Setenv("ROUTER_TEST", "true")
|
|
_ = os.Setenv("ENVIRONMENT", "dev")
|
|
defer os.Clearenv()
|
|
Handle(cfg)
|
|
defer Shutdown()
|
|
request := httptest.NewRequest("GET", "/health", http.NoBody)
|
|
response, err := app.Test(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.StatusCode != 200 {
|
|
t.Error("expected GET /health to return status code 200")
|
|
}
|
|
if app == nil {
|
|
t.Fatal("server should've been set (but because we set ROUTER_TEST, it shouldn't have been started)")
|
|
}
|
|
}
|
|
|
|
func TestHandleTLS(t *testing.T) {
|
|
scenarios := []struct {
|
|
name string
|
|
tls *web.TLSConfig
|
|
expectedStatusCode int
|
|
}{
|
|
{
|
|
name: "good-tls-config",
|
|
tls: &web.TLSConfig{CertificateFile: "../testdata/cert.pem", PrivateKeyFile: "../testdata/cert.key"},
|
|
expectedStatusCode: 200,
|
|
},
|
|
}
|
|
for _, scenario := range scenarios {
|
|
t.Run(scenario.name, func(t *testing.T) {
|
|
cfg := &config.Config{
|
|
Web: &web.Config{Address: "0.0.0.0", Port: rand.Intn(65534), TLS: scenario.tls},
|
|
Endpoints: []*endpoint.Endpoint{
|
|
{Name: "frontend", Group: "core"},
|
|
{Name: "backend", Group: "core"},
|
|
},
|
|
}
|
|
if err := cfg.Web.ValidateAndSetDefaults(); err != nil {
|
|
t.Error("expected no error from web (TLS) validation, got", err)
|
|
}
|
|
_ = os.Setenv("ROUTER_TEST", "true")
|
|
_ = os.Setenv("ENVIRONMENT", "dev")
|
|
defer os.Clearenv()
|
|
Handle(cfg)
|
|
defer Shutdown()
|
|
request := httptest.NewRequest("GET", "/health", http.NoBody)
|
|
response, err := app.Test(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.StatusCode != scenario.expectedStatusCode {
|
|
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, scenario.expectedStatusCode, response.StatusCode)
|
|
}
|
|
if app == nil {
|
|
t.Fatal("server should've been set (but because we set ROUTER_TEST, it shouldn't have been started)")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestShutdown(t *testing.T) {
|
|
// Pretend that we called controller.Handle(), which initializes the server variable
|
|
app = fiber.New()
|
|
Shutdown()
|
|
if app != nil {
|
|
t.Error("server should've been shut down")
|
|
}
|
|
}
|