2023-07-09 02:37:41 +02:00
|
|
|
package api
|
2021-09-13 00:39:09 +02:00
|
|
|
|
|
|
|
import (
|
2023-07-09 02:37:41 +02:00
|
|
|
"io"
|
2021-09-13 00:39:09 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"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"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
timestamp = time.Now()
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
testEndpoint = endpoint.Endpoint{
|
2021-09-13 00:39:09 +02:00
|
|
|
Name: "name",
|
|
|
|
Group: "group",
|
|
|
|
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{endpoint.Condition("[STATUS] == 200"), endpoint.Condition("[RESPONSE_TIME] < 500"), endpoint.Condition("[CERTIFICATE_EXPIRATION] < 72h")},
|
2021-09-13 00:39:09 +02:00
|
|
|
Alerts: nil,
|
|
|
|
NumberOfFailuresInARow: 0,
|
|
|
|
NumberOfSuccessesInARow: 0,
|
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
testSuccessfulResult = endpoint.Result{
|
2021-09-13 00:39:09 +02:00
|
|
|
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,
|
2024-05-10 04:56:16 +02:00
|
|
|
ConditionResults: []*endpoint.ConditionResult{
|
2021-09-13 00:39:09 +02:00
|
|
|
{
|
|
|
|
Condition: "[STATUS] == 200",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Condition: "[RESPONSE_TIME] < 500",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Condition: "[CERTIFICATE_EXPIRATION] < 72h",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
testUnsuccessfulResult = endpoint.Result{
|
2021-09-13 00:39:09 +02:00
|
|
|
Hostname: "example.org",
|
|
|
|
IP: "127.0.0.1",
|
|
|
|
HTTPStatus: 200,
|
|
|
|
Errors: []string{"error-1", "error-2"},
|
|
|
|
Connected: true,
|
|
|
|
Success: false,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Duration: 750 * time.Millisecond,
|
|
|
|
CertificateExpiration: 10 * time.Hour,
|
2024-05-10 04:56:16 +02:00
|
|
|
ConditionResults: []*endpoint.ConditionResult{
|
2021-09-13 00:39:09 +02:00
|
|
|
{
|
|
|
|
Condition: "[STATUS] == 200",
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Condition: "[RESPONSE_TIME] < 500",
|
|
|
|
Success: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Condition: "[CERTIFICATE_EXPIRATION] < 72h",
|
|
|
|
Success: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
func TestEndpointStatus(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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
watchdog.UpdateEndpointStatuses(cfg.Endpoints[0], &endpoint.Result{Success: true, Duration: time.Millisecond, Timestamp: time.Now()})
|
|
|
|
watchdog.UpdateEndpointStatuses(cfg.Endpoints[1], &endpoint.Result{Success: 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{
|
|
|
|
{
|
2021-10-23 22:47:12 +02:00
|
|
|
Name: "endpoint-status",
|
|
|
|
Path: "/api/v1/endpoints/core_frontend/statuses",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-10-23 22:47:12 +02:00
|
|
|
Name: "endpoint-status-gzip",
|
|
|
|
Path: "/api/v1/endpoints/core_frontend/statuses",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
Gzip: true,
|
|
|
|
},
|
|
|
|
{
|
2021-10-23 22:47:12 +02:00
|
|
|
Name: "endpoint-status-pagination",
|
|
|
|
Path: "/api/v1/endpoints/core_frontend/statuses?page=1&pageSize=20",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
2021-10-23 22:47:12 +02:00
|
|
|
Name: "endpoint-status-for-invalid-key",
|
|
|
|
Path: "/api/v1/endpoints/invalid_key/statuses",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
func TestEndpointStatuses(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()
|
|
|
|
firstResult := &testSuccessfulResult
|
|
|
|
secondResult := &testUnsuccessfulResult
|
2021-10-29 01:35:46 +02:00
|
|
|
store.Get().Insert(&testEndpoint, firstResult)
|
|
|
|
store.Get().Insert(&testEndpoint, secondResult)
|
2021-09-13 00:39:09 +02:00
|
|
|
// Can't be bothered dealing with timezone issues on the worker that runs the automated tests
|
|
|
|
firstResult.Timestamp = time.Time{}
|
|
|
|
secondResult.Timestamp = time.Time{}
|
2023-07-09 02:37:41 +02:00
|
|
|
api := New(&config.Config{Metrics: true})
|
|
|
|
router := api.Router()
|
2021-09-13 00:39:09 +02:00
|
|
|
type Scenario struct {
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
ExpectedCode int
|
|
|
|
ExpectedBody string
|
|
|
|
}
|
|
|
|
scenarios := []Scenario{
|
|
|
|
{
|
|
|
|
Name: "no-pagination",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/statuses",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
2021-11-05 02:33:13 +01:00
|
|
|
ExpectedBody: `[{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"},{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}]`,
|
2021-09-13 00:39:09 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pagination-first-result",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/statuses?page=1&pageSize=1",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
2021-11-05 02:33:13 +01:00
|
|
|
ExpectedBody: `[{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}]`,
|
2021-09-13 00:39:09 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pagination-second-result",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/statuses?page=2&pageSize=1",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
2021-11-05 02:33:13 +01:00
|
|
|
ExpectedBody: `[{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"}]}]`,
|
2021-09-13 00:39:09 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pagination-no-results",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/statuses?page=5&pageSize=20",
|
2021-09-13 00:39:09 +02:00
|
|
|
ExpectedCode: http.StatusOK,
|
2021-11-05 02:33:13 +01:00
|
|
|
ExpectedBody: `[{"name":"name","group":"group","key":"group_name","results":[]}]`,
|
2021-09-13 00:39:09 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "invalid-pagination-should-fall-back-to-default",
|
2021-10-23 22:47:12 +02:00
|
|
|
Path: "/api/v1/endpoints/statuses?page=INVALID&pageSize=INVALID",
|
|
|
|
ExpectedCode: http.StatusOK,
|
2021-11-05 02:33:13 +01:00
|
|
|
ExpectedBody: `[{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"},{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}]`,
|
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)
|
|
|
|
response, err := router.Test(request)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
body, err := io.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("expected err to be nil, but was", err)
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2023-07-09 02:37:41 +02:00
|
|
|
if string(body) != scenario.ExpectedBody {
|
|
|
|
t.Errorf("expected:\n %s\n\ngot:\n %s", scenario.ExpectedBody, string(body))
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|