-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhealth_test.go
More file actions
113 lines (86 loc) · 2.79 KB
/
Copy pathhealth_test.go
File metadata and controls
113 lines (86 loc) · 2.79 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/hellofresh/health-go/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "modernc.org/sqlite"
)
func init() {
gin.SetMode(gin.TestMode)
}
func TestHealthReq_ReturnsOK(t *testing.T) {
router := gin.New()
router.GET("/health", HealthReq)
req := httptest.NewRequest(http.MethodGet, "/health", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var result health.Check
err := json.Unmarshal(w.Body.Bytes(), &result)
require.NoError(t, err)
assert.Equal(t, health.StatusOK, result.Status)
}
func TestHealth_ReturnsSingleton(t *testing.T) {
h := Health()
assert.NotNil(t, h)
// Should be the same singleton on multiple calls
assert.Equal(t, h, Health())
}
func TestNewConfigServerHealth_WithValidDB(t *testing.T) {
db, err := sql.Open("sqlite", ":memory:")
require.NoError(t, err)
defer func() { _ = db.Close() }()
h := NewConfigServerHealth(db)
assert.NotNil(t, h)
// Measure health — should be OK with a live SQLite connection
result := h.Measure(t.Context())
assert.Equal(t, health.StatusOK, result.Status)
}
func TestNewConfigServerHealth_WithClosedDB(t *testing.T) {
db, err := sql.Open("sqlite", ":memory:")
require.NoError(t, err)
_ = db.Close() // Close immediately to simulate a dead connection
h := NewConfigServerHealth(db)
assert.NotNil(t, h)
// Measure health — should be unhealthy with a closed connection
result := h.Measure(t.Context())
assert.Equal(t, health.StatusUnavailable, result.Status)
}
func TestConfigServerHealthReq_ReturnsOKWithLiveDB(t *testing.T) {
db, err := sql.Open("sqlite", ":memory:")
require.NoError(t, err)
defer func() { _ = db.Close() }()
h := NewConfigServerHealth(db)
router := gin.New()
router.GET("/health", ConfigServerHealthReq(h))
req := httptest.NewRequest(http.MethodGet, "/health", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var result health.Check
err = json.Unmarshal(w.Body.Bytes(), &result)
require.NoError(t, err)
assert.Equal(t, health.StatusOK, result.Status)
}
func TestConfigServerHealthReq_ReturnsUnavailableWithClosedDB(t *testing.T) {
db, err := sql.Open("sqlite", ":memory:")
require.NoError(t, err)
_ = db.Close()
h := NewConfigServerHealth(db)
router := gin.New()
router.GET("/health", ConfigServerHealthReq(h))
req := httptest.NewRequest(http.MethodGet, "/health", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusServiceUnavailable, w.Code)
var result health.Check
err = json.Unmarshal(w.Body.Bytes(), &result)
require.NoError(t, err)
assert.Equal(t, health.StatusUnavailable, result.Status)
}