blob: 1dea300a9af492c1808954c4ce33b7d8789d28c2 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002'''
adminbae64d82013-08-01 10:50:15 -07003Created on 26-Dec-2012
Jeremy Songsterae01bba2016-07-11 15:39:17 -07004Modified 2015 by ON.Lab
adminbae64d82013-08-01 10:50:15 -07005
Jeremy Songsterae01bba2016-07-11 15:39:17 -07006Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
adminbae64d82013-08-01 10:50:15 -07009
10 TestON is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000013 (at your option) any later version.
adminbae64d82013-08-01 10:50:15 -070014
15 TestON is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
Jon Hallfeff3082015-05-19 10:23:26 -070021 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070022
23
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000024'''
adminbae64d82013-08-01 10:50:15 -070025import re
26import sys
27class TestParser:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070028 def __init__( self, testFile ):
29 try:
30 testFileHandler = open( testFile, 'r' )
Jon Hallfeff3082015-05-19 10:23:26 -070031 except IOError:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070032 print "No such file " + testFile
33 sys.exit( 0 )
Jon Hallfeff3082015-05-19 10:23:26 -070034
adminbae64d82013-08-01 10:50:15 -070035 testFileList = testFileHandler.readlines()
Jon Hallfeff3082015-05-19 10:23:26 -070036 self.testscript = testFileList
adminbae64d82013-08-01 10:50:15 -070037 self.caseCode = {}
38 self.caseBlock = ''
39 self.statementsList = []
Jon Hallfeff3082015-05-19 10:23:26 -070040 index = 0
adminbae64d82013-08-01 10:50:15 -070041 self.statementsList = []
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070042 # initialSpaces = len(line) -len(line.lstrip())
43 while index < len( testFileList ):
44 testFileList[ index ] = re.sub( "^\s{8}|^\s{4}", "", testFileList[ index ] )
Jon Hallfeff3082015-05-19 10:23:26 -070045 # Skip multiline comments
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070046 if re.match( '^(\'\'\')|^(\"\"\")', testFileList[ index ], 0 ):
adminbae64d82013-08-01 10:50:15 -070047 index = index + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070048 try:
49 while not re.match( '^\s*(\'\'\')|^\s*(\"\"\")', testFileList[ index ], 0 ):
adminbae64d82013-08-01 10:50:15 -070050 index = index + 1
Jon Hall1306a562015-09-04 11:21:24 -070051 except IndexError:
adminbae64d82013-08-01 10:50:15 -070052 print ''
adminbae64d82013-08-01 10:50:15 -070053
Jon Hallfeff3082015-05-19 10:23:26 -070054 # skip empty lines and single line comments
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070055 elif not re.match( '#|^\s*$', testFileList[ index ], 0 ):
56 self.statementsList.append( testFileList[ index ] )
adminbae64d82013-08-01 10:50:15 -070057 index = index + 1
Jon Hallfeff3082015-05-19 10:23:26 -070058
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070059 def case_code( self ):
Jon Hallfeff3082015-05-19 10:23:26 -070060 index = 0
61 statementsList = self.statementsList
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070062 while index < len( statementsList ):
63 m = re.match( 'def\s+CASE(\d+)', statementsList[ index ], 0 )
adminbae64d82013-08-01 10:50:15 -070064 self.caseBlock = []
65 if m:
adminbae64d82013-08-01 10:50:15 -070066 index = index + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070067 try:
68 while not re.match( '\s*def\s+CASE(\d+)', statementsList[ index ], 0 ):
69 self.caseBlock.append( statementsList[ index ] )
70 if index < len( statementsList )-1:
adminbae64d82013-08-01 10:50:15 -070071 index = index + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070072 else:
adminbae64d82013-08-01 10:50:15 -070073 break
74 index = index - 1
Jon Hall1306a562015-09-04 11:21:24 -070075 except IndexError:
adminbae64d82013-08-01 10:50:15 -070076 print ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070077 self.caseCode[ str( m.group( 1 ) ) ] = self.caseBlock
adminbae64d82013-08-01 10:50:15 -070078 index = index + 1
Jon Hallfeff3082015-05-19 10:23:26 -070079 return self.caseCode
80
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070081 def step_code( self, caseStatements ):
adminbae64d82013-08-01 10:50:15 -070082 index = 0
Jon Hallfeff3082015-05-19 10:23:26 -070083 step = 0
84 stepCode = {}
85 step_flag = False
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070086 while index < len( caseStatements ):
87 m = re.match( 'main\.step', caseStatements[ index ], 0 )
adminbae64d82013-08-01 10:50:15 -070088 stepBlock = ''
89 if m:
90 step_flag = True
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070091 if step == 0:
adminbae64d82013-08-01 10:50:15 -070092 i = 0
93 block = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070094 while i < index:
95 block += caseStatements[ i ]
adminbae64d82013-08-01 10:50:15 -070096 i = i + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070097 stepCode[ step ] = block
adminbae64d82013-08-01 10:50:15 -070098 step = step + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070099 stepBlock = stepBlock + caseStatements[ index ]
adminbae64d82013-08-01 10:50:15 -0700100 index = index + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700101 try:
102 while not re.match( 'main\.step', caseStatements[ index ], 0 ):
103 stepBlock = stepBlock + caseStatements[ index ]
104 if index < len( caseStatements )-1:
adminbae64d82013-08-01 10:50:15 -0700105 index = index + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700106 else:
adminbae64d82013-08-01 10:50:15 -0700107 break
108 index = index - 1
Jon Hall1306a562015-09-04 11:21:24 -0700109 except IndexError:
adminbae64d82013-08-01 10:50:15 -0700110 print ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700111 stepCode[ step ] = stepBlock
adminbae64d82013-08-01 10:50:15 -0700112 step = step + 1
113 index = index + 1
114 # If there is no step defined !!
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700115 if not step_flag:
116 stepCode[ step ] = "".join( caseStatements )
adminbae64d82013-08-01 10:50:15 -0700117 return stepCode
Jon Hallfeff3082015-05-19 10:23:26 -0700118
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700119 def getStepCode( self ):
adminbae64d82013-08-01 10:50:15 -0700120 case_step_code = {}
121 case_block = self.case_code()
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700122 for case in case_block:
123 case_step_code[ case ] = {}
124 step_block = self.step_code( case_block[ case ] )
125 for step in step_block:
126 case_step_code[ case ][ step ] = step_block[ step ]
adminbae64d82013-08-01 10:50:15 -0700127 return case_step_code