# HG changeset patch
# User Kaitlin Rupert <karupert(a)us.ibm.com>
# Date 1218470737 25200
# Node ID 30dd178dfe0f86b01fe36d36166e7e6c024be6ba
# Parent dfd2ac2440a2543fffe6e8c6ae27445e14932ca0
[TEST] #2 Add reporting library.
Include CIMOM type in email heading.
Updates:
-Read from the file using xreadlines()
-Reorganize some of the report building pieces
-Fixed issue where mail was being sent to the from address
Signed-off-by: Kaitlin Rupert <karupert(a)us.ibm.com>
diff -r dfd2ac2440a2 -r 30dd178dfe0f suites/libvirt-cim/lib/XenKvmLib/reporting.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/suites/libvirt-cim/lib/XenKvmLib/reporting.py Mon Aug 11 09:05:37 2008 -0700
@@ -0,0 +1,176 @@
+#
+# Copyright 2008 IBM Corp.
+#
+# Authors:
+# Kaitlin Rupert <karupert(a)us.ibm.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+import os
+import sys
+import commands
+import smtplib
+from time import gmtime, strftime
+from VirtLib import utils
+
+def get_cmd_val(cmd, ip):
+ rc, out = utils.run_remote(ip, cmd)
+ if rc != 0:
+ return "Unknown"
+ return out
+
+def get_libvirt_ver(ip):
+ libvirt_ver = "Unknown"
+ hyp_ver = "Unknown"
+ cmd = "virsh version"
+ virsh_ver = get_cmd_val(cmd, ip)
+ if virsh_ver != "Unknown":
+ if len(virsh_ver.splitlines()) == 4:
+ if virsh_ver.splitlines()[0].find("libvir"):
+ libvirt_ver = virsh_ver.splitlines()[0].split()[4]
+
+ if virsh_ver.splitlines()[3].find("hypervisor"):
+ hyp_ver = virsh_ver.splitlines()[3].split("hypervisor")[1]
+ hyp_ver = hyp_ver.split(": ")[1]
+
+ return libvirt_ver, hyp_ver
+
+
+def get_cimom_ver(ip):
+ cimom = get_cmd_val("ps -ef | grep cimserver | grep -v grep", ip)
+ if cimom != "Unknown":
+ cimom = "Pegasus"
+ else:
+ cimom = get_cmd_val("ps -ef | grep sfcb | grep -v grep", ip)
+ if cimom != "Unknown":
+ cimom = "sfcb"
+
+ if cimom == "Pegasus":
+ cimom_ver = get_cmd_val("cimserver -v", ip)
+ elif cimom == "sfcb":
+ cimom_ver = get_cmd_val("sfcbd -v", ip)
+ else:
+ cimom_ver = "unknown version"
+
+ return cimom, cimom_ver
+
+
+def get_env_data(rev, changeset, ip):
+ distro = get_cmd_val("cat /etc/issue | awk 'NR<=1'", ip)
+ kernel_ver = get_cmd_val("uname -r", ip)
+
+ libvirt_ver, hyp_ver = get_libvirt_ver(ip)
+
+ cimom, cimom_ver = get_cimom_ver(ip)
+
+ env = "Distro: %s\nKernel: %s\nlibvirt: %s\nHypervisor: %s\nCIMOM: %s
%s\n"\
+ % (distro, kernel_ver, libvirt_ver, hyp_ver, cimom, cimom_ver)
+
+ lc_ver = "Libvirt-cim revision: %s\nLibvirt-cim changeset: %s\n" % \
+ (rev, changeset)
+
+ return env + lc_ver
+
+def parse_run_output(log_file):
+ rvals = { 'PASS' : 0,
+ 'FAIL' : 0,
+ 'XFAIL' : 0,
+ 'SKIP' : 0,
+ }
+
+ tstr = { 'PASS' : "",
+ 'FAIL' : "",
+ 'XFAIL' : "",
+ 'SKIP' : "",
+ }
+
+ fd = open(log_file, "r")
+
+ run_output = ""
+ for line in fd.xreadlines():
+ for type, val in rvals.iteritems():
+ if type in line:
+ if type == "FAIL" and "py: FAIL" not in line:
+ continue
+ rvals[type] += 1
+ tstr[type] += "%s" % line
+ run_output += line
+
+ fd.close()
+
+ return rvals, tstr, run_output
+
+def build_report_body(rvals, tstr, div):
+ results = ""
+ test_total = 0
+ for type, val in rvals.iteritems():
+ results += " %s:\t%d\n" % (type, val)
+ test_total += val
+
+ results_total = " -----------------\n Total:\t%d\n" % test_total
+
+ test_block = ""
+ for type, str in tstr.iteritems():
+ if type == "PASS" or str == "":
+ continue
+ test_block += "%s Test Summary:\n%s\n%s" % (type, str, div)
+
+ return results, results_total, test_block
+
+def gen_report(rev, changeset, virt, ip, log_file):
+ date = strftime("%b %d %Y", gmtime())
+
+ cimom, cimom_ver = get_cimom_ver(ip)
+
+ heading = "%s on %s Test Run Summary for %s" % (virt, cimom, date)
+ sys_env = get_env_data(rev, changeset, ip)
+
+ divider = "=================================================\n"
+
+ rvals, tstr, run_output = parse_run_output(log_file)
+
+ res, res_total, test_block = build_report_body(rvals, tstr, divider)
+
+ report = divider + heading + "\n" + divider + sys_env + divider + res \
+ + res_total + divider + test_block + "Full report:\n" \
+ + run_output
+
+ fd = open(log_file, "w")
+ rc = fd.write(report)
+ if rc is not None:
+ print "Error %s writing report to: %s." % (rc, log_file)
+ fd.close()
+
+ return report, heading
+
+
+def send_report(to_addr, from_addr, relay, report, heading):
+ headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (from_addr,
to_addr,
+ heading)
+
+ message = headers + report
+
+ try:
+ server = smtplib.SMTP(relay)
+ result = server.sendmail(from_addr, to_addr, message)
+ server.quit()
+
+ if result:
+ for recip in result.keys():
+ print "Could not deliver mail to: %s" % recip
+
+ except Exception, details:
+ print "Encountered a problem mailing report: %s" % details
+