-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
213 lines (191 loc) · 3.99 KB
/
main_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
package main
import (
"bytes"
"errors"
"fmt"
"os"
"strconv"
"testing"
)
type MockLogger struct {
buf *bytes.Buffer
}
func (ml *MockLogger) Print(v ...interface{}) {
var err error
for _, i := range v {
switch v := i.(type) {
case string:
_, err = ml.buf.WriteString(v)
case error:
_, err = ml.buf.WriteString(v.Error())
}
if err != nil {
fmt.Println(err)
}
}
}
type MockSqlDB struct {
PingFunc func() error
Pinged int
}
func (m *MockSqlDB) Ping() error {
return m.PingFunc()
}
type MockConnection struct {
OpenFunc func() (SqlDB, error)
}
func (m *MockConnection) Open(driverName, dataSourceName string) (SqlDB, error) {
return m.OpenFunc()
}
type MockDelayer struct {
i int
DelayFunc func()
}
func (m *MockDelayer) Delay() {
m.DelayFunc()
}
func TestMain_exitNonZeroWithoutEnvVar(t *testing.T) {
os.Setenv("PINGLIMIT", "1")
var buf bytes.Buffer
expected := 1
db := &MockSqlDB{
PingFunc: func() error {
return nil
},
}
c := &MockConnection{
OpenFunc: func() (SqlDB, error) {
return db, nil
},
}
dly := &MockDelayer{}
dly.DelayFunc = func() {
dly.i++
}
got := realMain(&MockLogger{buf: &buf}, c, dly)
if expected != got {
msg := fmt.Sprintf("Expected exit code %d, got %d.", expected, got)
t.Error(msg)
}
if buf.String() == "" {
t.Error("Expected buffer to have error")
}
}
func TestMain_successfullyPingsTheDatabase(t *testing.T) {
os.Setenv("PINGLIMIT", "1")
os.Setenv("PGCONN", "goodconnstring")
var buf bytes.Buffer
db := &MockSqlDB{
PingFunc: func() error {
return nil
},
}
c := &MockConnection{
OpenFunc: func() (SqlDB, error) {
return db, nil
},
}
dly := &MockDelayer{}
dly.DelayFunc = func() {
dly.i++
}
status := realMain(&MockLogger{buf: &buf}, c, dly)
if status != 0 {
t.Error("Expected a zero status code")
}
if buf.String() == "" {
t.Error("Expected success log")
}
os.Unsetenv("PGCONN")
}
func TestMain_unsuccessfullyPingsTheDatabase(t *testing.T) {
os.Setenv("PINGLIMIT", "1")
os.Setenv("PGCONN", "badconnstring")
var buf bytes.Buffer
db := &MockSqlDB{
PingFunc: func() error {
return errors.New("Bad ping")
},
}
c := &MockConnection{
OpenFunc: func() (SqlDB, error) {
return db, nil
},
}
dly := &MockDelayer{}
dly.DelayFunc = func() {
dly.i++
}
status := realMain(&MockLogger{buf: &buf}, c, dly)
if status != 1 {
t.Error("Expected a non-zero status code")
}
os.Unsetenv("PGCONN")
}
func TestMain_writesToLoggerWhenErroring(t *testing.T) {
os.Setenv("PINGLIMIT", "1")
var buf bytes.Buffer
ml := &MockLogger{buf: &buf}
mc := &MockConnection{}
dly := &MockDelayer{}
dly.DelayFunc = func() {
dly.i++
}
realMain(ml, mc, dly)
if ml.buf.String() == "" {
t.Error("Expected non-empty log buffer")
}
}
func TestMain_returnsNonZeroWhenSqlOpenErrors(t *testing.T) {
os.Setenv("PINGLIMIT", "1")
var buf bytes.Buffer
os.Setenv("PGCONN", "pgconnstring")
ml := &MockLogger{buf: &buf}
mc := &MockConnection{
OpenFunc: func() (SqlDB, error) {
return nil, errors.New("Error")
},
}
dly := &MockDelayer{}
dly.DelayFunc = func() {
dly.i++
}
if realMain(ml, mc, dly) == 0 {
t.Error("Expected bad connection string to get non-zero code")
}
if ml.buf.String() == "" {
t.Error("Expected error to be logged")
}
os.Unsetenv("PGCONN")
}
func TestMain_repeatedlyPingsUpToLimit(t *testing.T) {
pingLmt := "5"
os.Setenv("PINGLIMIT", pingLmt)
var buf bytes.Buffer
os.Setenv("PGCONN", "pgconnstring")
ml := &MockLogger{buf: &buf}
db := &MockSqlDB{}
db.PingFunc = func() error {
db.Pinged++
return errors.New("Keep pinging")
}
mc := &MockConnection{
OpenFunc: func() (SqlDB, error) {
return db, nil
},
}
dly := &MockDelayer{}
dly.DelayFunc = func() {
dly.i++
}
realMain(ml, mc, dly)
if pingLmt != strconv.Itoa(db.Pinged) {
msg := fmt.Sprintf("Expected db.Pinged to be %s, got %d", pingLmt, db.Pinged)
t.Error(msg)
}
if dly.i == 0 {
msg := fmt.Sprintf("Expected DelayFunc to be called > 0 times, got %d", dly.i)
t.Error(msg)
}
os.Unsetenv("PGCONN")
}