Supress error logging for passed negative unit tests

Change-Id: Ifec457bef42a4276ae0bd9f2b38ffc5712b3b3b7
This commit is contained in:
Feodor Tersin 2015-03-07 17:34:21 +03:00
parent 0cd182d402
commit 6058f48d47
14 changed files with 185 additions and 40 deletions

View File

@ -89,6 +89,7 @@ class AddressTestCase(base.ApiTestCase):
self.assert_execution_error('AddressLimitExceeded', 'AllocateAddress',
{})
@tools.screen_unexpected_exception_logs
def test_allocate_address_vpc_rollback(self):
address.address_engine = (
address.AddressEngineNeutron())
@ -327,6 +328,7 @@ class AddressTestCase(base.ApiTestCase):
{'AllocationId': fakes.ID_EC2_ADDRESS_1,
'InstanceId': fakes.ID_EC2_INSTANCE_1})
@tools.screen_unexpected_exception_logs
def test_associate_address_vpc_rollback(self):
address.address_engine = (
address.AddressEngineNeutron())
@ -436,6 +438,7 @@ class AddressTestCase(base.ApiTestCase):
do_check({'AssociationId': fakes.ID_EC2_ASSOCIATION_2},
'InvalidAssociationID.NotFound')
@tools.screen_unexpected_exception_logs
def test_dissassociate_address_vpc_rollback(self):
address.address_engine = (
address.AddressEngineNeutron())
@ -528,6 +531,7 @@ class AddressTestCase(base.ApiTestCase):
do_check({'AllocationId': fakes.ID_EC2_ADDRESS_2},
'InvalidIPAddress.InUse')
@tools.screen_unexpected_exception_logs
def test_release_address_vpc_rollback(self):
address.address_engine = (
address.AddressEngineNeutron())

View File

@ -28,6 +28,7 @@ from ec2api.api import apirequest
from ec2api import exception
from ec2api.tests.unit import fakes_request_response as fakes
from ec2api.tests.unit import matchers
from ec2api.tests.unit import tools
from ec2api import wsgi
@ -68,6 +69,7 @@ class ApiInitTestCase(test_base.BaseTestCase):
param='fake_param')
def test_execute_error(self):
@tools.screen_all_logs
def do_check(ex, status, code, message):
self.controller.reset_mock()
self.controller.fake_action.side_effect = ex

View File

@ -141,6 +141,7 @@ class DhcpOptionsTestCase(base.ApiTestCase):
check('default', None, {'extra_dhcp_opts': []})
@tools.screen_unexpected_exception_logs
def test_associate_dhcp_options_rollback(self):
vpc = tools.update_dict(
fakes.DB_VPC_1,

View File

@ -22,6 +22,7 @@ import testtools
from ec2api.api import common
from ec2api.api import ec2utils
from ec2api import exception
from ec2api.tests.unit import tools
class EC2ValidationTestCase(testtools.TestCase):
@ -166,6 +167,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
expired = ec2utils.is_ec2_timestamp_expired(params)
self.assertFalse(expired)
@tools.screen_all_logs
def test_validate_ec2_timestamp_old_format(self):
params = {'Timestamp': '2011-04-22T11:29:49'}
expired = ec2utils.is_ec2_timestamp_expired(params)
@ -188,6 +190,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
result = ec2utils._ms_time_regex.match('2011-04-22T11:29:49Z')
self.assertIsNone(result)
@tools.screen_all_logs
def test_validate_ec2_timestamp_aws_sdk_format(self):
params = {'Timestamp': '2011-04-22T11:29:49.123Z'}
expired = ec2utils.is_ec2_timestamp_expired(params)
@ -195,6 +198,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
expired = ec2utils.is_ec2_timestamp_expired(params, expires=300)
self.assertTrue(expired)
@tools.screen_all_logs
def test_validate_ec2_timestamp_invalid_format(self):
params = {'Timestamp': '2011-04-22T11:29:49.000P'}
expired = ec2utils.is_ec2_timestamp_expired(params)
@ -205,14 +209,15 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
# EC2 request with Timestamp in advanced time
timestamp = timeutils.utcnow() + datetime.timedelta(seconds=250)
params = {'Timestamp': timeutils.strtime(timestamp,
"%Y-%m-%dT%H:%M:%SZ")}
"%Y-%m-%dT%H:%M:%SZ")}
expired = ec2utils.is_ec2_timestamp_expired(params, expires=300)
self.assertFalse(expired)
@tools.screen_all_logs
def test_validate_ec2_timestamp_advanced_time_expired(self):
timestamp = timeutils.utcnow() + datetime.timedelta(seconds=350)
params = {'Timestamp': timeutils.strtime(timestamp,
"%Y-%m-%dT%H:%M:%SZ")}
"%Y-%m-%dT%H:%M:%SZ")}
expired = ec2utils.is_ec2_timestamp_expired(params, expires=300)
self.assertTrue(expired)
@ -221,11 +226,13 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
expired = ec2utils.is_ec2_timestamp_expired(params, expires=15)
self.assertFalse(expired)
@tools.screen_all_logs
def test_validate_ec2_req_timestamp_expired(self):
params = {'Timestamp': '2011-04-22T12:00:00Z'}
compare = ec2utils.is_ec2_timestamp_expired(params, expires=300)
self.assertTrue(compare)
@tools.screen_all_logs
def test_validate_ec2_req_expired(self):
params = {'Expires': timeutils.isotime()}
expired = ec2utils.is_ec2_timestamp_expired(params)
@ -237,6 +244,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
expired = ec2utils.is_ec2_timestamp_expired(params)
self.assertFalse(expired)
@tools.screen_all_logs
def test_validate_Expires_timestamp_invalid_format(self):
# EC2 request with invalid Expires
@ -244,6 +252,7 @@ class EC2TimestampValidationTestCase(testtools.TestCase):
expired = ec2utils.is_ec2_timestamp_expired(params)
self.assertTrue(expired)
@tools.screen_all_logs
def test_validate_ec2_req_timestamp_Expires(self):
# EC2 request with both Timestamp and Expires

View File

@ -469,6 +469,7 @@ class InstanceTestCase(base.ApiTestCase):
image={'id': fakes.ID_OS_IMAGE_1}))
self.novadb.instance_get_by_uuid.side_effect = Exception()
@tools.screen_unexpected_exception_logs
def do_check(params, new_port=True, delete_on_termination=None):
mock_manager = mock.MagicMock()
mock_manager.attach_mock(self.network_interface_api,
@ -581,14 +582,16 @@ class InstanceTestCase(base.ApiTestCase):
self.nova.servers.reset_mock()
self.db_api.reset_mock()
do_check(instance_api.InstanceEngineNeutron())
(self.network_interface_api._detach_network_interface_item.
assert_called_once_with(mock.ANY, network_interfaces[2]))
(self.network_interface_api.delete_network_interface.
assert_called_once_with(
mock.ANY, network_interface_id=network_interfaces[2]['id']))
with tools.ScreeningLogger(log_name='ec2api.api'):
do_check(instance_api.InstanceEngineNeutron())
(self.network_interface_api._detach_network_interface_item.
assert_called_once_with(mock.ANY, network_interfaces[2]))
(self.network_interface_api.delete_network_interface.
assert_called_once_with(
mock.ANY, network_interface_id=network_interfaces[2]['id']))
do_check(instance_api.InstanceEngineNova())
with tools.ScreeningLogger(log_name='ec2api.api'):
do_check(instance_api.InstanceEngineNova())
def test_run_instances_invalid_parameters(self):
self.assert_execution_error('InvalidParameterValue', 'RunInstances',

View File

@ -19,6 +19,7 @@ from neutronclient.common import exceptions as neutron_exception
from ec2api.tests.unit import base
from ec2api.tests.unit import fakes
from ec2api.tests.unit import matchers
from ec2api.tests.unit import tools
class IgwTestCase(base.ApiTestCase):
@ -93,6 +94,7 @@ class IgwTestCase(base.ApiTestCase):
fakes.gen_db_igw(fakes.ID_EC2_IGW_1, fakes.ID_EC2_VPC_2))
do_check('InvalidParameterValue')
@tools.screen_unexpected_exception_logs
def test_attach_igw_rollback(self):
self.configure(external_network=fakes.NAME_OS_PUBLIC_NETWORK)
self.set_mock_db_items(fakes.DB_IGW_1, fakes.DB_IGW_2, fakes.DB_VPC_2)
@ -162,6 +164,7 @@ class IgwTestCase(base.ApiTestCase):
self.neutron.remove_gateway_router.assert_called_once_with(
fakes.ID_OS_ROUTER_1)
@tools.screen_unexpected_exception_logs
def test_detach_igw_rollback(self):
self.set_mock_db_items(fakes.DB_IGW_1, fakes.DB_VPC_1)
self.neutron.remove_gateway_router.side_effect = Exception()

View File

@ -25,6 +25,7 @@ import webob.exc
from ec2api import api as ec2
from ec2api import context
from ec2api import exception
from ec2api.tests.unit import tools
from ec2api import wsgi
CONF = cfg.CONF
@ -149,6 +150,7 @@ class KeystoneAuthTestCase(test_base.BaseTestCase):
CONF.keystone_url + '/ec2tokens',
data=mock.ANY, headers=mock.ANY)
@tools.screen_all_logs
@mock.patch.object(requests, 'request', return_value=FakeResponse(200))
def test_no_result_data(self, mock_request):
req = wsgi.Request.blank('/test')

View File

@ -236,6 +236,7 @@ class NetworkInterfaceTestCase(base.ApiTestCase):
'PrivateIpAddress': fakes.IP_NETWORK_INTERFACE_1},
'InvalidParameterValue')
@tools.screen_unexpected_exception_logs
@mock.patch('ec2api.api.dhcp_options._add_dhcp_opts_to_port')
def test_create_network_interface_rollback(self, _add_dhcp_opts_to_port):
self.set_mock_db_items(
@ -321,6 +322,7 @@ class NetworkInterfaceTestCase(base.ApiTestCase):
['network_interface_id',
'private_ip_address']))
@tools.screen_unexpected_exception_logs
def test_delete_network_interface_rollback(self):
self.set_mock_db_items(fakes.DB_NETWORK_INTERFACE_1)
self.neutron.delete_port.side_effect = Exception()
@ -467,6 +469,7 @@ class NetworkInterfaceTestCase(base.ApiTestCase):
'InstanceId': fakes.ID_EC2_INSTANCE_1,
'DeviceIndex': '0'})
@tools.screen_unexpected_exception_logs
def test_attach_network_interface_rollback(self):
self.set_mock_db_items(fakes.DB_NETWORK_INTERFACE_1,
fakes.DB_INSTANCE_1)
@ -522,6 +525,7 @@ class NetworkInterfaceTestCase(base.ApiTestCase):
{'AttachmentId': ec2utils.change_ec2_id_kind(
fakes.ID_EC2_NETWORK_INTERFACE_2, 'eni-attach')})
@tools.screen_unexpected_exception_logs
def test_detach_network_interface_rollback(self):
network_interface = tools.update_dict(fakes.DB_NETWORK_INTERFACE_2,
{'device_index': 1})

View File

@ -64,8 +64,7 @@ class RouteTableTestCase(base.ApiTestCase):
self.assertEqual(True, resp['return'])
self.db_api.update_item.assert_called_once_with(
mock.ANY,
route_table)
mock.ANY, route_table)
routes_updater.assert_called_once_with(
mock.ANY, route_table, mock.ANY,
rollback_route_table_state)
@ -209,23 +208,25 @@ class RouteTableTestCase(base.ApiTestCase):
fakes.gen_db_igw(fakes.ID_EC2_IGW_2, fakes.ID_EC2_VPC_1))
routes_updater.side_effect = Exception()
self.assert_execution_error(
self.ANY_EXECUTE_ERROR, 'CreateRoute',
{'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1,
'DestinationCidrBlock': '0.0.0.0/0',
'GatewayId': fakes.ID_EC2_IGW_1})
with tools.ScreeningLogger(log_name='ec2api.api'):
self.assert_execution_error(
self.ANY_EXECUTE_ERROR, 'CreateRoute',
{'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_1,
'DestinationCidrBlock': '0.0.0.0/0',
'GatewayId': fakes.ID_EC2_IGW_1})
self.db_api.update_item.assert_any_call(mock.ANY,
fakes.DB_ROUTE_TABLE_1)
self.db_api.update_item.assert_any_call(mock.ANY,
fakes.DB_ROUTE_TABLE_1)
self.assert_execution_error(
self.ANY_EXECUTE_ERROR, 'ReplaceRoute',
{'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2,
'DestinationCidrBlock': '0.0.0.0/0',
'GatewayId': fakes.ID_EC2_IGW_2})
with tools.ScreeningLogger(log_name='ec2api.api'):
self.assert_execution_error(
self.ANY_EXECUTE_ERROR, 'ReplaceRoute',
{'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2,
'DestinationCidrBlock': '0.0.0.0/0',
'GatewayId': fakes.ID_EC2_IGW_2})
self.db_api.update_item.assert_any_call(mock.ANY,
fakes.DB_ROUTE_TABLE_2)
self.db_api.update_item.assert_any_call(mock.ANY,
fakes.DB_ROUTE_TABLE_2)
@mock.patch('ec2api.api.route_table._update_routes_in_associated_subnets')
def test_replace_route(self, routes_updater):
@ -296,6 +297,7 @@ class RouteTableTestCase(base.ApiTestCase):
{'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2,
'DestinationCidrBlock': fakes.CIDR_VPC_1})
@tools.screen_unexpected_exception_logs
@mock.patch('ec2api.api.route_table._update_routes_in_associated_subnets')
def test_delete_route_rollback(self, routes_updater):
self.set_mock_db_items(fakes.DB_ROUTE_TABLE_2)
@ -359,6 +361,7 @@ class RouteTableTestCase(base.ApiTestCase):
'SubnetId': fakes.ID_EC2_SUBNET_2},
'Resource.AlreadyAssociated')
@tools.screen_unexpected_exception_logs
@mock.patch('ec2api.api.route_table._update_subnet_host_routes')
def test_associate_route_table_rollback(self, routes_updater):
self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1,
@ -462,23 +465,26 @@ class RouteTableTestCase(base.ApiTestCase):
fakes.DB_VPC_1)
multiply_routes_updater.side_effect = Exception()
self.assert_execution_error(
self.ANY_EXECUTE_ERROR, 'ReplaceRouteTableAssociation',
{'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_1,
'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2})
with tools.ScreeningLogger(log_name='ec2api.api'):
self.assert_execution_error(
self.ANY_EXECUTE_ERROR, 'ReplaceRouteTableAssociation',
{'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_1,
'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2})
self.db_api.update_item.assert_any_call(
mock.ANY, fakes.DB_VPC_1)
self.db_api.update_item.assert_any_call(
mock.ANY, fakes.DB_VPC_1)
self.db_api.reset_mock()
routes_updater.side_effect = Exception()
self.assert_execution_error(
self.ANY_EXECUTE_ERROR, 'ReplaceRouteTableAssociation',
{'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_3,
'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2})
with tools.ScreeningLogger(log_name='ec2api.api'):
self.assert_execution_error(
self.ANY_EXECUTE_ERROR, 'ReplaceRouteTableAssociation',
{'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_3,
'RouteTableId': fakes.ID_EC2_ROUTE_TABLE_2})
self.db_api.update_item.assert_any_call(
mock.ANY, fakes.DB_SUBNET_2)
self.db_api.update_item.assert_any_call(
mock.ANY, fakes.DB_SUBNET_2)
@mock.patch('ec2api.api.route_table._update_subnet_host_routes')
def test_disassociate_route_table(self, routes_updater):
@ -514,6 +520,7 @@ class RouteTableTestCase(base.ApiTestCase):
do_check({'AssociationId': fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_1},
'InvalidParameterValue')
@tools.screen_unexpected_exception_logs
@mock.patch('ec2api.api.route_table._update_subnet_host_routes')
def test_disassociate_route_table_rollback(self, routes_updater):
self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1, fakes.DB_ROUTE_TABLE_3,

View File

@ -117,6 +117,7 @@ class SecurityGroupTestCase(base.ApiTestCase):
self.nova.security_groups.create.assert_called_once_with(
'groupname', 'Group description')
@tools.screen_unexpected_exception_logs
def test_create_security_group_rollback(self):
security_group.security_group_engine = (
security_group.SecurityGroupEngineNova())

View File

@ -134,6 +134,7 @@ class SubnetTestCase(base.ApiTestCase):
test_overlimit(self.neutron.create_network)
test_overlimit(self.neutron.create_subnet)
@tools.screen_unexpected_exception_logs
def test_create_subnet_rollback(self):
self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)
self.db_api.add_item.side_effect = (
@ -220,6 +221,7 @@ class SubnetTestCase(base.ApiTestCase):
self.assert_execution_error('DependencyViolation', 'DeleteSubnet',
{'SubnetId': fakes.ID_EC2_SUBNET_1})
@tools.screen_unexpected_exception_logs
def test_delete_subnet_rollback(self):
self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_SUBNET_1)
self.neutron.show_subnet.side_effect = Exception()

View File

@ -12,7 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import fixtures
from oslo_log import log as logging
from oslotest import base as test_base
import testtools
from ec2api import exception
@ -50,5 +52,41 @@ class TestToolsTestCase(testtools.TestCase):
class TestBaseTestCase(base.ApiTestCase):
def test_validate_exception_format_is_enabled_for_tests(self):
self.assertRaises(KeyError, exception.InvalidVpcRange, fake='value')
self.assertRaises(TypeError, exception.InvalidID, {'id': 'value'})
with tools.ScreeningLogger():
self.assertRaises(KeyError, exception.InvalidVpcRange,
fake='value')
with tools.ScreeningLogger():
self.assertRaises(TypeError, exception.InvalidID, {'id': 'value'})
class LoggingTestCase(test_base.BaseTestCase):
def test_hide_logs(self):
with fixtures.FakeLogger() as logger:
with tools.ScreeningLogger():
LOG = logging.getLogger('ec2api.api')
LOG.critical('critical message')
LOG.error('error message')
LOG.warning('warning message')
self.assertEqual(0, len(logger.output))
def test_screen_logs(self):
with fixtures.FakeLogger() as logger:
with tools.ScreeningLogger(log_name='ec2api.api'):
LOG1 = logging.getLogger('ec2api.api')
LOG1.error('error message')
LOG2 = logging.getLogger('ec2api.api.vpc')
LOG2.warning('warning message')
self.assertIn('warning message', logger.output)
self.assertNotIn('error message', logger.output)
def test_show_logs_on_unhandled_exception(self):
with fixtures.FakeLogger() as logger:
try:
with tools.ScreeningLogger():
LOG = logging.getLogger('ec2api.api')
LOG.error('error message')
raise Exception()
except Exception:
pass
self.assertIn('error message', logger.output)

View File

@ -94,6 +94,7 @@ class VpcTestCase(base.ApiTestCase):
self.neutron.create_router.assert_called_with({'router': {}})
self.assertEqual(0, self.db_api.add_item.call_count)
@tools.screen_unexpected_exception_logs
def test_create_vpc_rollback(self):
self.neutron.create_router.side_effect = (
tools.get_neutron_create('router', fakes.ID_OS_ROUTER_1))
@ -177,6 +178,7 @@ class VpcTestCase(base.ApiTestCase):
resp = self.execute('DeleteVpc', {'VpcId': fakes.ID_EC2_VPC_1})
check_response(resp)
@tools.screen_unexpected_exception_logs
def test_delete_vpc_rollback(self):
self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)
self.neutron.delete_router.side_effect = Exception()

View File

@ -14,8 +14,10 @@
import copy
import logging
import re
import fixtures
from lxml import etree
import mock
@ -165,3 +167,68 @@ def parse_xml(xml_string):
return node.tag, val
return dict([convert_node(xml)])
class KeepingHandler(logging.Handler):
def __init__(self):
super(KeepingHandler, self).__init__()
self._storage = []
def emit(self, record):
self._storage.append(record)
def emit_records_to(self, handlers, record_filter=None):
for record in self._storage:
if not record_filter or record_filter.filter(record):
for handler in handlers:
if self != handler:
handler.emit(record)
class ScreeningFilter(logging.Filter):
def __init__(self, name=None):
self._name = name
def filter(self, record):
if self._name is not None and record.name == self._name:
return False
return True
class ScreeningLogger(fixtures.Fixture):
def __init__(self, log_name=None):
super(ScreeningLogger, self).__init__()
self.handler = KeepingHandler()
if log_name:
self._filter = ScreeningFilter(name=log_name)
else:
self._filter = None
def setUp(self):
super(ScreeningLogger, self).setUp()
self.useFixture(fixtures.LogHandler(self.handler))
def __exit__(self, exc_type, exc_val, exc_tb):
res = super(ScreeningLogger, self).__exit__(exc_type, exc_val, exc_tb)
handlers = logging.getLogger().handlers
if exc_type:
self.handler.emit_records_to(handlers)
elif self._filter:
self.handler.emit_records_to(handlers, self._filter)
return res
def screen_logs(log_name=None):
def decorator(func):
def wrapper(*args, **kwargs):
with ScreeningLogger(log_name):
return func(*args, **kwargs)
return wrapper
return decorator
screen_unexpected_exception_logs = screen_logs('ec2api.api')
screen_all_logs = screen_logs()