~bigbes/huntsman

ref: 766fa8055977fbe223afd8a84bcf37a3d13bb1ce huntsman/internal/platform/health/health_test.go -rw-r--r-- 2.5 KiB
766fa805 — Eugene Blikh Add BSD 2-Clause license 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package health

import (
	"context"
	"encoding/json"
	"errors"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestHealthzAlwaysOK(t *testing.T) {
	h := New()
	rr := httptest.NewRecorder()
	h.Healthz(rr, httptest.NewRequestWithContext(t.Context(), "GET", "/healthz", http.NoBody))

	if rr.Code != 200 {
		t.Errorf("status = %d", rr.Code)
	}
	if rr.Body.String() != "ok" {
		t.Errorf("body = %q", rr.Body.String())
	}
}

func TestReadyzNoCheckersIsOK(t *testing.T) {
	h := New()
	rr := httptest.NewRecorder()
	h.Readyz(rr, httptest.NewRequestWithContext(t.Context(), "GET", "/readyz", http.NoBody))

	if rr.Code != 200 {
		t.Errorf("status = %d", rr.Code)
	}

	var body map[string]string
	if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil {
		t.Fatalf("decode: %v", err)
	}
	if len(body) != 0 {
		t.Errorf("expected empty results, got %v", body)
	}
}

func TestReadyzAllPass(t *testing.T) {
	h := New()
	h.Register("db", CheckerFunc(func(_ context.Context) error { return nil }))
	h.Register("cache", CheckerFunc(func(_ context.Context) error { return nil }))

	rr := httptest.NewRecorder()
	h.Readyz(rr, httptest.NewRequestWithContext(t.Context(), "GET", "/readyz", http.NoBody))

	if rr.Code != 200 {
		t.Errorf("status = %d", rr.Code)
	}
	var body map[string]string
	if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil {
		t.Fatalf("decode: %v", err)
	}
	if body["db"] != "ok" || body["cache"] != "ok" {
		t.Errorf("body = %v", body)
	}
}

func TestReadyzOneFails(t *testing.T) {
	h := New()
	h.Register("db", CheckerFunc(func(_ context.Context) error { return nil }))
	h.Register("upstream", CheckerFunc(func(_ context.Context) error { return errors.New("dns fail") }))

	rr := httptest.NewRecorder()
	h.Readyz(rr, httptest.NewRequestWithContext(t.Context(), "GET", "/readyz", http.NoBody))

	if rr.Code != 503 {
		t.Errorf("status = %d, want 503", rr.Code)
	}
	var body map[string]string
	if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil {
		t.Fatalf("decode: %v", err)
	}
	if body["db"] != "ok" {
		t.Errorf("db = %q", body["db"])
	}
	if body["upstream"] != "dns fail" {
		t.Errorf("upstream = %q", body["upstream"])
	}
}

func TestRegisterReplaces(t *testing.T) {
	h := New()
	h.Register("svc", CheckerFunc(func(_ context.Context) error { return errors.New("first") }))
	h.Register("svc", CheckerFunc(func(_ context.Context) error { return nil }))

	rr := httptest.NewRecorder()
	h.Readyz(rr, httptest.NewRequestWithContext(t.Context(), "GET", "/readyz", http.NoBody))
	if rr.Code != 200 {
		t.Errorf("status = %d, want 200 after replace", rr.Code)
	}
}