2018-11-15 12:54:45 +00:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
2018-12-30 21:20:43 +00:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
2018-11-15 12:54:45 +00:00
|
|
|
"testing"
|
2020-04-05 19:36:18 +00:00
|
|
|
"time"
|
2018-11-15 12:54:45 +00:00
|
|
|
|
2018-12-30 21:20:43 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
2018-11-15 12:54:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLifecycle(t *testing.T) {
|
2018-12-30 21:20:43 +00:00
|
|
|
prometheusService := NewPrometheusService(":2112", nil)
|
2018-11-15 12:54:45 +00:00
|
|
|
prometheusService.Start()
|
2020-04-05 19:36:18 +00:00
|
|
|
// Give service time to start.
|
|
|
|
time.Sleep(time.Second)
|
2018-11-15 12:54:45 +00:00
|
|
|
|
2020-04-05 19:36:18 +00:00
|
|
|
// Query the service to ensure it really started.
|
|
|
|
resp, err := http.Get("http://localhost:2112/metrics")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if resp.ContentLength == 0 {
|
|
|
|
t.Error("Unexpected content length 0")
|
|
|
|
}
|
2018-11-15 12:54:45 +00:00
|
|
|
|
|
|
|
prometheusService.Stop()
|
2020-04-05 19:36:18 +00:00
|
|
|
// Give service time to stop.
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
// Query the service to ensure it really stopped.
|
|
|
|
_, err = http.Get("http://localhost:2112/metrics")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Service still running after Stop()")
|
|
|
|
}
|
2018-11-15 12:54:45 +00:00
|
|
|
}
|
2018-12-30 21:20:43 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-01-24 01:26:25 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|