-
Notifications
You must be signed in to change notification settings - Fork 18
308 lines (291 loc) · 11.2 KB
/
go-test.yml
File metadata and controls
308 lines (291 loc) · 11.2 KB
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
name: go-test
on:
push:
branches:
- master
pull_request:
permissions:
pull-requests: read
contents: read
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
-
uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
with:
go-version: stable
check-latest: true
cache: true
-
name: golangci-lint
uses: golangci/golangci-lint-action@0a35821d5c230e903fcfe077583637dea1b27b47 # v9.0.0
with:
version: latest
only-new-issues: true
skip-cache: true
test:
name: Unit tests
runs-on: ${{ matrix.os }}
needs: [lint]
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
go: ['oldstable', 'stable' ]
steps:
-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
-
uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
with:
go-version: '${{ matrix.go }}'
check-latest: true
cache: true
-
name: Install Tools
# TODO: pin version -> fork + update dedicated github action
run: |
go install gotest.tools/gotestsum@latest
-
name: Run unit tests
shell: bash
run: >
gotestsum
--jsonfile 'unit.report.${{ matrix.os }}-${{ matrix.go }}.json'
--
-race
-p 2
-count 1
-timeout=20m
-coverprofile='unit.coverage.${{ matrix.os }}-${{ matrix.go }}.out'
-covermode=atomic
-coverpkg=$(go list)/...
./...
-
name: Upload coverage artifacts
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
with:
# *.coverage.* pattern is automatically detected by codecov
path: '**/*.coverage.*.out'
name: 'unit.coverage.${{ matrix.os }}-${{ matrix.go }}'
retention-days: 1
-
name: Upload test report artifacts
# upload report even if test fail. BTW, this is when they are valuable.
if: ${{ !cancelled() }}
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
with:
path: '**/unit.report.*.json'
name: 'unit.report.${{ matrix.os }}-${{ matrix.go }}'
retention-days: 1
fuzz-test:
name: fuzz test
runs-on: ubuntu-latest
env:
CORPUS_MAX_SIZE_MB: 100
steps:
-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
-
uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
with:
go-version: stable
check-latest: true
cache: true
-
name: Locate go fuzz cache
run: |
GOCACHE=$(go env GOCACHE)
echo "CORPUS_DIR=${GOCACHE}/fuzz" >> "${GITHUB_ENV}"
-
name: Retrieve fuzz corpus from cache
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
with:
key: ${{ runner.os }}-go-fuzz
path:
${{ env.CORPUS_DIR }}
-
name: Manage fuzz corpus cache size
run: |
mkdir -p "${CORPUS_DIR}"
CURRENT_SIZE=$(du -sm "${CORPUS_DIR}"|cut -f1)
echo "corpus size: ${CURRENT_SIZE}MB"
if [[ "${CURRENT_SIZE}" -gt "${CORPUS_MAX_SIZE}" ]] ; then
# remove the 50 oldest corpus files
echo "::warning:Large fuzz corpus pruned"
find "${CORPUS_DIR}" -type f|ls -t|tail -n +50|xargs rm -f
fi
-
name: Run go fuzz tests
run: >
go test
-fuzz=Fuzz
-run=Fuzz
-fuzztime=1m30s
-fuzzminimizetime=5m
./...
-
name: Upload failed corpus
if: ${{ failure() }}
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
with:
path: ${{ env.CORPUS_DIR }}
name: '${{ runner.os }}-fuzz-corpus-failure'
retention-days: 60
-
name: Report fuzz corpus cache size
run: |
FINAL_SIZE=$(du -m "${CORPUS_DIR}"|cut -f1)
echo "::notice title=fuzz corpus size:${FINAL_SIZE}MB"
test-complete:
# description: |
# Be explicit about all tests being passed. This allows for setting up only a few status checks on PRs.
name: tests completed
needs: [test,fuzz-test]
runs-on: ubuntu-latest
steps:
-
name: Tests completed
run: |
echo "::notice title=Success:All tests passed"
collect-coverage:
# description: |
# Gather, merge then uploads test coverage files from all test jobs (this includes integration tests,
# like codegen-test). This reduces the number of failures due to codecov hitting github API rate limit.
name: collect test coverage
needs: [test-complete]
if: ${{ !cancelled() && needs.test-complete.result == 'success' }}
runs-on: ubuntu-latest
steps:
-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
-
name: Download coverage artifacts
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
with:
run-id: "${{ github.run_id }}"
pattern: "*.coverage.*"
# artifacts resolve as folders
path: coverage/
-
name: Upload coverage to codecov
uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1
with:
name: Aggregated coverage
# All *.coverage.*.out files uploaded should be detected by the codecov action.
# NOTE: we lose the flags on individual test reports (e.g. by os, by go version, unit vs integration tests)
fail_ci_if_error: false
verbose: false
collect-reports:
# description: |
# Gather, merge then uploads test report files from unit test jobs.
#
# At this moment test reports are published on both codecov
# (see <https://app.codecov.io/gh/go-swagger/go-swagger/tests>) and the github actions UI
# (see <https://github.com/go-swagger/go-swagger/actions>).
name: collect test reports
needs: [test]
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
steps:
-
uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
with:
go-version: stable
check-latest: true
cache: true
-
name: Download test report artifacts
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
with:
run-id: "${{ github.run_id }}"
pattern: "*.report.*"
# artifacts resolve as folders
path: reports/
-
name: Convert test reports to a merged JUnit XML
# NOTE: codecov test reports only support JUnit format at this moment. See https://docs.codecov.com/docs/test-analytics.
# Ideally, codecov improve a bit their platform, so we may only need a single pass to CTRF format.
#
# As a contemplated alternative, we could use gotestsum above to produce the JUnit XML directly.
# At this moment, we keep a json format to dispatch test reports to codecov as well as to CTRF reports.
#
# TODO(fredbi): sec compliance - pin go-junit-report
# TODO(fredbi): investigate - use mikepenz/action-junit-report@v5, that packages most of the following scripts
# in a single action. Alternative: for that action.
run: |
go install github.com/jstemmer/go-junit-report/v2@latest
go-junit-report -version
find reports/ -name \*.json | xargs cat | go-junit-report -parser gojson -out=reports/junit_report.xml
-
name: Upload test results to Codecov
# This allows for using the test results UI on codecov
uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1
with:
files: '**/junit_report.xml'
report_type: 'test_results'
fail_ci_if_error: false
handle_no_reports_found: true
verbose: true
-
name: Convert test reports to CTRF JSON
# description: |
# This step publishes CTRF test reports on github UI (actions)
# TODO: pin this dependency
run: |
go install github.com/ctrf-io/go-ctrf-json-reporter/cmd/go-ctrf-json-reporter@v0.0.10
appName="${{ github.repository }}"
buildNumber="${{ github.run_id }}"
appVersion="${{ github.event.pull_request.head.sha }}"
if [[ -z "${appVersion}" ]] ; then
# for push events
appVersion="${{ github.sha }}"
fi
# reconstruct platform information from the file name
# set -x
while read report ; do
# 'unit.report.${{ matrix.os }}-${{ matrix.go }}.json'
reformated=$(echo "${report##*/}"|sed -E 's/(go)([[:digit:]]+)\.([[:digit:]]+)/\1\2\3/') # e.g. go1.24 becomes go124
mapfile -d'.' -t -s 2 -n 2 split < <(echo $reformated) # skip the first 2 parts, stop on 2 more parts
envstring="${split[0]}"
osPlatform="${envstring%-*}"
osRelease="${envstring##*-}"
# this is a best effort only: tests may be cancelled upstream and produce incorrect reports
go-ctrf-json-reporter \
-quiet \
-appName "${appName}" \
-appVersion "${appVersion}" \
-buildNumber "${buildNumber}" \
-osPlatform "${osPlatform}" \
-osRelease "${osRelease}" \
-output "./reports/ctrf_report_${osPlatform}_${osRelease}.json" < "${report}" || true
done < <(find reports -name \*.json)
# NOTE: at this moment, we don't upload CTRF reports as artifacts.
# Some of the CTRF reports are therefore not available (flaky tests, history, ...).
#
# See https://github.com/ctrf-io/github-test-reporter?tab=readme-ov-file#report-showcase
# for more reporting possibilities. At the moment, we keep it simple, as most advanced features
# require a github token (thus adding the complexity of a separate workflow starting on pull_request_target).
#
# For the moment, we are contented with these simple reports. This is an opportunity to compare the insight they
# provide as compared to what is uploaded to codecov.
#
# Codecov analytics are pretty poor at this moment. On the other hand, they manage the bot that pushes back
# PR comments.
#
# They also handle the storage of past test reports, so as to assess flaky tests.
-
name: Publish Test Summary Results
uses: ctrf-io/github-test-reporter@024bc4b64d997ca9da86833c6b9548c55c620e40 # v1.0.26
with:
report-path: 'reports/ctrf_report_*.json'
use-suite-name: true
summary-report: true # post a report to the github actions summary
github-report: true
failed-folded-report: true