blob: 904ebc0f5fa2df5f1d7be723f6d679968440d0c1 [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 Ronquillo4d5f1d02017-10-13 20:23:57 +000028 def __init__(self,testFile):
29 try :
30 testFileHandler = open(testFile, 'r')
Jon Hallfeff3082015-05-19 10:23:26 -070031 except IOError:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000032 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000042 #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 Ronquillo4d5f1d02017-10-13 20:23:57 +000046 if re.match('^(\'\'\')|^(\"\"\")',testFileList[index],0) :
adminbae64d82013-08-01 10:50:15 -070047 index = index + 1
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000048 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000055 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000059 def case_code(self):
Jon Hallfeff3082015-05-19 10:23:26 -070060 index = 0
61 statementsList = self.statementsList
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000062 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000067 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000072 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000077 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000081 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000086 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000091 if step == 0 :
adminbae64d82013-08-01 10:50:15 -070092 i = 0
93 block = ''
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000094 while i < index :
95 block += caseStatements[i]
adminbae64d82013-08-01 10:50:15 -070096 i = i + 1
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000097 stepCode[step] = block
adminbae64d82013-08-01 10:50:15 -070098 step = step + 1
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000099 stepBlock = stepBlock + caseStatements[index]
adminbae64d82013-08-01 10:50:15 -0700100 index = index + 1
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000101 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 Ronquillo4d5f1d02017-10-13 20:23:57 +0000106 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 Ronquillo4d5f1d02017-10-13 20:23:57 +0000111 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 Ronquillo4d5f1d02017-10-13 20:23:57 +0000115 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 Ronquillo4d5f1d02017-10-13 20:23:57 +0000119 def getStepCode(self):
adminbae64d82013-08-01 10:50:15 -0700120 case_step_code = {}
121 case_block = self.case_code()
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000122 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