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."""
21 import xml.etree.ElementTree as ET
23 from robot.api import ExecutionResult, ResultVisitor
26 class ExecutionTestChecker(ResultVisitor):
27 """Iterates through test cases."""
29 def __init__(self, args):
30 self.root = ET.Element('build',
31 attrib={'vdevice': args.vdevice})
33 def visit_test(self, test):
34 """Overloaded function. Called when test is found to process data.
36 :param test: Test to process.
37 :type test: ExecutionTestChecker
40 if any("PERFTEST_LONG" in tag for tag in test.tags):
41 if test.status == 'PASS':
45 for keyword in test.keywords:
46 for assign in keyword.assign:
47 if assign == '${framesize}':
48 framesize = keyword.args[0]
49 if 'worker threads' in keyword.name:
50 workers = keyword.name.split('\'')[1]
51 workers_per_nic = keyword.name.split('\'')[3]
53 test_elem = ET.SubElement(self.root,
54 test.longname.split('.')[3].replace(" ",""))
55 test_elem.attrib['name'] = test.longname.split('.')[3]
56 test_elem.attrib['workerthreads'] = workers
57 test_elem.attrib['workerspernic'] = workers_per_nic
58 test_elem.attrib['framesize'] = framesize
59 test_elem.attrib['tags'] = ', '.join(tags)
60 test_elem.text = test.message.split(' ')[1]
63 def parse_tests(args):
64 """Parser result of robot output file and return.
66 :param args: Parsed arguments.
67 :type args: ArgumentParser
69 :return: XML formatted output.
73 result = ExecutionResult(args.input)
74 checker = ExecutionTestChecker(args)
81 """Print error message on stderr.
83 :param msg: Error message to print.
88 sys.stderr.write(msg+'\n')
92 """Parse arguments from cmd line.
94 :return: Parsed arguments.
98 parser = argparse.ArgumentParser()
99 parser.add_argument("-i", "--input", required=True,
100 type=argparse.FileType('r'),
101 help="Robot XML log file")
102 parser.add_argument("-o", "--output", required=True,
103 type=argparse.FileType('w'),
104 help="XML output file")
105 parser.add_argument("-v", "--vdevice", required=True,
108 return parser.parse_args()
116 root = parse_tests(args)
117 ET.ElementTree.write(ET.ElementTree(root), args.output)
120 if __name__ == "__main__":