Add smoke/health tests to OSM stage_3
[osm/devops.git] / jenkins / ci-pipelines / ci_stage_3.groovy
1 /* Copyright 2017 Sandvine
2  *
3  * All Rights Reserved.
4  * 
5  *   Licensed under the Apache License, Version 2.0 (the "License"); you may
6  *   not use this file except in compliance with the License. You may obtain
7  *   a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  *   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  *   License for the specific language governing permissions and limitations
15  *   under the License.
16  */
17
18 properties([
19     parameters([
20         string(defaultValue: env.GERRIT_BRANCH, description: '', name: 'GERRIT_BRANCH'),
21         string(defaultValue: 'system', description: '', name: 'NODE'),
22         string(defaultValue: '', description: '', name: 'BUILD_FROM_SOURCE'),
23         string(defaultValue: 'unstable', description: '', name: 'REPO_DISTRO'),
24         string(defaultValue: '', description: '', name: 'COMMIT_ID'),
25         string(defaultValue: '-stage_2', description: '', name: 'UPSTREAM_SUFFIX'),
26         string(defaultValue: 'pubkey.asc', description: '', name: 'REPO_KEY_NAME'),
27         string(defaultValue: 'release', description: '', name: 'RELEASE'),
28         string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NAME'),
29         string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NUMBER'),
30         string(defaultValue: '', description: '', name: 'UPSTREAM_JOB_NUMBER'),
31         string(defaultValue: 'dpkg1', description: '', name: 'GPG_KEY_NAME'),
32         string(defaultValue: 'artifactory-osm', description: '', name: 'ARTIFACTORY_SERVER'),
33         string(defaultValue: 'osm-stage_4', description: '', name: 'DOWNSTREAM_STAGE_NAME'),
34         booleanParam(defaultValue: false, description: '', name: 'SAVE_CONTAINER_ON_FAIL'),
35         booleanParam(defaultValue: false, description: '', name: 'SAVE_CONTAINER_ON_PASS'),
36         booleanParam(defaultValue: false, description: '', name: 'DO_STAGE_4'),
37         booleanParam(defaultValue: true, description: '',  name: 'DO_BUILD'),
38         booleanParam(defaultValue: true, description: '', name: 'DO_INSTALL'),
39         booleanParam(defaultValue: true, description: '', name: 'DO_SMOKE'),
40         booleanParam(defaultValue: false, description: '', name: 'SAVE_ARTIFACTS_OVERRIDE'),
41     ])
42 ])
43
44 def uninstall_osm(stackName) {
45     sh """
46          export OSM_USE_LOCAL_DEVOPS=true
47          export PATH=$PATH:/snap/bin
48          installers/full_install_osm.sh -y -w /tmp/osm -t ${stackName} -s ${stackName} --test --nolxd --nodocker --nojuju --nohostports --nohostclient --uninstall
49        """
50 }
51
52 def run_systest(stackName,tagName,testName) {
53     sh """
54         tempdir=$(mktemp -d)
55         docker run -ti --network net${stackName} -v $tempdir:/usr/share/osm-devops/systest/reports osm/osmclient:${tagName} make -C /usr/share/osm-devops/systest ${testName}
56         cp $tempdir/*.xml .
57        """
58     junit  '*.xml'
59 }
60
61 node("${params.NODE}") {
62
63     sh 'env'
64
65     tag_or_branch = params.GERRIT_BRANCH.replaceAll(/\./,"")
66
67     stage("Checkout") {
68         checkout scm
69     }
70
71     ci_helper = load "jenkins/ci-pipelines/ci_helper.groovy"
72
73     def upstream_main_job = params.UPSTREAM_SUFFIX
74
75     // upstream jobs always use merged artifacts
76     upstream_main_job += '-merge'
77     container_name_prefix = "osm-${tag_or_branch}"
78     container_name = "${container_name_prefix}"
79     if ( JOB_NAME.contains('merge') ) {
80         container_name += "-merge"
81     }
82     container_name += "-${BUILD_NUMBER}"
83
84     // Copy the artifacts from the upstream jobs
85     stage("Copy Artifacts") {
86         // cleanup any previous repo
87         sh 'rm -rf repo'
88         dir("repo") {
89             // grab all stable upstream builds based on the
90
91             dir("${RELEASE}") {
92                 def list = ["RO", "openvim", "osmclient", "IM", "devops", "MON", "N2VC", "NBI", "common", "LCM"]
93                 for (component in list) {
94                     step ([$class: 'CopyArtifact',
95                            projectName: "${component}${upstream_main_job}/${GERRIT_BRANCH}"])
96
97                     // grab the build name/number
98                     //options = get_env_from_build('build.env')
99                     build_num = ci_helper.get_env_value('build.env','BUILD_NUMBER')
100
101                     // grab the archives from the stage_2 builds (ie. this will be the artifacts stored based on a merge)
102                     ci_helper.get_archive(params.ARTIFACTORY_SERVER,component,GERRIT_BRANCH, "${component}${upstream_main_job} :: ${GERRIT_BRANCH}", build_num)
103
104                     // cleanup any prevously defined dists
105                     sh "rm -rf dists"
106                 }
107
108                 // check if an upstream artifact based on specific build number has been requested
109                 // This is the case of a merge build and the upstream merge build is not yet complete (it is not deemed
110                 // a successful build yet). The upstream job is calling this downstream job (with the its build artifiact)
111                 if ( params.UPSTREAM_JOB_NAME ) {
112                     step ([$class: 'CopyArtifact',
113                            projectName: "${params.UPSTREAM_JOB_NAME}",
114                            selector: [$class: 'SpecificBuildSelector', buildNumber: "${params.UPSTREAM_JOB_NUMBER}"]
115                           ])
116
117                     //options = get_env_from_build('build.env')
118                     // grab the build name/number
119                     //build_num = sh(returnStdout:true,  script: "cat build.env | awk -F= '/BUILD_NUMBER/{print \$2}'").trim()
120                     build_num = ci_helper.get_env_value('build.env','BUILD_NUMBER')
121                     component = ci_helper.get_mdg_from_project(ci_helper.get_env_value('build.env','GERRIT_PROJECT'))
122
123                     // the upstream job name contains suffix with the project. Need this stripped off
124                     def project_without_branch = params.UPSTREAM_JOB_NAME.split('/')[0]
125
126                     ci_helper.get_archive(params.ARTIFACTORY_SERVER,component,GERRIT_BRANCH, "${project_without_branch} :: ${GERRIT_BRANCH}", build_num)
127
128                     sh "rm -rf dists"
129                 }
130                 
131                 // sign all the components
132                 for (component in list) {
133                     sh "dpkg-sig --sign builder -k ${GPG_KEY_NAME} pool/${component}/*"
134                 }
135
136                 // now create the distro
137                 for (component in list) {
138                     sh "mkdir -p dists/${params.REPO_DISTRO}/${component}/binary-amd64/"
139                     sh "apt-ftparchive packages pool/${component} > dists/${params.REPO_DISTRO}/${component}/binary-amd64/Packages"
140                     sh "gzip -9fk dists/${params.REPO_DISTRO}/${component}/binary-amd64/Packages"
141                 }
142
143                 // create and sign the release file
144                 sh "apt-ftparchive release dists/${params.REPO_DISTRO} > dists/${params.REPO_DISTRO}/Release"
145                 sh "gpg --yes -abs -u ${GPG_KEY_NAME} -o dists/${params.REPO_DISTRO}/Release.gpg dists/${params.REPO_DISTRO}/Release"
146
147                 // copy the public key into the release folder
148                 // this pulls the key from the home dir of the current user (jenkins)
149                 sh "cp ~/${REPO_KEY_NAME} ."
150
151                 // merge the change logs
152                 sh """
153                    rm -f changelog/changelog-osm.html
154                    [ ! -d changelog ] || for mdgchange in \$(ls changelog); do cat changelog/\$mdgchange >> changelog/changelog-osm.html; done
155                    """
156             }
157             // start an apache server to serve up the images
158             http_server_name = "${container_name}-apache"
159
160             pwd = sh(returnStdout:true,  script: 'pwd').trim()
161             repo_base_url = ci_helper.start_http_server(pwd,http_server_name)
162         }
163     }
164
165     error = null
166     if ( params.DO_BUILD ) {
167         stage("Build") {
168             sh "make -C docker clean"
169             sh "make -j4 -C docker CMD_DOCKER_ARGS= TAG=${container_name} RELEASE=${params.RELEASE} REPOSITORY_BASE=${repo_base_url} REPOSITORY_KEY=${params.REPO_KEY_NAME} REPOSITORY=${params.REPO_DISTRO}"
170         }
171     }
172
173     try {
174         if ( params.DO_INSTALL ) {
175             stage("Install") {
176
177                 //will by default always delete containers on complete
178                 //sh "jenkins/system/delete_old_containers.sh ${container_name_prefix}"
179
180                 commit_id = ''
181                 repo_distro = ''
182                 repo_key_name = ''
183                 release = ''
184
185                 if ( params.COMMIT_ID )
186                 {
187                     commit_id = "-b ${params.COMMIT_ID}"
188                 }
189
190                 if ( params.REPO_DISTRO )
191                 {
192                     repo_distro = "-r ${params.REPO_DISTRO}"
193                 }
194
195                 if ( params.REPO_KEY_NAME )
196                 {
197                     repo_key_name = "-k ${params.REPO_KEY_NAME}"
198                 }
199
200                 if ( params.RELEASE )
201                 {
202                     release = "-R ${params.RELEASE}"
203                 }
204          
205                 sh """
206                     export PATH=$PATH:/snap/bin
207                     installers/full_install_osm.sh -y -s ${container_name} --test --nolxd --nodocker --nojuju --nohostports --nohostclient \
208                                                     --nodockerbuild -t ${container_name} \
209                                                     -w /tmp/osm \
210                                                     ${commit_id} \
211                                                     ${repo_distro} \
212                                                     ${repo_base_url} \
213                                                     ${repo_key_name} \
214                                                     ${release} \
215                                                     ${params.BUILD_FROM_SOURCE}
216                    """
217             }
218         }
219
220         if ( params.DO_SMOKE ) {
221             stage("OSM Health") {
222                 sh "installers/osm_health.sh -s ${container_name}"
223             }
224             stage("Smoke") {
225                 run_systest(container_name,container_name,"smoke")
226             }
227         }
228
229         stage_4_archive = false
230         if ( params.DO_STAGE_4 ) {
231             stage("stage_4") {
232                 def downstream_params = [
233                     string(name: 'CONTAINER_NAME', value: container_name),
234                     string(name: 'NODE', value: NODE_NAME.split()[0]),
235                 ]
236                 stage_4_result = build job: "${params.DOWNSTREAM_STAGE_NAME}/${GERRIT_BRANCH}", parameters: downstream_params, propagate: false 
237                 currentBuild.result = stage_4_result.result
238
239                 if ( stage_4_result.getResult().equals('SUCCESS') ) {
240                     stage_4_archive = true;
241                 }
242             }
243         }
244
245         // override to save the artifacts
246         if ( params.SAVE_ARTIFACTS_OVERRIDE || stage_4_archive ) {
247             stage("Archive") {
248                 sh "echo ${container_name} > build_version.txt"
249                 archiveArtifacts artifacts: "build_version.txt", fingerprint: true
250
251                 // Archive the tested repo
252                 dir("repo/${RELEASE}") {
253                     ci_helper.archive(params.ARTIFACTORY_SERVER,RELEASE,GERRIT_BRANCH,'tested')
254                 }
255             }
256         }
257     }
258     catch(caughtError) {
259         println("Caught error!")
260         error = caughtError
261         currentBuild.result = 'FAILURE'
262     }
263     finally {
264         sh "docker stop ${http_server_name}"
265         sh "docker rm ${http_server_name}"
266
267         if ( params.DO_INSTALL ) {
268             if (error) {
269                 if ( !params.SAVE_CONTAINER_ON_FAIL ) {
270                     uninstall_osm container_name
271                 }
272                 throw error 
273             }
274             else {
275                 if ( !params.SAVE_CONTAINER_ON_PASS ) {
276                     uninstall_osm container_name
277                 }
278             }
279         }
280     }
281 }