|
17 | 17 | package signing
|
18 | 18 |
|
19 | 19 | import (
|
20 |
| - "fmt" |
| 20 | + "net/http" |
| 21 | + "net/http/httptest" |
21 | 22 | "os"
|
22 | 23 | "testing"
|
| 24 | + "time" |
23 | 25 |
|
24 | 26 | "github.com/ossf/scorecard-action/options"
|
25 | 27 | )
|
@@ -75,26 +77,128 @@ import (
|
75 | 77 | // }
|
76 | 78 | // }
|
77 | 79 |
|
78 |
| -// Test using scorecard results that have already been signed & uploaded. |
79 |
| -func Test_ProcessSignature(t *testing.T) { |
80 |
| - t.Parallel() |
81 |
| - |
82 |
| - jsonPayload, err := os.ReadFile("testdata/results.json") |
83 |
| - repoName := "ossf-tests/scorecard-action" |
84 |
| - repoRef := "refs/heads/main" |
85 |
| - accessToken := os.Getenv("GITHUB_AUTH_TOKEN") |
86 |
| - os.Setenv(options.EnvInputInternalPublishBaseURL, "https://api.securityscorecards.dev") |
| 80 | +//nolint:paralleltest // we are using t.Setenv |
| 81 | +func TestProcessSignature(t *testing.T) { |
| 82 | + tests := []struct { |
| 83 | + name string |
| 84 | + payloadPath string |
| 85 | + status int |
| 86 | + wantErr bool |
| 87 | + }{ |
| 88 | + { |
| 89 | + name: "post succeeded", |
| 90 | + status: http.StatusCreated, |
| 91 | + payloadPath: "testdata/results.json", |
| 92 | + wantErr: false, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "post failed", |
| 96 | + status: http.StatusBadRequest, |
| 97 | + payloadPath: "testdata/results.json", |
| 98 | + wantErr: true, |
| 99 | + }, |
| 100 | + } |
| 101 | + // use smaller backoffs for the test so they run faster |
| 102 | + setBackoffs(t, []time.Duration{0, time.Millisecond, 2 * time.Millisecond}) |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + jsonPayload, err := os.ReadFile(tt.payloadPath) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("Unexpected error reading testdata: %v", err) |
| 108 | + } |
| 109 | + repoName := "ossf-tests/scorecard-action" |
| 110 | + repoRef := "refs/heads/main" |
| 111 | + //nolint:gosec // dummy credentials |
| 112 | + accessToken := "ghs_foo" |
| 113 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 114 | + w.WriteHeader(tt.status) |
| 115 | + })) |
| 116 | + t.Setenv(options.EnvInputInternalPublishBaseURL, server.URL) |
| 117 | + t.Cleanup(server.Close) |
87 | 118 |
|
88 |
| - if err != nil { |
89 |
| - t.Errorf("Error reading testdata:, %v", err) |
| 119 | + s, err := New(accessToken) |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("Unexpected error New: %v", err) |
| 122 | + } |
| 123 | + err = s.ProcessSignature(jsonPayload, repoName, repoRef) |
| 124 | + if (err != nil) != tt.wantErr { |
| 125 | + t.Errorf("ProcessSignature() error: %v, wantErr: %v", err, tt.wantErr) |
| 126 | + } |
| 127 | + }) |
90 | 128 | }
|
| 129 | +} |
91 | 130 |
|
92 |
| - s, err := New(accessToken) |
93 |
| - if err != nil { |
94 |
| - panic(fmt.Sprintf("error SigningNew: %v", err)) |
| 131 | +//nolint:paralleltest // we are using t.Setenv |
| 132 | +func TestProcessSignature_retries(t *testing.T) { |
| 133 | + tests := []struct { |
| 134 | + name string |
| 135 | + nFailures int |
| 136 | + wantNRequests int |
| 137 | + wantErr bool |
| 138 | + }{ |
| 139 | + { |
| 140 | + name: "succeeds immediately", |
| 141 | + nFailures: 0, |
| 142 | + wantNRequests: 1, |
| 143 | + wantErr: false, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "one retry", |
| 147 | + nFailures: 1, |
| 148 | + wantNRequests: 2, |
| 149 | + wantErr: false, |
| 150 | + }, |
| 151 | + { |
| 152 | + // limit corresponds to backoffs set in test body |
| 153 | + name: "retry limit exceeded", |
| 154 | + nFailures: 4, |
| 155 | + wantNRequests: 3, |
| 156 | + wantErr: true, |
| 157 | + }, |
95 | 158 | }
|
96 |
| - if err := s.ProcessSignature(jsonPayload, repoName, repoRef); err != nil { |
97 |
| - t.Errorf("ProcessSignature() error:, %v", err) |
98 |
| - return |
| 159 | + // use smaller backoffs for the test so they run faster |
| 160 | + setBackoffs(t, []time.Duration{0, time.Millisecond, 2 * time.Millisecond}) |
| 161 | + for _, tt := range tests { |
| 162 | + t.Run(tt.name, func(t *testing.T) { |
| 163 | + var jsonPayload []byte |
| 164 | + repoName := "ossf-tests/scorecard-action" |
| 165 | + repoRef := "refs/heads/main" |
| 166 | + //nolint:gosec // dummy credentials |
| 167 | + accessToken := "ghs_foo" |
| 168 | + var nRequests int |
| 169 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 170 | + nRequests++ |
| 171 | + status := http.StatusCreated |
| 172 | + if tt.nFailures > 0 { |
| 173 | + status = http.StatusBadRequest |
| 174 | + tt.nFailures-- |
| 175 | + } |
| 176 | + w.WriteHeader(status) |
| 177 | + })) |
| 178 | + t.Setenv(options.EnvInputInternalPublishBaseURL, server.URL) |
| 179 | + t.Cleanup(server.Close) |
| 180 | + |
| 181 | + s, err := New(accessToken) |
| 182 | + if err != nil { |
| 183 | + t.Fatalf("Unexpected error New: %v", err) |
| 184 | + } |
| 185 | + err = s.ProcessSignature(jsonPayload, repoName, repoRef) |
| 186 | + if (err != nil) != tt.wantErr { |
| 187 | + t.Errorf("ProcessSignature() error: %v, wantErr: %v", err, tt.wantErr) |
| 188 | + } |
| 189 | + if nRequests != tt.wantNRequests { |
| 190 | + t.Errorf("ProcessSignature() made %d requests, wanted %d", nRequests, tt.wantNRequests) |
| 191 | + } |
| 192 | + }) |
99 | 193 | }
|
100 | 194 | }
|
| 195 | + |
| 196 | +// temporarily sets the backoffs for a given test. |
| 197 | +func setBackoffs(t *testing.T, newBackoffs []time.Duration) { |
| 198 | + t.Helper() |
| 199 | + old := backoffSchedule |
| 200 | + backoffSchedule = newBackoffs |
| 201 | + t.Cleanup(func() { |
| 202 | + backoffSchedule = old |
| 203 | + }) |
| 204 | +} |
0 commit comments