@@ -41,7 +41,7 @@ import (
41
41
trackerMocks "github.com/snyk/code-client-go/scan/mocks"
42
42
)
43
43
44
- func setup (t * testing.T ) (* confMocks.MockConfig , * httpmocks.MockHTTPClient , * mocks.MockInstrumentor , * mocks.MockErrorReporter , * trackerMocks.MockTracker , * trackerMocks.MockTrackerFactory , zerolog.Logger ) {
44
+ func setup (t * testing.T , timeout * time. Duration ) (* confMocks.MockConfig , * httpmocks.MockHTTPClient , * mocks.MockInstrumentor , * mocks.MockErrorReporter , * trackerMocks.MockTracker , * trackerMocks.MockTrackerFactory , zerolog.Logger ) {
45
45
t .Helper ()
46
46
ctrl := gomock .NewController (t )
47
47
mockSpan := mocks .NewMockSpan (ctrl )
@@ -50,6 +50,11 @@ func setup(t *testing.T) (*confMocks.MockConfig, *httpmocks.MockHTTPClient, *moc
50
50
mockConfig := confMocks .NewMockConfig (ctrl )
51
51
mockConfig .EXPECT ().Organization ().AnyTimes ().Return ("" )
52
52
mockConfig .EXPECT ().SnykApi ().AnyTimes ().Return ("http://localhost" )
53
+ if timeout == nil {
54
+ defaultTimeout := 120 * time .Second
55
+ timeout = & defaultTimeout
56
+ }
57
+ mockConfig .EXPECT ().SnykCodeAnalysisTimeout ().AnyTimes ().Return (* timeout )
53
58
54
59
mockHTTPClient := httpmocks .NewMockHTTPClient (ctrl )
55
60
@@ -66,7 +71,7 @@ func setup(t *testing.T) (*confMocks.MockConfig, *httpmocks.MockHTTPClient, *moc
66
71
}
67
72
68
73
func TestAnalysis_CreateWorkspace (t * testing.T ) {
69
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
74
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
70
75
71
76
mockTracker .EXPECT ().Begin (gomock .Eq ("Creating file bundle workspace" ), gomock .Eq ("" )).Return ()
72
77
mockTracker .EXPECT ().End (gomock .Eq ("" )).Return ()
@@ -109,7 +114,7 @@ func TestAnalysis_CreateWorkspace(t *testing.T) {
109
114
}
110
115
111
116
func TestAnalysis_CreateWorkspace_NotARepository (t * testing.T ) {
112
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
117
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
113
118
114
119
mockTracker .EXPECT ().Begin (gomock .Eq ("Creating file bundle workspace" ), gomock .Eq ("" )).Return ()
115
120
mockTracker .EXPECT ().End (gomock .Eq ("" )).Return ()
@@ -155,7 +160,7 @@ func TestAnalysis_CreateWorkspace_NotARepository(t *testing.T) {
155
160
}
156
161
157
162
func TestAnalysis_CreateWorkspace_Failure (t * testing.T ) {
158
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
163
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
159
164
160
165
mockTracker .EXPECT ().Begin (gomock .Eq ("Creating file bundle workspace" ), gomock .Eq ("" )).Return ()
161
166
mockTracker .EXPECT ().End (gomock .Eq ("" )).Return ()
@@ -291,7 +296,7 @@ func TestAnalysis_CreateWorkspace_KnownErrors(t *testing.T) {
291
296
var fakeResponse []byte
292
297
293
298
func TestAnalysis_RunAnalysis (t * testing.T ) {
294
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
299
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
295
300
296
301
mockTracker .EXPECT ().Begin (gomock .Eq ("Snyk Code analysis for rootPath" ), gomock .Eq ("Retrieving results..." )).Return ()
297
302
mockTracker .EXPECT ().End (gomock .Eq ("Analysis complete." )).Return ()
@@ -338,15 +343,14 @@ func TestAnalysis_RunAnalysis(t *testing.T) {
338
343
analysis .WithErrorReporter (mockErrorReporter ),
339
344
)
340
345
341
- analysis .WithTimeoutInSeconds (120 * time .Second )
342
346
actual , err := analysisOrchestrator .RunAnalysis (context .Background (), "b6fc8954-5918-45ce-bc89-54591815ce1b" , "rootPath" , "c172d1db-b465-4764-99e1-ecedad03b06a" )
343
347
344
348
require .NoError (t , err )
345
349
assert .Equal (t , "scripts/db/migrations/20230811153738_add_generated_grouping_columns_to_collections_table.ts" , actual .Sarif .Runs [0 ].Results [0 ].Locations [0 ].PhysicalLocation .ArtifactLocation .URI )
346
350
}
347
351
348
352
func TestAnalysis_RunAnalysis_TriggerFunctionError (t * testing.T ) {
349
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
353
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
350
354
351
355
mockTracker .EXPECT ().Begin (gomock .Eq ("Snyk Code analysis for rootPath" ), gomock .Eq ("Retrieving results..." )).Return ()
352
356
mockTracker .EXPECT ().End (gomock .Eq ("Analysis failed: failed to trigger scan: error" )).Return ()
@@ -412,7 +416,7 @@ func TestAnalysis_RunAnalysis_TriggerFunctionErrorCodes(t *testing.T) {
412
416
413
417
for _ , tc := range testCases {
414
418
t .Run (tc .name , func (t * testing.T ) {
415
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
419
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
416
420
417
421
mockTracker .EXPECT ().Begin (gomock .Eq ("Snyk Code analysis for rootPath" ), gomock .Eq ("Retrieving results..." )).Return ()
418
422
mockTracker .EXPECT ().End (gomock .Eq ("Analysis failed: failed to trigger scan: " + tc .expectedError )).Return ()
@@ -439,7 +443,7 @@ func TestAnalysis_RunAnalysis_TriggerFunctionErrorCodes(t *testing.T) {
439
443
}
440
444
441
445
func TestAnalysis_RunAnalysis_PollingFunctionError (t * testing.T ) {
442
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
446
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
443
447
444
448
mockTracker .EXPECT ().Begin (gomock .Eq ("Snyk Code analysis for rootPath" ), gomock .Eq ("Retrieving results..." )).Return ()
445
449
mockTracker .EXPECT ().End (gomock .Eq ("Analysis failed: error" )).Return ()
@@ -517,7 +521,7 @@ func TestAnalysis_RunAnalysis_PollingFunctionErrorCodes(t *testing.T) {
517
521
518
522
for _ , tc := range testCases {
519
523
t .Run (tc .name , func (t * testing.T ) {
520
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
524
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
521
525
522
526
mockTracker .EXPECT ().Begin (gomock .Eq ("Snyk Code analysis for rootPath" ), gomock .Eq ("Retrieving results..." )).Return ()
523
527
mockTracker .EXPECT ().End (gomock .Eq ("Analysis failed: " + tc .expectedError )).Return ()
@@ -556,7 +560,8 @@ func TestAnalysis_RunAnalysis_PollingFunctionErrorCodes(t *testing.T) {
556
560
}
557
561
558
562
func TestAnalysis_RunAnalysis_PollingFunctionTimeout (t * testing.T ) {
559
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
563
+ timeout := 1 * time .Second
564
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , & timeout )
560
565
561
566
mockTracker .EXPECT ().Begin (gomock .Eq ("Snyk Code analysis for rootPath" ), gomock .Eq ("Retrieving results..." )).Return ()
562
567
mockTracker .EXPECT ().End (gomock .Eq ("Analysis failed: Snyk Code analysis timed out" )).Return ()
@@ -593,15 +598,14 @@ func TestAnalysis_RunAnalysis_PollingFunctionTimeout(t *testing.T) {
593
598
analysis .WithInstrumentor (mockInstrumentor ),
594
599
analysis .WithTrackerFactory (mockTrackerFactory ),
595
600
analysis .WithErrorReporter (mockErrorReporter ),
596
- analysis .WithTimeoutInSeconds (1 * time .Second ),
597
601
)
598
602
599
603
_ , err := analysisOrchestrator .RunAnalysis (context .Background (), "b6fc8954-5918-45ce-bc89-54591815ce1b" , "rootPath" , "c172d1db-b465-4764-99e1-ecedad03b06a" )
600
604
assert .ErrorContains (t , err , "Snyk Code analysis timed out" )
601
605
}
602
606
603
607
func TestAnalysis_RunAnalysis_GetFindingsError (t * testing.T ) {
604
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
608
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
605
609
606
610
mockTracker .EXPECT ().Begin (gomock .Eq ("Snyk Code analysis for rootPath" ), gomock .Eq ("Retrieving results..." )).Return ()
607
611
mockTracker .EXPECT ().End (gomock .Eq ("Analysis failed: error" )).Return ()
@@ -649,7 +653,7 @@ func TestAnalysis_RunAnalysis_GetFindingsError(t *testing.T) {
649
653
require .ErrorContains (t , err , "error" )
650
654
}
651
655
func TestAnalysis_RunAnalysis_GetFindingsNotSuccessful (t * testing.T ) {
652
- mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t )
656
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
653
657
654
658
mockTracker .EXPECT ().Begin (gomock .Eq ("Snyk Code analysis for rootPath" ), gomock .Eq ("Retrieving results..." )).Return ()
655
659
mockTracker .EXPECT ().End (gomock .Eq ("Analysis failed: failed to retrieve findings from findings URL" )).Return ()
0 commit comments