mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-25 14:35:40 +00:00
Add health-check test
This commit is contained in:
parent
d2ff8f8720
commit
84f5115bd1
|
@ -19,7 +19,7 @@ import (
|
||||||
type status string
|
type status string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// pass healthy (acceptable aliases: "ok" to support Node's Terminus and "up" for Java's SpringBoot)
|
// Pass healthy (acceptable aliases: "ok" to support Node's Terminus and "up" for Java's SpringBoot)
|
||||||
// fail unhealthy (acceptable aliases: "error" to support Node's Terminus and "down" for Java's SpringBoot), and
|
// fail unhealthy (acceptable aliases: "error" to support Node's Terminus and "down" for Java's SpringBoot), and
|
||||||
// warn healthy, with some concerns.
|
// warn healthy, with some concerns.
|
||||||
//
|
//
|
||||||
|
@ -27,19 +27,19 @@ const (
|
||||||
// status: (required) indicates whether the service status is acceptable
|
// status: (required) indicates whether the service status is acceptable
|
||||||
// or not. API publishers SHOULD use following values for the field:
|
// or not. API publishers SHOULD use following values for the field:
|
||||||
// The value of the status field is case-insensitive and is tightly
|
// The value of the status field is case-insensitive and is tightly
|
||||||
// related with the HTTP response code returned by the health endpoint.
|
// related with the HTTP Response code returned by the health endpoint.
|
||||||
// For "pass" status, HTTP response code in the 2xx-3xx range MUST be
|
// For "pass" status, HTTP Response code in the 2xx-3xx range MUST be
|
||||||
// used. For "fail" status, HTTP response code in the 4xx-5xx range
|
// used. For "fail" status, HTTP Response code in the 4xx-5xx range
|
||||||
// MUST be used. In case of the "warn" status, endpoints MUST return
|
// MUST be used. In case of the "warn" status, endpoints MUST return
|
||||||
// HTTP status in the 2xx-3xx range, and additional information SHOULD
|
// HTTP status in the 2xx-3xx range, and additional information SHOULD
|
||||||
// be provided, utilizing optional fields of the response.
|
// be provided, utilizing optional fields of the Response.
|
||||||
pass status = "pass"
|
Pass status = "pass"
|
||||||
fail status = "fail"
|
Fail status = "fail"
|
||||||
warn status = "warn"
|
warn status = "warn"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s status) ToHTTPStatus() int {
|
func (s status) ToHTTPStatus() int {
|
||||||
if s == pass || s == warn {
|
if s == Pass || s == warn {
|
||||||
return http.StatusOK
|
return http.StatusOK
|
||||||
}
|
}
|
||||||
return http.StatusFailedDependency
|
return http.StatusFailedDependency
|
||||||
|
@ -47,8 +47,8 @@ func (s status) ToHTTPStatus() int {
|
||||||
|
|
||||||
type checks map[string][]componentStatus
|
type checks map[string][]componentStatus
|
||||||
|
|
||||||
// response is the data returned by the health endpoint, which will be marshaled to JSON format
|
// Response is the data returned by the health endpoint, which will be marshaled to JSON format
|
||||||
type response struct {
|
type Response struct {
|
||||||
Status status `json:"status"`
|
Status status `json:"status"`
|
||||||
Description string `json:"description"` // a human-friendly description of the service
|
Description string `json:"description"` // a human-friendly description of the service
|
||||||
Checks checks `json:"checks,omitempty"` // The Checks Object, should be omitted on installation route
|
Checks checks `json:"checks,omitempty"` // The Checks Object, should be omitted on installation route
|
||||||
|
@ -65,8 +65,8 @@ type componentStatus struct {
|
||||||
|
|
||||||
// Check is the health check API handler
|
// Check is the health check API handler
|
||||||
func Check(w http.ResponseWriter, r *http.Request) {
|
func Check(w http.ResponseWriter, r *http.Request) {
|
||||||
rsp := response{
|
rsp := Response{
|
||||||
Status: pass,
|
Status: Pass,
|
||||||
Description: setting.AppName,
|
Description: setting.AppName,
|
||||||
Checks: make(checks),
|
Checks: make(checks),
|
||||||
}
|
}
|
||||||
|
@ -77,8 +77,8 @@ func Check(w http.ResponseWriter, r *http.Request) {
|
||||||
statuses = append(statuses, checkCache(rsp.Checks))
|
statuses = append(statuses, checkCache(rsp.Checks))
|
||||||
}
|
}
|
||||||
for _, s := range statuses {
|
for _, s := range statuses {
|
||||||
if s != pass {
|
if s != Pass {
|
||||||
rsp.Status = fail
|
rsp.Status = Fail
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,22 +94,22 @@ func Check(w http.ResponseWriter, r *http.Request) {
|
||||||
func checkDatabase(ctx context.Context, checks checks) status {
|
func checkDatabase(ctx context.Context, checks checks) status {
|
||||||
st := componentStatus{}
|
st := componentStatus{}
|
||||||
if err := db.GetEngine(ctx).Ping(); err != nil {
|
if err := db.GetEngine(ctx).Ping(); err != nil {
|
||||||
st.Status = fail
|
st.Status = Fail
|
||||||
st.Time = getCheckTime()
|
st.Time = getCheckTime()
|
||||||
log.Error("database ping failed with error: %v", err)
|
log.Error("database ping failed with error: %v", err)
|
||||||
} else {
|
} else {
|
||||||
st.Status = pass
|
st.Status = Pass
|
||||||
st.Time = getCheckTime()
|
st.Time = getCheckTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
if setting.Database.Type.IsSQLite3() && st.Status == pass {
|
if setting.Database.Type.IsSQLite3() && st.Status == Pass {
|
||||||
if !setting.EnableSQLite3 {
|
if !setting.EnableSQLite3 {
|
||||||
st.Status = fail
|
st.Status = Fail
|
||||||
st.Time = getCheckTime()
|
st.Time = getCheckTime()
|
||||||
log.Error("SQLite3 health check failed with error: %v", "this Forgejo binary is built without SQLite3 enabled")
|
log.Error("SQLite3 health check failed with error: %v", "this Forgejo binary is built without SQLite3 enabled")
|
||||||
} else {
|
} else {
|
||||||
if _, err := os.Stat(setting.Database.Path); err != nil {
|
if _, err := os.Stat(setting.Database.Path); err != nil {
|
||||||
st.Status = fail
|
st.Status = Fail
|
||||||
st.Time = getCheckTime()
|
st.Time = getCheckTime()
|
||||||
log.Error("SQLite3 file exists check failed with error: %v", err)
|
log.Error("SQLite3 file exists check failed with error: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -124,11 +124,11 @@ func checkDatabase(ctx context.Context, checks checks) status {
|
||||||
func checkCache(checks checks) status {
|
func checkCache(checks checks) status {
|
||||||
st := componentStatus{}
|
st := componentStatus{}
|
||||||
if err := cache.GetCache().Ping(); err != nil {
|
if err := cache.GetCache().Ping(); err != nil {
|
||||||
st.Status = fail
|
st.Status = Fail
|
||||||
st.Time = getCheckTime()
|
st.Time = getCheckTime()
|
||||||
log.Error("cache ping failed with error: %v", err)
|
log.Error("cache ping failed with error: %v", err)
|
||||||
} else {
|
} else {
|
||||||
st.Status = pass
|
st.Status = Pass
|
||||||
st.Time = getCheckTime()
|
st.Time = getCheckTime()
|
||||||
}
|
}
|
||||||
checks["cache:ping"] = []componentStatus{st}
|
checks["cache:ping"] = []componentStatus{st}
|
||||||
|
|
28
tests/integration/api_health_test.go
Normal file
28
tests/integration/api_health_test.go
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package integration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
"code.gitea.io/gitea/routers/web/healthcheck"
|
||||||
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestApiHeatlhCheck(t *testing.T) {
|
||||||
|
defer tests.PrepareTestEnv(t)()
|
||||||
|
t.Run("Test health-check pass", func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
|
|
||||||
|
req := NewRequest(t, "GET", "/api/healthz")
|
||||||
|
resp := MakeRequest(t, req, http.StatusOK)
|
||||||
|
assert.Contains(t, resp.Header().Values("Cache-Control"), "no-store")
|
||||||
|
|
||||||
|
var status healthcheck.Response
|
||||||
|
DecodeJSON(t, resp, &status)
|
||||||
|
assert.Equal(t, healthcheck.Pass, status.Status)
|
||||||
|
assert.Equal(t, setting.AppName, status.Description)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue