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+:\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+)\'\\]')
38 def __init__(self, args):
39 self.root = ET.Element('build',
40 attrib={'vdevice': args.vdevice})
42 def visit_suite(self, suite):
43 """Implements traversing through the suite and its direct children.
45 :param suite: Suite to process.
49 if self.start_suite(suite) is not False:
50 suite.suites.visit(self)
51 suite.tests.visit(self)
54 def start_suite(self, suite):
55 """Called when suite starts.
57 :param suite: Suite to process.
63 def end_suite(self, suite):
64 """Called when suite ends.
66 :param suite: Suite to process.
72 def visit_test(self, test):
73 """Implements traversing through the test.
75 :param test: Test to process.
79 if self.start_test(test) is not False:
82 def start_test(self, test):
83 """Called when test starts.
85 :param test: Test to process.
89 if any("PERFTEST_LONG" in tag for tag in test.tags):
90 if test.status == 'PASS':
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))
107 def end_test(self, test):
108 """Called when test ends.
110 :param test: Test to process.
117 def parse_tests(args):
118 """Process data from robot output.xml file and return XML data.
120 :param args: Parsed arguments.
121 :type args: ArgumentParser
123 :return: XML formatted output.
127 result = ExecutionResult(args.input)
128 checker = ExecutionChecker(args)
129 result.visit(checker)
134 def print_error(msg):
135 """Print error message on stderr.
137 :param msg: Error message to print.
142 sys.stderr.write(msg+'\n')
146 """Parse arguments from cmd line.
148 :return: Parsed arguments.
149 :rtype ArgumentParser
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,
162 return parser.parse_args()
170 root = parse_tests(args)
171 ET.ElementTree.write(ET.ElementTree(root), args.output)
174 if __name__ == "__main__":