Move QuietLogger and logwrap to core
1. Move QuietLogger and logwrap to core
2. Fix QuietLogger (if log level was upper, than in param,
QuietLogger ebabled additional logging instead of pass)
3. Cover by unit-tests
4. Added missed license text
5. add *.log to gitignore
Change-Id: If842791000c76852fe1dec1e66edcc081c278b2c
Blueprint: fuel-qa-join-helpers
(cherry picked from commit b7ca0ff
)
This commit is contained in:
parent
43eabe378f
commit
7ec0f37fa1
|
@ -50,7 +50,7 @@ doc/_build/
|
|||
|
||||
# Logs
|
||||
/logs
|
||||
sys_test.log
|
||||
*.log
|
||||
|
||||
# Certs
|
||||
/ca.crt
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
# Copyright 2016 Mirantis, Inc.
|
||||
#
|
||||
# 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.
|
||||
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger('fuel-qa.{}'.format(__name__))
|
|
@ -0,0 +1,127 @@
|
|||
# Copyright 2016 Mirantis, Inc.
|
||||
#
|
||||
# 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.
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
# pylint: disable=import-error
|
||||
from mock import call
|
||||
from mock import Mock
|
||||
from mock import patch
|
||||
# pylint: enable=import-error
|
||||
|
||||
from core.helpers import log_helpers
|
||||
|
||||
# pylint: disable=no-self-use
|
||||
|
||||
|
||||
@patch('core.helpers.log_helpers.logger', autospec=True)
|
||||
class TestLogWrap(unittest.TestCase):
|
||||
def test_positive(self, logger):
|
||||
@log_helpers.logwrap
|
||||
def func(*args, **kwargs):
|
||||
return 'complete with {} {}'.format(args, kwargs)
|
||||
|
||||
call_args = 't', 'e'
|
||||
call_kwargs = dict(s='s', t='t')
|
||||
|
||||
result = func(*call_args, **call_kwargs)
|
||||
self.assertEqual(
|
||||
result,
|
||||
'complete with {} {}'.format(call_args, call_kwargs)
|
||||
)
|
||||
|
||||
logger.assert_has_calls((
|
||||
call.debug(
|
||||
"Calling: func with args: {} {}".format(
|
||||
call_args, call_kwargs)),
|
||||
call.debug(
|
||||
"Done: func with result: {}".format(result))
|
||||
))
|
||||
|
||||
def test_negative(self, logger):
|
||||
@log_helpers.logwrap
|
||||
def func(*args, **kwargs):
|
||||
raise ValueError(args, kwargs)
|
||||
|
||||
call_args = 't', 'e'
|
||||
call_kwargs = dict(s='s', t='t')
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
func(*call_args, **call_kwargs)
|
||||
|
||||
logger.assert_has_calls((
|
||||
call.debug(
|
||||
"Calling: func with args: {} {}".format(
|
||||
call_args, call_kwargs)),
|
||||
call.exception(
|
||||
'func raised: ValueError({}, {})\n'.format(
|
||||
call_args, call_kwargs))
|
||||
))
|
||||
|
||||
|
||||
@patch('logging.StreamHandler')
|
||||
@patch('core.helpers.log_helpers.logger', autospec=True)
|
||||
class TestQuietLogger(unittest.TestCase):
|
||||
def test_default(self, logger_obj, handler_cls):
|
||||
handler = Mock()
|
||||
handler.configure_mock(level=logging.INFO)
|
||||
handler_cls.return_value = handler
|
||||
|
||||
with log_helpers.QuietLogger():
|
||||
log_helpers.logger.warning('Test')
|
||||
|
||||
handler.assert_has_calls((
|
||||
call.setLevel(logging.INFO + 1),
|
||||
call.setLevel(logging.INFO)
|
||||
))
|
||||
|
||||
logger_obj.assert_has_calls((call.warning('Test'), ))
|
||||
|
||||
def test_upper_level(self, logger_obj, handler_cls):
|
||||
handler = Mock()
|
||||
handler.configure_mock(level=logging.INFO)
|
||||
handler_cls.return_value = handler
|
||||
|
||||
with log_helpers.QuietLogger(logging.WARNING):
|
||||
log_helpers.logger.warning('Test')
|
||||
|
||||
handler.assert_has_calls((
|
||||
call.setLevel(logging.WARNING + 1),
|
||||
call.setLevel(logging.INFO)
|
||||
))
|
||||
|
||||
logger_obj.assert_has_calls((call.warning('Test'), ))
|
||||
|
||||
def test_lower_level(self, logger_obj, handler_cls):
|
||||
handler = Mock()
|
||||
handler.configure_mock(level=logging.INFO)
|
||||
handler_cls.return_value = handler
|
||||
|
||||
with log_helpers.QuietLogger(logging.DEBUG):
|
||||
log_helpers.logger.warning('Test')
|
||||
|
||||
handler.assert_has_calls((
|
||||
call.setLevel(logging.INFO),
|
||||
))
|
||||
|
||||
logger_obj.assert_has_calls((
|
||||
call.debug(
|
||||
'QuietLogger requested lower level, than is already set. '
|
||||
'Not changing level'),
|
||||
call.warning('Test'),
|
||||
))
|
|
@ -1,3 +1,17 @@
|
|||
# Copyright 2016 Mirantis, Inc.
|
||||
#
|
||||
# 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.
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
|
||||
|
|
|
@ -1,3 +1,17 @@
|
|||
# Copyright 2016 Mirantis, Inc.
|
||||
#
|
||||
# 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.
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
# Copyright 2016 Mirantis, Inc.
|
||||
#
|
||||
# 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.
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import functools
|
||||
import logging
|
||||
|
||||
from core import logger
|
||||
|
||||
|
||||
def logwrap(func):
|
||||
@functools.wraps(func)
|
||||
def wrapped(*args, **kwargs):
|
||||
logger.debug(
|
||||
"Calling: {} with args: {} {}".format(
|
||||
func.__name__, args, kwargs
|
||||
)
|
||||
)
|
||||
try:
|
||||
result = func(*args, **kwargs)
|
||||
logger.debug(
|
||||
"Done: {} with result: {}".format(func.__name__, result))
|
||||
except BaseException as e:
|
||||
logger.exception(
|
||||
'{func} raised: {exc!r}\n'.format(func=func.__name__, exc=e))
|
||||
raise
|
||||
return result
|
||||
return wrapped
|
||||
|
||||
|
||||
class QuietLogger(object):
|
||||
"""Reduce logging level while context is executed."""
|
||||
|
||||
def __init__(self, upper_log_level=None):
|
||||
"""Reduce logging level while context is executed.
|
||||
|
||||
:param upper_log_level: log level to ignore
|
||||
:type upper_log_level: int
|
||||
"""
|
||||
self.log_level = upper_log_level
|
||||
self.level = None
|
||||
|
||||
def __enter__(self):
|
||||
console = logging.StreamHandler()
|
||||
self.level = console.level
|
||||
if self.log_level is None:
|
||||
self.log_level = self.level
|
||||
elif self.log_level < self.level:
|
||||
logger.debug(
|
||||
'QuietLogger requested lower level, than is already set. '
|
||||
'Not changing level')
|
||||
return
|
||||
console.setLevel(self.log_level + 1)
|
||||
|
||||
def __exit__(self, exc_type, exc_value, exc_tb):
|
||||
logging.StreamHandler().setLevel(self.level)
|
||||
|
||||
__all__ = ['logwrap', 'QuietLogger', 'logger']
|
|
@ -16,7 +16,7 @@ from __future__ import unicode_literals
|
|||
|
||||
import requests
|
||||
|
||||
from fuelweb_test import logwrap
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
|
||||
class CollectorClient(object):
|
||||
|
|
|
@ -14,12 +14,13 @@
|
|||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import functools
|
||||
import logging.config
|
||||
import os
|
||||
import traceback
|
||||
import warnings
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
from core.helpers.log_helpers import QuietLogger
|
||||
|
||||
from fuelweb_test.settings import LOGS_DIR
|
||||
|
||||
if not os.path.exists(LOGS_DIR):
|
||||
|
@ -100,40 +101,4 @@ warnings.filterwarnings(
|
|||
|
||||
logger = logging.getLogger('fuel-qa.{}'.format(__name__))
|
||||
|
||||
|
||||
def logwrap(func):
|
||||
@functools.wraps(func)
|
||||
def wrapped(*args, **kwargs):
|
||||
logger.debug(
|
||||
"Calling: {} with args: {} {}".format(
|
||||
func.__name__, args, kwargs
|
||||
)
|
||||
)
|
||||
try:
|
||||
result = func(*args, **kwargs)
|
||||
logger.debug(
|
||||
"Done: {} with result: {}".format(func.__name__, result))
|
||||
except BaseException as e:
|
||||
logger.error(
|
||||
'{func} raised: {exc!r}\n'
|
||||
'Traceback: {tb!s}'.format(
|
||||
func=func.__name__, exc=e, tb=traceback.format_exc()))
|
||||
raise
|
||||
return result
|
||||
return wrapped
|
||||
|
||||
|
||||
class QuietLogger(object):
|
||||
"""Reduce logging level while context is executed."""
|
||||
|
||||
def __init__(self, upper_log_level=logging.WARNING):
|
||||
self.log_level = upper_log_level
|
||||
self.storage = None
|
||||
|
||||
def __enter__(self):
|
||||
console = logging.StreamHandler()
|
||||
self.storage = console.level
|
||||
console.setLevel(self.log_level + 1)
|
||||
|
||||
def __exit__(self, exc_type, exc_value, exc_tb):
|
||||
logging.StreamHandler().setLevel(self.storage)
|
||||
__all__ = ['QuietLogger', 'logwrap', 'logger']
|
||||
|
|
|
@ -36,8 +36,9 @@ from six.moves import xrange
|
|||
# pylint: enable=redefined-builtin
|
||||
import yaml
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test.helpers.ssh_manager import SSHManager
|
||||
from fuelweb_test.helpers.utils import get_mongo_partitions
|
||||
from fuelweb_test.settings import DNS_SUFFIX
|
||||
|
|
|
@ -12,7 +12,8 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from fuelweb_test import logwrap
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test.helpers.ssh_manager import SSHManager
|
||||
|
||||
|
||||
|
|
|
@ -37,10 +37,11 @@ from six.moves import xrange
|
|||
from six.moves import urllib
|
||||
# pylint: enable=import-error
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test.helpers import checkers
|
||||
from fuelweb_test.helpers.ssh_manager import SSHManager
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test.settings import DISABLE_SSL
|
||||
from fuelweb_test.settings import PATH_TO_CERT
|
||||
from fuelweb_test.settings import VERIFY_SSL
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
import subprocess
|
||||
|
||||
from fuelweb_test import logwrap
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
|
||||
class Ebtables(object):
|
||||
|
|
|
@ -25,8 +25,9 @@ from proboscis.asserts import assert_true
|
|||
from six.moves import cStringIO
|
||||
import yaml
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test.helpers.decorators import retry
|
||||
from fuelweb_test.helpers.regenerate_repo import regenerate_centos_repo
|
||||
from fuelweb_test.helpers.regenerate_repo import regenerate_ubuntu_repo
|
||||
|
|
|
@ -16,7 +16,7 @@ import select
|
|||
import socket
|
||||
import threading
|
||||
|
||||
from fuelweb_test import logwrap
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
|
||||
class LogServer(threading.Thread):
|
||||
|
|
|
@ -21,7 +21,8 @@
|
|||
|
||||
from proboscis.asserts import assert_equal
|
||||
|
||||
from fuelweb_test import logwrap
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test.helpers.ssh_manager import SSHManager
|
||||
|
||||
|
||||
|
|
|
@ -16,7 +16,8 @@ from random import randrange
|
|||
|
||||
from proboscis.asserts import assert_equal
|
||||
|
||||
from fuelweb_test import logwrap
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test.helpers.decorators import retry
|
||||
from fuelweb_test.settings import OPENSTACK_RELEASE
|
||||
|
|
|
@ -16,9 +16,9 @@ import os
|
|||
|
||||
from OpenSSL import crypto
|
||||
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import logwrap
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test.settings import DISABLE_SSL
|
||||
from fuelweb_test.settings import PATH_TO_CERT
|
||||
from fuelweb_test.settings import PATH_TO_PEM
|
||||
|
|
|
@ -45,8 +45,9 @@ from six.moves import xrange
|
|||
# pylint: enable=redefined-builtin
|
||||
import yaml
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test import settings
|
||||
from fuelweb_test.helpers.ssh_manager import SSHManager
|
||||
from gates_tests.helpers import exceptions
|
||||
|
|
|
@ -28,6 +28,8 @@ from proboscis.asserts import assert_true
|
|||
import six
|
||||
|
||||
from core.models.collector_client import CollectorClient
|
||||
from core.helpers.log_helpers import logwrap
|
||||
from core.helpers.log_helpers import QuietLogger
|
||||
|
||||
from fuelweb_test.helpers.decorators import revert_info
|
||||
from fuelweb_test.helpers.decorators import update_rpm_packages
|
||||
|
@ -48,8 +50,6 @@ from fuelweb_test.models.fuel_web_client import FuelWebClient
|
|||
from fuelweb_test import settings
|
||||
from fuelweb_test.settings import CUSTOM_FUEL_SETTING_YAML
|
||||
from fuelweb_test.settings import iface_alias
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test import QuietLogger
|
||||
from fuelweb_test import logger
|
||||
|
||||
|
||||
|
|
|
@ -42,10 +42,11 @@ from proboscis.asserts import assert_raises
|
|||
from proboscis.asserts import assert_true
|
||||
import yaml
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
from core.helpers.log_helpers import QuietLogger
|
||||
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test import ostf_test_mapping
|
||||
from fuelweb_test import QuietLogger
|
||||
from fuelweb_test.helpers import ceph
|
||||
from fuelweb_test.helpers import checkers
|
||||
from fuelweb_test.helpers import replace_repos
|
||||
|
|
|
@ -17,7 +17,8 @@ from warnings import warn
|
|||
from keystoneauth1.identity import V2Password
|
||||
from keystoneauth1.session import Session as KeystoneSession
|
||||
|
||||
from fuelweb_test import logwrap
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test.settings import FORCE_HTTPS_MASTER_NODE
|
||||
from fuelweb_test.settings import KEYSTONE_CREDS
|
||||
|
|
|
@ -24,12 +24,13 @@ from devops.helpers.helpers import wait
|
|||
from six.moves import urllib
|
||||
# pylint: enable=import-error
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test.helpers.checkers import fail_deploy
|
||||
from fuelweb_test.helpers.checkers import incomplete_deploy
|
||||
from fuelweb_test.helpers.checkers import incomplete_tasks
|
||||
from fuelweb_test.helpers.ssl_helpers import change_cluster_ssl_config
|
||||
from fuelweb_test.tests.base_test_case import TestBasic
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test.helpers.utils import hiera_json_out
|
||||
from fuelweb_test.settings import iface_alias
|
||||
|
|
|
@ -18,11 +18,12 @@ from keystoneauth1.exceptions import BadRequest
|
|||
from proboscis import asserts
|
||||
from proboscis import test
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test.helpers.decorators import log_snapshot_after_test
|
||||
from fuelweb_test.helpers import os_actions
|
||||
from fuelweb_test.helpers import utils
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test import settings
|
||||
from fuelweb_test.tests.base_test_case import SetupEnvironment
|
||||
from fuelweb_test.tests.base_test_case import TestBasic
|
||||
|
|
|
@ -18,8 +18,9 @@ from proboscis.asserts import assert_equal
|
|||
from proboscis.asserts import assert_true
|
||||
from proboscis.asserts import fail
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test.helpers.utils import get_ip_listen_stats
|
||||
from fuelweb_test.tests.base_test_case import TestBasic
|
||||
|
||||
|
|
|
@ -17,6 +17,8 @@ from proboscis.asserts import assert_equal
|
|||
from proboscis.asserts import assert_true
|
||||
from proboscis import test
|
||||
|
||||
from core.helpers.log_helpers import QuietLogger
|
||||
|
||||
from fuelweb_test.helpers.common import Common
|
||||
from fuelweb_test.helpers import checkers
|
||||
from fuelweb_test.helpers import os_actions
|
||||
|
@ -27,7 +29,6 @@ from fuelweb_test.tests.base_test_case import SetupEnvironment
|
|||
from fuelweb_test.tests.base_test_case import TestBasic
|
||||
from fuelweb_test.tests.test_neutron_tun_base import NeutronTunHaBase
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import QuietLogger
|
||||
|
||||
|
||||
@test(groups=["ha_neutron_tun", "neutron", "smoke_neutron", "deployment"])
|
||||
|
|
|
@ -30,8 +30,9 @@ from six.moves import xrange
|
|||
# pylint: enable=redefined-builtin
|
||||
import yaml
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test.helpers import os_actions
|
||||
from fuelweb_test.helpers.checkers import check_mysql
|
||||
from fuelweb_test.helpers.checkers import check_ping
|
||||
|
|
|
@ -19,10 +19,11 @@ from proboscis.asserts import assert_equal
|
|||
from proboscis.asserts import assert_true
|
||||
from proboscis import SkipTest
|
||||
|
||||
from core.helpers.log_helpers import logwrap
|
||||
|
||||
from fuelweb_test.helpers.decorators import retry
|
||||
from fuelweb_test.helpers import os_actions
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test import settings
|
||||
from fuelweb_test.tests import base_test_case
|
||||
|
||||
|
|
Loading…
Reference in New Issue