-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathJenkinsfile-testing
212 lines (199 loc) · 8.25 KB
/
Jenkinsfile-testing
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
#!/usr/bin/env groovy
void setBuildStatus(String message, String state) {
step([
$class: "GitHubCommitStatusSetter",
reposSource: [$class: "ManuallyEnteredRepositorySource", url: "https://github.com/paladinarcher/padawan"],
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/test-status"],
errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
]);
}
pipeline {
agent {
dockerfile {
filename "Dockerfile-jenkins-build"
args "--entrypoint=''"
}
}
environment {
LC_ALL='en_US.UTF-8'
LANG='en_US.UTF-8'
METEOR_ALLOW_SUPERUSER=true
NODE_ENV='development'
}
stages {
stage('Notify Github') {
steps {
setBuildStatus("Starting tests.", "PENDING")
}
}
stage('Istanbul') {
steps {
script {
env.JUNIT_REPORT_PATH = 'reports/report.xml'
// env.JUNIT_REPORT_PATH = '/report.xml'
}
//echo "${env.JUNIT_REPORT_PATH}"
// beggining of xml report tags
sh "cd $WORKSPACE"
sh '''
cd $WORKSPACE
rm -rf reports
mkdir reports
touch ./reports/report.xml
echo '<testsuites name="Mocha Tests">' >> ./reports/report.xml
echo ' <testsuite name="For Jenkins" tests="3" errors="0" failures="0" skipped="0" timestamp="2019-11-26T21:32:43" time="0.004">' >> ./reports/report.xml
'''
//sh "cat reports/report.xml"
sh '/usr/local/bin/meteor add lmieulet:meteor-coverage meteortesting:mocha'
sh '/usr/local/bin/meteor npm install --save-dev babel-plugin-istanbul'
echo 'Istanbul installed'
sh 'set +e' // this should help Jenkins not crash
sh '''
if /usr/local/bin/meteor npm run coverage:unit ; then
echo "Initial Istanbul suceeded"
else
echo "Initial Istanbul failed"
usr/local/bin/meteor npm run coverage:unit || usr/local/bin/meteor npm run coverage:unit || true
fi
'''
sh 'set -e' // this should help Jenkins not crash
echo 'coverage:unit script ran'
sh 'ls .coverage/' // istanbul should have created the .coverage directory. If mongodb doesn't start, this line should fail.
// middle of xml report tags
sh '''
failingTest='false'
linePrint ()
{
myReg=$(echo $1 | awk 'match($0, /pic high|pic low/) {print substr($0, RSTART, RLENGTH)}')
coverageName=$(echo $2 | awk 'match($0, /data-value="[^"]+/) {print substr($0, RSTART + 12, RLENGTH - 12)}')
percent=$(echo $1 | awk 'match($0, /data-value="[0-9][0-9][.][0-9][0-9]|data-value="[0-9][.][0-9][0-9]|data-value="[0-9][0-9][0-9]|data-value="[0-9][0-9]|data-value="[0-9]/) {print substr($0, RSTART + 12, RLENGTH - 12)}')
myName='name="'$myReg' '$percent'% coverage: '$coverageName'"'
qt='"'
if [ "$myReg" = "pic high" ]
then
echo "MYREG is pic high"
echo " <testcase classname=${qt}Istanbul Coverage$qt $myName time=${qt}0$qt>" >> ./reports/report.xml
echo " <system-out><![CDATA[$1 $2]]></system-out>" >> ./reports/report.xml
echo ' </testcase>' >> ./reports/report.xml
elif [ "$myReg" = "pic low" ]
then
echo "MYREG is pic low"
#failingTest='true'
#sed -i '1s/^/true/' ./failingTestVariableFileIstanbul
#echo "true" | ./failingTestVariableFileIstanbul
echo "true" >> ./failingTestVariableFileIstanbul
echo " <testcase classname=${qt}Istanbul Coverage$qt $myName time=${qt}0$qt status=${qt}Failed$qt>" >> ./reports/report.xml
echo ' <failure message="Coverage Percentage is below 80%"></failure>' >> ./reports/report.xml
echo " <system-out><![CDATA[$1 $2]]></system-out>" >> ./reports/report.xml
echo ' </testcase>' >> ./reports/report.xml
elif [ "$myReg" = "" ];
then
echo "MYREG is empty"
fi
echo "==============================\n"
}
prevLine='none'
echo "false" >> ./failingTestVariableFileIstanbul # if this gets set to true, then the build fails (Istanbul < 80%)
ls -al
while read -r line; do linePrint "$line" "$prevLine"; prevLine="$line"; done < .coverage/index.html
'''
// closing of xml report tags
sh '''
echo ' </testsuite>' >> ./reports/report.xml
echo '</testsuites>' >> ./reports/report.xml
'''
// If there is an istanbul test below 80%, the folowing code should fail the pipeline
// turn on following code to make failing tests fail the build
sh '''
#failingTest=`cat ./failingTestVariableFileIstanbul`
#failingTest=$(cut -c1-4 < ./failingTestVariableFileIstanbul)
if grep -q "true" "./failingTestVariableFileIstanbul"; then
echo "true" >> ./failingTestVariableFileIstanbulFinal
failingTest="true"
else
echo "false" >> ./failingTestVariableFileIstanbulFinal
failingTest="false"
fi
echo "failingTest: "
echo "$failingTest"
if [ "$failingTest" = "true" ]
then
echo "failing the build because Istanbul is below 80%"
false
else
echo "Istanbul coverage is above 80%"
fi
'''
}
}
stage('Unit Tests') {
steps {
echo 'Testing...'
sh 'echo "LC_ALL=en_US.UTF-8" >> /etc/environment'
sh 'echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen'
sh 'echo "LANG=en_US.UTF-8" > /etc/locale.conf'
sh 'locale-gen en_US.UTF-8'
//sh 'meteor --allow-superuser remove-platform android'
sh 'meteor npm --allow-superuser install --save babel-runtime nightwatch'
sh 'meteor --allow-superuser test --once --settings settings.local.json --driver-package meteortesting:mocha'
}
}
stage('Functional Tests') {
steps {
sh 'java -jar /opt/selenium/selenium-server-standalone.jar > selenium_startup.log 2>&1 &'
//sh 'meteor npm install --save mongodb-client-encryption'
sh 'meteor --allow-superuser reset'
sh 'meteor --allow-superuser --settings settings.staging.json > meteor_startup.log 2>&1 &'
sh '''
LOGFILE=meteor_startup.log
STR_SUCCESS="Started your app"
STR_FAILURE="Can't start"
STR_FAILURE2="Your application has errors"
STR_FAILURE3="Waiting for file change"
TIMEOUT=600
RETRY_SEC=10
ELAPSED_SEC=0
until [ "$ELAPSED_SEC" -ge "$TIMEOUT" ]; do
if grep -q "$STR_FAILURE" $LOGFILE; then
echo "failed to start"
cat $LOGFILE
exit 1
fi
if grep -q "$STR_FAILURE2" $LOGFILE; then
echo "failed to start"
cat $LOGFILE
exit 1
fi
if grep -q "$STR_FAILURE3" $LOGFILE; then
echo "failed to start"
cat $LOGFILE
exit 1
fi
if grep -q "$STR_SUCCESS" $LOGFILE; then
echo "started successfully"
exit 0
fi
sleep $RETRY_SEC
echo $((ELAPSED_SEC+=$RETRY_SEC))
done
echo "timed out"
exit 1
'''
sh 'cat selenium_startup.log'
sh 'cat meteor_startup.log'
sh 'meteor npm --allow-superuser run test-e2e'
}
}
}
post {
success {
setBuildStatus("Testing complete.", "SUCCESS")
cleanWs()
}
failure {
setBuildStatus("Testing failed.", "FAILURE")
cleanWs()
}
}
}