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 interested 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':
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):
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).
112 except AttributeError:
113 test_elem.attrib['lat_100'] = "-1/-1/-1/-1/-1/-1"
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).
119 except AttributeError:
120 test_elem.attrib['lat_50'] = "-1/-1/-1/-1/-1/-1"
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).
126 except AttributeError:
127 test_elem.attrib['lat_10'] = "-1/-1/-1/-1/-1/-1"
128 test_elem.attrib['tags'] = ', '.join(tags)
130 test_elem.text = str(re.search(
131 self.rate_regexp, test.message).group(1))
132 except AttributeError:
133 test_elem.text = "-1"
135 def end_test(self, test):
136 """Called when test ends.
138 :param test: Test to process.
145 def parse_tests(args):
146 """Process data from robot output.xml file and return XML data.
148 :param args: Parsed arguments.
149 :type args: ArgumentParser
151 :return: XML formatted output.
155 result = ExecutionResult(args.input)
156 checker = ExecutionChecker(args)
157 result.visit(checker)
162 def print_error(msg):
163 """Print error message on stderr.
165 :param msg: Error message to print.
170 sys.stderr.write(msg + '\n')
174 """Parse arguments from cmd line.
176 :return: Parsed arguments.
177 :rtype ArgumentParser
180 parser = argparse.ArgumentParser()
181 parser.add_argument("-i", "--input",
183 type=argparse.FileType('r'),
184 help="Robot XML log file")
185 parser.add_argument("-o", "--output",
187 type=argparse.FileType('w'),
188 help="XML output file")
189 parser.add_argument("-v", "--vdevice",
195 return parser.parse_args()
203 root = parse_tests(args)
204 ET.ElementTree.write(ET.ElementTree(root), args.output)
207 if __name__ == "__main__":