#! /usr/bin/env python # -*- coding: UTF-8 -*- """ Running all eGovMon unit tests $Id: runalltests 1584 2009-07-31 13:30:57Z goodwin $ """ # Copyright 2008-2010 eGovMon # This program is distributed under the terms of the GNU General # Public License. # # This file is part of the eGovernment Monitoring # (eGovMon) # # eGovMon 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 of the License, or # (at your option) any later version. # # eGovMon 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 eGovMon; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, # MA 02110-1301 USA __author__ = "$Author$" __version__ = "$Revision$" __updated__ = "$LastChangedDate: 2009-07-31 13:30:57 +0000 (Fri, 31 Jul 2009) $" import os import sys import socket import sc sc = sc.SystemConfiguration() import random import time import smtplib from logit import * def getKeyWord(data,keyword,multiple=False): if not multiple: return [i for i in data if i.lower().startswith(keyword.lower()+':')][0].split(':')[-1].strip() else: return [i.split(':')[-1].strip() for i in data if i.lower().startswith(keyword.lower()+':')] def presentpretty(lines,test): retval = [] status = getKeyWord(lines,'status') desc = test.strip('/').split('/')[-1] duration = getKeyWord(lines,'Elapsed time (sec)') if not 'OK' in status: retval += 'ERROR running tests for',desc,status,'!\n' elif int(getKeyWord(lines,'tests'))==0: retval += 'ERROR no tests found for',desc,status,'!\n' else: retval += 'Tests for',desc,'passed (',getKeyWord(lines,'tests'),'tests - duration ',duration,')\n' return False,' '.join(retval),lines,0 retval += 'Number of tests applied:',getKeyWord(lines,'tests'),'\n' retval += 'Number of tests failed:',getKeyWord(lines,'failures'),'\n' retval += 'Number of tests threw unexpected exceptions:',getKeyWord(lines,'errors'),'\n' retval += 'Complete duration of tests:',duration,'\n' if getKeyWord(lines,'fail in test',multiple=True)+getKeyWord(lines,'error in test',multiple=True): retval += 'The following tests failed:','\n' for appliedtests in getKeyWord(lines,'fail in test',multiple=True)+getKeyWord(lines,'error in test',multiple=True): retval += ' ',appliedtests,'\n' retval += 'If this is not already done, please report this bug to trac as soon as possible and fix the issues.','\n'*5 return True,' '.join(retval),lines,int(getKeyWord(lines,'failures'))+int(getKeyWord(lines,'errors')) #smtplib.SMTPDataError: (451, 'Greylisting in action, please try again in 5 minutes') def sendemail(text,subject): headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sc.administratoremail, sc.administratoremail, subject) message = headers + text mailServer = smtplib.SMTP('smtp.uia.no') mailServer.sendmail(sc.administratoremail, sc.administratoremail, message) mailServer.quit() if __name__ == '__main__': if 'sleep' in sys.argv: sleeptime = random.randint(0,60*60*2) logit('Sleeping '+str(sleeptime/60) + ' minutes to avoid deterministic effects.','testresults') time.sleep(sleeptime) error = '' #Sometimes previous tests are hanging. Kill these. runningtests = [i.split()[1] for i in os.popen('ps aux | grep test.py | grep -v grep').read().split('\n') if i] for runningtest in runningtests: os.popen('kill -9 '+runningtest) error += 'Stray tests where found which had to be killed. Some of the tests are most likely hanging.\n' #Actual testing if sc.homedir.rstrip('/').endswith('trunk'): sc.homedir = sc.homedir.rstrip('/')[:-5] os.chdir(sc.homedir) os.system('svn update') os.chdir(os.path.join(sc.homedir,"tests")) directories = [i for i in os.listdir('./') if os.path.isdir(i) and not i.startswith('.') and not i.startswith('build')] + ['../trunk/egovmontime','../trunk/SystemConfiguration','../trunk/DBCleaner','../trunk/MemCache','../trunk/CronWAM/','../trunk/SiteURLServer','../trunk/logit','../trunk/SamplingAlgorithm','../trunk/Crawler','../trunk/eGovMonDB/','../trunk/PyTripleStore','../trunk/AddURLs','../trunk/CrawlerServer','../trunk/Monitoring'] try: if sc.eaccchecker: directories.append('../trunk/eAccessibilityChecker') except AttributeError: print 'eAccessility checker ignored' random.shuffle(directories) #Avoiding deterministic effects subject = 'Automatic Status Report for eGovMon '+socket.gethostname().strip()+' ' + time.asctime() text = subject +'\n\n' noterror = '' sumerrors = 0 if not os.popen('proctorbatch --version').read(): error += 'Proctorbatch was not found. This is needed to run all unit tests. Please install this.\n\n' for dir in ['WAMConformance']: os.chdir(os.path.join(sc.homedir,"tests",dir)) if os.path.isfile('setup.py'): print 'Installing',dir os.system('python setup.py install') print 'Runing tests for',dir logit('Running tests for '+dir,'testresults') os.chdir(os.path.join(sc.homedir,"tests",dir)) try: failed,temp,rawdata,numerrors =presentpretty(os.popen('proctorbatch --parsable ./test.py 2>>temp').readlines(),dir) except IndexError: numerrors =1 failed = True temp = dir.strip('/').split('/')[-1]+' Could not parse output. Missing proctorbatch?\n' rawdata = temp sumerrors += numerrors if failed: error += temp else: noterror += temp if error: text += 'The sanity checking discovered errors! Please look further into the report.\n' text += 'The total number of errors which should be fixed: '+str(sumerrors)+'\n\n' text += error + noterror if not 'noemail' in sys.argv: sendemail(text,subject) else: print text logit(temp,'testresults') logit(rawdata,'testresults')