blob: 3a2eddd9cb521c773754e73d946079346221d972 [file] [log] [blame]
Devin Lim61643762017-12-07 15:55:38 -08001#!groovy
2import groovy.time.*
Devin Lim81ab48b2018-02-09 14:24:57 -08003generalFuncs = evaluate readTrusted( 'TestON/JenkinsFile/GeneralFuncs.groovy' )
Devin Lim61643762017-12-07 15:55:38 -08004def initializeTrend( machine ){
5 table_name = "executed_test_tests"
6 result_name = "executed_test_results"
7 testMachine = "TestStation-" + machine + "s";
8 this.machine = machine
9 isSCPF = false
10 generalFuncs.initBasicVars();
11}
12def initialize( type, SCPFfuncs ){
13 init( type )
14 SCPFfunc = SCPFfuncs
15 isSCPF = true
16 machine = machineType[ type ]
17}
18def initialize( type ){
19 init( type )
20 SCPFfunc = null
21 table_name = "executed_test_tests"
22 result_name = "executed_test_results"
23 trend_generator_file = generalFuncs.rScriptLocation + "testCategoryTrend.R"
24 build_stats_generator_file = generalFuncs.rScriptLocation + "testCategoryBuildStats.R"
25 isSCPF = false
26}
27def init( type ){
28 machineType = [ "FUNC" : "VM",
29 "HA" : "VM",
30 "SR" : "VM",
31 "SCPF" : "BM",
32 "USECASE" : "BM" ]
33 testType = type;
34 testMachine = "TestStation-" + machineType[ type ] + "s";
35 generalFuncs.initBasicVars();
36}
37
38def printType(){
39 echo testType;
40 echo testMachine;
41}
42def getProperties(){
43 node( testMachine ){
44 return readProperties( file:'/var/jenkins/TestONOS.property' );
45 }
46}
47def getTestsToRun( testList ){
48 testList.tokenize("\n;, ")
49}
50def getCurrentTime(){
Devin Limd1fb8e92018-02-28 16:29:33 -080051 TimeZone.setDefault( TimeZone.getTimeZone('PST') )
Devin Lim61643762017-12-07 15:55:38 -080052 return new Date();
53}
54def getTotalTime( start, end ){
55 return TimeCategory.minus( end, start );
56}
57def printTestToRun( testList ){
58 for ( String test : testList ) {
59 println test;
60 }
61}
62def sendResultToSlack( start, isManualRun, branch ){
63 try{
64 if( isManualRun == "false" ){
65 end = getCurrentTime();
66 TimeDuration duration = TimeCategory.minus( end , start );
67 slackSend( color:"#5816EE",
68 message: testType + "-" + branch + " tests ended at: " + end.toString() + "\nTime took : " + duration )
69 }
70 }
71 catch( all ){}
72}
73def initAndRunTest( testName, testCategory ){
74 return '''#!/bin/bash -l
75 set -i # interactive
76 set +e
77 shopt -s expand_aliases # expand alias in non-interactive mode
78 export PYTHONUNBUFFERED=1
79 ifconfig
Devin Lim4104c002018-02-28 16:42:19 -080080 ''' //+ borrowCell( testName )
81 + '''
Devin Lim61643762017-12-07 15:55:38 -080082 echo "ONOS Branch is: $ONOSBranch"
83 echo "TestON Branch is: $TestONBranch"
84 echo "Test date: "
85 date
86 cd ~
87 export PATH=$PATH:onos/tools/test/bin
88 timeout 240 stc shutdown | head -100
89 timeout 240 stc teardown | head -100
90 timeout 240 stc shutdown | head -100
91 cd ~/OnosSystemTest/TestON/bin
92 git log |head
93 ./cleanup.sh -f
94 ''' + "./cli.py run " + ( !isSCPF ? testName : testCategory[ testName ][ 'test' ] ) + '''
95 ./cleanup.sh -f
96 # cleanup config changes
97 cd ~/onos/tools/package/config
98 git clean -df'''
99}
100def copyLogs( testName ){
101 result = ""
102 if( testType == "SR" ){
103 result = '''
104 sudo rm /var/jenkins/workspace/SR-log-${WikiPrefix}/*
105 sudo cp *karaf.log.* /var/jenkins/workspace/SR-log-${WikiPrefix}/
106 sudo cp *Flows* /var/jenkins/workspace/SR-log-${WikiPrefix}/
107 sudo cp *Groups* /var/jenkins/workspace/SR-log-${WikiPrefix}/
108 '''
109 }
110 return result
111}
112def cleanAndCopyFiles( testName ){
113 return '''#!/bin/bash -i
114 set +e
115 echo "ONOS Branch is: ${ONOSBranch}"
116 echo "TestON Branch is: ${TestONBranch}"
117 echo "Job name is: "''' + testName + '''
118 echo "Workspace is: ${WORKSPACE}/"
119 echo "Wiki page to post is: ${WikiPrefix}-"
120 # remove any leftover files from previous tests
121 sudo rm ${WORKSPACE}/*Wiki.txt
122 sudo rm ${WORKSPACE}/*Summary.txt
123 sudo rm ${WORKSPACE}/*Result.txt
124 sudo rm ${WORKSPACE}/*.csv
125 #copy files to workspace
126 cd `ls -t ~/OnosSystemTest/TestON/logs/*/ | head -1 | sed 's/://'`
127 ''' + copyLogs( testName ) + '''
128 sudo cp *.txt ${WORKSPACE}/
129 sudo cp *.csv ${WORKSPACE}/
130 cd ${WORKSPACE}/
131 for i in *.csv
132 do mv "$i" "$WikiPrefix"-"$i"
133 done
134 ls -al
135 cd '''
136}
137def fetchLogs( testName ){
138 return '''#!/bin/bash
139 set +e
140 cd ~/OnosSystemTest/TestON/logs
141 echo "Job Name is: " + ''' + testName + '''
142 TestONlogDir=$(ls -t | grep ${TEST_NAME}_ |head -1)
143 echo "########################################################################################"
144 echo "##### copying ONOS logs from all nodes to TestON/logs directory: ${TestONlogDir}"
145 echo "########################################################################################"
146 cd $TestONlogDir
147 if [ $? -eq 1 ]
148 then
149 echo "Job name does not match any test suite name to move log!"
150 else
151 pwd
152 for i in $OC{1..7}; do onos-fetch-logs $i || echo log does not exist; done
153 fi
154 cd'''
155}
156def isPostingResult( manual, postresult ){
157 return manual == "false" || postresult == "true"
158}
159def postResult( prop, graphOnly ){
160 if( graphOnly || isPostingResult( prop[ "manualRun" ], prop[ "postResult" ] ) ){
161 def post = build job: "postjob-" + ( graphOnly ? machine : machineType[ testType ] ), propagate: false
162 }
163}
164def postLogs( testName, prefix ){
165 resultURL = ""
166 if( testType == "SR" ){
167 def post = build job: "SR-log-" + prefix, propagate: false
168 resultURL = post.getAbsoluteUrl()
169 }
170 return resultURL
171}
172def getSlackChannel(){
173 return "#" + ( testType == "SR" ? "sr-failures" : "jenkins-related" )
174}
175def analyzeResult( prop, workSpace, testName, otherTestName, resultURL, wikiLink, isSCPF ){
176 node( testMachine ){
177 resultContents = readFile workSpace + "/" + testName + "Result.txt"
178 resultContents = resultContents.split("\n")
179 if( resultContents[ 0 ] == "1" ){
180 print "All passed"
181 }else{
182 print "Failed"
183 if( prop[ "manualRun" ] == "false" ){
184 slackSend( channel:getSlackChannel(), color:"FF0000", message: "[" + prop[ "ONOSBranch" ] + "]"
185 + otherTestName + " : Failed!\n" + resultContents[ 1 ] + "\n"
186 + "[TestON log] : \n"
Devin Limd1fb8e92018-02-28 16:29:33 -0800187 + "https://jenkins.onosproject.org/blue/organizations/jenkins/${env.JOB_NAME}/detail/${env.JOB_NAME}/${env.BUILD_NUMBER}/pipeline"
Devin Lim61643762017-12-07 15:55:38 -0800188 + ( isSCPF ? "" : ( "\n[Result on Wiki] : \n" + "https://wiki.onosproject.org/display/ONOS/" + wikiLink.replaceAll( "\\s","+" ) ) )
189 + ( resultURL != "" ? ( "\n[Karaf log] : \n" + resultURL + "artifact/" ) : "" ),
190 teamDomain: 'onosproject' )
191 }
192 Failed
193 }
194 }
195}
196def publishToConfluence( prop, wikiLink, file ){
197 if( isPostingResult( prop[ "manualRun" ], prop[ "postResult" ] ) ){
198 publishConfluence siteName: 'wiki.onosproject.org', pageName: wikiLink, spaceName: 'ONOS',
Devin Lim1253ae82018-02-26 11:13:36 -0800199 attachArchivedArtifacts: true, buildIfUnstable: true,
Devin Lim61643762017-12-07 15:55:38 -0800200 editorList: [
201 confluenceWritePage( confluenceFile( file ) )
202 ]
203 }
204
205}
206def runTest( testName, toBeRun, prop, pureTestName, graphOnly, testCategory, graph_generator_file, graph_saved_directory ) {
207 return {
208 catchError{
209 stage( testName ) {
210 if ( toBeRun ){
211 workSpace = "/var/jenkins/workspace/" + testName
212 def fileContents = ""
213 node( testMachine ){
214 withEnv( [ 'ONOSBranch=' + prop[ "ONOSBranch" ],
215 'ONOSJVMHeap=' + prop[ "ONOSJVMHeap" ],
216 'TestONBranch=' + prop[ "TestONBranch" ],
217 'ONOSTag=' + prop[ "ONOSTag" ],
218 'WikiPrefix=' + prop[ "WikiPrefix" ],
219 'WORKSPACE=' + workSpace ] ){
220 if( ! graphOnly ){
221 sh initAndRunTest( testName, testCategory )
222 // For the Wiki page
223 sh cleanAndCopyFiles( pureTestName )
224 }
225 databaseAndGraph( prop, testName, graphOnly, graph_generator_file, graph_saved_directory )
226 if( ! graphOnly ){
227 sh fetchLogs( pureTestName )
228 if( !isSCPF )
229 publishToConfluence( prop, testCategory[ testName ][ 'wiki_link' ], workSpace + "/" + testCategory[ testName ][ 'wiki_file' ] )
230 }
231 }
232
233
234 }
235 postResult( prop, graphOnly )
236 if( ! graphOnly ){
237 resultURL = postLogs( testName, prop[ "WikiPrefix" ] )
238 analyzeResult( prop, workSpace, pureTestName, testName, resultURL, isSCPF ? "" : testCategory[ testName ][ 'wiki_link' ], isSCPF )
239 }
240 }
241 }
242 }
243 }
244}
245def borrowCell( testName ){
246 result = ""
247 if( testType == "SR" ){
248 result = '''
249 cd
250 source ~/borrow.cell
251 '''
252 }
253 return result
254}
255def databaseAndGraph( prop, testName, graphOnly, graph_generator_file, graph_saved_directory ){
256 if( graphOnly || isPostingResult( prop[ "manualRun" ], prop[ "postResult" ] ) ){
257 // Post Results
258 withCredentials( [
259 string( credentialsId: 'db_pass', variable: 'pass' ),
260 string( credentialsId: 'db_user', variable: 'user' ),
261 string( credentialsId: 'db_host', variable: 'host' ),
262 string( credentialsId: 'db_port', variable: 'port' ) ] ) {
263 def database_command = generalFuncs.database_command_create( pass, host, port, user ) + ( !isSCPF ? sqlCommand( testName ) : SCPFfunc.sqlCommand( testName ) )
264 sh '''#!/bin/bash
265 export DATE=\$(date +%F_%T)
266 cd ~
267 pwd ''' + ( graphOnly ? "" : ( !isSCPF ? databasePart( prop[ "WikiPrefix" ], testName, database_command ) :
268 SCPFfunc.databasePart( testName, database_command ) ) ) + '''
269 ''' + ( !isSCPF ? graphGenerating( host, port, user, pass, testName, prop, graph_saved_directory, graph_generator_file ) : SCPFfunc.getGraphGeneratingCommand( host, port, user, pass, testName, prop ) )
270 }
271 }
272}
273def generateCategoryStatsGraph( manualRun, postresult, stat_file, pie_file, type, branch, testListPart, save_path, pieTestListPart ){
274
275 if( isPostingResult( manualRun, postresult ) ){
276 node( testMachine ){
277
278 withCredentials( [
279 string( credentialsId: 'db_pass', variable: 'pass' ),
280 string( credentialsId: 'db_user', variable: 'user' ),
281 string( credentialsId: 'db_host', variable: 'host' ),
282 string( credentialsId: 'db_port', variable: 'port' ) ] ) {
283 sh '''#!/bin/bash
284 ''' + generalFuncs.basicGraphPart( generalFuncs.rScriptLocation + stat_file, host, port, user, pass, type, branch ) + " \"" + testListPart + "\" latest " + save_path + '''
285 ''' + getOverallPieGraph( generalFuncs.rScriptLocation + pie_file, host, port, user, pass, branch, type, pieTestListPart, 'y', save_path ) + '''
286 ''' + getOverallPieGraph( generalFuncs.rScriptLocation + pie_file, host, port, user, pass, branch, type, pieTestListPart, 'n', save_path )
287 }
288 }
289 postResult( [], true )
290 }
291}
292def makeTestList( list, commaNeeded ){
293 return generalFuncs.getTestList( list ) + ( commaNeeded ? "," : "" )
294}
295def createStatsList( testCategory, list, semiNeeded ){
296 return testCategory + "-" + generalFuncs.getTestList( list ) + ( semiNeeded ? ";" : "" )
297}
298def generateOverallGraph( prop, testCategory, graph_saved_directory ){
299
300 if( isPostingResult( prop[ "manualRun" ], prop[ "postResult" ] ) ){
301 node( testMachine ){
302
303 withCredentials( [
304 string( credentialsId: 'db_pass', variable: 'pass' ),
305 string( credentialsId: 'db_user', variable: 'user' ),
306 string( credentialsId: 'db_host', variable: 'host' ),
307 string( credentialsId: 'db_port', variable: 'port' ) ] ) {
308 testList = generalFuncs.getTestList( testCategory )
309 sh '''#!/bin/bash
310 ''' + generalFuncs.basicGraphPart( trend_generator_file, host, port, user, pass, testType, prop[ "ONOSBranch" ] ) + " " + testList + " 20 " + graph_saved_directory
311 }
312 }
313 postResult( prop, false )
314 }
315}
316def getOverallPieGraph( file, host, port, user, pass, branch, type, testList, yOrN, path ){
317 return generalFuncs.basicGraphPart( file, host, port, user, pass, type, branch ) + " \"" + testList + "\" latest " + yOrN + " " + path
318}
319def sqlCommand( testName ){
320 return "\"INSERT INTO " + table_name + " VALUES('\$DATE','" + result_name + "','" + testName + "',\$BUILD_NUMBER, '\$ONOSBranch', \$line);\" "
321}
322def graphGenerating( host, port, user, pass, testName, prop, graph_saved_directory, graph_generator_file ){
323 return generalFuncs.basicGraphPart( graph_generator_file, host, port, user, pass, testName, prop[ "ONOSBranch" ] ) + " 20 " + graph_saved_directory
324}
325def databasePart( wikiPrefix, testName, database_command ){
326 return '''
327 sed 1d ''' + workSpace + "/" + wikiPrefix + "-" + testName + '''.csv | while read line
328 do
329 echo \$line
330 echo ''' + database_command + '''
331 done '''
332}
Devin Lim1253ae82018-02-26 11:13:36 -0800333return this;