Пример #1
0
def runHeadless(options):
    headless_options=dict()
    with open(options["headless_config"], 'r') as f:
        config = f.read().splitlines()
        for line in config:
            line = line.strip()
            if line:
                line = line.split('=')
                headless_options[line[0]]=line[1]

    os.chdir(os.path.dirname(__file__) + "/..")

    class CleanFileHandler(logging.FileHandler):
        """File handler that does not write terminal escape codes to files, which
        makes it easier to do things like send a log via email"""
        def emit(self, record):
            message = record.getMessage()
            message = re.sub("\x1b\[[0-9][0-9]?;?[0-9]?[0-9]?m", "", message)
            self.stream.write(message)
            self.flush()

    os.mkdir('/tmp/forcebalance')
    warningHandler = CleanFileHandler('/tmp/forcebalance/test.err','w')
    warningHandler.setLevel(logging.WARNING)
    logfile = "/tmp/forcebalance/%s.log" % time.strftime('%m%d%y_%H%M%S')
    debugHandler = CleanFileHandler(logfile,'w')
    debugHandler.setLevel(logging.DEBUG)
    
    logging.getLogger("test").addHandler(warningHandler)
    logging.getLogger("test").addHandler(debugHandler)

    options['loglevel']=logging.DEBUG
    
    runner=ForceBalanceTestRunner()
    results=runner.run(**options)

    if headless_options.has_key('enable_smtp')\
    and headless_options['enable_smtp'].lower() in ['true','error']:
        if headless_options['enable_smtp'].lower()=='true' or not results.wasSuccessful():
            import smtplib
            from email.mime.text import MIMEText
            from email.mime.multipart import MIMEMultipart

            # establish connection with smtp server
            server = smtplib.SMTP(host = headless_options["smtp_server"],
                                  port = headless_options["smtp_port"])
            if headless_options.has_key("smtp_tls") and headless_options["smtp_tls"].lower()=="true":
                server.starttls()
            server.login(user = headless_options["smtp_user"],
                         password = headless_options["smtp_password"])

            # prepare message text
            
            text = "ForceBalance unit test suite ran at %s with %d failures, %d errors. " %\
            (time.strftime('%x %X %Z'),len(results.failures), len(results.errors)) 
            text += "See attached debug log for more details\n"
            if not results.wasSuccessful():
                text += "Error Output:\n"
                with open('/tmp/forcebalance/test.err','r') as warnings:
                    text += warnings.read()

            log = ''
            with open(logfile,'r') as debug:
                log += debug.read()

            # format message headers
            msg = MIMEMultipart()
            msg['From']= headless_options["smtp_source"]
            msg['To']= headless_options["smtp_destination"]
            msg['Subject']=headless_options["smtp_subject"]

            content = MIMEText(text)
            attachment = MIMEText(log)
            attachment.add_header('Content-Disposition', 'attachment', filename="debug.log")
            msg.attach(content)
            msg.attach(attachment)

            # send message
            server.sendmail(headless_options["smtp_source"],
                            [ headless_options["smtp_destination"] ],
                            msg.as_string())

            # close connection
            server.quit()
    if headless_options.has_key('log_location'):
        shutil.copy(logfile, headless_options['log_location'])
    shutil.rmtree('/tmp/forcebalance')
Пример #2
0
def run(options):
    logging.getLogger("test").addHandler(forcebalance.nifty.RawStreamHandler(sys.stderr))
    runner=ForceBalanceTestRunner()
    runner.run(**options)