-
Notifications
You must be signed in to change notification settings - Fork 19
/
.gitlab-ci-check-golang-unittests.yml
144 lines (135 loc) · 5.23 KB
/
.gitlab-ci-check-golang-unittests.yml
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
# .gitlab-ci-check-golang-unittests.yml
#
# This gitlab-ci template runs unit tests and publishes code coverage
# from a Go repository
#
# Add it to the project in hand through Gitlab's include functionality
#
# include:
# - project: 'Northern.tech/Mender/mendertesting'
# file: '.gitlab-ci-check-golang-unittests.yml'
#
# If the repo has some compile time dependencies, the template expect to
# exist a deb-requirements.txt file with the Debian OS required packages
#
# The template uses latest Mender supported golang version.
# To override the version to use, append to your .gitlab-ci.yml:
# test:unit:
# image: golang:1.11.4
#
# Requires the following variables set in the project CI/CD settings:
# COVERALLS_TOKEN: Token from coveralls.io for this repository
#
# By default, it installs MongoDB version 4.4 for Debian buster. You can
# specify the Debian release name for the upstream APT repository and
# the MongoDB version overriding the variables below:
#
# test:unit:
# variables:
# MONGODB_DEBIAN_RELEASE: "stretch"
# MONGODB_VERSION: "3.6"
#
stages:
- test
- publish
test:unit:
tags:
- mender-qa-worker-generic-light
stage: test
needs: []
except:
- /^saas-[a-zA-Z0-9.]+$/
image: golang:1.20.4
services:
- mongo:6.0
variables:
GIT_STRATEGY: clone # clone entire repo instead of reusing workspace
GIT_DEPTH: 0 # avoid shallow clone, this test requires full git history
MONGODB_DEBIAN_RELEASE: "buster"
MONGODB_VERSION: "4.4"
TEST_MONGO_URL: "mongodb://mongo"
before_script:
# Install JUnit test reporting formatter
- go install github.com/jstemmer/[email protected]
# mongodb-org package needs systemd, this circumnavigates the issue providing a fake systemctl command
- ln -s /bin/true /usr/sbin/systemctl
# Install mongodb for the tests that use it locally
- wget -qO - https://www.mongodb.org/static/pgp/server-${MONGODB_VERSION}.asc | apt-key add -
- echo "deb http://repo.mongodb.org/apt/debian ${MONGODB_DEBIAN_RELEASE}/mongodb-org/${MONGODB_VERSION} main" | tee /etc/apt/sources.list.d/mongodb-org-${MONGODB_VERSION}.list
- apt-get -qq update
- apt-get install -qy mongodb-org
# Install compile dependencies
- if [ -f deb-requirements.txt ]; then
- apt install -yq $(cat deb-requirements.txt)
- fi
# Rename the branch we're on, so that it's not in the way for the
# subsequent fetch. It's ok if this fails, it just means we're not on any
# branch.
- git branch -m temp-branch || true
# Git trick: Fetch directly into our local branches instead of remote
# branches.
- git fetch -f origin 'refs/heads/*:refs/heads/*'
# Get last remaining tags, if any.
- git fetch --tags origin
# Prepare GOPATH
- mkdir -p /go/src/github.com/mendersoftware
- cp -r ${CI_PROJECT_DIR} /go/src/github.com/mendersoftware/${CI_PROJECT_NAME}
- cd /go/src/github.com/mendersoftware/${CI_PROJECT_NAME}
script:
- go list ./... | grep -v vendor | xargs -n1 -I {} go test -v -covermode=atomic -coverprofile=../../../{}/coverage.txt {} 2>&1 | tee /dev/stderr | go-junit-report > ${CI_PROJECT_DIR}/test-results.xml || exit $?
- mkdir -p tests/unit-coverage && find . -name 'coverage.txt' -exec cp --parents {} ./tests/unit-coverage \;
- tar -cvf ${CI_PROJECT_DIR}/unit-coverage.tar tests/unit-coverage
artifacts:
expire_in: 2w
paths:
- unit-coverage.tar
reports:
junit: test-results.xml
when: always
publish:unittests:
tags:
- mender-qa-worker-generic-light
stage: publish
except:
- /^saas-[a-zA-Z0-9.]+$/
image: golang:1.20-alpine3.17
dependencies:
- test:unit
before_script:
# Install dependencies
- apk add --no-cache git
- go install github.com/mattn/goveralls@latest
# Prepare GOPATH
- mkdir -p /go/src/github.com/mendersoftware
- cp -r ${CI_PROJECT_DIR} /go/src/github.com/mendersoftware/${CI_PROJECT_NAME}
- cd /go/src/github.com/mendersoftware/${CI_PROJECT_NAME}
# Coveralls env variables:
# According to https://docs.coveralls.io/supported-ci-services
# we should set CI_NAME, CI_BUILD_NUMBER, etc. But according
# to goveralls source code (https://github.com/mattn/goveralls)
# many of these are not supported. Set CI_BRANCH,
# and pass few others as command line arguments.
# See also https://docs.coveralls.io/api-reference
- export CI_BRANCH=${CI_COMMIT_BRANCH}
script:
- tar -xvf unit-coverage.tar
- goveralls
-repotoken ${COVERALLS_TOKEN}
-service gitlab-ci
-jobid $CI_PIPELINE_ID
-covermode set
-flagname unittests
-parallel
-coverprofile $(find tests/unit-coverage -name 'coverage.txt' | tr '\n' ',' | sed 's/,$//')
coveralls:finish-build:
tags:
- mender-qa-worker-generic-light
stage: .post
# See https://docs.coveralls.io/parallel-build-webhook
variables:
COVERALLS_WEBHOOK_URL: "https://coveralls.io/webhook"
COVERALLS_RERUN_BUILD_URL: "https://coveralls.io/rerun_build"
image: curlimages/curl-base
script:
- 'curl -k ${COVERALLS_WEBHOOK_URL}?repo_token=${COVERALLS_TOKEN} -d "payload[build_num]=$CI_PIPELINE_ID&payload[status]=done"'
- 'curl -k "${COVERALLS_RERUN_BUILD_URL}?repo_token=${COVERALLS_TOKEN}&build_num=${CI_PIPELINE_ID}"'