forked from RedHatInsights/vmaas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
236 lines (213 loc) · 10.5 KB
/
Jenkinsfile
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
/*
* Pipeline for running integration tests in Jenkins job with
* every pull request or change in master
*
* Requires: https://github.com/RedHatInsights/insights-pipeline-lib
*/
@Library("github.com/RedHatInsights/insights-pipeline-lib@v3") _
// Code coverage failure threshold
codecovThreshold = 89
if (env.BRANCH_NAME == 'master') {
pipelineUtils.cancelPriorBuilds()
runStages()
}
def deployVmaas(project) {
gitUtils.withStatusContext("deploy") {
dir(pipelineVars.e2eDeployDir) {
String GIT_REF = "${env.BRANCH_NAME}"
if (env.BRANCH_NAME.startsWith("PR")) {
String GIT_PR = "${env.BRANCH_NAME}" - "PR-"
GIT_REF = "+refs/pull/${GIT_PR}/head"
}
// set needed env.yml
// build vmaas-webapp from Dockerfile-webapp-qe - it won't start main.py
sh """
# Create an env.yaml to have the builder pull from a different branch
echo "vmaas/vmaas-reposcan:" >> env/builder-env.yml
echo " parameters:" >> env/builder-env.yml
echo " SOURCE_REPOSITORY_REF: ${GIT_REF}" >> env/builder-env.yml
echo " SOURCE_REPOSITORY_URL: ${scmVars.GIT_URL}" >> env/builder-env.yml
echo "vmaas/vmaas-webapp:" >> env/builder-env.yml
echo " parameters:" >> env/builder-env.yml
echo " SOURCE_REPOSITORY_REF: ${GIT_REF}" >> env/builder-env.yml
echo " SOURCE_REPOSITORY_URL: ${scmVars.GIT_URL}" >> env/builder-env.yml
echo " DOCKERFILE_PATH: webapp/Dockerfile-qe" >> env/builder-env.yml
echo "vmaas/vmaas-websocket:" >> env/builder-env.yml
echo " parameters:" >> env/builder-env.yml
echo " SOURCE_REPOSITORY_REF: ${GIT_REF}" >> env/builder-env.yml
echo " SOURCE_REPOSITORY_URL: ${scmVars.GIT_URL}" >> env/builder-env.yml
echo "vmaas/vmaas-database:" >> env/builder-env.yml
echo " parameters:" >> env/builder-env.yml
echo " SOURCE_REPOSITORY_REF: ${GIT_REF}" >> env/builder-env.yml
echo " SOURCE_REPOSITORY_URL: ${scmVars.GIT_URL}" >> env/builder-env.yml
# Deploy these customized builders into 'vmaas-qe' project
ocdeployer deploy -f --sets vmaas --template-dir buildfactory \
-e builder-env ${project} --secrets-src-project secrets
"""
// deploy vmaas service set
sh "ocdeployer deploy -f --sets vmaas -e vmaas-qe \
${project} --secrets-src-project secrets"
}
}
}
def runStages() {
// withNode is a helper to spin up a jnlp slave using the Kubernetes plugin, and run the body code on that slave
openShiftUtils.withNode() {
lock("vmaas-qe-deploy") {
stage("Check out repos") {
// check out source again to get it in this node"s workspace
scmVars = checkout scm
gitUtils.checkOutRepo(targetDir: pipelineVars.e2eDeployDir, repoUrl: pipelineVars.e2eDeployRepoSsh, credentialsId: pipelineVars.gitSshCreds)
// checkout vmaas_tests git repository
gitUtils.checkOutRepo(targetDir: "vulnerability", repoUrl: "https://github.com/RedHatInsights/vmaas_tests", credentialsId: "github")
}
stage("Login to oc cluster") {
withCredentials([string(credentialsId: "openshift_token", variable: "TOKEN")]) {
sh "oc login https://${pipelineVars.devCluster} --token=${TOKEN}"
}
sh "oc project vmaas-qe"
}
stage("Pip install") {
String pip_check
try {
sh "pip install -U iqe-integration-tests pytest-html"
sh "iqe plugin install vulnerability"
dir(pipelineVars.e2eDeployDir) {
sh "pip install -r requirements.txt"
}
pip_check = sh(
script: "pip check || true",
returnStdout: true
).trim()
if (pip_check.contains("has requirement attrs>=19.2.0")) {
// workaround for incorrect pinned version in iqe-tests
sh "pip install attrs>=19.2.0"
}
} catch (err) {
echo("Error during installing test dependencies!")
echo(err.toString())
throw err
}
}
stage("Inject local settings") {
withCredentials([file(credentialsId: "vmaas-settings-local-yaml", variable: "settings")]) {
sh "cp \$settings \$IQE_VENV/lib/python3.6/site-packages/iqe_vulnerability/conf"
}
}
stage("Wipe test environment") {
sh "ocdeployer wipe -f vmaas-qe -l app=vmaas"
// make sure that DB volume is deleted
sh "oc delete pvc vmaas-database-data || true"
}
stage("Deploy VMaaS") {
// Deploy VMaaS to vmaas-qe project
try {
deployVmaas("vmaas-qe")
} catch (err) {
echo("Error during VMaaS deploy! Look at oc logs in Artifacts.")
echo(err.toString())
openShiftUtils.collectLogs(project: "vmaas-qe")
throw err
}
}
stage("Integration tests") {
// Run integration tests
gitUtils.withStatusContext("integration-tests") {
stage("Run vmaas-webapp with coverage") {
// Start webapp as `coverage run ...` instead of `python ...` to collect coverage
sh '''
webapp_pod="$(oc get pods | grep 'Running' | grep 'webapp' | awk '{print $1}')"
oc exec "${webapp_pod}" -- bash -c "coverage run main.py --source webapp &>/proc/1/fd/1" &
'''
}
stage("Setup DB") {
withCredentials([string(credentialsId: "vmaas-bot-token", variable: "TOKEN"),
file(credentialsId: "repolist-json", variable: "REPOLIST")]) {
sh """
cd vulnerability
vmaas/scripts/setup_db.sh ${REPOLIST} \
http://vmaas-reposcan.vmaas-qe.svc:8081 \
http://vmaas-webapp.vmaas-qe.svc:8080 \
${TOKEN}
sleep 10
"""
}
}
stage("Run tests") {
// Running pytest can result in six different exit codes:
// 0: All tests were collected and passed successfully
// 1: Tests were collected and run but some of the tests failed
// 2: Test execution was interrupted by the user
// 3: Internal error happened while executing tests
// 4: pytest command line usage error
// 5: No tests were collected
String LONG_RUNNING = ""
if (env.BRANCH_NAME == "master") {
LONG_RUNNING = "--long-running"
}
pytest_status = sh(
script: "iqe tests plugin vulnerability -vvv -r s -k vmaas/ ${LONG_RUNNING} --html='report.html' --self-contained-html --generate-report",
returnStatus: true
)
assert pytest_status <= 1 : "Pytest error: ${pytest_status}"
sh '''
mkdir html_report
mv report.html html_report
'''
}
}
openShiftUtils.collectLogs(project: "vmaas-qe")
junit "iqe-junit-report.xml"
}
stage("Code coverage") {
// Notifies GitHub with the "continuous-integration/jenkins/coverage" status
// Kill webapp and copy coverage in html format
webapp_pod = sh(
script: "oc get pods | grep 'Running' | grep 'webapp' | awk '{print \$1}'",
returnStdout: true
).trim()
sh "oc exec ${webapp_pod} -- pkill -sigint coverage"
def status = 99
status = sh(
script: "oc exec ${webapp_pod} -- coverage html --fail-under=${codecovThreshold} --omit /usr/\\* -d /tmp/htmlcov",
returnStatus: true
)
sh "mkdir htmlcov"
sh "oc cp ${webapp_pod}:/tmp/htmlcov htmlcov"
gitUtils.withStatusContext("coverage") {
assert status == 0
}
// run webapp's main.py again
sh "oc exec ${webapp_pod} -- bash -c 'main.py &>/proc/1/fd/1' &"
}
stage("Publish in Polarion") {
// Publish results for tagged commits in Polarion
sh '''
last_commit="$(git rev-parse --short HEAD)"
versioned="$(git describe --tags --exact-match "$last_commit" 2>/dev/null)" || true
if [ -n "$versioned" ]; then
iqe results plugin vulnerability -t test-run-vmaas-$versioned
fi
'''
}
stage("Publish HTML") {
publishHTML (target: [
allowMissing: true,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'htmlcov',
reportFiles: 'index.html',
reportName: "Coverage Report"
])
publishHTML (target: [
allowMissing: true,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'html_report',
reportFiles: 'report.html',
reportName: "Test Report"
])
}
}
}
}