Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Try another approach for saving epu harness logs

Printing out in teardown doesn't work for jenkins because it is not captured
in test results. Instead we copy the logfiles to a directory specified by env
and print out a funny [[ATTACHMENT|/path/to/file] line to stdout.
  • Loading branch information...
commit c95dfca38ae2682c07b95e546716c684738b652c 1 parent f389d32
@labisso labisso authored
Showing with 56 additions and 21 deletions.
  1. +2 −3 epuharness/fixture.py
  2. +54 −18 epuharness/harness.py
View
5 epuharness/fixture.py
@@ -14,6 +14,7 @@
log = logging.getLogger(__name__)
+
class TestFixture(object):
"""A mixin to provide some helper methods to test classes
"""
@@ -22,7 +23,6 @@ class TestFixture(object):
libcloud_drivers = None
dashi = None
-
def setup_harness(self, *args, **kwargs):
if os.path.exists(self.epuh_persistence):
raise Exception("EPUHarness running. Can't run this test")
@@ -33,7 +33,7 @@ def setup_harness(self, *args, **kwargs):
def teardown_harness(self, remove_dir=True):
if self.epuharness:
try:
- self.epuharness.stop(remove_dir=remove_dir, print_logs=True)
+ self.epuharness.stop(remove_dir=remove_dir)
except Exception:
log.exception("Error shutting down epuharness!")
@@ -50,7 +50,6 @@ def teardown_harness(self, remove_dir=True):
except Exception:
log.exception("Error removing fake libcloud db: %s", driver.sqlite_db)
-
cleanup_harness = teardown_harness
def get_clients(self, deployment_str, dashi):
View
72 epuharness/harness.py
@@ -46,6 +46,7 @@ def __init__(self, exchange=None, pidantic_dir=None, amqp_uri=None, config=None)
self.amqp_cfg = dict(self.CFG.server.amqp)
self.factory = None
+ self.savelogs_dir = None
def _setup_factory(self):
@@ -81,7 +82,21 @@ def status(self, exit=True):
else:
return status
- def stop(self, services=None, force=False, remove_dir=True, print_logs=False):
+ def get_logfiles(self):
+ """Returns a list of logfile paths relevant to epuharness instance
+ """
+ # pretty hacky. we could get these over the supd API instead.
+ # but that's certainly slower and not really better.
+ pidantic_dir = os.path.abspath(self.pidantic_dir)
+ epuharness_dir = os.path.join(pidantic_dir, "epu-harness")
+
+ logfiles = []
+ for f in os.listdir(epuharness_dir):
+ if os.path.splitext(f)[1].lower() == ".log":
+ logfiles.append(os.path.join(epuharness_dir, f))
+ return logfiles
+
+ def stop(self, services=None, force=False, remove_dir=True):
"""Stop services that were previously started by epuharness
@param force: When False raises an exception when there is something
@@ -115,12 +130,11 @@ def stop(self, services=None, force=False, remove_dir=True, print_logs=False):
instance.cleanup()
if cleanup:
-
- if print_logs:
+ if self.savelogs_dir:
try:
- self._print_logs()
+ self._save_logs(self.savelogs_dir)
except Exception:
- log.exception("Problem printing logs. Proceeding.")
+ log.exception("Problem saving logs. Proceeding.")
self.factory.terminate()
if remove_dir:
@@ -129,19 +143,14 @@ def stop(self, services=None, force=False, remove_dir=True, print_logs=False):
self.dashi.cancel()
self.dashi.disconnect()
- def _print_logs(self):
- # pretty hacky. we could get these over the supd API instead.
- # but that's certainly slower and not really better.
- epuharness_dir = os.path.join(self.pidantic_dir, "epu-harness")
- for f in os.listdir(epuharness_dir):
- if os.path.splitext(f)[1].lower() == ".log":
- try:
- with file(os.path.join(epuharness_dir, f)) as logfile:
- print "\n===== %s %s" % (f, "=" * max(72 - len(f), 0))
- shutil.copyfileobj(logfile, sys.stdout)
- print "=" * 79
- except Exception:
- log.exception("Error printing logfile %s", f)
+ def _save_logs(self, output_dir):
+ for logfile in self.get_logfiles():
+ basename = os.path.basename(logfile)
+ dest_path = os.path.join(output_dir, basename)
+ try:
+ shutil.copy2(logfile, dest_path)
+ except Exception:
+ log.exception("Error copying logfile %s", logfile)
def start(self, deployment_file=None, deployment_str=None):
"""Start services defined in the deployment file provided. If a
@@ -236,6 +245,33 @@ def start(self, deployment_file=None, deployment_str=None):
users = phantom.get('users', [])
self._start_phantom(phantom_name, phantom.get('config', {}), users, port=port)
+ self.savelogs_dir = self._get_savelogs_dir()
+ if self.savelogs_dir:
+
+ # by printing out this funny format, Jenkins will pick up these
+ # files and include them in the test results UI as attachments.
+ # Note that we print out the path the file will be COPIED to when
+ # the harness is stopped. We can't just wait and print it out when
+ # we actually copy it because that happens during tearDown, output
+ # from which is apparently difficult for nose to capture.
+ for logfile in self.get_logfiles():
+ basename = os.path.basename(logfile)
+ print "[[ATTACHMENT|%s]]" % os.path.join(self.savelogs_dir, basename)
+
+ def _get_savelogs_dir(self):
+ savelogs_dir = os.environ.get("EPUHARNESS_SAVELOGS_DIR")
+ if savelogs_dir and not os.path.exists(savelogs_dir):
+ log.warn("savelogs directory doesn't exist: %s", savelogs_dir)
+ savelogs_dir = None
+
+ if savelogs_dir:
+ savelogs_dir = os.path.abspath(savelogs_dir)
+ # create a subdirectory just for this harness
+ prefix = "test"
+ savelogs_dir = tempfile.mkdtemp(prefix=prefix, dir=savelogs_dir)
+
+ return savelogs_dir
+
def _start_phantom(self, name, config, users, port=None, exe_name='phantomcherrypy'):
if not port:
Please sign in to comment.
Something went wrong with that request. Please try again.