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