-
Notifications
You must be signed in to change notification settings - Fork 183
Add retries for the raw result upload from the lurker sidecar #3785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,8 @@ | |
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| main | ||
| lurker | ||
| bin | ||
|
|
||
| # Output of the go coverage tool | ||
| *.out | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # SPDX-FileCopyrightText: the secureCodeBox authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| version: "3.48.0" | ||
|
|
||
| tasks: | ||
| fmt: | ||
| desc: "Run go fmt against code" | ||
| dir: '{{ .TASKFILE_DIR }}' | ||
| cmds: | ||
| - go fmt ./... | ||
|
|
||
| vet: | ||
| desc: "Run go vet against code" | ||
| dir: '{{ .TASKFILE_DIR }}' | ||
| cmds: | ||
| - go vet ./... | ||
|
|
||
| test: | ||
| desc: "Run all tests" | ||
| deps: [fmt, vet] | ||
| dir: '{{ .TASKFILE_DIR }}' | ||
| cmds: | ||
| - go test ./... -coverprofile cover.out | ||
|
|
||
| view-coverage: | ||
| desc: "View test coverage in browser" | ||
| dir: '{{ .TASKFILE_DIR }}' | ||
| cmds: | ||
| - go tool cover -html=cover.out | ||
|
|
||
| build: | ||
| desc: "Build the lurker binary" | ||
| deps: [fmt, vet] | ||
| dir: '{{ .TASKFILE_DIR }}' | ||
| cmds: | ||
| - go build -o bin/lurker main.go |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // SPDX-FileCopyrightText: the secureCodeBox authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| const testFileContents = `{"findings": []}` | ||
|
|
||
| // writeResultFile creates a temporary scan result file and returns its path. | ||
| func writeResultFile(t *testing.T) string { | ||
| t.Helper() | ||
|
|
||
| path := filepath.Join(t.TempDir(), "findings.json") | ||
| if err := os.WriteFile(path, []byte(testFileContents), 0o600); err != nil { | ||
| t.Fatalf("failed to write test file: %v", err) | ||
| } | ||
| return path | ||
| } | ||
|
|
||
| // stubSleep replaces the backoff sleep with a recorder, so that tests don't | ||
| // have to actually wait. Returns a pointer to the recorded delays. | ||
| func stubSleep(t *testing.T) *[]time.Duration { | ||
| t.Helper() | ||
|
|
||
| original := sleep | ||
| delays := []time.Duration{} | ||
| sleep = func(d time.Duration) { | ||
| delays = append(delays, d) | ||
| } | ||
| t.Cleanup(func() { sleep = original }) | ||
|
|
||
| return &delays | ||
| } | ||
|
|
||
| func TestUploadFileWithRetriesSucceedsOnFirstAttempt(t *testing.T) { | ||
| delays := stubSleep(t) | ||
|
|
||
| requests := 0 | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| requests++ | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| if err := uploadFileWithRetries(writeResultFile(t), server.URL); err != nil { | ||
| t.Fatalf("expected upload to succeed, got: %v", err) | ||
| } | ||
| if requests != 1 { | ||
| t.Errorf("expected 1 request, got %d", requests) | ||
| } | ||
| if len(*delays) != 0 { | ||
| t.Errorf("expected no backoff, got %v", *delays) | ||
| } | ||
| } | ||
|
|
||
| func TestUploadFileWithRetriesDoesNotRetryHTTPFailures(t *testing.T) { | ||
| delays := stubSleep(t) | ||
|
|
||
| requests := 0 | ||
| receivedBodies := []string{} | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| requests++ | ||
| body, err := io.ReadAll(r.Body) | ||
| if err != nil { | ||
| t.Errorf("failed to read request body: %v", err) | ||
| } | ||
| receivedBodies = append(receivedBodies, string(body)) | ||
|
|
||
| w.WriteHeader(http.StatusInternalServerError) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| if err := uploadFileWithRetries(writeResultFile(t), server.URL); err == nil { | ||
| t.Fatal("expected upload to fail") | ||
| } | ||
| if requests != 1 { | ||
| t.Errorf("expected 1 request, got %d", requests) | ||
| } | ||
|
|
||
| // every attempt, especially the last one, has to send the complete file | ||
| for attempt, body := range receivedBodies { | ||
| if body != testFileContents { | ||
| t.Errorf("attempt %d uploaded %q, expected %q", attempt+1, body, testFileContents) | ||
| } | ||
| } | ||
|
|
||
| if len(*delays) != 0 { | ||
| t.Errorf("expected no backoff, got %v", *delays) | ||
| } | ||
| } | ||
|
|
||
| func TestUploadFileWithRetriesRetriesTransportErrors(t *testing.T) { | ||
| delays := stubSleep(t) | ||
|
|
||
| // closing the server up front makes every attempt fail on the transport level | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | ||
| url := server.URL | ||
| server.Close() | ||
|
|
||
| if err := uploadFileWithRetries(writeResultFile(t), url); err == nil { | ||
| t.Fatal("expected upload to fail") | ||
| } | ||
| if len(*delays) != len(uploadBackoffs) { | ||
| t.Errorf("expected %d backoffs, got %v", len(uploadBackoffs), *delays) | ||
| } | ||
| } | ||
|
|
||
| func TestUploadFileWithRetriesFailsForMissingFile(t *testing.T) { | ||
| delays := stubSleep(t) | ||
|
|
||
| if err := uploadFileWithRetries(filepath.Join(t.TempDir(), "does-not-exist.json"), "http://127.0.0.1:1"); err == nil { | ||
| t.Fatal("expected upload of a missing file to fail") | ||
| } | ||
| if len(*delays) != 0 { | ||
| t.Errorf("expected no backoff, got %v", *delays) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.