-
Notifications
You must be signed in to change notification settings - Fork 33
/
api.go
355 lines (319 loc) · 9.73 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package server
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"reflect"
"runtime"
"strings"
"time"
"github.com/dependabot/cli/internal/model"
"gopkg.in/yaml.v3"
)
// API intercepts calls to the Dependabot API
type API struct {
// Expectations is the list of expectations that haven't been met yet
Expectations []model.Output
// Errors is the error list populated by doing a Dependabot run
Errors []error
// Actual will contain the scenario output that actually happened after the run is Complete
Actual model.Scenario
server *http.Server
cursor int
hasExpectations bool
port int
writer io.Writer
}
// NewAPI creates a new API instance and starts the server
func NewAPI(expected []model.Output, writer io.Writer) *API {
fakeAPIHost := "127.0.0.1"
if runtime.GOOS == "linux" {
fakeAPIHost = "0.0.0.0"
}
if os.Getenv("FAKE_API_HOST") != "" {
fakeAPIHost = os.Getenv("FAKE_API_HOST")
}
// Bind to port 0 for arbitrary port assignment
port := "0"
if os.Getenv("FAKE_API_PORT") != "" {
port = os.Getenv("FAKE_API_PORT")
}
l, err := net.Listen("tcp", fakeAPIHost+":"+port)
if err != nil {
panic(err)
}
server := &http.Server{
ReadTimeout: 5 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
}
api := &API{
server: server,
Expectations: expected,
writer: writer,
cursor: 0,
hasExpectations: len(expected) > 0,
port: l.Addr().(*net.TCPAddr).Port,
}
server.Handler = api
go func() {
if err := server.Serve(l); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()
return api
}
// Port returns the port the API is listening on
func (a *API) Port() int {
return a.port
}
// Stop stops the server
func (a *API) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
_ = a.server.Shutdown(ctx)
cancel()
}
// Complete adds any remaining expectations to the error queue
func (a *API) Complete() {
for i := a.cursor; i < len(a.Expectations); i++ {
exp := &a.Expectations[i]
a.Errors = append(a.Errors, fmt.Errorf("expectation not met: %v\n%v", exp.Type, exp.Expect))
}
}
// ServeHTTP handles requests to the server
func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data, err := io.ReadAll(r.Body)
if err != nil {
err = fmt.Errorf("failed to read body: %w", err)
a.pushError(err)
return
}
if err = r.Body.Close(); err != nil {
err = fmt.Errorf("failed to close body: %w", err)
a.pushError(err)
return
}
parts := strings.Split(r.URL.String(), "/")
kind := parts[len(parts)-1]
actual, err := decodeWrapper(kind, data)
if err != nil {
a.pushError(err)
}
if actual == nil {
// indicates the kind (endpoint) isn't implemented in decodeWrapper, so return a 501
w.WriteHeader(http.StatusNotImplemented)
return
}
if kind == "increment_metric" {
// Let's just output the metrics data and stop
a.outputRequestData(kind, actual)
return
}
if err := a.pushResult(kind, actual); err != nil {
a.pushError(err)
return
}
if !a.hasExpectations {
a.outputRequestData(kind, actual)
return
}
a.assertExpectation(kind, actual)
}
func (a *API) assertExpectation(kind string, actual *model.UpdateWrapper) {
if len(a.Expectations) <= a.cursor {
err := fmt.Errorf("missing expectation")
a.pushError(err)
return
}
expect := &a.Expectations[a.cursor]
a.cursor++
if kind != expect.Type {
err := fmt.Errorf("type was unexpected: expected %v got %v", expect.Type, kind)
a.pushError(err)
return
}
// need to use decodeWrapper to get the right type to match the actual type
data, err := json.Marshal(expect.Expect)
if err != nil {
panic(err)
}
expected, err := decodeWrapper(expect.Type, data)
if err != nil {
panic(err)
}
if err = compare(expected, actual); err != nil {
a.pushError(err)
}
}
func (a *API) outputRequestData(kind string, actual *model.UpdateWrapper) {
if a.writer != nil {
// output the data received to stdout
if err := json.NewEncoder(a.writer).Encode(map[string]any{
"type": kind,
"data": actual.Data,
}); err != nil {
// Fail so the user knows stdout is not working
log.Panicln("Failed to write to stdout: ", err)
}
}
}
func (a *API) pushError(err error) {
escapedError := strings.ReplaceAll(err.Error(), "\n", "")
escapedError = strings.ReplaceAll(escapedError, "\r", "")
log.Println(escapedError)
a.Errors = append(a.Errors, err)
}
func (a *API) pushResult(kind string, actual *model.UpdateWrapper) error {
// TODO validate required data
output := model.Output{
Type: kind,
Expect: *actual,
}
a.Actual.Output = append(a.Actual.Output, output)
if msg, ok := actual.Data.(model.MarkAsProcessed); ok {
// record the commit SHA so the test is reproducible
a.Actual.Input.Job.Source.Commit = msg.BaseCommitSha
}
return nil
}
func decodeWrapper(kind string, data []byte) (actual *model.UpdateWrapper, err error) {
actual = &model.UpdateWrapper{}
switch kind {
case "update_dependency_list":
actual.Data, err = decode[model.UpdateDependencyList](data)
case "create_pull_request":
var createPR model.CreatePullRequest
createPR, err = decode[model.CreatePullRequest](data)
createPR.UpdatedDependencyFiles = replaceBinaryWithHash(createPR.UpdatedDependencyFiles)
actual.Data = createPR
case "update_pull_request":
var updatePR model.UpdatePullRequest
updatePR, err = decode[model.UpdatePullRequest](data)
updatePR.UpdatedDependencyFiles = replaceBinaryWithHash(updatePR.UpdatedDependencyFiles)
actual.Data = updatePR
case "close_pull_request":
actual.Data, err = decode[model.ClosePullRequest](data)
case "mark_as_processed":
actual.Data, err = decode[model.MarkAsProcessed](data)
case "record_ecosystem_versions":
actual.Data, err = decode[model.RecordEcosystemVersions](data)
case "record_update_job_error":
actual.Data, err = decode[model.RecordUpdateJobError](data)
case "record_update_job_unknown_error":
actual.Data, err = decode[model.RecordUpdateJobUnknownError](data)
case "increment_metric":
actual.Data, err = decode[model.IncrementMetric](data)
default:
return nil, fmt.Errorf("unexpected output type: %s", kind)
}
return actual, err
}
// to avoid having massive base64 encoded strings in the test fixtures, replace the content with a hash
func replaceBinaryWithHash(files []model.DependencyFile) []model.DependencyFile {
for i := range files {
file := &files[i]
if file.ContentEncoding == "base64" {
// since this is also called for the expected value, this needs to not be base64
// otherwise it will calculate the checksum of the checksum
file.ContentEncoding = "sha256"
hash := sha256.Sum256([]byte(file.Content))
file.Content = hex.EncodeToString(hash[:])
}
}
return files
}
func decode[T any](data []byte) (T, error) {
var wrapper struct {
Data T `json:"data" yaml:"data"`
}
decoder := yaml.NewDecoder(bytes.NewBuffer(data))
decoder.KnownFields(true)
err := decoder.Decode(&wrapper)
if err != nil {
return *new(T), err
}
return wrapper.Data, nil
}
func compare(expect, actual *model.UpdateWrapper) error {
switch v := expect.Data.(type) {
case model.UpdateDependencyList:
return compareUpdateDependencyList(v, actual.Data.(model.UpdateDependencyList))
case model.CreatePullRequest:
return compareCreatePullRequest(v, actual.Data.(model.CreatePullRequest))
case model.UpdatePullRequest:
return compareUpdatePullRequest(v, actual.Data.(model.UpdatePullRequest))
case model.ClosePullRequest:
return compareClosePullRequest(v, actual.Data.(model.ClosePullRequest))
case model.RecordEcosystemVersions:
return compareRecordEcosystemVersions(v, actual.Data.(model.RecordEcosystemVersions))
case model.MarkAsProcessed:
return compareMarkAsProcessed(v, actual.Data.(model.MarkAsProcessed))
case model.RecordUpdateJobError:
return compareRecordUpdateJobError(v, actual.Data.(model.RecordUpdateJobError))
case model.RecordUpdateJobUnknownError:
return compareRecordUpdateJobUnknownError(v, actual.Data.(model.RecordUpdateJobUnknownError))
default:
return fmt.Errorf("unexpected type: %s", reflect.TypeOf(v))
}
}
func unexpectedBody(kind string) error {
return fmt.Errorf("unexpected body for %s", kind)
}
func compareUpdateDependencyList(expect, actual model.UpdateDependencyList) error {
if reflect.DeepEqual(expect, actual) {
return nil
}
return unexpectedBody("update_dependency_list")
}
func compareCreatePullRequest(expect, actual model.CreatePullRequest) error {
if reflect.DeepEqual(expect, actual) {
return nil
}
return unexpectedBody("create_pull_request")
}
func compareUpdatePullRequest(expect, actual model.UpdatePullRequest) error {
if reflect.DeepEqual(expect, actual) {
return nil
}
return unexpectedBody("update_pull_request")
}
func compareClosePullRequest(expect, actual model.ClosePullRequest) error {
if reflect.DeepEqual(expect, actual) {
return nil
}
return unexpectedBody("close_pull_request")
}
func compareRecordEcosystemVersions(expect, actual model.RecordEcosystemVersions) error {
if reflect.DeepEqual(expect, actual) {
return nil
}
return unexpectedBody("record_ecosystem_versions")
}
func compareMarkAsProcessed(expect, actual model.MarkAsProcessed) error {
if reflect.DeepEqual(expect, actual) {
return nil
}
return unexpectedBody("mark_as_processed")
}
func compareRecordUpdateJobError(expect, actual model.RecordUpdateJobError) error {
if reflect.DeepEqual(expect, actual) {
return nil
}
return unexpectedBody("record_update_job_error")
}
func compareRecordUpdateJobUnknownError(expect, actual model.RecordUpdateJobUnknownError) error {
if reflect.DeepEqual(expect, actual) {
return nil
}
return unexpectedBody("record_update_job_unknown_error")
}