This repository has been archived by the owner on Aug 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
main_test.go
191 lines (183 loc) · 4.33 KB
/
main_test.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
package main
import (
"context"
"net/http"
"net/http/httptest"
"reflect"
"sort"
"sync"
"testing"
"time"
)
func TestAggregateIssues(t *testing.T) {
issues := func(issues ...issue) []issue {
return issues
}
ignoredIssue := func(id, severity, title string) issue {
return issue{
ID: id,
IssueData: issueData{
Severity: severity,
Title: title,
},
Ignored: true,
}
}
iss := func(id, severity, title string) issue {
return issue{
ID: id,
IssueData: issueData{
Severity: severity,
Title: title,
},
}
}
aggregateResults := func(aggregateResults ...aggregateResult) []aggregateResult {
return aggregateResults
}
result := func(severity, title string, count int, ignored bool) aggregateResult {
return aggregateResult{
severity: severity,
title: title,
count: count,
ignored: ignored,
}
}
tt := []struct {
name string
issues []issue
aggregates []aggregateResult
}{
{
name: "nil issues",
issues: nil,
aggregates: nil,
},
{
name: "single issue",
issues: issues(iss("iss-1", "high", "DDoS")),
aggregates: aggregateResults(result("high", "DDoS", 1, false)),
},
{
name: "multiple of different severity and same title",
issues: issues(
iss("iss-1", "high", "DDoS"),
iss("iss-2", "low", "DDoS"),
),
aggregates: aggregateResults(
result("high", "DDoS", 1, false),
result("low", "DDoS", 1, false),
),
},
{
name: "multiple of same severity and title",
issues: issues(
iss("iss-1", "high", "DDoS"),
iss("iss-2", "high", "DDoS"),
),
aggregates: aggregateResults(
result("high", "DDoS", 2, false),
),
},
{
name: "multiple of same severity and title but some ignored",
issues: issues(
iss("iss-1", "high", "DDoS"),
ignoredIssue("iss-2", "high", "DDoS"),
),
aggregates: aggregateResults(
result("high", "DDoS", 1, false),
result("high", "DDoS", 1, true),
),
},
{
name: "multiple of same severity different title",
issues: issues(
iss("iss-1", "high", "DDoS"),
iss("iss-2", "high", "ReDoS"),
),
aggregates: aggregateResults(
result("high", "DDoS", 1, false),
result("high", "ReDoS", 1, false),
),
},
{
name: "multiple of same severity different title some ignored",
issues: issues(
iss("iss-1", "high", "DDoS"),
ignoredIssue("iss-2", "high", "ReDoS"),
),
aggregates: aggregateResults(
result("high", "DDoS", 1, false),
result("high", "ReDoS", 1, true),
),
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
output := aggregateIssues(tc.issues)
if len(output) != len(tc.aggregates) {
t.Logf("output: %v\n", output)
t.Errorf("Length of aggregate results not as expected: expected %d got %d", len(tc.aggregates), len(output))
return
}
// sort as aggregateIssues does not provide a stable ordered slice
sort.Slice(output, func(i, j int) bool {
return aggregationKey(issue{
IssueData: issueData{
Severity: output[i].severity,
Title: output[i].title,
},
Ignored: output[i].ignored,
}) < aggregationKey(issue{
IssueData: issueData{
Severity: output[j].severity,
Title: output[j].title,
},
Ignored: output[j].ignored,
})
})
if !reflect.DeepEqual(output, tc.aggregates) {
t.Errorf("Aggregates are not matching expectations: expected %v got %v", tc.aggregates, output)
}
})
}
}
func TestRunAPIPolling_issuesTimeout(t *testing.T) {
calls := 0
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
calls++
// allow organizations call to succeed
if calls == 1 {
//nolint:errcheck
rw.Write([]byte(`{
"orgs": [{
"id": "id",
"name": "name"
}]
}`))
return
}
time.Sleep(1 * time.Second)
rw.WriteHeader(http.StatusOK)
}))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err := runAPIPolling(ctx, server.URL, "token", nil, 20*time.Millisecond, 1*time.Millisecond)
if err != nil {
t.Errorf("unexpected error result: %v", err)
}
}()
// stop the polling again after 100ms
<-time.After(100 * time.Millisecond)
cancel()
// wait for the polling to stop
wg.Wait()
if !ready {
t.Fatalf("Ready not set but it should be")
}
}