Skip to content

Commit 9d7170c

Browse files
committed
test: fail fast on missing VCR cassettes by converting errors to 404
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 wraps the VCR client transport in tests to intercept the "requested interaction not found" error and convert it to a "404 Not Found" response. Since the OSV client does not retry on 404 status codes, this allows tests to fail immediately (~0.02s) on missing cassettes/interactions while still preserving retry behavior for genuine transient network errors. - Implemented vcrErrorWrappingTransport in internal/testcmd/vcr.go - Wrapped the VCR client transport in InsertCassette
1 parent f9ffc38 commit 9d7170c

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

  • cmd/osv-scanner/internal/testcmd

cmd/osv-scanner/internal/testcmd/vcr.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ func InsertCassette(t *testing.T) *http.Client {
173173
sortCassetteInteractions(t, path)
174174
})
175175

176-
return r.GetDefaultClient()
176+
client := r.GetDefaultClient()
177+
client.Transport = &vcrErrorWrappingTransport{wrapper: client.Transport}
178+
179+
return client
177180
}
178181

179182
// sortCassetteInteractions reorders the interactions in the given cassette, based
@@ -255,3 +258,23 @@ func matchBody(r *http.Request, i cassette.Request) bool {
255258

256259
return true
257260
}
261+
262+
type vcrErrorWrappingTransport struct {
263+
wrapper http.RoundTripper
264+
}
265+
266+
func (t *vcrErrorWrappingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
267+
resp, err := t.wrapper.RoundTrip(req)
268+
if err != nil && strings.Contains(err.Error(), "requested interaction not found") {
269+
// Convert VCR error to a 404 response to avoid retries by the client
270+
return &http.Response{
271+
StatusCode: http.StatusNotFound,
272+
Status: "404 Not Found (VCR: requested interaction not found)",
273+
Body: io.NopCloser(strings.NewReader("VCR: requested interaction not found")),
274+
Header: make(http.Header),
275+
Request: req,
276+
}, nil
277+
}
278+
279+
return resp, err
280+
}

0 commit comments

Comments
 (0)