Update: robot parser scripts
[csit.git] / resources / tools / robot_output_parser.py
1 #!/usr/bin/python
2
3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """Script parses the data taken by robot framework (output.xml) and dumps
17 intereted values into XML output file."""
18
19 import argparse
20 import re
21 import sys
22 import xml.etree.ElementTree as ET
23
24 from robot.api import ExecutionResult, ResultVisitor
25
26
27 class ExecutionChecker(ResultVisitor):
28     """Iterates through test cases."""
29
30     tc_regexp = re.compile(ur'^tc\d+-((\d+)B|IMIX)-(\d)t(\d)c-(.*)')
31     rate_regexp = re.compile(ur'^[\D\d]*FINAL_RATE:\s(\d+\.\d+)[\D\d]*')
32     lat_regexp = re.compile(ur'^[\D\d]*'\
33         ur'LAT_\d+%NDR:\s\[\'(-?\d+\/-?\d+\/-?\d+)\','\
34         ur'\s\'(-?\d+\/-?\d+\/-?\d+)\'\]\s\n'\
35         ur'LAT_\d+%NDR:\s\[\'(-?\d+\/-?\d+\/-?\d+)\','\
36         ur'\s\'(-?\d+\/-?\d+\/-?\d+)\'\]\s\n'\
37         ur'LAT_\d+%NDR:\s\[\'(-?\d+\/-?\d+\/-?\d+)\','\
38         ur'\s\'(-?\d+\/-?\d+\/-?\d+)\'\]')
39
40     def __init__(self, args):
41         self.root = ET.Element('build',
42                                attrib={'vdevice': args.vdevice})
43
44     def visit_suite(self, suite):
45         """Implements traversing through the suite and its direct children.
46
47         :param suite: Suite to process.
48         :type suite: Suite
49         :return: Nothing.
50         """
51         if self.start_suite(suite) is not False:
52             suite.suites.visit(self)
53             suite.tests.visit(self)
54             self.end_suite(suite)
55
56     def start_suite(self, suite):
57         """Called when suite starts.
58
59         :param suite: Suite to process.
60         :type suite: Suite
61         :return: Nothing.
62         """
63         pass
64
65     def end_suite(self, suite):
66         """Called when suite ends.
67
68         :param suite: Suite to process.
69         :type suite: Suite
70         :return: Nothing.
71         """
72         pass
73
74     def visit_test(self, test):
75         """Implements traversing through the test.
76
77         :param test: Test to process.
78         :type test: Test
79         :return: Nothing.
80         """
81         if self.start_test(test) is not False:
82             self.end_test(test)
83
84     def start_test(self, test):
85         """Called when test starts.
86
87         :param test: Test to process.
88         :type test: Test
89         :return: Nothing.
90         """
91         if any("NDRPDRDISC" in tag for tag in test.tags):
92             if test.status == 'PASS':
93                 tags = []
94                 for tag in test.tags:
95                     tags.append(tag)
96                 test_elem = ET.SubElement(self.root,
97                                           "S"+test.parent.name.replace(" ", ""))
98                 test_elem.attrib['name'] = test.parent.name
99                 test_elem.attrib['framesize'] = str(re.search(\
100                     self.tc_regexp, test.name).group(1))
101                 test_elem.attrib['threads'] = str(re.search(\
102                     self.tc_regexp, test.name).group(3))
103                 test_elem.attrib['cores'] = str(re.search(\
104                     self.tc_regexp, test.name).group(4))
105                 if any("NDRDISC" in tag for tag in test.tags):
106                     test_elem.attrib['lat_100'] = str(re.search(\
107                         self.lat_regexp, test.message).group(1)) + '/' +\
108                         str(re.search(self.lat_regexp, test.message).group(2))
109                     test_elem.attrib['lat_50'] = str(re.search(\
110                         self.lat_regexp, test.message).group(3)) + '/' +\
111                         str(re.search(self.lat_regexp, test.message).group(4))
112                     test_elem.attrib['lat_10'] = str(re.search(\
113                         self.lat_regexp, test.message).group(5)) + '/' +\
114                         str(re.search(self.lat_regexp, test.message).group(6))
115                 test_elem.attrib['tags'] = ', '.join(tags)
116                 test_elem.text = str(re.search(\
117                     self.rate_regexp, test.message).group(1))
118
119     def end_test(self, test):
120         """Called when test ends.
121
122         :param test: Test to process.
123         :type test: Test
124         :return: Nothing.
125         """
126         pass
127
128
129 def parse_tests(args):
130     """Process data from robot output.xml file and return XML data.
131
132     :param args: Parsed arguments.
133     :type args: ArgumentParser
134
135     :return: XML formatted output.
136     :rtype: ElementTree
137     """
138
139     result = ExecutionResult(args.input)
140     checker = ExecutionChecker(args)
141     result.visit(checker)
142
143     return checker.root
144
145
146 def print_error(msg):
147     """Print error message on stderr.
148
149     :param msg: Error message to print.
150     :type msg: str
151     :return: nothing
152     """
153
154     sys.stderr.write(msg+'\n')
155
156
157 def parse_args():
158     """Parse arguments from cmd line.
159
160     :return: Parsed arguments.
161     :rtype ArgumentParser
162     """
163
164     parser = argparse.ArgumentParser()
165     parser.add_argument("-i", "--input", required=True,
166                         type=argparse.FileType('r'),
167                         help="Robot XML log file")
168     parser.add_argument("-o", "--output", required=True,
169                         type=argparse.FileType('w'),
170                         help="XML output file")
171     parser.add_argument("-v", "--vdevice", required=True,
172                         help="VPP version")
173
174     return parser.parse_args()
175
176
177 def main():
178     """Main function."""
179
180     args = parse_args()
181
182     root = parse_tests(args)
183     ET.ElementTree.write(ET.ElementTree(root), args.output)
184
185
186 if __name__ == "__main__":
187     sys.exit(main())