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