-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthcheck_test.go
94 lines (85 loc) · 3.02 KB
/
healthcheck_test.go
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
package http_handlers
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/surahman/mcq-platform/pkg/cassandra"
http_common "github.com/surahman/mcq-platform/pkg/http"
"github.com/surahman/mcq-platform/pkg/mocks"
model_rest "github.com/surahman/mcq-platform/pkg/model/http"
)
func TestHealthcheck(t *testing.T) {
router := http_common.GetTestRouter()
testCases := []struct {
name string
path string
expectedMsg string
expectedStatus int
cassandraHealthData *http_common.MockCassandraData
redisHealthData *http_common.MockRedisData
}{
// ----- test cases start ----- //
{
name: "cassandra failure",
path: "/healthcheck/cassandra-failure",
expectedMsg: "Cassandra",
expectedStatus: http.StatusServiceUnavailable,
cassandraHealthData: &http_common.MockCassandraData{
OutputErr: &cassandra.Error{
Message: "Cassandra failure",
Status: http.StatusInternalServerError,
},
Times: 1,
},
redisHealthData: &http_common.MockRedisData{Times: 0},
}, {
name: "redis failure",
path: "/healthcheck/redis-failure",
expectedMsg: "Redis",
expectedStatus: http.StatusServiceUnavailable,
cassandraHealthData: &http_common.MockCassandraData{Times: 1},
redisHealthData: &http_common.MockRedisData{
Err: errors.New("Redis failure"),
Times: 1,
},
}, {
name: "success",
path: "/healthcheck/success",
expectedMsg: "healthy",
expectedStatus: http.StatusOK,
cassandraHealthData: &http_common.MockCassandraData{Times: 1},
redisHealthData: &http_common.MockRedisData{Times: 1},
},
// ----- test cases end ----- //
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
// Mock configurations.
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockCassandra := mocks.NewMockCassandra(mockCtrl)
mockRedis := mocks.NewMockRedis(mockCtrl)
mockCassandra.EXPECT().Execute(gomock.Any(), gomock.Any()).Return(
testCase.cassandraHealthData.OutputParam,
testCase.cassandraHealthData.OutputErr,
).Times(testCase.cassandraHealthData.Times)
mockRedis.EXPECT().Healthcheck().Return(
testCase.redisHealthData.Err,
).Times(testCase.redisHealthData.Times)
// Endpoint setup for test.
router.GET(testCase.path, Healthcheck(zapLogger, mockCassandra, mockRedis))
req, _ := http.NewRequest("GET", testCase.path, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
// Verify responses
require.Equal(t, testCase.expectedStatus, w.Code, "expected status codes do not match")
response := model_rest.Success{}
require.NoError(t, json.NewDecoder(w.Body).Decode(&response), "failed to unmarshall response body.")
require.Containsf(t, response.Message, testCase.expectedMsg, "got incorrect message %s", response.Message)
})
}
}