-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_test.go
118 lines (96 loc) · 3.18 KB
/
app_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
// SPDX-FileCopyrightText: 2021 Lightmeter <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-only
package main
import (
"context"
"os"
"path"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"gitlab.com/lightmeter/controlcenter/config"
"gitlab.com/lightmeter/controlcenter/detective/escalator"
"gitlab.com/lightmeter/controlcenter/insights/core"
"gitlab.com/lightmeter/controlcenter/insights/detectiveescalation"
"gitlab.com/lightmeter/controlcenter/lmsqlite3"
"gitlab.com/lightmeter/controlcenter/pkg/runner"
"gitlab.com/lightmeter/controlcenter/util/testutil"
"gitlab.com/lightmeter/controlcenter/util/timeutil"
"gitlab.com/lightmeter/controlcenter/workspace"
)
func init() {
lmsqlite3.Initialize(lmsqlite3.Options{})
}
func fetchInsights(ws *workspace.Workspace, cat core.Category) []core.FetchedInsight {
fetcher := ws.InsightsFetcher()
insights, err := fetcher.FetchInsights(context.Background(), core.FetchOptions{
Interval: timeutil.MustParseTimeInterval("0000-01-01", "5000-01-01"),
FilterBy: core.FilterByCategory,
Category: cat,
}, timeutil.RealClock{})
So(err, ShouldBeNil)
return insights
}
func TestMain(t *testing.T) {
Convey("Test Main", t, func() {
wsDir, clearWsDir := testutil.TempDir(t)
defer clearWsDir()
logsDir, clearLogsDir := testutil.TempDir(t)
defer clearLogsDir()
logsArchive, err := os.Open(path.Join("test_files", "postfix_logs", "complete.tar.gz"))
So(err, ShouldBeNil)
testutil.ExtractTarGz(logsArchive, logsDir)
// Only import the logs in a workspace
config := config.Config{
WorkspaceDirectory: wsDir,
DirsToWatch: []string{path.Join(logsDir, "logs_sample")},
ImportOnly: true,
LogFormat: "default",
MultiNodeType: "single",
}
// first execution
func() {
ws, reader, err := buildWorkspaceAndLogReader(config)
So(err, ShouldBeNil)
defer ws.Close()
done, cancel := runner.Run(ws)
reader.Run()
cancel()
So(done(), ShouldBeNil)
// ensure there are no detective insights
for _, i := range fetchInsights(ws, core.LocalCategory) {
So(i.ContentType(), ShouldNotEqual, detectiveescalation.ContentType)
}
}()
// second execution, using the same workspace.
// We create a new insight and it should appear here
func() {
ws, reader, err := buildWorkspaceAndLogReader(config)
So(err, ShouldBeNil)
defer ws.Close()
done, cancel := runner.Run(ws)
// Won't reimport the logs
err = reader.Run()
So(err, ShouldBeNil)
// Request insight to be created
ws.DetectiveEscalationRequester().Request(escalator.Request{
Sender: "[email protected]",
Recipient: "[email protected]",
Interval: timeutil.MustParseTimeInterval("0000-01-01", "5000-01-01"),
})
// Sleep time enough for an insight cycle to be executed (2s)
time.Sleep(4 * time.Second)
cancel()
So(done(), ShouldBeNil)
numberOfDetectiveInsights := 0
// ensure there are no detective insights
for _, i := range fetchInsights(ws, core.LocalCategory) {
if i.ContentType() == detectiveescalation.ContentType {
numberOfDetectiveInsights++
}
}
So(numberOfDetectiveInsights, ShouldEqual, 1)
}()
})
}