Process output of robot to XML file for Plot plugin
[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 sys
21 import xml.etree.ElementTree as ET
22
23 from robot.api import ExecutionResult, ResultVisitor
24
25
26 class ExecutionTestChecker(ResultVisitor):
27     """Iterates through test cases."""
28
29     def __init__(self, args):
30         self.root = ET.Element('build',
31                                attrib={'vdevice': args.vdevice})
32
33     def visit_test(self, test):
34         """Overloaded function. Called when test is found to process data.
35
36         :param test: Test to process.
37         :type test: ExecutionTestChecker
38         """
39
40         if any("PERFTEST_LONG" in tag for tag in test.tags):
41             if test.status == 'PASS':
42                 tags = []
43                 for tag in test.tags:
44                     tags.append(tag)
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]
52
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]
61
62
63 def parse_tests(args):
64     """Parser result of robot output file and return.
65
66     :param args: Parsed arguments.
67     :type args: ArgumentParser
68
69     :return: XML formatted output.
70     :rtype: ElementTree
71     """
72
73     result = ExecutionResult(args.input)
74     checker = ExecutionTestChecker(args)
75     result.visit(checker)
76
77     return checker.root
78
79
80 def print_error(msg):
81     """Print error message on stderr.
82
83     :param msg: Error message to print.
84     :type msg: str
85     :return: nothing
86     """
87
88     sys.stderr.write(msg+'\n')
89
90
91 def parse_args():
92     """Parse arguments from cmd line.
93
94     :return: Parsed arguments.
95     :rtype ArgumentParser
96     """
97
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,
106                         help="VPP version")
107
108     return parser.parse_args()
109
110
111 def main():
112     """Main function."""
113
114     args = parse_args()
115
116     root = parse_tests(args)
117     ET.ElementTree.write(ET.ElementTree(root), args.output)
118
119
120 if __name__ == "__main__":
121     sys.exit(main())