Skip to content

Commit b15ead4

Browse files
committed
Disable network retries in tests to fail fast on missing cassettes
If a VCR cassette or a specific interaction is missing, the OSV client would previously retry the request 4 times with exponential backoff, causing significant delays in test execution (over 20 seconds). This change disables retries by setting MaxNetworkQueryAttempts to 1 when running in a test environment (detected via testing.Testing()). This allows tests to fail immediately (~0.02s) when a cassette or interaction is missing. - Added MaxNetworkQueryAttempts to osvscanner.ExperimentalScannerActions - Configured osvmatcher to use MaxNetworkQueryAttempts - Automatically set MaxNetworkQueryAttempts to 1 in CLI tests
1 parent 6aa55e7 commit b15ead4

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

cmd/osv-scanner/internal/helper/getters.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"strings"
7+
"testing"
78

89
"github.com/google/osv-scanner/v2/internal/spdx"
910
"github.com/google/osv-scanner/v2/pkg/osvscanner"
@@ -50,11 +51,19 @@ func GetCommonScannerActions(cmd *cli.Command, scanLicensesAllowlist []string) o
5051
}
5152

5253
func GetExperimentalScannerActions(cmd *cli.Command, client *http.Client) osvscanner.ExperimentalScannerActions {
54+
maxNetworkQueryAttempts := 0
55+
if testing.Testing() {
56+
// In tests, we want to fail fast on missing VCR cassettes or missing interactions.
57+
// Setting MaxNetworkQueryAttempts to 1 means only the initial attempt is made (0 retries).
58+
maxNetworkQueryAttempts = 1
59+
}
60+
5361
return osvscanner.ExperimentalScannerActions{
5462
PluginsEnabled: cmd.StringSlice("experimental-plugins"),
5563
PluginsDisabled: cmd.StringSlice("experimental-disable-plugins"),
5664
PluginsNoDefaults: cmd.Bool("experimental-no-default-plugins"),
5765
HTTPClient: client,
66+
MaxNetworkQueryAttempts: maxNetworkQueryAttempts,
5867
FlagDeprecatedPackages: cmd.Bool("experimental-flag-deprecated-packages"),
5968
}
6069
}

internal/clients/clientimpl/osvmatcher/osvmatcher.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ type OSVMatcher struct {
4545
InitialQueryTimeout time.Duration
4646
}
4747

48-
func New(initialQueryTimeout time.Duration, userAgent string, httpClient *http.Client) *OSVMatcher {
48+
// New creates a new OSVMatcher.
49+
// maxNetworkQueryAttempts specifies the maximum number of attempts for a network request (including the initial attempt).
50+
// A value of 1 means only the initial attempt is made (0 retries).
51+
// If set to 0, the default number of attempts from the underlying client (4) will be used.
52+
func New(initialQueryTimeout time.Duration, userAgent string, httpClient *http.Client, maxNetworkQueryAttempts int) *OSVMatcher {
4953
if httpClient == nil {
5054
httpClient = http.DefaultClient
5155
}
5256

5357
config := osvdev.DefaultConfig()
5458
config.UserAgent = userAgent
59+
if maxNetworkQueryAttempts > 0 {
60+
// osvdev.Config.MaxRetryAttempts actually controls the maximum number of attempts,
61+
// where 1 means 1 attempt (0 retries).
62+
config.MaxRetryAttempts = maxNetworkQueryAttempts
63+
}
5564

5665
return &OSVMatcher{
5766
Client: osvdev.OSVClient{

pkg/osvscanner/osvscanner.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ type ExperimentalScannerActions struct {
7979

8080
HTTPClient *http.Client
8181

82+
// MaxNetworkQueryAttempts specifies the maximum number of attempts for a network request.
83+
// This includes the initial attempt.
84+
// A value of 1 means only the initial attempt is made (0 retries).
85+
// If set to 0, the default number of attempts from the underlying client will be used.
86+
MaxNetworkQueryAttempts int
87+
8288
// Report deprecated packages as findings
8389
FlagDeprecatedPackages bool
8490

@@ -138,7 +144,7 @@ func initializeExternalAccessors(actions ScannerActions) (ExternalAccessors, err
138144
// Online Mode
139145
// -----------
140146
// --- Vulnerability Matcher ---
141-
externalAccessors.VulnMatcher = osvmatcher.New(5*time.Minute, userAgent, actions.HTTPClient)
147+
externalAccessors.VulnMatcher = osvmatcher.New(5*time.Minute, userAgent, actions.HTTPClient, actions.MaxNetworkQueryAttempts)
142148

143149
// --- License Matcher ---
144150
if len(actions.ScanLicensesAllowlist) > 0 || actions.ScanLicensesSummary {

0 commit comments

Comments
 (0)