Skip to content

Commit 038d96a

Browse files
committed
Make all tests have there outputs saveable to junit files, add Jenkinsfile for kubernetes pipeline, ignore new test_results folder
1 parent 00f7c06 commit 038d96a

File tree

3 files changed

+253
-7
lines changed

3 files changed

+253
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode
22
values.yaml
3+
test/test_results

Jenkinsfile

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/* groovylint-disable CompileStatic, LineLength, VariableTypeRequired */
2+
// This Jenkinsfile defines internal MarkLogic build pipeline.
3+
4+
//Shared library definitions: https://github.com/marklogic/MarkLogic-Build-Libs/tree/1.0-declarative/vars
5+
@Library('[email protected]')
6+
import groovy.json.JsonSlurperClassic
7+
8+
9+
gitCredID = '550650ab-ee92-4d31-a3f4-91a11d5388a3'
10+
mlVersion = ''
11+
JIRA_ID = ''
12+
JIRA_ID_PATTERN = /CLD-\d{3,4}/
13+
LINT_OUTPUT = ''
14+
SCAN_OUTPUT = ''
15+
IMAGE_INFO = 0
16+
17+
// Define local funtions
18+
void preBuildCheck() {
19+
// Initialize parameters as env variables as workaround for https://issues.jenkins-ci.org/browse/JENKINS-41929
20+
evaluate """${ def script = ''; params.each { k, v -> script += "env.${k } = '''${v}'''\n" }; return script}"""
21+
22+
JIRA_ID = extractJiraID()
23+
echo 'Jira ticket number: ' + JIRA_ID
24+
25+
if (env.GIT_URL) {
26+
githubAPIUrl = GIT_URL.replace('.git', '').replace('github.com', 'api.github.com/repos')
27+
echo 'githubAPIUrl: ' + githubAPIUrl
28+
} else {
29+
echo 'Warning: GIT_URL is not defined'
30+
}
31+
32+
if (env.CHANGE_ID) {
33+
if (prDraftCheck()) { sh 'exit 1' }
34+
if (getReviewState().equalsIgnoreCase('CHANGES_REQUESTED')) {
35+
println(reviewState)
36+
sh 'exit 1'
37+
}
38+
}
39+
}
40+
41+
@NonCPS
42+
def extractJiraID() {
43+
// Extract Jira ID from one of the environment variables
44+
def match
45+
if (env.CHANGE_TITLE) {
46+
match = env.CHANGE_TITLE =~ JIRA_ID_PATTERN
47+
}
48+
else if (env.BRANCH_NAME) {
49+
match = env.BRANCH_NAME =~ JIRA_ID_PATTERN
50+
}
51+
else if (env.GIT_BRANCH) {
52+
match = env.GIT_BRANCH =~ JIRA_ID_PATTERN
53+
}
54+
else {
55+
echo 'Warning: Jira ticket number not detected.'
56+
return ''
57+
}
58+
try {
59+
return match[0]
60+
} catch (any) {
61+
echo 'Warning: Jira ticket number not detected.'
62+
return ''
63+
}
64+
}
65+
66+
def prDraftCheck() {
67+
withCredentials([usernameColonPassword(credentialsId: gitCredID, variable: 'Credentials')]) {
68+
PrObj = sh(returnStdout: true, script:'''
69+
curl -s -u $Credentials -X GET ''' + githubAPIUrl + '''/pulls/$CHANGE_ID
70+
''')
71+
}
72+
def jsonObj = new JsonSlurperClassic().parseText(PrObj.toString().trim())
73+
return jsonObj.draft
74+
}
75+
76+
def getReviewState() {
77+
def reviewResponse
78+
def commitHash
79+
withCredentials([usernameColonPassword(credentialsId: gitCredID, variable: 'Credentials')]) {
80+
reviewResponse = sh(returnStdout: true, script:'''
81+
curl -s -u $Credentials -X GET ''' + githubAPIUrl + '''/pulls/$CHANGE_ID/reviews
82+
''')
83+
commitHash = sh(returnStdout: true, script:'''
84+
curl -s -u $Credentials -X GET ''' + githubAPIUrl + '''/pulls/$CHANGE_ID
85+
''')
86+
}
87+
def jsonObj = new JsonSlurperClassic().parseText(commitHash.toString().trim())
88+
def commitId = jsonObj.head.sha
89+
println(commit_id)
90+
def reviewState = getReviewStateOfPR reviewResponse, 2, commitId
91+
echo reviewState
92+
return reviewState
93+
}
94+
95+
void resultNotification(message) {
96+
def author, authorEmail, emailList
97+
if (env.CHANGE_AUTHOR) {
98+
author = env.CHANGE_AUTHOR.toString().trim().toLowerCase()
99+
authorEmail = getEmailFromGITUser author
100+
emailList = params.emailList + ',' + authorEmail
101+
} else {
102+
emailList = params.emailList
103+
}
104+
jira_link = "https://project.marklogic.com/jira/browse/${JIRA_ID}"
105+
email_body = "<b>Jenkins pipeline for</b> ${env.JOB_NAME} <br><b>Build Number: </b>${env.BUILD_NUMBER} <b><br><br>Lint Output: <br></b><pre><code>${LINT_OUTPUT}</code></pre><br><br><b>Build URL: </b><br>${env.BUILD_URL}"
106+
jira_email_body = "${email_body} <br><br><b>Jira URL: </b><br>${jira_link}"
107+
108+
if (JIRA_ID) {
109+
def comment = [ body: "Jenkins pipeline build result: ${message}" ]
110+
jiraAddComment site: 'JIRA', idOrKey: JIRA_ID, failOnError: false, input: comment
111+
mail charset: 'UTF-8', mimeType: 'text/html', to: "${emailList}", body: "${jira_email_body}", subject: "${message}: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
112+
} else {
113+
mail charset: 'UTF-8', mimeType: 'text/html', to: "${emailList}", body: "${email_body}", subject: "${message}: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
114+
}
115+
}
116+
117+
String getServerVersion(branchName) {
118+
switch (branchName) {
119+
case 'develop':
120+
mlVersion = '11.0'
121+
return mlVersion
122+
case 'develop-10.0':
123+
mlVersion = '10.0'
124+
return mlVersion
125+
case 'develop-9.0':
126+
mlVersion = '9.0'
127+
return mlVersion
128+
default:
129+
return 'INVALID BRANCH'
130+
}
131+
}
132+
133+
void lint() {
134+
sh '''
135+
make lint saveOutput=true
136+
echo helm template lint output: ;echo; helm-lint-output.txt; echo all tests lint output: ;echo; test-lint-output.txt
137+
'''
138+
139+
LINT_OUTPUT = sh(returnStdout: true, script: 'echo helm template lint output: ;echo; helm-lint-output.txt; echo all tests lint output: ;echo; test-lint-output.txt').trim()
140+
141+
sh '''
142+
rm -f helm-lint-output.txt test-lint-output.txt
143+
'''
144+
}
145+
146+
void publishTestResults() {
147+
junit allowEmptyResults:true, testResults: '**/test/test_results/*.xml'
148+
publishHTML allowMissing: true, alwaysLinkToLastBuild: true, keepAll: true, reportDir: 'test/test_results', reportFiles: 'report.html', reportName: 'Kubernetes Tests Report', reportTitles: ''
149+
}
150+
151+
void pullImage() {
152+
withCredentials([usernamePassword(credentialsId: '8c2e0b38-9e97-4953-aa60-f2851bb70cc8', passwordVariable: 'docker_password', usernameVariable: 'docker_user')]) {
153+
sh """
154+
echo "${docker_password}" | docker login --username ${docker_user} --password-stdin ${dockerRegistry}
155+
docker pull ml-docker-dev.marklogic.com/marklogic/marklogic-server-centos:${dockerVersion}
156+
"""
157+
}
158+
}
159+
160+
161+
pipeline {
162+
agent {
163+
label {
164+
label 'cld-kubernetes'
165+
}
166+
}
167+
options {
168+
checkoutToSubdirectory '.'
169+
buildDiscarder logRotator(artifactDaysToKeepStr: '7', artifactNumToKeepStr: '', daysToKeepStr: '30', numToKeepStr: '')
170+
skipStagesAfterUnstable()
171+
}
172+
triggers {
173+
parameterizedCron( env.BRANCH_NAME == 'develop' ? '''00 03 * * * % ML_SERVER_BRANCH=develop-10.0
174+
00 04 * * * % ML_SERVER_BRANCH=develop''' : '')
175+
}
176+
environment {
177+
timeStamp = sh(returnStdout: true, script: 'date +%Y%m%d').trim()
178+
dockerRegistry = 'https://ml-docker-dev.marklogic.com'
179+
dockerVersion = "${mlVersion}-${timeStamp}-centos-1.0.0"
180+
}
181+
182+
parameters {
183+
string(name: 'emailList', defaultValue: emailList, description: 'List of email for build notification', trim: true)
184+
choice(name: 'ML_SERVER_BRANCH', choices: 'develop-10.0\ndevelop\ndevelop-9.0', description: 'MarkLogic Server Branch. used to pick appropriate rpm')
185+
booleanParam(name: 'KUBERNETES_TESTS', defaultValue: true, description: 'Run kubernetes tests')
186+
}
187+
188+
stages {
189+
stage('Pre-Build-Check') {
190+
steps {
191+
preBuildCheck()
192+
}
193+
}
194+
195+
stage('Pull-Image') {
196+
steps {
197+
// TODO: Figure out how to build image
198+
pullImage()
199+
}
200+
}
201+
202+
stage('Lint') {
203+
steps {
204+
lint()
205+
}
206+
}
207+
208+
stage('Kubernetes-Run-Tests') {
209+
when {
210+
expression { return params.KUBERNETES_TESTS }
211+
}
212+
steps {
213+
sh "make test dockerImage=marklogic-centos/marklogic-server-centos:${mlVersion}-${env.platformString}-${env.dockerVersion} saveOutput=true"
214+
}
215+
}
216+
}
217+
218+
post {
219+
always {
220+
sh '''
221+
cd src/centos
222+
rm -rf *.rpm
223+
docker system prune --force --filter "until=720h"
224+
docker volume prune --force
225+
docker image prune --force --all
226+
'''
227+
publishTestResults()
228+
}
229+
success {
230+
resultNotification('BUILD SUCCESS ✅')
231+
}
232+
failure {
233+
resultNotification('BUILD ERROR ❌')
234+
}
235+
unstable {
236+
resultNotification('BUILD UNSTABLE ❌')
237+
}
238+
}
239+
}

makefile

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ dockerImage?=marklogic-centos/marklogic-server-centos:10-internal
22

33
## System requirement:
44
## - Go
5+
## - gotestsum (if you want to enable saveOutput for testing commands)
56
## - Helm
67
## - Minikube
8+
## - Docker
79
## - GNU Make >= 3.8.2 (preferrably >=4.2.1)
810

911
#***************************************************************************
@@ -59,14 +61,14 @@ prepare:
5961
#***************************************************************************
6062
## Lint the code
6163
## Options:
62-
## * [Jenkins] optional. If we are running in Jenkins enviroment. Example: Jenkins=true
64+
## * [saveOutput] optional. Save the output to a text file. Example: saveOutput=true
6365
.PHONY: lint
6466
lint:
6567
@echo "> Linting helm charts....."
66-
helm lint --with-subcharts charts/ $(if $(Jenkins), > helm-lint-output.txt,)
68+
helm lint --with-subcharts charts/ $(if $(saveOutput), > helm-lint-output.txt,)
6769

68-
@echo "> Linting tests....."
69-
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.49.0 golangci-lint run $(if $(Jenkins), > test-lint-output.txt,)
70+
@echo "> Linting all tests....."
71+
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.49.0 golangci-lint run $(if $(saveOutput), > test-lint-output.txt,)
7072

7173
## ---------- Testing Tasks ----------
7274

@@ -76,6 +78,7 @@ lint:
7678
## Run all end to end tests
7779
## Options:
7880
## * [dockerImage] optional. default is marklogicdb/marklogic-db:latest. Example: dockerImage=marklogic-centos/marklogic-server-centos:10-internal
81+
## * [saveOutput] optional. Save the output to a xml file. Example: saveOutput=true
7982
.PHONY: e2e-test
8083
e2e-test: prepare
8184
@echo "=====Installing minikube cluster"
@@ -84,8 +87,8 @@ e2e-test: prepare
8487
@echo "=====Loading marklogc image $(dockerImage) to minikube cluster"
8588
minikube image load $(dockerImage)
8689

87-
@echo "=====Running tests"
88-
go test -v -count=1 ./test/e2e/...
90+
@echo "=====Running e2e tests"
91+
cd test; $(if $(saveOutput), gotestsum --junitfile test_results/e2e-tests.xml ./e2e/... -count=1, go test -v -count=1 ./test/e2e/...)
8992

9093
@echo "=====Delete minikube cluster"
9194
minikube delete
@@ -94,15 +97,18 @@ e2e-test: prepare
9497
# template-test
9598
#***************************************************************************
9699
## Run all template tests
100+
## * [saveOutput] optional. Save the output to a xml file. Example: saveOutput=true
97101
.PHONY: template-test
98102
template-test: prepare
99-
go test -v ./test/template/...
103+
@echo "=====Running template tests"
104+
cd test; $(if $(saveOutput), gotestsum --junitfile test_results/testplate-tests.xml ./template/... -count=1, go test -v -count=1 ./test/template/...)
100105

101106
#***************************************************************************
102107
# test
103108
#***************************************************************************
104109
## Run all tests
105110
## Options:
106111
## * [dockerImage] optional. default is marklogicdb/marklogic-db:latest. Example: dockerImage=marklogic-centos/marklogic-server-centos:10-internal
112+
## * [saveOutput] optional. Save the output to a xml file. Example: saveOutput=true
107113
.PHONY: test
108114
test: template-test e2e-test

0 commit comments

Comments
 (0)