cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.4.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/reporter/github_test.go

173lines · modecode

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