snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s

This commit is contained in:
upstream-mirror
2026-09-18 01:22:23 +05:00
commit 7e385fc126
8391 changed files with 1766236 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
package healthcheck
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"sync"
)
type (
checkFn func(ctx context.Context) error
Meta struct {
Label string
Description string
}
check struct {
fn checkFn
*Meta
}
result struct {
err error
*Meta
}
results []*result
checks struct {
l sync.RWMutex
cc []*check
}
)
var (
defaults *checks
)
func init() {
defaults = New()
}
func Defaults() *checks {
return defaults
}
func New() *checks {
return &checks{cc: []*check{}}
}
// Add appends new check
func (c *checks) Add(fn checkFn, label string, description ...string) {
c.l.Lock()
defer c.l.Unlock()
c.cc = append(c.cc, &check{fn, &Meta{Label: label, Description: strings.Join(description, "")}})
}
func (c *checks) Run(ctx context.Context) results {
c.l.RLock()
defer c.l.RUnlock()
var rr = make([]*result, len(c.cc))
for i, c := range c.cc {
rr[i] = &result{c.fn(ctx), c.Meta}
}
return rr
}
func (rr results) Healthy() bool {
for _, c := range rr {
if c.err != nil {
return false
}
}
return true
}
func (rr results) String() string {
buf := &bytes.Buffer{}
rr.WriteTo(buf)
return buf.String()
}
func (rr results) WriteTo(w io.Writer) (int64, error) {
var (
p = func(f string, aa ...interface{}) {
_, _ = fmt.Fprintf(w, f, aa...)
}
)
for _, r := range rr {
if r.IsHealthy() {
p("PASS")
} else {
p("FAIL")
}
p(" %s", r.Label)
if !r.IsHealthy() {
p(": %v", r.Error())
}
p("\n")
}
return 0, nil
}
func (r *result) IsHealthy() bool {
return r != nil && r.err == nil
}
func (r *result) Error() string {
if r == nil || r.err == nil {
return ""
}
return r.err.Error()
}

View File

@@ -0,0 +1,82 @@
package healthcheck
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Healthy(t *testing.T) {
tests := []struct {
name string
checks []*check
healthy bool
string string
}{
{
"should be healthy with handle for stringer output",
[]*check{{func(ctx context.Context) error { return nil }, &Meta{Label: "check01"}}},
true,
"PASS check01\n",
},
{
"should handle multiple healthy checks",
[]*check{
{func(ctx context.Context) error { return nil }, &Meta{Label: "check01"}},
{func(ctx context.Context) error { return nil }, &Meta{Label: "check02"}},
},
true,
"PASS check01\nPASS check02\n",
},
{
"should handle healthy and unhealthy checks",
[]*check{
{func(ctx context.Context) error { return nil }, &Meta{Label: "check01"}},
{func(ctx context.Context) error { return fmt.Errorf("x") }, &Meta{Label: "check02"}},
{func(ctx context.Context) error { return nil }, &Meta{Label: "check03"}},
},
false,
"PASS check01\nFAIL check02: x\nPASS check03\n",
},
{
"should handle labels",
[]*check{
{func(ctx context.Context) error { return nil }, &Meta{Label: "check01"}},
{func(ctx context.Context) error { return nil }, &Meta{Label: "Pretty check"}},
},
true,
"PASS check01\nPASS Pretty check\n",
},
{
"should handle empty check list",
[]*check{},
true,
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := assert.New(t)
r := (&checks{cc: tt.checks}).Run(context.Background())
a.Equal(tt.healthy, r.Healthy(), "healthy result failed")
a.Equal(tt.string, r.String(), "stringer output match failed")
})
}
}
// tested with
// go test -count 10 -race -run Test_DataRace ./pkg/healthcheck/...
func Test_DataRace(t *testing.T) {
hc := New()
go func() {
hc.Add(func(ctx context.Context) error { return nil }, "hc1")
hc.Add(func(ctx context.Context) error { return nil }, "hc2")
hc.Add(func(ctx context.Context) error { return nil }, "hc3")
}()
go hc.Run(context.Background())
}

View File

@@ -0,0 +1,16 @@
package healthcheck
import "net/http"
func HttpHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
results := Defaults().Run(r.Context())
if results.Healthy() {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
results.WriteTo(w)
}
}