HC Test: Disable NSH plugin installation and tests
[csit.git] / resources / tools / scripts / 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 interested 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
97                 test_elem = ET.SubElement(
98                     self.root, "S" + test.parent.name.replace(" ", ""))
99                 test_elem.attrib['name'] = test.parent.name
100                 test_elem.attrib['framesize'] = str(re.search(
101                     self.tc_regexp, test.name).group(1))
102                 test_elem.attrib['threads'] = str(re.search(
103                     self.tc_regexp, test.name).group(3))
104                 test_elem.attrib['cores'] = str(re.search(
105                     self.tc_regexp, test.name).group(4))
106                 if any("NDRDISC" in tag for tag in test.tags):
107                     try:
108                         test_elem.attrib['lat_100'] = str(re.search(
109                             self.lat_regexp, test.message).group(1)) + '/' +\
110                             str(re.search(self.lat_regexp, test.message).
111                                 group(2))
112                     except AttributeError:
113                         test_elem.attrib['lat_100'] = "-1/-1/-1/-1/-1/-1"
114                     try:
115                         test_elem.attrib['lat_50'] = str(re.search(
116                             self.lat_regexp, test.message).group(3)) + '/' +\
117                             str(re.search(self.lat_regexp, test.message).
118                                 group(4))
119                     except AttributeError:
120                         test_elem.attrib['lat_50'] = "-1/-1/-1/-1/-1/-1"
121                     try:
122                         test_elem.attrib['lat_10'] = str(re.search(
123                             self.lat_regexp, test.message).group(5)) + '/' +\
124                             str(re.search(self.lat_regexp, test.message).
125                                 group(6))
126                     except AttributeError:
127                         test_elem.attrib['lat_10'] = "-1/-1/-1/-1/-1/-1"
128                 test_elem.attrib['tags'] = ', '.join(tags)
129                 try:
130                     test_elem.text = str(re.search(
131                         self.rate_regexp, test.message).group(1))
132                 except AttributeError:
133                     test_elem.text = "-1"
134
135     def end_test(self, test):
136         """Called when test ends.
137
138         :param test: Test to process.
139         :type test: Test
140         :return: Nothing.
141         """
142         pass
143
144
145 def parse_tests(args):
146     """Process data from robot output.xml file and return XML data.
147
148     :param args: Parsed arguments.
149     :type args: ArgumentParser
150
151     :return: XML formatted output.
152     :rtype: ElementTree
153     """
154
155     result = ExecutionResult(args.input)
156     checker = ExecutionChecker(args)
157     result.visit(checker)
158
159     return checker.root
160
161
162 def print_error(msg):
163     """Print error message on stderr.
164
165     :param msg: Error message to print.
166     :type msg: str
167     :return: nothing
168     """
169
170     sys.stderr.write(msg + '\n')
171
172
173 def parse_args():
174     """Parse arguments from cmd line.
175
176     :return: Parsed arguments.
177     :rtype ArgumentParser
178     """
179
180     parser = argparse.ArgumentParser()
181     parser.add_argument("-i", "--input",
182                         required=True,
183                         type=argparse.FileType('r'),
184                         help="Robot XML log file")
185     parser.add_argument("-o", "--output",
186                         required=True,
187                         type=argparse.FileType('w'),
188                         help="XML output file")
189     parser.add_argument("-v", "--vdevice",
190                         required=False,
191                         default="",
192                         type=str,
193                         help="VPP version")
194
195     return parser.parse_args()
196
197
198 def main():
199     """Main function."""
200
201     args = parse_args()
202
203     root = parse_tests(args)
204     ET.ElementTree.write(ET.ElementTree(root), args.output)
205
206
207 if __name__ == "__main__":
208     sys.exit(main())