mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-27 21:57:16 +00:00
d9c0e65cef
* info logs beacon node improvements * prom test fixes * info logging changes * wrapped up node info logging * changed to debug level * warn logs taken care of * Terence suggestion * warn spacing * better logging in initial sync * debug level standardized * complete debug standardization * participation at epoch end * fix archive tests * even more test fixes * prom test * ops test * powtest * rpc sync test * rem part * log formatting
95 lines
2.0 KiB
Go
95 lines
2.0 KiB
Go
package prometheus
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
)
|
|
|
|
func TestLifecycle(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
prometheusService := NewPrometheusService(":2112", nil)
|
|
|
|
prometheusService.Start()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Collecting metrics")
|
|
|
|
prometheusService.Stop()
|
|
}
|
|
|
|
type mockService struct {
|
|
status error
|
|
}
|
|
|
|
func (m *mockService) Start() {
|
|
}
|
|
|
|
func (m *mockService) Stop() error {
|
|
return nil
|
|
}
|
|
|
|
func (m *mockService) Status() error {
|
|
return m.status
|
|
}
|
|
|
|
func TestHealthz(t *testing.T) {
|
|
registry := shared.NewServiceRegistry()
|
|
m := &mockService{}
|
|
if err := registry.RegisterService(m); err != nil {
|
|
t.Fatalf("failed to registry service %v", err)
|
|
}
|
|
s := NewPrometheusService("" /*addr*/, registry)
|
|
|
|
req, err := http.NewRequest("GET", "/healthz", nil /*reader*/)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
handler := http.HandlerFunc(s.healthzHandler)
|
|
|
|
rr := httptest.NewRecorder()
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
t.Errorf("expected OK status but got %v", rr.Code)
|
|
}
|
|
|
|
body := rr.Body.String()
|
|
if !strings.Contains(body, "*prometheus.mockService: OK") {
|
|
t.Errorf("Expected body to contain mockService status, but got %v", body)
|
|
}
|
|
|
|
m.status = errors.New("something really bad has happened")
|
|
|
|
rr = httptest.NewRecorder()
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
if status := rr.Code; status != http.StatusInternalServerError {
|
|
t.Errorf("expected error status but got %v", rr.Code)
|
|
}
|
|
|
|
body = rr.Body.String()
|
|
if !strings.Contains(
|
|
body,
|
|
"*prometheus.mockService: ERROR something really bad has happened",
|
|
) {
|
|
t.Errorf("Expected body to contain mockService status, but got %v", body)
|
|
}
|
|
|
|
}
|
|
|
|
func TestStatus(t *testing.T) {
|
|
failError := errors.New("failure")
|
|
s := &Service{failStatus: failError}
|
|
|
|
if err := s.Status(); err != s.failStatus {
|
|
t.Errorf("Wanted: %v, got: %v", s.failStatus, s.Status())
|
|
}
|
|
}
|