@@ -11,9 +11,9 @@ import (
1111 "time"
1212
1313 "github.com/sirupsen/logrus"
14+ logtest "github.com/sirupsen/logrus/hooks/test"
1415 "github.com/stretchr/testify/assert"
1516 "github.com/stretchr/testify/require"
16- logtest "github.com/sirupsen/logrus/hooks/test"
1717
1818 "github.com/crowdsecurity/go-cs-lib/cstest"
1919
@@ -27,26 +27,25 @@ func TestBadConfiguration(t *testing.T) {
2727 ctx := t .Context ()
2828
2929 tests := []struct {
30- config string
31- expectedErr string
30+ config string
31+ wantErr string
3232 }{
3333 {
34- config : `foobar: asd.log` ,
35- expectedErr : `cannot parse: [1:1] unknown field "foobar"` ,
34+ config : `foobar: asd.log` ,
35+ wantErr : `cannot parse: [1:1] unknown field "foobar"` ,
3636 },
3737 {
3838 config : `
3939mode: tail
4040source: journalctl` ,
41- expectedErr : "journalctl_filter is required" ,
41+ wantErr : "journalctl_filter is required" ,
4242 },
4343 {
4444 config : `
4545mode: cat
4646source: journalctl
4747journalctl_filter:
4848 - _UID=42` ,
49- expectedErr : "" ,
5049 },
5150 }
5251
@@ -55,7 +54,7 @@ journalctl_filter:
5554 f := Source {}
5655 logger , _ := logtest .NewNullLogger ()
5756 err := f .Configure (ctx , []byte (tc .config ), logrus .NewEntry (logger ), metrics .AcquisitionMetricsLevelNone )
58- cstest .RequireErrorContains (t , err , tc .expectedErr )
57+ cstest .RequireErrorContains (t , err , tc .wantErr )
5958 })
6059 }
6160}
@@ -66,44 +65,44 @@ func TestConfigureDSN(t *testing.T) {
6665 ctx := t .Context ()
6766
6867 tests := []struct {
69- dsn string
70- expectedErr string
68+ dsn string
69+ wantErr string
7170 }{
7271 {
73- dsn : "asd://" ,
74- expectedErr : "invalid DSN asd:// for journalctl source, must start with journalctl://" ,
72+ dsn : "asd://" ,
73+ wantErr : "invalid DSN asd:// for journalctl source, must start with journalctl://" ,
7574 },
7675 {
77- dsn : "journalctl://" ,
78- expectedErr : "empty journalctl:// DSN" ,
76+ dsn : "journalctl://" ,
77+ wantErr : "empty journalctl:// DSN" ,
7978 },
8079 {
81- dsn : "journalctl://foobar=42" ,
82- expectedErr : "unsupported key foobar in journalctl DSN" ,
80+ dsn : "journalctl://foobar=42" ,
81+ wantErr : "unsupported key foobar in journalctl DSN" ,
8382 },
8483 {
85- dsn : "journalctl://filters=%ZZ" ,
86- expectedErr : "could not parse journalctl DSN: invalid URL escape \" %ZZ\" " ,
84+ dsn : "journalctl://filters=%ZZ" ,
85+ wantErr : "could not parse journalctl DSN: invalid URL escape \" %ZZ\" " ,
8786 },
8887 {
89- dsn : "journalctl://filters=_UID=42?log_level=warn" ,
90- expectedErr : "" ,
88+ dsn : "journalctl://filters=_UID=42?log_level=warn" ,
9189 },
9290 {
93- dsn : "journalctl://filters=_UID=1000&log_level=foobar" ,
94- expectedErr : `not a valid logrus Level: "foobar"` ,
91+ dsn : "journalctl://filters=_UID=1000&log_level=foobar" ,
92+ wantErr : `not a valid logrus Level: "foobar"` ,
9593 },
9694 {
97- dsn : "journalctl://filters=_UID=1000&log_level=warn&since=yesterday" ,
98- expectedErr : "" ,
95+ dsn : "journalctl://filters=_UID=1000&log_level=warn&since=yesterday" ,
9996 },
10097 }
10198
102- for _ , test := range tests {
103- f := Source {}
104- logger , _ := logtest .NewNullLogger ()
105- err := f .ConfigureByDSN (ctx , test .dsn , map [string ]string {"type" : "testtype" }, logrus .NewEntry (logger ), "" )
106- cstest .RequireErrorContains (t , err , test .expectedErr )
99+ for _ , tc := range tests {
100+ t .Run (tc .dsn , func (t * testing.T ) {
101+ f := Source {}
102+ logger , _ := logtest .NewNullLogger ()
103+ err := f .ConfigureByDSN (ctx , tc .dsn , map [string ]string {"type" : "testtype" }, logrus .NewEntry (logger ), "" )
104+ cstest .RequireErrorContains (t , err , tc .wantErr )
105+ })
107106 }
108107}
109108
@@ -113,55 +112,55 @@ func TestOneShot(t *testing.T) {
113112 ctx := t .Context ()
114113
115114 tests := []struct {
116- config string
117- expectedErr string
118- expectedLines int
119- expectedLog []string
115+ config string
116+ wantErr string
117+ wantLines int
118+ wantLog []string
120119 }{
121120 {
122121 config : `
123122source: journalctl
124123mode: cat
125124journalctl_filter:
126125 - "-_UID=42"` ,
127- expectedErr : "exit status 1" ,
128- expectedLog : []string {
126+ wantErr : "exit status 1" ,
127+ wantLog : []string {
129128 "Got stderr: journalctl: invalid option -- '_'" ,
130129 },
131- expectedLines : 0 ,
130+ wantLines : 0 ,
132131 },
133132 {
134133 config : `
135134source: journalctl
136135mode: cat
137136journalctl_filter:
138137 - _SYSTEMD_UNIT=ssh.service` ,
139- expectedLines : 14 ,
138+ wantLines : 14 ,
140139 },
141140 }
142141 for _ , ts := range tests {
143- out := make (chan pipeline.Event , 100 )
144- j := Source {}
142+ t .Run (ts .config , func (t * testing.T ) {
143+ out := make (chan pipeline.Event , 100 )
144+ j := Source {}
145145
146- logger , hook := logtest .NewNullLogger ()
146+ logger , hook := logtest .NewNullLogger ()
147147
148- err := j .Configure (ctx , []byte (ts .config ), logrus .NewEntry (logger ), metrics .AcquisitionMetricsLevelNone )
149- require .NoError (t , err )
148+ err := j .Configure (ctx , []byte (ts .config ), logrus .NewEntry (logger ), metrics .AcquisitionMetricsLevelNone )
149+ require .NoError (t , err )
150150
151- err = j .OneShot (ctx , out )
152- cstest .RequireErrorContains (t , err , ts .expectedErr )
151+ err = j .OneShot (ctx , out )
152+ cstest .RequireErrorContains (t , err , ts .wantErr )
153153
154- for _ , expectedMessage := range ts .expectedLog {
155- cstest .RequireLogContains (t , hook , expectedMessage )
156- }
154+ for _ , wantMessage := range ts .wantLog {
155+ cstest .RequireLogContains (t , hook , wantMessage )
156+ }
157157
158- if ts .expectedErr != "" {
159- continue
160- }
158+ if ts .wantErr != "" {
159+ return
160+ }
161161
162- if ts .expectedLines != 0 {
163- assert .Len (t , out , ts .expectedLines )
164- }
162+ assert .Len (t , out , ts .wantLines )
163+ })
165164 }
166165}
167166
@@ -171,17 +170,17 @@ func TestStreaming(t *testing.T) {
171170 ctx := t .Context ()
172171
173172 tests := []struct {
174- config string
175- expectedErr string
176- expectedLines int
173+ config string
174+ wantErr string
175+ wantLines int
177176 }{
178177 {
179178 config : `
180179source: journalctl
181180mode: cat
182181journalctl_filter:
183182 - _SYSTEMD_UNIT=ssh.service` ,
184- expectedLines : 14 ,
183+ wantLines : 14 ,
185184 },
186185 }
187186 for idx , ts := range tests {
@@ -195,36 +194,34 @@ journalctl_filter:
195194 err := j .Configure (ctx , []byte (ts .config ), logrus .NewEntry (logger ), metrics .AcquisitionMetricsLevelNone )
196195 require .NoError (t , err )
197196
198- actualLines := 0
197+ gotLines := 0
199198 var wg sync.WaitGroup
200199
201- if ts .expectedLines != 0 {
202- wg .Add (1 )
203- go func () {
204- defer wg .Done ()
200+ if ts .wantLines != 0 {
201+ wg .Go (func () {
205202 for {
206203 select {
207204 case <- out :
208- actualLines ++
205+ gotLines ++
209206 case <- time .After (1 * time .Second ):
210207 cancel ()
211208 return
212209 }
213210 }
214- }( )
211+ })
215212 }
216213
217214 err = j .Stream (ctx , out )
218- cstest .RequireErrorContains (t , err , ts .expectedErr )
215+ cstest .RequireErrorContains (t , err , ts .wantErr )
219216
220- if ts .expectedErr != "" {
217+ if ts .wantErr != "" {
221218 cancel ()
222219 return
223220 }
224221
225- if ts .expectedLines != 0 {
222+ if ts .wantLines != 0 {
226223 wg .Wait ()
227- assert .Equal (t , ts .expectedLines , actualLines )
224+ assert .Equal (t , ts .wantLines , gotLines )
228225 }
229226
230227 cancel ()
0 commit comments