forked from RedHatInsights/insights-results-aggregator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregator_test.go
422 lines (325 loc) · 13.5 KB
/
aggregator_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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
Copyright © 2020, 2021, 2022 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main_test
import (
"bytes"
"fmt"
"os"
"testing"
"time"
"github.com/RedHatInsights/insights-operator-utils/tests/helpers"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
main "github.com/RedHatInsights/insights-results-aggregator"
"github.com/RedHatInsights/insights-results-aggregator/conf"
"github.com/RedHatInsights/insights-results-aggregator/migration"
"github.com/RedHatInsights/insights-results-aggregator/storage"
ira_helpers "github.com/RedHatInsights/insights-results-aggregator/tests/helpers"
)
const (
testsTimeout = 60 * time.Second
)
func mustSetEnv(t testing.TB, key, val string) {
err := os.Setenv(key, val)
helpers.FailOnError(t, err)
}
func mustLoadConfiguration(path string) {
err := conf.LoadConfiguration(path)
if err != nil {
panic(err)
}
}
func setEnvSettings(t testing.TB, settings map[string]string) {
os.Clearenv()
for key, val := range settings {
mustSetEnv(t, key, val)
}
mustLoadConfiguration("/non_existing_path")
}
func TestCreateStorage(t *testing.T) {
os.Clearenv()
mustLoadConfiguration("tests/config1")
_, err := main.CreateStorage()
helpers.FailOnError(t, err)
}
func TestStartService(t *testing.T) {
// It is necessary to perform migrations for this test
// because the service won't run on top of an empty DB.
*main.AutoMigratePtr = true
helpers.RunTestWithTimeout(t, func(t testing.TB) {
os.Clearenv()
mustLoadConfiguration("./tests/tests")
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "sqlite3",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": ":memory:",
})
go func() {
main.StartService()
}()
errCode := main.StopService()
assert.Equal(t, main.ExitStatusOK, errCode)
}, testsTimeout)
*main.AutoMigratePtr = false
}
func TestStartService_DBError(t *testing.T) {
helpers.RunTestWithTimeout(t, func(t testing.TB) {
buf := new(bytes.Buffer)
log.Logger = zerolog.New(buf)
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "sqlite3",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": "/non/existing/path",
})
exitCode := main.StartService()
assert.Equal(t, main.ExitStatusPrepareDbError, exitCode)
assert.Contains(t, buf.String(), "unable to open database file: no such file or directory")
}, testsTimeout)
}
func TestCreateStorage_BadDriver(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "non-existing-driver",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": "/non/existing/path",
})
_, err := main.CreateStorage()
assert.EqualError(t, err, "driver non-existing-driver is not supported")
}
func TestCloseStorage_Error(t *testing.T) {
const errStr = "close error"
buf := new(bytes.Buffer)
log.Logger = zerolog.New(buf)
mockStorage, expects := ira_helpers.MustGetMockStorageWithExpects(t)
expects.ExpectClose().WillReturnError(fmt.Errorf(errStr))
main.CloseStorage(mockStorage.(*storage.DBStorage))
assert.Contains(t, buf.String(), errStr)
}
func TestPrepareDB_DBError(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "non-existing-driver",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": "/non/existing/path",
})
errCode := main.PrepareDB()
assert.Equal(t, main.ExitStatusPrepareDbError, errCode)
}
func TestPrepareDB(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "sqlite3",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": ":memory:",
"INSIGHTS_RESULTS_AGGREGATOR__CONTENT__PATH": "./tests/content/ok/",
})
*main.AutoMigratePtr = true
errCode := main.PrepareDB()
assert.Equal(t, main.ExitStatusOK, errCode)
*main.AutoMigratePtr = false
}
func TestPrepareDB_NoRulesDirectory(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "sqlite3",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": ":memory:",
"INSIGHTS_RESULTS_AGGREGATOR__CONTENT__PATH": "/non-existing-path",
})
errCode := main.PrepareDB()
assert.Equal(t, main.ExitStatusPrepareDbError, errCode)
}
func TestPrepareDB_BadRules(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "sqlite3",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": ":memory:",
"INSIGHTS_RESULTS_AGGREGATOR__CONTENT__PATH": "./tests/content/bad_metadata_status/",
})
errCode := main.PrepareDB()
assert.Equal(t, main.ExitStatusPrepareDbError, errCode)
}
func TestStartConsumer_DBError(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "non-existing-driver",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": "bad-data-source",
})
err := main.StartConsumer(conf.GetBrokerConfiguration())
assert.EqualError(t, err, "driver non-existing-driver is not supported")
}
func TestStartConsumer_BadBrokerAddress(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "sqlite3",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": ":memory:",
"INSIGHTS_RESULTS_AGGREGATOR__BROKER__ADDRESS": "non-existing-host:999999",
"INSIGHTS_RESULTS_AGGREGATOR__BROKER__ENABLED": "true",
})
err := main.StartConsumer(conf.GetBrokerConfiguration())
assert.EqualError(
t, err, "kafka: client has run out of available brokers to talk to (Is your cluster reachable?)",
)
}
func TestStartServer_DBError(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "non-existing-driver",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": "bad-data-source",
})
err := main.StartServer()
assert.EqualError(t, err, "driver non-existing-driver is not supported")
}
func TestStartServer_BadServerAddress(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "sqlite3",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": ":memory:",
"INSIGHTS_RESULTS_AGGREGATOR__SERVER__ADDRESS": "localhost:999999",
"INSIGHTS_RESULTS_AGGREGATOR__SERVER__API_SPEC_FILE": "openapi.json",
})
err := main.StartServer()
assert.EqualError(t, err, "listen tcp: address 999999: invalid port")
}
func TestStartService_BadBrokerAndServerAddress(t *testing.T) {
setEnvSettings(t, map[string]string{
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__DB_DRIVER": "sqlite3",
"INSIGHTS_RESULTS_AGGREGATOR__STORAGE__SQLITE_DATASOURCE": ":memory:",
"INSIGHTS_RESULTS_AGGREGATOR__BROKER__ADDRESS": "non-existing-host:1",
"INSIGHTS_RESULTS_AGGREGATOR__BROKER__ENABLED": "true",
"INSIGHTS_RESULTS_AGGREGATOR__SERVER__ADDRESS": "non-existing-host:1",
"INSIGHTS_RESULTS_AGGREGATOR__SERVER__API_SPEC_FILE": "openapi.json",
"INSIGHTS_RESULTS_AGGREGATOR__CONTENT__PATH": "./tests/content/ok/",
})
*main.AutoMigratePtr = true
errCode := main.StartService()
assert.Equal(t, main.ExitStatusError, errCode)
*main.AutoMigratePtr = false
}
// TestPrintVersionInfo is dummy ATM - we'll check versions etc. in integration tests
func TestPrintVersionInfo(_ *testing.T) {
main.PrintVersionInfo()
}
// TestPrintHelp checks that printing help returns OK exit code.
func TestPrintHelp(t *testing.T) {
assert.Equal(t, main.ExitStatusOK, main.PrintHelp())
}
// TestPrintConfig checks that printing configuration info returns OK exit code.
func TestPrintConfig(t *testing.T) {
assert.Equal(t, main.ExitStatusOK, main.PrintConfig())
}
// TestPrintEnv checks that printing environment variables returns OK exit code.
func TestPrintEnv(t *testing.T) {
assert.Equal(t, main.ExitStatusOK, main.PrintEnv())
}
// TestGetDBForMigrations checks that the function ensures the existence of
// the migration_info table and that the SQL DB connection works correctly.
func TestGetDBForMigrations(t *testing.T) {
db, dbConn, exitCode := main.GetDBForMigrations()
assert.Equal(t, main.ExitStatusOK, exitCode)
defer ira_helpers.MustCloseStorage(t, db)
row := dbConn.QueryRow("SELECT version FROM migration_info;")
var version migration.Version
err := row.Scan(&version)
assert.NoError(t, err, "unable to read version from migration info table")
}
// TestPrintMigrationInfo checks that printing migration info exits with OK code.
func TestPrintMigrationInfo(t *testing.T) {
db, dbConn, exitCode := main.GetDBForMigrations()
assert.Equal(t, exitCode, main.ExitStatusOK)
defer ira_helpers.MustCloseStorage(t, db)
exitCode = main.PrintMigrationInfo(dbConn)
assert.Equal(t, main.ExitStatusOK, exitCode)
}
// TestPrintMigrationInfoClosedDB checks that printing migration info with
// a closed DB connection results in a migration error exit code.
func TestPrintMigrationInfoClosedDB(t *testing.T) {
db, dbConn, exitCode := main.GetDBForMigrations()
assert.Equal(t, exitCode, main.ExitStatusOK)
// Close DB connection immediately.
ira_helpers.MustCloseStorage(t, db)
exitCode = main.PrintMigrationInfo(dbConn)
assert.Equal(t, main.ExitStatusMigrationError, exitCode)
}
// TestSetMigrationVersionZero checks that it is possible to set migration version to 0.
func TestSetMigrationVersionZero(t *testing.T) {
db, dbConn, exitCode := main.GetDBForMigrations()
assert.Equal(t, exitCode, main.ExitStatusOK)
defer ira_helpers.MustCloseStorage(t, db)
exitCode = main.SetMigrationVersion(dbConn, db.GetDBDriverType(), "0")
assert.Equal(t, main.ExitStatusOK, exitCode)
version, err := migration.GetDBVersion(dbConn)
assert.NoError(t, err, "unable to get migration version")
assert.Equal(t, migration.Version(0), version)
}
// TestSetMigrationVersionZero checks that it is to upgrade DB to the latest migration.
func TestSetMigrationVersionLatest(t *testing.T) {
db, dbConn, exitCode := main.GetDBForMigrations()
assert.Equal(t, exitCode, main.ExitStatusOK)
defer ira_helpers.MustCloseStorage(t, db)
exitCode = main.SetMigrationVersion(dbConn, db.GetDBDriverType(), "latest")
assert.Equal(t, main.ExitStatusOK, exitCode)
version, err := migration.GetDBVersion(dbConn)
assert.NoError(t, err, "unable to get migration version")
assert.Equal(t, migration.GetMaxVersion(), version)
}
// TestSetMigrationVersionClosedDB checks that setting the migration version
// with a closed DB connection results in a migration error exit code.
func TestSetMigrationVersionClosedDB(t *testing.T) {
db, dbConn, exitCode := main.GetDBForMigrations()
assert.Equal(t, exitCode, main.ExitStatusOK)
// Close DB connection immediately.
ira_helpers.MustCloseStorage(t, db)
exitCode = main.SetMigrationVersion(dbConn, db.GetDBDriverType(), "0")
assert.Equal(t, main.ExitStatusMigrationError, exitCode)
}
// TestSetMigrationVersionInvalid checks that when supplied an invalid version
// argument, the set version function exits with a migration error code.
func TestSetMigrationVersionInvalid(t *testing.T) {
db, dbConn, exitCode := main.GetDBForMigrations()
assert.Equal(t, exitCode, main.ExitStatusOK)
// Close DB connection immediately.
ira_helpers.MustCloseStorage(t, db)
exitCode = main.SetMigrationVersion(dbConn, db.GetDBDriverType(), "")
assert.Equal(t, main.ExitStatusMigrationError, exitCode)
}
// TestPerformMigrationsPrint checks that the command for
// printing migration info exits with the OK exit code.
func TestPerformMigrationsPrint(t *testing.T) {
oldArgs := os.Args
os.Args = []string{os.Args[0], "migrations"}
exitCode := main.PerformMigrations()
assert.Equal(t, main.ExitStatusOK, exitCode)
os.Args = oldArgs
}
// TestPerformMigrationsPrint checks that the command for
// setting migration version exits with the OK exit code.
func TestPerformMigrationsSet(t *testing.T) {
oldArgs := os.Args
os.Args = []string{os.Args[0], "migrations", "0"}
exitCode := main.PerformMigrations()
assert.Equal(t, main.ExitStatusOK, exitCode)
os.Args = oldArgs
}
// TestPerformMigrationsPrint checks that supplying too many arguments
// to the migration sub-commands results in the migration error exit code.
func TestPerformMigrationsTooManyArgs(t *testing.T) {
oldArgs := os.Args
os.Args = []string{os.Args[0], "migrations", "hello", "world"}
exitCode := main.PerformMigrations()
assert.Equal(t, main.ExitStatusMigrationError, exitCode)
os.Args = oldArgs
}
// TestFillInInfoParams test the behaviour of function fillInInfoParams
func TestFillInInfoParams(t *testing.T) {
// map to be used by this unit test
m := make(map[string]string)
// preliminary test if Go Universe is still ok
assert.Empty(t, m, "Map should be empty at the beginning")
// try to fill-in all info params
main.FillInInfoParams(m)
// preliminary test if Go Universe is still ok
assert.Len(t, m, 5, "Map should contains exactly five items")
// does the map contain all expected keys?
assert.Contains(t, m, "BuildVersion")
assert.Contains(t, m, "BuildTime")
assert.Contains(t, m, "BuildBranch")
assert.Contains(t, m, "BuildCommit")
assert.Contains(t, m, "UtilsVersion")
}