Skip to content

Commit

Permalink
validate: launcher: Move logfile handling out of Reporter and into Test
Browse files Browse the repository at this point in the history
This makes each Test handle its own logfile, allowing the Reporter to
work on multiple tests at the same time.

Patch 5/5 to move logfile handling out of Reporter and into Test.
  • Loading branch information
Ramiro Polla authored and tsaunier committed Feb 5, 2015
1 parent 500206d commit d12f55d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
56 changes: 42 additions & 14 deletions validate/launcher/baseclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def clean(self):
self._starting_time = None
self.result = Result.NOT_RUN
self.logfile = None
self.out = None
self.extra_logfiles = []
self._env_variable = ''

Expand Down Expand Up @@ -102,16 +103,41 @@ def add_env_variable(self, variable, value):
self._env_variable += " "
self._env_variable += "%s=%s" % (variable, value)

def get_extra_log_content(self, extralog):
if extralog not in self.extra_logfiles:
return ""
def open_logfile(self):
path = os.path.join(self.options.logsdir,
self.classname.replace(".", os.sep))
mkdir(os.path.dirname(path))
self.logfile = path

if self.options.redirect_logs == 'stdout':
self.out = sys.stdout
elif self.options.redirect_logs == 'stderr':
self.out = sys.stderr
else:
self.out = open(path, 'w+')

def close_logfile(self):
if not self.options.redirect_logs:
self.out.close()

self.out = None

f = open(extralog, 'r+')
def _get_file_content(self, file_name):
f = open(file_name, 'r+')
value = f.read()
f.close()

return value

def get_log_content(self):
return self._get_file_content(self.logfile)

def get_extra_log_content(self, extralog):
if extralog not in self.extra_logfiles:
return ""

return self._get_file_content(extralog)

def get_classname(self):
name = self.classname.split('.')[-1]
classname = self.classname.replace('.%s' % name, '')
Expand Down Expand Up @@ -235,13 +261,15 @@ def _kill_subprocess(self):

def thread_wrapper(self):
self.process = subprocess.Popen("exec " + self.command,
stderr=self.reporter.out,
stdout=self.reporter.out,
stderr=self.out,
stdout=self.out,
shell=True,
env=self.proc_env)
self.process.wait()

def run(self):
self.open_logfile()

self.command = "%s " % (self.application)
self._starting_time = time.time()
self.build_arguments()
Expand All @@ -256,12 +284,12 @@ def run(self):
for log in self.extra_logfiles:
message += "\n - %s" % log

self.reporter.out.write("=================\n"
"Test name: %s\n"
"Command: '%s'\n"
"=================\n\n"
% (self.classname, self.command))
self.reporter.out.flush()
self.out.write("=================\n"
"Test name: %s\n"
"Command: '%s'\n"
"=================\n\n"
% (self.classname, self.command))
self.out.flush()

printc(message, Colors.OKBLUE)

Expand All @@ -282,6 +310,8 @@ def run(self):
" (" + self.message + ")" if self.message else ""),
color=utils.get_color_for_result(self.result))

self.close_logfile()

return self.result


Expand Down Expand Up @@ -748,11 +778,9 @@ def run_tests(self, cur_test_num, total_num_tests):
i = cur_test_num
for test in self.tests:
sys.stdout.write("[%d / %d] " % (i + 1, total_num_tests))
self.reporter.open_logfile(test)
res = test.run()
i += 1
self.reporter.after_test(test)
self.reporter.close_logfile()
if res != Result.PASSED and (self.options.forever or
self.options.fatal_error):
return test.result
Expand Down
23 changes: 1 addition & 22 deletions validate/launcher/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class Reporter(Loggable):
def __init__(self, options):
Loggable.__init__(self)

self.out = None
self.options = options
self._start_time = 0
self.stats = {'timeout': 0,
Expand All @@ -69,19 +68,6 @@ def init_timer(self):
"""Initialize a timer before starting tests."""
self._start_time = time.time()

def open_logfile(self, test):
path = os.path.join(self.options.logsdir,
test.classname.replace(".", os.sep))
mkdir(os.path.dirname(path))
test.logfile = path

if self.options.redirect_logs == 'stdout':
self.out = sys.stdout
elif self.options.redirect_logs == 'stderr':
self.out = sys.stderr
else:
self.out = open(path, 'w+')

def set_failed(self, test):
self.stats["failure"] += 1

Expand All @@ -104,12 +90,6 @@ def after_test(self, test):

self.add_results(test)

def close_logfile(self):
if not self.options.redirect_logs:
self.out.close()

self.out = None

def final_report(self):
print "\n"
printc("Final Report:", title=True)
Expand Down Expand Up @@ -160,8 +140,7 @@ def final_report(self):
def _get_captured(self, test):
captured = ""
if not self.options.redirect_logs:
self.out.seek(0)
value = self.out.read()
value = test.get_log_content()
if value:
captured += '<system-out><![CDATA[%s' % \
escape_cdata(value)
Expand Down

1 comment on commit d12f55d

@thiblahute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Please sign in to comment.