Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

change test log level when running tests

change the arg parsing to use optionparsing module
and changed the logging method to create a dynamically generated
logging configuration when running each test

testrunner creates a logging.conf file under the tmp
folder and uses logging.config.fileConfig to use that
conf file

Change-Id: I4152be938ef180e3a7a9a983da4dfeb617507bb1
Reviewed-on: http://review.couchbase.org/12192
Reviewed-by: Keith Batten <kbatten@gmail.com>
Reviewed-by: Farshid Ghods <farshid.ghods@gmail.com>
Tested-by: Farshid Ghods <farshid.ghods@gmail.com>
  • Loading branch information...
commit 4e443d5785003f2918e5366cab8d7c8c7e124f24 1 parent 2e6df38
@farshidce farshidce authored
Showing with 106 additions and 122 deletions.
  1. +3 −57 lib/logger.py
  2. +33 −0 logging.conf.sample
  3. +70 −65 testrunner
View
60 lib/logger.py
@@ -1,7 +1,4 @@
import logging
-from logging.handlers import RotatingFileHandler
-import os
-import uuid
global _logger
@@ -15,64 +12,13 @@ def stop_logger():
@staticmethod
def get_logger():
- if not Logger._logger:
- Logger._logger = Logger.start_logger('console')
- return Logger._logger
+ return logging.getLogger()
@staticmethod
def start_logger(name):
# create logger
- logger = logging.getLogger(name)
- logger.setLevel(logging.INFO)
-
- # create console handler and set level to debug
- consoleHandler = logging.StreamHandler()
- consoleHandler.setLevel(logging.DEBUG)
-
- # create formatter
- formatter = logging.Formatter("[%(asctime)s] - [%(module)s] [%(thread)d] - %(levelname)s - %(message)s")
-
- max_size = 20 * 1024 * 1024 #max size is 50 megabytes
-
- filename = "{0}.log".format(name)
- if "TEMP-FOLDER" in os.environ:
- filename = "{0}/{1}.log".format(os.environ["TEMP-FOLDER"], name)
- fileHandler = RotatingFileHandler(filename, backupCount=2, maxBytes=max_size)
-
- # add formatter to ch
- consoleHandler.setFormatter(formatter)
- fileHandler.setFormatter(formatter)
-
- # add ch to logger
- logger.addHandler(consoleHandler)
- logger.addHandler(fileHandler)
- Logger._logger = logger
- print 'start logging to {0}'.format(filename)
- print ' cat {0}'.format(filename)
- return Logger._logger
+ return logging.getLogger()
def new_logger(name):
- logger = logging.getLogger(name)
- logger.setLevel(logging.INFO)
-
- # create console handler and set level to debug
- consoleHandler = logging.StreamHandler()
- consoleHandler.setLevel(logging.DEBUG)
-
- # create formatter
- formatter = logging.Formatter("[%(asctime)s] - [%(module)s] [%(thread)d] - %(levelname)s - %(message)s")
-
- max_size = 20 * 1024 * 1024 #max size is 50 megabytes
-
- filename = "{0}.log".format(name)
- fileHandler = RotatingFileHandler(filename, backupCount=2, maxBytes=max_size)
-
- # add formatter to ch
- consoleHandler.setFormatter(formatter)
- fileHandler.setFormatter(formatter)
-
- # add ch to logger
- logger.addHandler(consoleHandler)
- logger.addHandler(fileHandler)
- return logger
+ return logging.getLogger()
View
33 logging.conf.sample
@@ -0,0 +1,33 @@
+[loggers]
+keys=root
+
+[logger_root]
+handlers=screen,file
+level=DEBUG
+
+[formatters]
+keys=simple,complex
+
+[formatter_simple]
+format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
+
+[formatter_complex]
+format=[%(asctime)s] - [%(module)s] [%(thread)d] - %(levelname)s - %(message)s
+
+[handlers]
+keys=file,screen
+
+[handler_file]
+class=handlers.TimedRotatingFileHandler
+interval=midnight
+backupCount=5
+maxBytes=20000000
+formatter=complex
+level=@@LEVEL@@
+args=('@@FILENAME@@',)
+
+[handler_screen]
+class=StreamHandler
+formatter=simple
+level=@@LEVEL@@
+args=(sys.stdout,)
View
135 testrunner
@@ -4,88 +4,90 @@ import sys
sys.path.append("lib")
sys.path.append("pytests")
import time
-import getopt
import unittest
import os
import logger
+import logging.config
from xunit import XUnitTestResult
from TestInput import TestInputParser, TestInputSingleton
+from optparse import OptionParser, OptionGroup
def usage(err=None):
- if err:
- print "Error: %s\n" % err
- r = 1
- else:
- r = 0
-
print """\
Syntax: testrunner [options]
-Options:
- -c <file> Config file name (located in the conf subdirectory)
- -i <file> Path to .ini file containing server information
- -t <test> Test name (multiple -t options add more tests)
- -p k=v,k2=v2,... Optional key=value parameters, comma-separated
- -n NO-OP - emit test names, but don't actually run them
- -h Show this help message
-
-The config file can have a list of test names.
-
Examples:
./testrunner -i tmp/local.ini -t performance.perf.DiskDrainRate
./testrunner -i tmp/local.ini -t performance.perf.DiskDrainRate.test_9M
"""
- sys.exit(r)
+ sys.exit(0)
def parse_args(argv):
- noop = False
+
+ parser = OptionParser()
+
+ parser.add_option("-q", action="store_false", dest="verbose")
+
+ tgroup = OptionGroup(parser, "TestCase/Runlist Options")
+ tgroup.add_option("-i", "--ini",
+ dest="ini", help="Path to .ini file containing server information,e.g -i tmp/local.ini")
+ tgroup.add_option("-c", "--config", dest="runlist",
+ help="Config file name (located in the conf subdirectory), e.g -c py-view.conf")
+ tgroup.add_option("-t", "--test",
+ dest="testcase", help="Test name (multiple -t options add more tests) e.g -t performance.perf.DiskDrainRate")
+ parser.add_option_group(tgroup)
+
+ parser.add_option("-p", "--params",
+ dest="params", help="Optional key=value parameters, comma-separated -p k=v,k2=v2,...",
+ default="")
+ parser.add_option("-n", "--noop",action="store_true",
+ help="NO-OP - emit test names, but don't actually run them e.g -n true")
+ parser.add_option("-l", "--log-level",
+ dest="loglevel", default="INFO",help="e.g -l info,warning,error")
+ options, args = parser.parse_args()
+
tests = []
- arg_i = None
- arg_p = None
-
- try:
- (opts, args) = getopt.getopt(argv[1:],
- 'c:hi:np:t:v:', [])
- for o, a in opts:
- if o == "-h":
- usage()
- elif o == "-c":
- parse_runlist(tests, a)
- elif o == "-t":
- append_test(tests, a)
- elif o == "-i":
- arg_i = o + " " + a
- elif o == "-p":
- arg_p = o + " " + a
- elif o == "-n":
- noop = True
-
- if noop:
- print("\n".join(tests))
- exit(0)
-
- test_input = TestInputParser.get_test_input(argv)
- if not test_input.servers:
- usage("no servers specified")
- if not tests:
- usage("no tests specified")
- return tests, test_input, arg_i, arg_p
-
- except IndexError:
- usage()
- except getopt.GetoptError, err:
- usage(err)
+
+
+ if not options.ini:
+ parser.error("you need to specify a resource file")
+ parser.print_help()
+ if not options.testcase and not options.runlist:
+ parser.error("you need to specify runlist (-c) or a test case (-t)")
+ parser.print_help()
+ if options.runlist:
+ parse_runlist(tests,options.runlist)
+ if options.testcase:
+ tests.append(options.testcase)
+ if options.noop:
+ print("\n".join(tests))
+ sys.exit(0)
+
+ test_input = TestInputParser.get_test_input(argv)
+ return tests, test_input, options.ini, options.params , options
def find_runlist(filename):
- if os.path.exists(filename):
- return file(filename)
- if os.path.exists("conf/" + filename):
- return file("conf/" + filename)
+ if filename:
+ if os.path.exists(filename):
+ return file(filename)
+ if os.path.exists("conf/" + filename):
+ return file("conf/" + filename)
return None
+def create_log_file(log_config_file_name,log_file_name,level):
+ tmpl_log_file = open("logging.conf.sample")
+ log_file = open(log_config_file_name, "w")
+ log_file.truncate()
+ for line in tmpl_log_file:
+ newline = line.replace("@@LEVEL@@", level)
+ newline = newline.replace("@@FILENAME@@", log_file_name)
+ log_file.write(newline)
+ log_file.close()
+ tmpl_log_file.close()
+
def parse_runlist(tests, filename):
f = find_runlist(filename)
@@ -116,14 +118,15 @@ def append_test(tests, name):
if __name__ == "__main__":
str_time = time.strftime("%H:%M:%S", time.localtime()).replace(":", "-")
- names, test_input, arg_i, arg_p = parse_args(sys.argv)
+ names, test_input, arg_i, arg_p , options = parse_args(sys.argv)
xunit = XUnitTestResult()
tmp_folder = "tmp-{0}".format(str_time)
os.makedirs(tmp_folder)
#this only works on linux/mac
- os.environ["TEMP-FOLDER"] = os.getcwd() + "/" + tmp_folder
+ tmp_folder_abs_path = os.getcwd() + "/" + tmp_folder
+ os.environ["TEMP-FOLDER"] = tmp_folder_abs_path
print os.environ["TEMP-FOLDER"]
for name in names:
@@ -131,13 +134,16 @@ if __name__ == "__main__":
start_time = time.time()
argument_split = [a.strip() for a in re.split("[,]?([^,=]+)=", name)[1:]]
params = dict(zip(argument_split[::2], argument_split[1::2]))
+ log_config_filename = ""
if params:
- log_name = name
+ log_name = tmp_folder_abs_path + "/" + name + ".log"
+ log_config_filename = tmp_folder_abs_path + "/" + name + ".logging.conf"
else:
dotnames = name.split('.')
- log_name = dotnames[len(dotnames) - 1]
-
- logger.Logger.start_logger(log_name)
+ log_name = tmp_folder_abs_path + "/" + dotnames[len(dotnames) - 1] + ".log"
+ log_config_filename = tmp_folder_abs_path + "/" + dotnames[len(dotnames) - 1] + ".logging.conf"
+ create_log_file(log_config_filename,log_name, options.loglevel)
+ logging.config.fileConfig(log_config_filename)
name = name.split(",")[0]
print(" ./testrunner {0} {1} -t {2}".format(arg_i or "", arg_p or "", name))
@@ -145,7 +151,6 @@ if __name__ == "__main__":
TestInputSingleton.input.test_params.update(params)
suite = unittest.TestLoader().loadTestsFromName(name)
result = unittest.TextTestRunner(verbosity=2).run(suite)
- logger.Logger.stop_logger()
time_taken = time.time() - start_time
if result.failures or result.errors:
for failure in result.failures:
Please sign in to comment.
Something went wrong with that request. Please try again.