@@ -34,6 +34,7 @@ const (
34
34
)
35
35
36
36
func TestPostgres_FromChunk (t * testing.T ) {
37
+ ctx := context .Background ()
37
38
if err := startPostgres (); err != nil {
38
39
if exitErr , ok := err .(* exec.ExitError ); ok {
39
40
t .Fatalf ("could not start local postgres: %v w/stderr:\n %s" , err , string (exitErr .Stderr ))
@@ -58,6 +59,10 @@ func TestPostgres_FromChunk(t *testing.T) {
58
59
ctx context.Context
59
60
data []byte
60
61
verify bool
62
+
63
+ // For tests that require a timeout context, in which case the above ctx will be ignored and new ctx at the
64
+ // time of test execution will be created
65
+ requiresTimeoutContext bool
61
66
}
62
67
tests := []struct {
63
68
name string
@@ -70,7 +75,7 @@ func TestPostgres_FromChunk(t *testing.T) {
70
75
name : "not found" ,
71
76
s : Scanner {},
72
77
args : args {
73
- ctx : context . Background () ,
78
+ ctx : ctx ,
74
79
data : []byte ("You cannot find the secret within" ),
75
80
verify : true ,
76
81
},
@@ -81,7 +86,7 @@ func TestPostgres_FromChunk(t *testing.T) {
81
86
name : "found connection URI with ssl mode unset, verified" ,
82
87
s : Scanner {detectLoopback : true },
83
88
args : args {
84
- ctx : context . Background () ,
89
+ ctx : ctx ,
85
90
data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2` , postgresUser , postgresPass , postgresHost , postgresPort )),
86
91
verify : true ,
87
92
},
@@ -100,7 +105,7 @@ func TestPostgres_FromChunk(t *testing.T) {
100
105
name : "found connection URI with ssl mode 'prefer', verified" ,
101
106
s : Scanner {detectLoopback : true },
102
107
args : args {
103
- ctx : context . Background () ,
108
+ ctx : ctx ,
104
109
data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2?sslmode=prefer` , postgresUser , postgresPass , postgresHost , postgresPort )),
105
110
verify : true ,
106
111
},
@@ -119,7 +124,7 @@ func TestPostgres_FromChunk(t *testing.T) {
119
124
name : "found connection URI with ssl mode 'allow', verified" ,
120
125
s : Scanner {detectLoopback : true },
121
126
args : args {
122
- ctx : context . Background () ,
127
+ ctx : ctx ,
123
128
data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2?sslmode=allow` , postgresUser , postgresPass , postgresHost , postgresPort )),
124
129
verify : true ,
125
130
},
@@ -138,7 +143,7 @@ func TestPostgres_FromChunk(t *testing.T) {
138
143
name : "found connection URI with requiressl=0, verified" ,
139
144
s : Scanner {detectLoopback : true },
140
145
args : args {
141
- ctx : context . Background () ,
146
+ ctx : ctx ,
142
147
data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2?requiressl=0` , postgresUser , postgresPass , postgresHost , postgresPort )),
143
148
verify : true ,
144
149
},
@@ -157,7 +162,7 @@ func TestPostgres_FromChunk(t *testing.T) {
157
162
name : "found connection URI without database, verified" ,
158
163
s : Scanner {detectLoopback : true },
159
164
args : args {
160
- ctx : context . Background () ,
165
+ ctx : ctx ,
161
166
data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/` , postgresUser , postgresPass , postgresHost , postgresPort )),
162
167
verify : true ,
163
168
},
@@ -176,7 +181,7 @@ func TestPostgres_FromChunk(t *testing.T) {
176
181
name : "found connection URI, unverified" ,
177
182
s : Scanner {detectLoopback : true },
178
183
args : args {
179
- ctx : context . Background () ,
184
+ ctx : ctx ,
180
185
data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2` , postgresUser , inactivePass , postgresHost , postgresPort )),
181
186
verify : true ,
182
187
},
@@ -195,7 +200,7 @@ func TestPostgres_FromChunk(t *testing.T) {
195
200
name : "ignored localhost" ,
196
201
s : Scanner {},
197
202
args : args {
198
- ctx : context . Background () ,
203
+ ctx : ctx ,
199
204
data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2` , postgresUser , postgresPass , "localhost" , postgresPort )),
200
205
verify : true ,
201
206
},
@@ -206,7 +211,7 @@ func TestPostgres_FromChunk(t *testing.T) {
206
211
name : "ignored 127.0.0.1" ,
207
212
s : Scanner {},
208
213
args : args {
209
- ctx : context . Background () ,
214
+ ctx : ctx ,
210
215
data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2` , postgresUser , postgresPass , "127.0.0.1" , postgresPort )),
211
216
verify : true ,
212
217
},
@@ -216,15 +221,12 @@ func TestPostgres_FromChunk(t *testing.T) {
216
221
{
217
222
name : "found connection URI, unverified due to error - inactive host" ,
218
223
s : Scanner {},
219
- args : func () args {
220
- ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
221
- defer cancel ()
222
- return args {
223
- ctx : ctx ,
224
- data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2` , postgresUser , postgresPass , inactiveHost , postgresPort )),
225
- verify : true ,
226
- }
227
- }(),
224
+ args : args {
225
+ ctx : ctx ,
226
+ data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2` , postgresUser , postgresPass , inactiveHost , postgresPort )),
227
+ verify : true ,
228
+ requiresTimeoutContext : true ,
229
+ },
228
230
want : func () []detectors.Result {
229
231
r := detectors.Result {
230
232
DetectorType : detectorspb .DetectorType_Postgres ,
@@ -241,15 +243,12 @@ func TestPostgres_FromChunk(t *testing.T) {
241
243
{
242
244
name : "found connection URI, unverified due to error - wrong port" ,
243
245
s : Scanner {detectLoopback : true },
244
- args : func () args {
245
- ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
246
- defer cancel ()
247
- return args {
248
- ctx : ctx ,
249
- data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s/postgres2` , postgresUser , postgresPass , postgresHost )),
250
- verify : true ,
251
- }
252
- }(),
246
+ args : args {
247
+ ctx : ctx ,
248
+ data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s/postgres2` , postgresUser , postgresPass , postgresHost )),
249
+ verify : true ,
250
+ requiresTimeoutContext : true ,
251
+ },
253
252
want : func () []detectors.Result {
254
253
r := detectors.Result {
255
254
DetectorType : detectorspb .DetectorType_Postgres ,
@@ -266,15 +265,12 @@ func TestPostgres_FromChunk(t *testing.T) {
266
265
{
267
266
name : "found connection URI, unverified due to error - ssl not supported (using sslmode)" ,
268
267
s : Scanner {detectLoopback : true },
269
- args : func () args {
270
- ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
271
- defer cancel ()
272
- return args {
273
- ctx : ctx ,
274
- data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2?sslmode=require` , postgresUser , postgresPass , postgresHost , postgresPort )),
275
- verify : true ,
276
- }
277
- }(),
268
+ args : args {
269
+ ctx : ctx ,
270
+ data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2?sslmode=require` , postgresUser , postgresPass , postgresHost , postgresPort )),
271
+ verify : true ,
272
+ requiresTimeoutContext : true ,
273
+ },
278
274
want : func () []detectors.Result {
279
275
r := detectors.Result {
280
276
DetectorType : detectorspb .DetectorType_Postgres ,
@@ -291,15 +287,11 @@ func TestPostgres_FromChunk(t *testing.T) {
291
287
{
292
288
name : "found connection URI, unverified due to error - ssl not supported (using requiressl)" ,
293
289
s : Scanner {detectLoopback : true },
294
- args : func () args {
295
- ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
296
- defer cancel ()
297
- return args {
298
- ctx : ctx ,
299
- data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2?requiressl=1` , postgresUser , postgresPass , postgresHost , postgresPort )),
300
- verify : true ,
301
- }
302
- }(),
290
+ args : args {
291
+ ctx : ctx ,
292
+ data : []byte (fmt .Sprintf (`postgresql://%s:%s@%s:%s/postgres2?requiressl=1` , postgresUser , postgresPass , postgresHost , postgresPort )),
293
+ verify : true ,
294
+ },
303
295
want : func () []detectors.Result {
304
296
r := detectors.Result {
305
297
DetectorType : detectorspb .DetectorType_Postgres ,
@@ -316,7 +308,13 @@ func TestPostgres_FromChunk(t *testing.T) {
316
308
}
317
309
for _ , tt := range tests {
318
310
t .Run (tt .name , func (t * testing.T ) {
319
- got , err := tt .s .FromData (tt .args .ctx , tt .args .verify , tt .args .data )
311
+ ctx := tt .args .ctx
312
+ var cancel context.CancelFunc
313
+ if tt .args .requiresTimeoutContext {
314
+ ctx , cancel = context .WithTimeout (context .Background (), 3 * time .Second )
315
+ defer cancel ()
316
+ }
317
+ got , err := tt .s .FromData (ctx , tt .args .verify , tt .args .data )
320
318
if (err != nil ) != tt .wantErr {
321
319
t .Errorf ("postgres.FromData() error = %v, wantErr %v" , err , tt .wantErr )
322
320
return
0 commit comments