cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.13.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/reporter/github_test.go

174lines · modecode

1package reporter_test
2
3import (
4 "fmt"
5 "net/http"
6 "net/http/httptest"
7 "testing"
8 "time"
9
10 "github.com/rs/zerolog"
11
12 "github.com/cloudflare/pint/internal/checks"
13 "github.com/cloudflare/pint/internal/discovery"
14 "github.com/cloudflare/pint/internal/git"
15 "github.com/cloudflare/pint/internal/parser"
16 "github.com/cloudflare/pint/internal/reporter"
17)
18
19func TestGithubReporter(t *testing.T) {
20 zerolog.SetGlobalLevel(zerolog.FatalLevel)
21
22 type errorCheck func(t *testing.T, err error) error
23
24 type testCaseT struct {
25 description string
26 summary reporter.Summary
27 httpHandler http.Handler
28 errorHandler errorCheck
29 gitCmd git.CommandRunner
30
31 owner string
32 repo string
33 token string
34 prNum int
35 timeout time.Duration
36 }
37
38 p := parser.NewParser()
39 mockRules, _ := p.Parse([]byte(`
40- record: target is down
41 expr: up == 0
42- record: sum errors
43 expr: sum(errors) by (job)
44`))
45
46 for _, tcase := range []testCaseT{
47 {
48 description: "timeout errors out",
49 owner: "foo",
50 repo: "bar",
51 token: "something",
52 prNum: 123,
53 httpHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54 time.Sleep(1 * time.Second)
55 _, _ = w.Write([]byte("OK"))
56 }),
57 timeout: 100 * time.Millisecond,
58 gitCmd: func(args ...string) ([]byte, error) {
59 if args[0] == "rev-parse" {
60 return []byte("fake-commit-id"), nil
61 }
62 if args[0] == "blame" {
63 content := blameLine("fake-commit-id", 2, "foo.txt", "up == 0")
64 return []byte(content), nil
65 }
66 return nil, nil
67 },
68 errorHandler: func(t *testing.T, err error) error {
69 if err == nil {
70 return fmt.Errorf("expected an error")
71 }
72 if err.Error() != "creating review: context deadline exceeded" {
73 return fmt.Errorf("unexpected error")
74 }
75 return nil
76 },
77 summary: reporter.Summary{
78 Reports: []reporter.Report{
79 {
80 Path: "foo.txt",
81 Rule: mockRules[1],
82 Problem: checks.Problem{
83 Fragment: "syntax error",
84 Lines: []int{2},
85 Reporter: "mock",
86 Text: "syntax error",
87 Severity: checks.Fatal,
88 },
89 },
90 },
91 FileChanges: discovery.NewFileCommitsFromMap(map[string][]string{"foo.txt": {"fake-commit-id"}}),
92 },
93 },
94 {
95 description: "happy path",
96 owner: "foo",
97 repo: "bar",
98 token: "something",
99 prNum: 123,
100 timeout: 1000 * time.Millisecond,
101 errorHandler: func(t *testing.T, err error) error {
102 return err
103 },
104 gitCmd: func(args ...string) ([]byte, error) {
105 if args[0] == "rev-parse" {
106 return []byte("fake-commit-id"), nil
107 }
108 if args[0] == "blame" {
109 content := blameLine("fake-commit-id", 2, "foo.txt", "up == 0")
110 return []byte(content), nil
111 }
112 return nil, nil
113 },
114 summary: reporter.Summary{
115 Reports: []reporter.Report{
116 {
117 Path: "foo.txt",
118 Rule: mockRules[1],
119 Problem: checks.Problem{
120 Fragment: "syntax error",
121 Lines: []int{2},
122 Reporter: "mock",
123 Text: "syntax error",
124 Severity: checks.Fatal,
125 },
126 },
127 },
128 FileChanges: discovery.NewFileCommitsFromMap(map[string][]string{"foo.txt": {"fake-commit-id"}}),
129 },
130 },
131 } {
132 t.Run(tcase.description, func(t *testing.T) {
133 var handler http.Handler
134 if tcase.httpHandler != nil {
135 handler = tcase.httpHandler
136 } else {
137 // Handler that checks for token.
138 handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
139 auth := r.Header["Authorization"]
140 if len(auth) == 0 {
141 w.WriteHeader(500)
142 _, _ = w.Write([]byte("No token"))
143 t.Fatal("got a request with no token")
144 return
145 }
146 token := auth[0]
147 if token != fmt.Sprintf("Bearer %s", tcase.token) {
148 w.WriteHeader(500)
149 _, _ = w.Write([]byte("Invalid token"))
150 t.Fatalf("got a request with invalid token (got %s)", token)
151 }
152 })
153 }
154 srv := httptest.NewServer(handler)
155 defer srv.Close()
156 reporter := reporter.NewGithubReporter(
157 srv.URL,
158 srv.URL,
159 tcase.timeout,
160 tcase.token,
161 tcase.owner,
162 tcase.repo,
163 tcase.prNum,
164 tcase.gitCmd,
165 )
166
167 err := reporter.Submit(tcase.summary)
168 if e := tcase.errorHandler(t, err); e != nil {
169 t.Errorf("error check failure: %s", e)
170 return
171 }
172 })
173 }
174}
175