-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
242 lines (213 loc) · 6.66 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
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
package main
import (
"errors"
"io/ioutil"
"log"
"path"
"testing"
"github.com/kardianos/osext"
"github.com/stretchr/testify/assert"
"github.com/Clever/analytics-monitor/config"
"github.com/Clever/analytics-monitor/db"
)
// Copy kvconfig.yml to exec dir to simulate main.init()
// loading in a production Docker environment.
// This syntax is used to run the setup before main.init()
var _ = func() (_ struct{}) {
configContent, err := ioutil.ReadFile("kvconfig.yml")
if err != nil {
log.Fatal(err)
}
execDir, err := osext.ExecutableFolder()
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(path.Join(execDir, "kvconfig.yml"), configContent, 0777)
if err != nil {
log.Fatal(err)
}
return
}()
type mockRedshiftClient struct {
latencyHrs int64
hasRows bool
queryErr error
loadErrs []db.LoadError
tableMetadata map[string]db.TableMetadata
}
func (c *mockRedshiftClient) GetClusterName() string {
return "mockClusterName"
}
func (c *mockRedshiftClient) QueryTableMetadata(schemaName string) (map[string]db.TableMetadata, error) {
return c.tableMetadata, c.queryErr
}
func (c *mockRedshiftClient) QueryLatency(timestampColumn, schemaName, tableName string) (int64, bool, error) {
return c.latencyHrs, c.hasRows, c.queryErr
}
func (c *mockRedshiftClient) QuerySTLLoadErrors() ([]db.LoadError, error) {
return c.loadErrs, c.queryErr
}
type mockLogger struct {
assertions *assert.Assertions
expectedLogValue int
expectedLatencyReport string
expectedErrorsString string
}
func (l *mockLogger) JobFinishedEvent(payload string, didSucceed bool) {
// Dummy mocked to satisfy the Logger interface
return
}
func (l *mockLogger) CheckLatencyEvent(latencyErrValue int, fullTableName, reportedLatency, threshold string) {
l.assertions.Equal(latencyErrValue, l.expectedLogValue, "Incorrect latency log value")
l.assertions.Equal(reportedLatency, l.expectedLatencyReport, "Mismatched latency report string")
}
func (l *mockLogger) CheckLoadErrorEvent(loadErrValue int, loadErrors string) {
l.assertions.Equal(loadErrValue, l.expectedLogValue, "Incorrect latency log value")
l.assertions.Equal(loadErrors, l.expectedErrorsString, "Mismatched load errors")
}
// TestPerformLatencyChecks tests the performLatencyChecks
// function, mocking out latency results and verifying
// that the correct results are being logged
func TestPerformLatencyChecks(t *testing.T) {
assertions := assert.New(t)
tests := []struct {
title string
// Mocks out the results of QueryLatency
latencyHrs int64
hasRows bool
queryErr error
// Mocks out the config latency threshold
threshold string
// Specifies what we expect to log (or error)
expectedLogValue int
expectedLatencyReport string
expectedPanic bool
expectedErrorsReturned bool
}{
{
title: "logs a success value (0) when latencyHrs <= threshold",
latencyHrs: 1,
hasRows: true,
queryErr: nil,
threshold: "2h",
expectedLogValue: 0,
expectedLatencyReport: "1h",
},
{
title: "logs a failure value (1) when latencyHrs > threshold",
latencyHrs: 3,
hasRows: true,
queryErr: nil,
threshold: "2h",
expectedLogValue: 1,
expectedLatencyReport: "3h",
},
{
title: "logs a failure value (1) when no rows exist",
latencyHrs: 0,
hasRows: false,
queryErr: nil,
threshold: "2h",
expectedLogValue: 1,
expectedLatencyReport: "N/A - no rows",
},
{
title: "panics when threshold is malformatted",
latencyHrs: 0,
hasRows: false,
queryErr: nil,
threshold: "2j",
expectedPanic: true,
},
{
title: "panics when latency query errors out",
latencyHrs: 0,
hasRows: false,
queryErr: errors.New("Data Warehouse out of space - s/Redshift/Blueshift"),
threshold: "2h",
expectedErrorsReturned: true,
},
}
for _, test := range tests {
t.Logf("Testing that performLatencyChecks %s", test.title)
mockRsClient := &mockRedshiftClient{
latencyHrs: test.latencyHrs,
hasRows: test.hasRows,
queryErr: test.queryErr,
}
mockLog := &mockLogger{
assertions: assertions,
expectedLogValue: test.expectedLogValue,
expectedLatencyReport: test.expectedLatencyReport,
}
logger = mockLog // Overrides package level logger
mockChecks := make(Checks)
mockChecks["mockSchemaName"] = make(map[string]config.TableCheck)
mockChecks["mockSchemaName"]["mockTableName"] = config.TableCheck{
TableName: "mockTableName",
Latency: config.LatencyInfo{
TimestampColumn: "mockColumn",
Threshold: test.threshold,
},
}
if test.expectedPanic {
assert.Panics(t, func() {
performLatencyChecks(mockRsClient, mockChecks)
}, "Doesn't error when expected")
} else {
errors := performLatencyChecks(mockRsClient, mockChecks)
if test.expectedErrorsReturned {
assertions.True(len(errors) > 0, "Didn't return errors when expected")
}
}
}
}
// TestPerformLoadErrorsCheck tests the performLoadErrorsCheck
// function, mocking out load error results and verifying
// that the correct results are being logged
func TestPerformLoadErrorsCheck(t *testing.T) {
assertions := assert.New(t)
var emptyErrors []db.LoadError
someErrors := append(emptyErrors, db.LoadError{
TableNames: "table1, table2",
ErrorCode: 123,
Count: 10})
tests := []struct {
title string
// Mocks out the results of QuerySTLLoadErrors
loadErrs []db.LoadError
queryErr error
// Specifies what we expect to log (or error)
expectedLogValue int
expectedErrors string
}{
{
title: "logs a success value (0) when there are no load errors",
loadErrs: emptyErrors,
queryErr: nil,
expectedLogValue: 0,
expectedErrors: "",
},
{
title: "logs a failure value (1) when there is at least one load error",
loadErrs: someErrors,
queryErr: nil,
expectedLogValue: 1,
expectedErrors: "[{\"table_names\":\"table1, table2\",\"error_code\":123,\"count\":10}]",
},
}
for _, test := range tests {
t.Logf("Testing that performLoadErrorsCheck %s", test.title)
mockRsClient := &mockRedshiftClient{
queryErr: test.queryErr,
loadErrs: test.loadErrs,
}
mockLog := &mockLogger{
assertions: assertions,
expectedLogValue: test.expectedLogValue,
expectedErrorsString: test.expectedErrors,
}
logger = mockLog // Overrides package level logger
performLoadErrorsCheck(mockRsClient)
}
}