-
Notifications
You must be signed in to change notification settings - Fork 7
/
server_test.go
288 lines (222 loc) · 7.47 KB
/
server_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package balancer
import (
"database/sql"
"database/sql/driver"
"errors"
"regexp"
"testing"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/go-gorp/gorp"
. "github.com/smartystreets/goconvey/convey"
)
type LoggerMock struct {
ErrCalled map[error]bool
}
func (l *LoggerMock) Errorf(format string, args ...interface{}) {}
func (LoggerMock) Printf(format string, v ...interface{}) {}
func (l *LoggerMock) Error(args ...interface{}) {
for _, err := range args {
if err, ok := err.(error); ok {
l.ErrCalled[err] = true
}
}
}
func newLoggerMock() *LoggerMock {
return &LoggerMock{
ErrCalled: make(map[error]bool),
}
}
func getMock(t *testing.T) (*gorp.DbMap, sqlmock.Sqlmock) {
t.Helper()
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
dbMap := &gorp.DbMap{
Db: db,
Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"},
}
return dbMap, mock
}
func mockHealthQueries(t *testing.T, mock sqlmock.Sqlmock, ioStatus, secondsBehindMaster, openConnections, runningConnections driver.Value) {
t.Helper()
mock.ExpectQuery("SHOW STATUS LIKE 'Slave_running'").WillReturnRows(
sqlmock.NewRows([]string{"Value"}).AddRow(ioStatus))
mock.ExpectQuery("SHOW STATUS LIKE 'Threads_connected'").WillReturnRows(
sqlmock.NewRows([]string{"Value"}).AddRow(openConnections))
mock.ExpectQuery("SHOW STATUS LIKE 'Threads_running'").WillReturnRows(
sqlmock.NewRows([]string{"Value"}).AddRow(runningConnections))
mock.ExpectQuery("SHOW SLAVE STATUS").WillReturnRows(
sqlmock.NewRows([]string{"Seconds_Behind_Master"}).AddRow(secondsBehindMaster))
}
func TestServerAttributes(t *testing.T) {
Convey("When a valid server is given", t, func() {
expectedHealth := new(ServerHealth)
expectedConnections := new(gorp.DbMap)
server := Server{
name: "server_name",
health: expectedHealth,
connection: expectedConnections,
}
Convey("It should return the correct attributes", func() {
So(server.GetName(), ShouldEqual, "server_name")
So(server.GetConnection(), ShouldEqual, expectedConnections)
So(server.GetHealth(), ShouldEqual, expectedHealth)
})
})
}
func TestRawQuery(t *testing.T) {
Convey("Given a valid connection", t, func() {
db, mock := getMock(t)
server := Server{connection: db, replicationConnection: db}
logger := newLoggerMock()
Convey("When a valid query is given", func() {
query := "SHOW SLAVE STATUS"
mock.ExpectQuery(query).WillReturnRows(
sqlmock.NewRows([]string{"Seconds_Behind_Master"}).AddRow(1))
results, err := server.rawQuery(query, logger)
Convey("It should return the expected result", func() {
So(err, ShouldBeNil)
So(results, ShouldHaveLength, 1)
So(results["Seconds_Behind_Master"], ShouldEqual, "1")
mock.ExpectationsWereMet()
})
})
Convey("When an empty result is given", func() {
query := "SHOW * FROM None"
mock.ExpectQuery(regexp.QuoteMeta(query)).WillReturnRows(
sqlmock.NewRows([]string{"Foo"}))
results, err := server.rawQuery(query, logger)
Convey("It should fail with ErrNoRows", func() {
So(err, ShouldEqual, sql.ErrNoRows)
So(results, ShouldHaveLength, 0)
mock.ExpectationsWereMet()
})
})
Convey("When database connection fail", func() {
query := "SHOW * FROM Fail"
expectedError := errors.New("fail")
mock.ExpectQuery(regexp.QuoteMeta(query)).WillReturnError(expectedError)
results, err := server.rawQuery(query, logger)
Convey("It should fail with expected error", func() {
So(err, ShouldEqual, expectedError)
So(results, ShouldHaveLength, 0)
mock.ExpectationsWereMet()
})
})
Convey("When rows fail on close", func() {
query := "SHOW * FROM Fail"
expectedError := errors.New("fail")
mock.ExpectQuery(regexp.QuoteMeta(query)).WillReturnRows(
sqlmock.NewRows([]string{"Foo"}).
AddRow(1).CloseError(expectedError))
results, err := server.rawQuery(query, logger)
Convey("It should fail with expected error", func() {
So(err, ShouldBeNil)
So(results, ShouldHaveLength, 1)
So(logger.ErrCalled[expectedError], ShouldBeTrue)
mock.ExpectationsWereMet()
})
})
})
}
func TestCheckHealth(t *testing.T) {
Convey("Given a valid server", t, func() {
db, mock := getMock(t)
logger := newLoggerMock()
health := new(ServerHealth)
server := Server{
connection: db,
replicationConnection: db,
health: health,
}
Convey("When everything is ok", func() {
mockHealthQueries(t, mock, "ON", 0, 2, 1)
Convey("It should succeed without errors", func() {
server.CheckHealth(false, logger)
So(health.up, ShouldBeTrue)
So(health.ioRunning, ShouldBeTrue)
So(health.runningConnections, ShouldNotBeNil)
So(*health.runningConnections, ShouldEqual, 1)
So(health.openConnections, ShouldNotBeNil)
So(*health.openConnections, ShouldEqual, 2)
So(health.secondsBehindMaster, ShouldNotBeNil)
So(*health.secondsBehindMaster, ShouldEqual, 0)
So(mock.ExpectationsWereMet(), ShouldBeNil)
})
})
Convey("When slave status are empty", func() {
mockHealthQueries(t, mock, "ON", nil, 2, 1)
Convey("It should set error on check", func() {
server.CheckHealth(false, logger)
So(health.up, ShouldBeTrue)
So(health.err, ShouldNotBeNil)
So(health.err.Error(), ShouldContainSubstring, "empty or null value for Seconds_Behind_Master")
mock.ExpectationsWereMet()
})
})
Convey("When openConnections are empty", func() {
mockHealthQueries(t, mock, "ON", 0, nil, 1)
Convey("It should set error on check", func() {
server.CheckHealth(false, logger)
So(health.up, ShouldBeTrue)
So(health.err, ShouldNotBeNil)
So(health.err.Error(), ShouldContainSubstring, "unexpected value for Threads_connected")
mock.ExpectationsWereMet()
})
})
Convey("When runningConnections are empty", func() {
mockHealthQueries(t, mock, "ON", 0, 1, nil)
Convey("It should set error on check", func() {
server.CheckHealth(false, logger)
So(health.up, ShouldBeTrue)
So(health.err, ShouldNotBeNil)
So(health.err.Error(), ShouldContainSubstring, "unexpected value for Threads_running")
mock.ExpectationsWereMet()
})
})
Convey("When IO is not running", func() {
mockHealthQueries(t, mock, "OFF", 0, 1, 1)
Convey("It should set io running false", func() {
server.CheckHealth(false, logger)
So(health.up, ShouldBeTrue)
So(health.err, ShouldBeNil)
So(health.ioRunning, ShouldBeFalse)
mock.ExpectationsWereMet()
})
})
})
Convey("Given a invalid server", t, func() {
logger := newLoggerMock()
health := new(ServerHealth)
server := Server{
connection: nil,
replicationConnection: nil,
health: health,
}
Convey("When slave connection is nil", func() {
Convey("It should fail with server down and errors", func() {
server.CheckHealth(false, logger)
So(health.up, ShouldBeFalse)
So(health.err, ShouldNotBeNil)
})
})
})
Convey("Given a valid server without replication connection", t, func() {
db, _ := getMock(t)
logger := newLoggerMock()
health := new(ServerHealth)
server := Server{
connection: db,
replicationConnection: nil,
health: health,
}
Convey("When slave connection is nil", func() {
Convey("It should succeed with server down and errors", func() {
server.CheckHealth(false, logger)
So(health.up, ShouldBeTrue)
So(health.err, ShouldNotBeNil)
})
})
})
}