nose plugin to capture glance service logs

Adding custom nose plugin to capture entire glance service logs
for failed tests.

Disabled by default, enable by setting:

  $ NOSE_GLANCELOGCAPTURE=true ; ./run_tests.sh

Change-Id: I8a3bd9529ec6adb8b98804e0ddc266a7ea8eca08
This commit is contained in:
Eoghan Glynn 2012-03-16 20:52:44 +00:00
parent e1fae1d4bb
commit 2145c24edb
3 changed files with 83 additions and 1 deletions

View File

@ -395,6 +395,7 @@ class FunctionalTest(unittest.TestCase):
self.registry_server.pid_file,
self.scrubber_daemon.pid_file]
self.files_to_destroy = []
self.log_files = []
def tearDown(self):
if not self.disabled:
@ -485,6 +486,8 @@ class FunctionalTest(unittest.TestCase):
self.assertTrue(re.search("Starting glance-[a-z]+ with", out))
self.log_files.append(server.log_file)
self.wait_for_servers([server.bind_port], expect_launch)
def start_servers(self, **kwargs):
@ -500,6 +503,8 @@ class FunctionalTest(unittest.TestCase):
# Start up the API and default registry server
exitcode, out, err = self.api_server.start(**kwargs)
self.log_files.append(self.api_server.log_file)
self.assertEqual(0, exitcode,
"Failed to spin up the API server. "
"Got: %s" % err)
@ -507,6 +512,8 @@ class FunctionalTest(unittest.TestCase):
exitcode, out, err = self.registry_server.start(**kwargs)
self.log_files.append(self.registry_server.log_file)
self.assertEqual(0, exitcode,
"Failed to spin up the Registry server. "
"Got: %s" % err)
@ -619,3 +626,15 @@ class FunctionalTest(unittest.TestCase):
shutil.copy(src_file_name, dst_dir)
dst_file_name = os.path.join(dst_dir, file_name)
return dst_file_name
def dump_logs(self):
dump = ''
for log in self.log_files:
dump += '\nContent of %s:\n\n' % log
if os.path.exists(log):
f = open(log, 'r')
for line in f:
dump += line
else:
dump += '<empty>'
return dump

View File

@ -0,0 +1,60 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 Red Hat, Inc
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Custom nose plugin to capture the entire glance service logs for failed tests.
"""
from glance.tests import functional
from nose.plugins.base import Plugin
from nose.util import ln, safe_str
class GlanceLogCapture(Plugin):
enabled = False
env_opt = 'NOSE_GLANCELOGCAPTURE'
name = 'glance-logcapture'
def options(self, parser, env):
parser.add_option(
"--glance-logcapture",
action="store_true",
default=env.get(self.env_opt),
dest="glance_logcapture",
help="Enable glance log capture plugin [NOSE_GLANCELOGCAPTURE]")
def configure(self, options, conf):
self.enabled = options.glance_logcapture
def formatFailure(self, test, err):
return self.formatError(test, err)
def formatError(self, test, err):
if self.enabled:
ec, ev, tb = err
err = (ec, self._dump_logs(ev, test), tb)
return err
def _dump_logs(self, ev, test):
ret = ev
if isinstance(test.test, functional.FunctionalTest):
ret = '\n'.join([safe_str(ev),
ln('>> begin captured glance logging <<')] +
[test.test.dump_logs()] +
[ln('>> end captured glance logging <<')])
return ret

View File

@ -59,6 +59,8 @@ import sys
gettext.install('glance', unicode=1)
from glance.tests import logcapture
from nose import config
from nose import result
from nose import core
@ -290,4 +292,5 @@ if __name__ == '__main__':
runner = GlanceTestRunner(stream=c.stream,
verbosity=c.verbosity,
config=c)
sys.exit(not core.run(config=c, testRunner=runner))
sys.exit(not core.run(config=c, testRunner=runner,
addplugins=[logcapture.GlanceLogCapture()]))