
# HG changeset patch # User Kaitlin Rupert <karupert@us.ibm.com> # Date 1218470737 25200 # Node ID a5111986921da879e2dcc57a1529c7f4d902aebf # Parent 5cb81d3b311601846b23bc2288a0dfc9b255c683 [TEST] Add reporting library. Include CIMOM type in email heading. Signed-off-by: Kaitlin Rupert <karupert@us.ibm.com> diff -r 5cb81d3b3116 -r a5111986921d 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,170 @@ +# +# Copyright 2008 IBM Corp. +# +# Authors: +# Kaitlin Rupert <karupert@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 build_report(run_output, rev, changeset, virt, ip): + rvals = { 'PASS' : 0, + 'FAIL' : 0, + 'XFAIL' : 0, + 'SKIP' : 0, + } + + tstr = { 'PASS' : "", + 'FAIL' : "", + 'XFAIL' : "", + 'SKIP' : "", + } + + 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" + + for line in run_output.splitlines(): + 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\n" % line + + 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, divider) + + report = divider + heading + "\n" + divider + sys_env + divider + results \ + + results_total + divider + test_block + "Full report:\n" \ + + run_output + + return report, heading + + +def gen_report(rev, changeset, virt, ip, log_file): + fd = open(log_file, "r") + run_results = fd.read() + fd.close() + + msg_body, heading = build_report(run_results, rev, changeset, virt, ip) + + fd = open(log_file, "w") + rc = fd.write(msg_body) + if rc is not None: + print "Error %s writing report to: %s." % (rc, log_file) + fd.close() + + return msg_body, 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(to_addr, from_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 +