blob: 08e41988ae7ef965b1551def7242d7924f19be6a [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002"""
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 Ronquillo23fb2162017-09-15 14:59:57 -070013 ( 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 Ronquillo23fb2162017-09-15 14:59:57 -070024"""
adminbae64d82013-08-01 10:50:15 -070025import re
26import sys
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070027
28
adminbae64d82013-08-01 10:50:15 -070029class TestParser:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070030
31 def __init__( self, testFile ):
32 try:
33 testFileHandler = open( testFile, 'r' )
Jon Hallfeff3082015-05-19 10:23:26 -070034 except IOError:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070035 print "No such file " + testFile
36 sys.exit( 0 )
Jon Hallfeff3082015-05-19 10:23:26 -070037
adminbae64d82013-08-01 10:50:15 -070038 testFileList = testFileHandler.readlines()
Jon Hallfeff3082015-05-19 10:23:26 -070039 self.testscript = testFileList
adminbae64d82013-08-01 10:50:15 -070040 self.caseCode = {}
41 self.caseBlock = ''
42 self.statementsList = []
Jon Hallfeff3082015-05-19 10:23:26 -070043 index = 0
adminbae64d82013-08-01 10:50:15 -070044 self.statementsList = []
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070045 # initialSpaces = len( line ) -len( line.lstrip() )
46 while index < len( testFileList ):
47 testFileList[ index ] = re.sub( "^\s{8}|^\s{4}", "", testFileList[ index ] )
Jon Hallfeff3082015-05-19 10:23:26 -070048 # Skip multiline comments
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070049 if re.match( '^(\'\'\' )|^( \"\"\" )', testFileList[ index ], 0 ):
adminbae64d82013-08-01 10:50:15 -070050 index = index + 1
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070051 try:
52 while not re.match( '^\s*(\'\'\' )|^\s*( \"\"\" )', testFileList[ index ], 0 ):
adminbae64d82013-08-01 10:50:15 -070053 index = index + 1
Jon Hall1306a562015-09-04 11:21:24 -070054 except IndexError:
adminbae64d82013-08-01 10:50:15 -070055 print ''
adminbae64d82013-08-01 10:50:15 -070056
Jon Hallfeff3082015-05-19 10:23:26 -070057 # skip empty lines and single line comments
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070058 elif not re.match( '#|^\s*$', testFileList[ index ], 0 ):
59 self.statementsList.append( testFileList[ index ] )
adminbae64d82013-08-01 10:50:15 -070060 index = index + 1
Jon Hallfeff3082015-05-19 10:23:26 -070061
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070062 def case_code( self ):
Jon Hallfeff3082015-05-19 10:23:26 -070063 index = 0
64 statementsList = self.statementsList
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070065 while index < len( statementsList ):
66 m = re.match( 'def\s+CASE(\d+)', statementsList[ index ], 0 )
adminbae64d82013-08-01 10:50:15 -070067 self.caseBlock = []
68 if m:
adminbae64d82013-08-01 10:50:15 -070069 index = index + 1
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070070 try:
71 while not re.match( '\s*def\s+CASE(\d+)', statementsList[ index ], 0 ):
72 self.caseBlock.append( statementsList[ index ] )
73 if index < len( statementsList ) - 1:
adminbae64d82013-08-01 10:50:15 -070074 index = index + 1
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070075 else:
adminbae64d82013-08-01 10:50:15 -070076 break
77 index = index - 1
Jon Hall1306a562015-09-04 11:21:24 -070078 except IndexError:
adminbae64d82013-08-01 10:50:15 -070079 print ''
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070080 self.caseCode[ str( m.group( 1 ) ) ] = self.caseBlock
adminbae64d82013-08-01 10:50:15 -070081 index = index + 1
Jon Hallfeff3082015-05-19 10:23:26 -070082 return self.caseCode
83
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070084 def step_code( self, caseStatements ):
adminbae64d82013-08-01 10:50:15 -070085 index = 0
Jon Hallfeff3082015-05-19 10:23:26 -070086 step = 0
87 stepCode = {}
88 step_flag = False
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070089 while index < len( caseStatements ):
90 m = re.match( 'main\.step', caseStatements[ index ], 0 )
adminbae64d82013-08-01 10:50:15 -070091 stepBlock = ''
92 if m:
93 step_flag = True
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070094 if step == 0:
adminbae64d82013-08-01 10:50:15 -070095 i = 0
96 block = ''
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070097 while i < index:
98 block += caseStatements[ i ]
adminbae64d82013-08-01 10:50:15 -070099 i = i + 1
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700100 stepCode[ step ] = block
adminbae64d82013-08-01 10:50:15 -0700101 step = step + 1
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700102 stepBlock = stepBlock + caseStatements[ index ]
adminbae64d82013-08-01 10:50:15 -0700103 index = index + 1
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700104 try:
105 while not re.match( 'main\.step', caseStatements[ index ], 0 ):
106 stepBlock = stepBlock + caseStatements[ index ]
107 if index < len( caseStatements ) - 1:
adminbae64d82013-08-01 10:50:15 -0700108 index = index + 1
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700109 else:
adminbae64d82013-08-01 10:50:15 -0700110 break
111 index = index - 1
Jon Hall1306a562015-09-04 11:21:24 -0700112 except IndexError:
adminbae64d82013-08-01 10:50:15 -0700113 print ''
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700114 stepCode[ step ] = stepBlock
adminbae64d82013-08-01 10:50:15 -0700115 step = step + 1
116 index = index + 1
117 # If there is no step defined !!
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700118 if not step_flag:
119 stepCode[ step ] = "".join( caseStatements )
adminbae64d82013-08-01 10:50:15 -0700120 return stepCode
Jon Hallfeff3082015-05-19 10:23:26 -0700121
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700122 def getStepCode( self ):
adminbae64d82013-08-01 10:50:15 -0700123 case_step_code = {}
124 case_block = self.case_code()
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700125 for case in case_block:
126 case_step_code[ case ] = {}
127 step_block = self.step_code( case_block[ case ] )
128 for step in step_block:
129 case_step_code[ case ][ step ] = step_block[ step ]
adminbae64d82013-08-01 10:50:15 -0700130 return case_step_code