fb085980f6f716f58cd3bc2059c48d42f84d7e5f
[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+:\s((\d+)B|IMIX_v4_1)[\D\d]+\s(\d)'\
31         '(thread|threads)\\s(\\d)(core|cores)\\s(\\d)(rxq|rxqs)')
32     rate_regexp = re.compile(ur'^[\D\d]*FINAL_RATE:\s(\d+\.\d+)[\D\d]*')
33     lat_regexp = re.compile(ur'^[\D\d]*'\
34         'LAT_\\d+%NDR:\\s\\[\'(\\d+\\/\\d+\\/\\d+)\',\\s\'(\\d+\\/\\d+\\/\\d+)\'\\]\\s\n'\
35         'LAT_\\d+%NDR:\\s\\[\'(\\d+\\/\\d+\\/\\d+)\',\\s\'(\\d+\\/\\d+\\/\\d+)\'\\]\\s\n'\
36         'LAT_\\d+%NDR:\\s\\[\'(\\d+\\/\\d+\\/\\d+)\',\\s\'(\\d+\\/\\d+\\/\\d+)\'\\]')
37
38     def __init__(self, args):
39         self.root = ET.Element('build',
40                                attrib={'vdevice': args.vdevice})
41
42     def visit_suite(self, suite):
43         """Implements traversing through the suite and its direct children.
44
45         :param suite: Suite to process.
46         :type suite: Suite
47         :return: Nothing.
48         """
49         if self.start_suite(suite) is not False:
50             suite.suites.visit(self)
51             suite.tests.visit(self)
52             self.end_suite(suite)
53
54     def start_suite(self, suite):
55         """Called when suite starts.
56
57         :param suite: Suite to process.
58         :type suite: Suite
59         :return: Nothing.
60         """
61         pass
62
63     def end_suite(self, suite):
64         """Called when suite ends.
65
66         :param suite: Suite to process.
67         :type suite: Suite
68         :return: Nothing.
69         """
70         pass
71
72     def visit_test(self, test):
73         """Implements traversing through the test.
74
75         :param test: Test to process.
76         :type test: Test
77         :return: Nothing.
78         """
79         if self.start_test(test) is not False:
80             self.end_test(test)
81
82     def start_test(self, test):
83         """Called when test starts.
84
85         :param test: Test to process.
86         :type test: Test
87         :return: Nothing.
88         """
89         if any("PERFTEST_LONG" in tag for tag in test.tags):
90             if test.status == 'PASS':
91                 tags = []
92                 for tag in test.tags:
93                     tags.append(tag)
94                 test_elem = ET.SubElement(self.root,
95                                           test.parent.name.replace(" ", ""))
96                 test_elem.attrib['name'] = test.parent.name
97                 test_elem.attrib['framesize'] = str(re.search(\
98                     self.tc_regexp, test.name).group(2))
99                 test_elem.attrib['workerthreads'] = str(re.search(\
100                     self.tc_regexp, test.name).group(3))
101                 test_elem.attrib['workerspernic'] = str(re.search(\
102                     self.tc_regexp, test.name).group(7))
103                 test_elem.attrib['tags'] = ', '.join(tags)
104                 test_elem.text = str(re.search(\
105                     self.rate_regexp, test.message).group(1))
106
107     def end_test(self, test):
108         """Called when test ends.
109
110         :param test: Test to process.
111         :type test: Test
112         :return: Nothing.
113         """
114         pass
115
116
117 def parse_tests(args):
118     """Process data from robot output.xml file and return XML data.
119
120     :param args: Parsed arguments.
121     :type args: ArgumentParser
122
123     :return: XML formatted output.
124     :rtype: ElementTree
125     """
126
127     result = ExecutionResult(args.input)
128     checker = ExecutionChecker(args)
129     result.visit(checker)
130
131     return checker.root
132
133
134 def print_error(msg):
135     """Print error message on stderr.
136
137     :param msg: Error message to print.
138     :type msg: str
139     :return: nothing
140     """
141
142     sys.stderr.write(msg+'\n')
143
144
145 def parse_args():
146     """Parse arguments from cmd line.
147
148     :return: Parsed arguments.
149     :rtype ArgumentParser
150     """
151
152     parser = argparse.ArgumentParser()
153     parser.add_argument("-i", "--input", required=True,
154                         type=argparse.FileType('r'),
155                         help="Robot XML log file")
156     parser.add_argument("-o", "--output", required=True,
157                         type=argparse.FileType('w'),
158                         help="XML output file")
159     parser.add_argument("-v", "--vdevice", required=True,
160                         help="VPP version")
161
162     return parser.parse_args()
163
164
165 def main():
166     """Main function."""
167
168     args = parse_args()
169
170     root = parse_tests(args)
171     ET.ElementTree.write(ET.ElementTree(root), args.output)
172
173
174 if __name__ == "__main__":
175     sys.exit(main())