[PATCHv2 2/3] NetTestResultSerializer: add method print_summary

olichtne at redhat.com olichtne at redhat.com
Fri May 31 09:06:39 UTC 2013


From: Ondrej Lichtner <olichtne at redhat.com>

This commit adds the method print_summary to the class
NetTestResultSerializer. The instances of this class contain recipe
results in DOM form. This was previously used to write results in XML
form to a file.

The test results can however be also used to provide a nice summary to
be printed at the end of lnst-ctl execution. This can now be done by
using the new method.

Signed-off-by: Ondrej Lichtner <olichtne at redhat.com>
---
 lnst/Controller/NetTestResultSerializer.py | 152 +++++++++++++++++++++++++++++
 1 file changed, 152 insertions(+)

diff --git a/lnst/Controller/NetTestResultSerializer.py b/lnst/Controller/NetTestResultSerializer.py
index 17b7f51..e7198db 100644
--- a/lnst/Controller/NetTestResultSerializer.py
+++ b/lnst/Controller/NetTestResultSerializer.py
@@ -13,6 +13,7 @@ jpirko at redhat.com (Jiri Pirko)
 
 from xml.dom.minidom import getDOMImplementation
 from lnst.Common.NetTestCommand import str_command
+import logging
 
 def serialize_obj(obj, dom, el, upper_name="unnamed"):
     if isinstance(obj, dict):
@@ -29,6 +30,13 @@ def serialize_obj(obj, dom, el, upper_name="unnamed"):
         text = dom.createTextNode(str(obj))
         el.appendChild(text)
 
+def get_node_val(node):
+    content = []
+    for child in node.childNodes:
+        if child.nodeType == child.TEXT_NODE:
+            content.append(child.nodeValue)
+    return str(''.join(content).strip())
+
 class NetTestResultSerializer:
     def __init__(self):
         impl = getDOMImplementation()
@@ -86,3 +94,147 @@ class NetTestResultSerializer:
             res_data_el = self._dom.createElement("result_data")
             serialize_obj(cmd_res["res_data"], self._dom, res_data_el)
             command_el.appendChild(res_data_el)
+
+    def print_summary(self):
+        output_pairs = []
+
+        for recipe in self._top_el.getElementsByTagName("recipe"):
+            recipe_name = recipe.getAttribute("name")
+            recipe_res = recipe.getAttribute("result")
+            output_pairs.append((recipe_name, recipe_res))
+
+            seq_num = 1
+            for cmd_seq in recipe.getElementsByTagName("command_sequence"):
+                command_sequence = 4*" "+"cmd_seq: %s" % seq_num
+                output_pairs.append((command_sequence, ""))
+
+                seq_num = seq_num + 1
+
+                for command in cmd_seq.getElementsByTagName("command"):
+                    self._format_command(command, output_pairs)
+
+        self._print_pairs(output_pairs)
+
+    def _format_command(self, command, output_pairs):
+        cmd_type = command.getAttribute("type")
+        if cmd_type == "test":
+            self._format_test_command(command, output_pairs)
+        elif cmd_type == "wait":
+            self._format_wait_command(command, output_pairs)
+        elif cmd_type == "intr":
+            self._format_intr_command(command, output_pairs)
+        elif cmd_type == "kill":
+            self._format_kill_command(command, output_pairs)
+        elif cmd_type == "ctl_wait":
+            self._format_ctl_wait_command(command, output_pairs)
+        elif cmd_type == "exec":
+            self._format_exec_command(command, output_pairs)
+        elif cmd_type == "system_config":
+            self._format_system_config(command, output_pairs)
+
+        result_node = command.getElementsByTagName("result")[0]
+        cmd_res = result_node.getAttribute("result")
+
+        if cmd_res == "FAIL":
+            err_node = result_node.getElementsByTagName("error_message")
+            if len(err_node) != 0:
+                err_node = err_node[0]
+                text = get_node_val(err_node)
+                output_pairs.append((12*" "+"error message: "+text, ""))
+
+    def _format_test_command(self, command, output_pairs):
+        result_node = command.getElementsByTagName("result")[0]
+        cmd_res = result_node.getAttribute("result")
+
+        cmd_val = command.getAttribute("value")
+        cmd_type = command.getAttribute("type")
+        if command.hasAttribute("bg_id"):
+            bg_id = " bg_id: %s" %  command.getAttribute("bg_id")
+        else:
+            bg_id = ""
+        cmd = 8*" "+"%-14s%s%s" %(cmd_type, cmd_val, bg_id)
+        output_pairs.append((cmd, cmd_res))
+
+    def _format_wait_command(self, command, output_pairs):
+        result_node = command.getElementsByTagName("result")[0]
+        cmd_res = result_node.getAttribute("result")
+
+        cmd_val = command.getAttribute("value")
+        cmd_type = command.getAttribute("type")
+        if command.hasAttribute("bg_id"):
+            bg_id = " bg_id: %s" %  command.getAttribute("bg_id")
+        else:
+            bg_id = ""
+        cmd = 8*" "+"%-13s id: %s%s" %(cmd_type, cmd_val, bg_id)
+        output_pairs.append((cmd, cmd_res))
+
+    def _format_intr_command(self, command, output_pairs):
+        self._format_wait_command(command, output_pairs)
+
+    def _format_kill_command(self, command, output_pairs):
+        self._format_wait_command(command, output_pairs)
+
+    def _format_exec_command(self, command, output_pairs):
+        self._format_test_command(command, output_pairs)
+
+    def _format_ctl_wait_command(self, command, output_pairs):
+        result_node = command.getElementsByTagName("result")[0]
+        cmd_res = result_node.getAttribute("result")
+
+        cmd_val = command.getAttribute("value")
+        cmd_type = command.getAttribute("type")
+        cmd = 8*" "+"%-14s%ss" %(cmd_type, cmd_val)
+        output_pairs.append((cmd, cmd_res))
+
+    def _format_system_config(self, command, output_pairs):
+        result_node = command.getElementsByTagName("result")[0]
+        cmd_res = result_node.getAttribute("result")
+
+        cmd_type = command.getAttribute("type")
+        if command.hasAttribute("bg_id"):
+            bg_id = " bg_id: %s" %  command.getAttribute("bg_id")
+        else:
+            bg_id = ""
+        cmd = 8*" "+"%-14s%s" %(cmd_type, bg_id)
+        output_pairs.append((cmd, cmd_res))
+
+        result_data_nodes = command.getElementsByTagName("result_data")
+        if len(result_data_nodes) != 0:
+            result_data_node = result_data_nodes[0]
+            options_nodes = result_data_node.getElementsByTagName("options")
+            for options_node in options_nodes:
+                for option in options_node.childNodes:
+                    previous_node = option.getElementsByTagName("previous_val")[0]
+                    current_node = option.getElementsByTagName("current_val")[0]
+                    previous_val = get_node_val(previous_node)
+                    current_val = get_node_val(current_node)
+                    opt_left = 12*" "+"%s" % option.tagName
+                    opt_right = "previous: %s current: %s" \
+                                % (previous_val, current_val)
+                    output_pairs.append((opt_left, opt_right))
+
+    def _print_pairs(self, output_pairs):
+        max_left = 0
+        max_right = 0
+        for left, right in output_pairs:
+            if len(left) > max_left and right != "":
+                max_left = len(left)
+            if len(right) > max_right:
+                max_right = len(right)
+
+        full_length = max_left + max_right
+        if full_length % 2:
+            full_length = full_length+2
+        else:
+            full_length = full_length+1
+
+        logging.info("="*((full_length-9)/2) + " SUMMARY " + "="*((full_length-9)/2))
+        for left, right in output_pairs:
+            if right != "":
+                space_fill = full_length - len(left) - len(right)
+                output = left + (space_fill)*" " + right
+            else:
+                output = left
+            logging.info(output)
+        logging.info("="*(full_length))
+
-- 
1.8.1.4



More information about the LNST-developers mailing list