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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 """Script parses the data taken by robot framework (output.xml) and dumps
17 intereted values into XML output file."""
22 import xml.etree.ElementTree as ET
24 from robot.api import ExecutionResult, ResultVisitor
27 class ExecutionChecker(ResultVisitor):
28 """Iterates through test cases."""
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+)\'\]')
40 def __init__(self, args):
41 self.root = ET.Element('build',
42 attrib={'vdevice': args.vdevice})
44 def visit_suite(self, suite):
45 """Implements traversing through the suite and its direct children.
47 :param suite: Suite to process.
51 if self.start_suite(suite) is not False:
52 suite.suites.visit(self)
53 suite.tests.visit(self)
56 def start_suite(self, suite):
57 """Called when suite starts.
59 :param suite: Suite to process.
65 def end_suite(self, suite):
66 """Called when suite ends.
68 :param suite: Suite to process.
74 def visit_test(self, test):
75 """Implements traversing through the test.
77 :param test: Test to process.
81 if self.start_test(test) is not False:
84 def start_test(self, test):
85 """Called when test starts.
87 :param test: Test to process.
91 if any("NDRPDRDISC" in tag for tag in test.tags):
92 if test.status == 'PASS':
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))
119 def end_test(self, test):
120 """Called when test ends.
122 :param test: Test to process.
129 def parse_tests(args):
130 """Process data from robot output.xml file and return XML data.
132 :param args: Parsed arguments.
133 :type args: ArgumentParser
135 :return: XML formatted output.
139 result = ExecutionResult(args.input)
140 checker = ExecutionChecker(args)
141 result.visit(checker)
146 def print_error(msg):
147 """Print error message on stderr.
149 :param msg: Error message to print.
154 sys.stderr.write(msg+'\n')
158 """Parse arguments from cmd line.
160 :return: Parsed arguments.
161 :rtype ArgumentParser
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,
174 return parser.parse_args()
182 root = parse_tests(args)
183 ET.ElementTree.write(ET.ElementTree(root), args.output)
186 if __name__ == "__main__":