Skip to content

Commit cbae9e1

Browse files
committed
Unit tests for healtcheck service
Unit tests for healtcheck service.
1 parent 03fab18 commit cbae9e1

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

pkg/health/health.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ func (h *Listener) healthCheckReply(w http.ResponseWriter, r *http.Request) {
3333
}
3434

3535
// Start exposes an http healthcheck handler
36-
func (h *Listener) Start() (*Listener, error) {
36+
func (h *Listener) Start() *Listener {
3737
if h.config.HealthPort == 0 {
38-
return h, nil
38+
return h
3939
}
4040

4141
h.config.Logger.Info("Starting http healtcheck handler")
@@ -52,7 +52,7 @@ func (h *Listener) Start() (*Listener, error) {
5252
}
5353
}()
5454

55-
return h, nil
55+
return h
5656
}
5757

5858
// Stop halts the http health check handler

pkg/health/health_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package health
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/sirupsen/logrus"
9+
"github.com/sirupsen/logrus/hooks/test"
10+
11+
"github.com/bpineau/katafygio/config"
12+
"github.com/bpineau/katafygio/pkg/log"
13+
)
14+
15+
func TestNoopHealth(t *testing.T) {
16+
17+
conf := &config.KfConfig{
18+
Logger: log.New("error", "", "test"),
19+
HealthPort: 0,
20+
}
21+
22+
// shouldn't panic with 0 as port
23+
hc := New(conf)
24+
_ = hc.Start()
25+
hc.Stop()
26+
27+
conf.HealthPort = -42
28+
hc = New(conf)
29+
_ = hc.Start()
30+
hc.Stop()
31+
hook := hc.config.Logger.Hooks[logrus.InfoLevel][0].(*test.Hook)
32+
if len(hook.Entries) != 1 {
33+
t.Error("Failed to log an issue with a bogus port")
34+
}
35+
}
36+
37+
func TestHealthCheck(t *testing.T) {
38+
conf := &config.KfConfig{
39+
Logger: log.New("info", "", "test"),
40+
HealthPort: 0,
41+
}
42+
43+
hc := New(conf)
44+
45+
req, err := http.NewRequest("GET", "/health", nil)
46+
if err != nil {
47+
t.Error(err)
48+
}
49+
50+
rr := httptest.NewRecorder()
51+
52+
handler := http.HandlerFunc(hc.healthCheckReply)
53+
handler.ServeHTTP(rr, req)
54+
55+
if status := rr.Code; status != http.StatusOK {
56+
t.Errorf("healthCheckReply handler didn't return an HTTP 200 status code")
57+
}
58+
}

pkg/run/run.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ func Run(config *config.KfConfig) {
2626
evts := event.New()
2727
reco := recorder.New(config, evts).Start()
2828
obsv := observer.New(config, evts, &controller.Factory{}).Start()
29-
30-
http, err := health.New(config).Start()
31-
if err != nil {
32-
config.Logger.Fatalf("failed to start http healtcheck handler: %v", err)
33-
}
29+
http := health.New(config).Start()
3430

3531
sigterm := make(chan os.Signal, 1)
3632
signal.Notify(sigterm, syscall.SIGTERM)

0 commit comments

Comments
 (0)