From 9b809a11b3068552274340606eb76d2217411b85 Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson Date: Fri, 8 Mar 2019 20:38:12 -0800 Subject: [PATCH] Replace RCPDispatcher with decorator Overriding the RCPDispatcher has been deprecated for a long time and generates a warning when starting the service. Ideally we would use the inbuilt oslo.messaging decorator instead of our own decorator, but due to complexities with the current Central implementation this at least gets us functional rpc unit-tests, that will make future refactors easier. The decorator introduced in this commit will only catch exceptions from the first call. Any additional calls happening as a result of the first call will not be caught and converted until they have bubbled back up to the original call. This is to prevent internal calls that expect the original exception from failing. * Added expected_exceptions decorator. * Renamed Base -> DesignateException. * Removed RPCDispatcher override. Change-Id: Id6a83b286bc20a1dc078ca77af0e9c97686fd74d --- designate/api/middleware.py | 2 +- .../backend/agent_backend/impl_denominator.py | 2 +- designate/central/service.py | 92 ++- designate/exceptions.py | 50 +- designate/rpc.py | 49 +- designate/tests/test_central/test_service.py | 420 +++++++----- designate/tests/test_storage/__init__.py | 52 +- .../tests/unit/test_central/test_basic.py | 628 +++++++++++------- designate/worker/service.py | 6 + 9 files changed, 843 insertions(+), 458 deletions(-) diff --git a/designate/api/middleware.py b/designate/api/middleware.py index 2325ec951..1e5c80cdb 100644 --- a/designate/api/middleware.py +++ b/designate/api/middleware.py @@ -233,7 +233,7 @@ class FaultWrapperMiddleware(base.Middleware): def __call__(self, request): try: return request.get_response(self.application) - except exceptions.Base as e: + except exceptions.DesignateException as e: # Handle Designate Exceptions status = e.error_code if hasattr(e, 'error_code') else 500 diff --git a/designate/backend/agent_backend/impl_denominator.py b/designate/backend/agent_backend/impl_denominator.py index 056614d3d..e6bee4801 100644 --- a/designate/backend/agent_backend/impl_denominator.py +++ b/designate/backend/agent_backend/impl_denominator.py @@ -99,7 +99,7 @@ class Denominator(object): return stdout except utils.processutils.ProcessExecutionError as e: LOG.debug('Denominator call failure: %s' % e) - raise exceptions.Base(e) + raise exceptions.DesignateException(e) class DenominatorBackend(base.AgentBackend): diff --git a/designate/central/service.py b/designate/central/service.py index f6f734d47..9c65bf09e 100644 --- a/designate/central/service.py +++ b/designate/central/service.py @@ -43,10 +43,11 @@ from designate import notifications from designate import objects from designate import policy from designate import quota +from designate import rpc from designate import service from designate import scheduler -from designate import utils from designate import storage +from designate import utils from designate.mdns import rpcapi as mdns_rpcapi from designate.pool_manager import rpcapi as pool_manager_rpcapi from designate.storage import transaction @@ -654,11 +655,13 @@ class Service(service.RPCService, service.Service): recordset_records=recordset_records) # Misc Methods + @rpc.expected_exceptions() def get_absolute_limits(self, context): # NOTE(Kiall): Currently, we only have quota based limits.. return self.quota.get_quotas(context, context.tenant) # Quota Methods + @rpc.expected_exceptions() def get_quotas(self, context, tenant_id): target = {'tenant_id': tenant_id} policy.check('get_quotas', context, target) @@ -668,12 +671,14 @@ class Service(service.RPCService, service.Service): return self.quota.get_quotas(context, tenant_id) + @rpc.expected_exceptions() def get_quota(self, context, tenant_id, resource): target = {'tenant_id': tenant_id, 'resource': resource} policy.check('get_quota', context, target) return self.quota.get_quota(context, tenant_id, resource) + @rpc.expected_exceptions() @transaction def set_quota(self, context, tenant_id, resource, hard_limit): target = { @@ -696,6 +701,7 @@ class Service(service.RPCService, service.Service): self.quota.reset_quotas(context, tenant_id) # TLD Methods + @rpc.expected_exceptions() @notification('dns.tld.create') @transaction def create_tld(self, context, tld): @@ -706,6 +712,7 @@ class Service(service.RPCService, service.Service): return created_tld + @rpc.expected_exceptions() def find_tlds(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): policy.check('find_tlds', context) @@ -713,11 +720,13 @@ class Service(service.RPCService, service.Service): return self.storage.find_tlds(context, criterion, marker, limit, sort_key, sort_dir) + @rpc.expected_exceptions() def get_tld(self, context, tld_id): policy.check('get_tld', context, {'tld_id': tld_id}) return self.storage.get_tld(context, tld_id) + @rpc.expected_exceptions() @notification('dns.tld.update') @transaction def update_tld(self, context, tld): @@ -730,6 +739,7 @@ class Service(service.RPCService, service.Service): return tld + @rpc.expected_exceptions() @notification('dns.tld.delete') @transaction def delete_tld(self, context, tld_id): @@ -740,6 +750,7 @@ class Service(service.RPCService, service.Service): return tld # TSIG Key Methods + @rpc.expected_exceptions() @notification('dns.tsigkey.create') @transaction def create_tsigkey(self, context, tsigkey): @@ -751,6 +762,7 @@ class Service(service.RPCService, service.Service): return created_tsigkey + @rpc.expected_exceptions() def find_tsigkeys(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): policy.check('find_tsigkeys', context) @@ -758,11 +770,13 @@ class Service(service.RPCService, service.Service): return self.storage.find_tsigkeys(context, criterion, marker, limit, sort_key, sort_dir) + @rpc.expected_exceptions() def get_tsigkey(self, context, tsigkey_id): policy.check('get_tsigkey', context, {'tsigkey_id': tsigkey_id}) return self.storage.get_tsigkey(context, tsigkey_id) + @rpc.expected_exceptions() @notification('dns.tsigkey.update') @transaction def update_tsigkey(self, context, tsigkey): @@ -777,6 +791,7 @@ class Service(service.RPCService, service.Service): return tsigkey + @rpc.expected_exceptions() @notification('dns.tsigkey.delete') @transaction def delete_tsigkey(self, context, tsigkey_id): @@ -789,10 +804,12 @@ class Service(service.RPCService, service.Service): return tsigkey # Tenant Methods + @rpc.expected_exceptions() def find_tenants(self, context): policy.check('find_tenants', context) return self.storage.find_tenants(context) + @rpc.expected_exceptions() def get_tenant(self, context, tenant_id): target = { 'tenant_id': tenant_id @@ -802,6 +819,7 @@ class Service(service.RPCService, service.Service): return self.storage.get_tenant(context, tenant_id) + @rpc.expected_exceptions() def count_tenants(self, context): policy.check('count_tenants', context) return self.storage.count_tenants(context) @@ -831,6 +849,7 @@ class Service(service.RPCService, service.Service): pool = self.storage.get_pool(elevated_context, pool_id) return pool.ns_records + @rpc.expected_exceptions() @notification('dns.domain.create') @notification('dns.zone.create') @synchronized_zone(new_zone=True) @@ -949,6 +968,7 @@ class Service(service.RPCService, service.Service): return zone + @rpc.expected_exceptions() def get_zone(self, context, zone_id): """Get a zone, even if flagged for deletion """ @@ -963,6 +983,7 @@ class Service(service.RPCService, service.Service): return zone + @rpc.expected_exceptions() def get_zone_ns_records(self, context, zone_id=None, criterion=None): if zone_id is None: @@ -987,6 +1008,7 @@ class Service(service.RPCService, service.Service): return pool.ns_records + @rpc.expected_exceptions() def find_zones(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): """List existing zones including the ones flagged for deletion. @@ -997,12 +1019,14 @@ class Service(service.RPCService, service.Service): return self.storage.find_zones(context, criterion, marker, limit, sort_key, sort_dir) + @rpc.expected_exceptions() def find_zone(self, context, criterion=None): target = {'tenant_id': context.tenant} policy.check('find_zone', context, target) return self.storage.find_zone(context, criterion) + @rpc.expected_exceptions() @notification('dns.domain.update') @notification('dns.zone.update') @synchronized_zone() @@ -1068,6 +1092,7 @@ class Service(service.RPCService, service.Service): return zone + @rpc.expected_exceptions() @notification('dns.domain.delete') @notification('dns.zone.delete') @synchronized_zone() @@ -1122,6 +1147,7 @@ class Service(service.RPCService, service.Service): return zone + @rpc.expected_exceptions() def purge_zones(self, context, criterion, limit=None): """Purge deleted zones. :returns: number of purged zones @@ -1134,6 +1160,7 @@ class Service(service.RPCService, service.Service): return self.storage.purge_zones(context, criterion, limit) + @rpc.expected_exceptions() def xfr_zone(self, context, zone_id): zone = self.storage.get_zone(context, zone_id) @@ -1162,6 +1189,7 @@ class Service(service.RPCService, service.Service): {"srv_serial": serial, "serial": zone.serial}) self.mdns_api.perform_zone_xfr(context, zone) + @rpc.expected_exceptions() def count_zones(self, context, criterion=None): if criterion is None: criterion = {} @@ -1175,6 +1203,7 @@ class Service(service.RPCService, service.Service): return self.storage.count_zones(context, criterion) # Report combining all the count reports based on criterion + @rpc.expected_exceptions() def count_report(self, context, criterion=None): reports = [] @@ -1203,6 +1232,7 @@ class Service(service.RPCService, service.Service): return reports + @rpc.expected_exceptions() @notification('dns.zone.touch') @synchronized_zone() def touch_zone(self, context, zone_id): @@ -1230,6 +1260,7 @@ class Service(service.RPCService, service.Service): return zone # RecordSet Methods + @rpc.expected_exceptions() @notification('dns.recordset.create') @synchronized_zone() def create_recordset(self, context, zone_id, recordset, @@ -1319,6 +1350,7 @@ class Service(service.RPCService, service.Service): # Return the zone too in case it was updated return (recordset, zone) + @rpc.expected_exceptions() def get_recordset(self, context, zone_id, recordset_id): recordset = self.storage.get_recordset(context, recordset_id) @@ -1345,6 +1377,7 @@ class Service(service.RPCService, service.Service): return recordset + @rpc.expected_exceptions() def find_recordsets(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None, force_index=False): target = {'tenant_id': context.tenant} @@ -1356,6 +1389,7 @@ class Service(service.RPCService, service.Service): return recordsets + @rpc.expected_exceptions() def find_recordset(self, context, criterion=None): target = {'tenant_id': context.tenant} policy.check('find_recordset', context, target) @@ -1364,6 +1398,7 @@ class Service(service.RPCService, service.Service): return recordset + @rpc.expected_exceptions() def export_zone(self, context, zone_id): zone = self.get_zone(context, zone_id) @@ -1374,6 +1409,7 @@ class Service(service.RPCService, service.Service): zone=zone, recordsets=recordsets) + @rpc.expected_exceptions() @notification('dns.recordset.update') @synchronized_zone() def update_recordset(self, context, recordset, increment_serial=True): @@ -1447,6 +1483,7 @@ class Service(service.RPCService, service.Service): return (recordset, zone) + @rpc.expected_exceptions() @notification('dns.recordset.delete') @synchronized_zone() def delete_recordset(self, context, zone_id, recordset_id, @@ -1506,6 +1543,7 @@ class Service(service.RPCService, service.Service): return (recordset, zone) + @rpc.expected_exceptions() def count_recordsets(self, context, criterion=None): if criterion is None: criterion = {} @@ -1519,6 +1557,7 @@ class Service(service.RPCService, service.Service): return self.storage.count_recordsets(context, criterion) # Record Methods + @rpc.expected_exceptions() @notification('dns.record.create') @synchronized_zone() def create_record(self, context, zone_id, recordset_id, record, @@ -1571,6 +1610,7 @@ class Service(service.RPCService, service.Service): return (record, zone) + @rpc.expected_exceptions() def get_record(self, context, zone_id, recordset_id, record_id): zone = self.storage.get_zone(context, zone_id) recordset = self.storage.get_recordset(context, recordset_id) @@ -1597,6 +1637,7 @@ class Service(service.RPCService, service.Service): return record + @rpc.expected_exceptions() def find_records(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): target = {'tenant_id': context.tenant} @@ -1605,12 +1646,14 @@ class Service(service.RPCService, service.Service): return self.storage.find_records(context, criterion, marker, limit, sort_key, sort_dir) + @rpc.expected_exceptions() def find_record(self, context, criterion=None): target = {'tenant_id': context.tenant} policy.check('find_record', context, target) return self.storage.find_record(context, criterion) + @rpc.expected_exceptions() @notification('dns.record.update') @synchronized_zone() def update_record(self, context, record, increment_serial=True): @@ -1679,6 +1722,7 @@ class Service(service.RPCService, service.Service): return (record, zone) + @rpc.expected_exceptions() @notification('dns.record.delete') @synchronized_zone() def delete_record(self, context, zone_id, recordset_id, record_id, @@ -1739,6 +1783,7 @@ class Service(service.RPCService, service.Service): return (record, zone) + @rpc.expected_exceptions() def count_records(self, context, criterion=None): if criterion is None: criterion = {} @@ -1754,6 +1799,7 @@ class Service(service.RPCService, service.Service): def _sync_zone(self, context, zone): return self.pool_manager_api.update_zone(context, zone) + @rpc.expected_exceptions() @transaction def sync_zones(self, context): policy.check('diagnostics_sync_zones', context) @@ -1766,6 +1812,7 @@ class Service(service.RPCService, service.Service): return results + @rpc.expected_exceptions() @transaction def sync_zone(self, context, zone_id): zone = self.storage.get_zone(context, zone_id) @@ -1780,6 +1827,7 @@ class Service(service.RPCService, service.Service): return self._sync_zone(context, zone) + @rpc.expected_exceptions() @transaction def sync_record(self, context, zone_id, recordset_id, record_id): zone = self.storage.get_zone(context, zone_id) @@ -1798,6 +1846,7 @@ class Service(service.RPCService, service.Service): self.zone_api.update_zone(context, zone) + @rpc.expected_exceptions() def ping(self, context): policy.check('diagnostics_ping', context) @@ -1956,6 +2005,7 @@ class Service(service.RPCService, service.Service): return fips[region, floatingip_id] # PTR ops + @rpc.expected_exceptions() def list_floatingips(self, context): """ List Floating IPs PTR @@ -1976,6 +2026,7 @@ class Service(service.RPCService, service.Service): return self._format_floatingips(context, valid) + @rpc.expected_exceptions() def get_floatingip(self, context, region, floatingip_id): """ Get Floating IP PTR @@ -2123,6 +2174,7 @@ class Service(service.RPCService, service.Service): recordset_id=record['recordset_id'], record_id=record['id']) + @rpc.expected_exceptions() @transaction def update_floatingip(self, context, region, floatingip_id, values): """ @@ -2137,6 +2189,7 @@ class Service(service.RPCService, service.Service): context, region, floatingip_id, values) # Blacklisted zones + @rpc.expected_exceptions() @notification('dns.blacklist.create') @transaction def create_blacklist(self, context, blacklist): @@ -2146,6 +2199,7 @@ class Service(service.RPCService, service.Service): return created_blacklist + @rpc.expected_exceptions() def get_blacklist(self, context, blacklist_id): policy.check('get_blacklist', context) @@ -2153,6 +2207,7 @@ class Service(service.RPCService, service.Service): return blacklist + @rpc.expected_exceptions() def find_blacklists(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): policy.check('find_blacklists', context) @@ -2163,6 +2218,7 @@ class Service(service.RPCService, service.Service): return blacklists + @rpc.expected_exceptions() def find_blacklist(self, context, criterion): policy.check('find_blacklist', context) @@ -2170,6 +2226,7 @@ class Service(service.RPCService, service.Service): return blacklist + @rpc.expected_exceptions() @notification('dns.blacklist.update') @transaction def update_blacklist(self, context, blacklist): @@ -2182,6 +2239,7 @@ class Service(service.RPCService, service.Service): return blacklist + @rpc.expected_exceptions() @notification('dns.blacklist.delete') @transaction def delete_blacklist(self, context, blacklist_id): @@ -2192,6 +2250,7 @@ class Service(service.RPCService, service.Service): return blacklist # Server Pools + @rpc.expected_exceptions() @notification('dns.pool.create') @transaction def create_pool(self, context, pool): @@ -2205,6 +2264,7 @@ class Service(service.RPCService, service.Service): return created_pool + @rpc.expected_exceptions() def find_pools(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): @@ -2213,18 +2273,21 @@ class Service(service.RPCService, service.Service): return self.storage.find_pools(context, criterion, marker, limit, sort_key, sort_dir) + @rpc.expected_exceptions() def find_pool(self, context, criterion=None): policy.check('find_pool', context) return self.storage.find_pool(context, criterion) + @rpc.expected_exceptions() def get_pool(self, context, pool_id): policy.check('get_pool', context) return self.storage.get_pool(context, pool_id) + @rpc.expected_exceptions() @notification('dns.pool.update') @transaction def update_pool(self, context, pool): @@ -2281,6 +2344,7 @@ class Service(service.RPCService, service.Service): return updated_pool + @rpc.expected_exceptions() @notification('dns.pool.delete') @transaction def delete_pool(self, context, pool_id): @@ -2303,6 +2367,7 @@ class Service(service.RPCService, service.Service): return pool # Pool Manager Integration + @rpc.expected_exceptions() @notification('dns.domain.update') @notification('dns.zone.update') @transaction @@ -2434,6 +2499,7 @@ class Service(service.RPCService, service.Service): sysrand = SystemRandom() return ''.join(sysrand.choice(chars) for _ in range(size)) + @rpc.expected_exceptions() @notification('dns.zone_transfer_request.create') @transaction def create_zone_transfer_request(self, context, zone_transfer_request): @@ -2461,6 +2527,7 @@ class Service(service.RPCService, service.Service): return created_zone_transfer_request + @rpc.expected_exceptions() def get_zone_transfer_request(self, context, zone_transfer_request_id): elevated_context = context.elevated(all_tenants=True) @@ -2478,6 +2545,7 @@ class Service(service.RPCService, service.Service): return zone_transfer_request + @rpc.expected_exceptions() def find_zone_transfer_requests(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): @@ -2490,6 +2558,7 @@ class Service(service.RPCService, service.Service): return requests + @rpc.expected_exceptions() def find_zone_transfer_request(self, context, criterion): target = { 'tenant_id': context.tenant, @@ -2497,6 +2566,7 @@ class Service(service.RPCService, service.Service): policy.check('find_zone_transfer_request', context, target) return self.storage.find_zone_transfer_requests(context, criterion) + @rpc.expected_exceptions() @notification('dns.zone_transfer_request.update') @transaction def update_zone_transfer_request(self, context, zone_transfer_request): @@ -2513,6 +2583,7 @@ class Service(service.RPCService, service.Service): return request + @rpc.expected_exceptions() @notification('dns.zone_transfer_request.delete') @transaction def delete_zone_transfer_request(self, context, zone_transfer_request_id): @@ -2527,6 +2598,7 @@ class Service(service.RPCService, service.Service): context, zone_transfer_request_id) + @rpc.expected_exceptions() @notification('dns.zone_transfer_accept.create') @transaction def create_zone_transfer_accept(self, context, zone_transfer_accept): @@ -2590,6 +2662,7 @@ class Service(service.RPCService, service.Service): return created_zone_transfer_accept + @rpc.expected_exceptions() def get_zone_transfer_accept(self, context, zone_transfer_accept_id): # Get zone transfer accept @@ -2603,6 +2676,7 @@ class Service(service.RPCService, service.Service): return zone_transfer_accept + @rpc.expected_exceptions() def find_zone_transfer_accepts(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): policy.check('find_zone_transfer_accepts', context) @@ -2610,10 +2684,12 @@ class Service(service.RPCService, service.Service): marker, limit, sort_key, sort_dir) + @rpc.expected_exceptions() def find_zone_transfer_accept(self, context, criterion): policy.check('find_zone_transfer_accept', context) return self.storage.find_zone_transfer_accept(context, criterion) + @rpc.expected_exceptions() @notification('dns.zone_transfer_accept.update') @transaction def update_zone_transfer_accept(self, context, zone_transfer_accept): @@ -2626,6 +2702,7 @@ class Service(service.RPCService, service.Service): return accept + @rpc.expected_exceptions() @notification('dns.zone_transfer_accept.delete') @transaction def delete_zone_transfer_accept(self, context, zone_transfer_accept_id): @@ -2642,6 +2719,7 @@ class Service(service.RPCService, service.Service): zone_transfer_accept_id) # Zone Import Methods + @rpc.expected_exceptions() @notification('dns.zone_import.create') def create_zone_import(self, context, request_body): target = {'tenant_id': context.tenant} @@ -2737,6 +2815,7 @@ class Service(service.RPCService, service.Service): self.update_zone_import(context, zone_import) + @rpc.expected_exceptions() def find_zone_imports(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): target = {'tenant_id': context.tenant} @@ -2748,11 +2827,13 @@ class Service(service.RPCService, service.Service): return self.storage.find_zone_imports(context, criterion, marker, limit, sort_key, sort_dir) + @rpc.expected_exceptions() def get_zone_import(self, context, zone_import_id): target = {'tenant_id': context.tenant} policy.check('get_zone_import', context, target) return self.storage.get_zone_import(context, zone_import_id) + @rpc.expected_exceptions() @notification('dns.zone_import.update') def update_zone_import(self, context, zone_import): target = { @@ -2762,6 +2843,7 @@ class Service(service.RPCService, service.Service): return self.storage.update_zone_import(context, zone_import) + @rpc.expected_exceptions() @notification('dns.zone_import.delete') @transaction def delete_zone_import(self, context, zone_import_id): @@ -2776,6 +2858,7 @@ class Service(service.RPCService, service.Service): return zone_import # Zone Export Methods + @rpc.expected_exceptions() @notification('dns.zone_export.create') def create_zone_export(self, context, zone_id): # Try getting the zone to ensure it exists @@ -2832,6 +2915,7 @@ class Service(service.RPCService, service.Service): return created_zone_export + @rpc.expected_exceptions() def find_zone_exports(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): target = {'tenant_id': context.tenant} @@ -2843,12 +2927,14 @@ class Service(service.RPCService, service.Service): return self.storage.find_zone_exports(context, criterion, marker, limit, sort_key, sort_dir) + @rpc.expected_exceptions() def get_zone_export(self, context, zone_export_id): target = {'tenant_id': context.tenant} policy.check('get_zone_export', context, target) return self.storage.get_zone_export(context, zone_export_id) + @rpc.expected_exceptions() @notification('dns.zone_export.update') def update_zone_export(self, context, zone_export): target = { @@ -2858,6 +2944,7 @@ class Service(service.RPCService, service.Service): return self.storage.update_zone_export(context, zone_export) + @rpc.expected_exceptions() @notification('dns.zone_export.delete') @transaction def delete_zone_export(self, context, zone_export_id): @@ -2871,6 +2958,7 @@ class Service(service.RPCService, service.Service): return zone_export + @rpc.expected_exceptions() def find_service_statuses(self, context, criterion=None, marker=None, limit=None, sort_key=None, sort_dir=None): """List service statuses. @@ -2880,11 +2968,13 @@ class Service(service.RPCService, service.Service): return self.storage.find_service_statuses( context, criterion, marker, limit, sort_key, sort_dir) + @rpc.expected_exceptions() def find_service_status(self, context, criterion=None): policy.check('find_service_status', context) return self.storage.find_service_status(context, criterion) + @rpc.expected_exceptions() def update_service_status(self, context, service_status): policy.check('update_service_status', context) diff --git a/designate/exceptions.py b/designate/exceptions.py index c5a91a19e..e5e9d7bc2 100644 --- a/designate/exceptions.py +++ b/designate/exceptions.py @@ -16,7 +16,7 @@ import six -class Base(Exception): +class DesignateException(Exception): error_code = 500 error_type = None error_message = None @@ -27,9 +27,9 @@ class Base(Exception): self.errors = kwargs.pop('errors', None) self.object = kwargs.pop('object', None) - super(Base, self).__init__(*args, **kwargs) + super(DesignateException, self).__init__(*args, **kwargs) - if len(args) > 0 and isinstance(args[0], six.string_types): + if args and isinstance(args[0], six.string_types): self.error_message = args[0] @@ -37,7 +37,7 @@ class Backend(Exception): pass -class RelationNotLoaded(Base): +class RelationNotLoaded(DesignateException): error_code = 500 error_type = 'relation_not_loaded' @@ -54,7 +54,7 @@ class RelationNotLoaded(Base): return self.error_message -class AdapterNotFound(Base): +class AdapterNotFound(DesignateException): error_code = 500 error_type = 'adapter_not_found' @@ -63,24 +63,24 @@ class NSD4SlaveBackendError(Backend): pass -class NotImplemented(Base, NotImplementedError): +class NotImplemented(DesignateException, NotImplementedError): pass -class XFRFailure(Base): +class XFRFailure(DesignateException): pass -class ConfigurationError(Base): +class ConfigurationError(DesignateException): error_type = 'configuration_error' -class UnknownFailure(Base): +class UnknownFailure(DesignateException): error_code = 500 error_type = 'unknown_failure' -class CommunicationFailure(Base): +class CommunicationFailure(DesignateException): error_code = 504 error_type = 'communication_failure' @@ -119,27 +119,27 @@ class NoPoolTargetsConfigured(ConfigurationError): error_type = 'no_pool_targets_configured' -class OverQuota(Base): +class OverQuota(DesignateException): error_code = 413 error_type = 'over_quota' expected = True -class QuotaResourceUnknown(Base): +class QuotaResourceUnknown(DesignateException): error_type = 'quota_resource_unknown' -class InvalidObject(Base): +class InvalidObject(DesignateException): error_code = 400 error_type = 'invalid_object' expected = True -class BadAction(Base): +class BadAction(DesignateException): error_type = 'bad_action' -class BadRequest(Base): +class BadRequest(DesignateException): error_code = 400 error_type = 'bad_request' expected = True @@ -206,40 +206,40 @@ class UnsupportedContentType(BadRequest): error_type = 'unsupported_content_type' -class InvalidZoneName(Base): +class InvalidZoneName(DesignateException): error_code = 400 error_type = 'invalid_zone_name' expected = True -class InvalidRecordSetName(Base): +class InvalidRecordSetName(DesignateException): error_code = 400 error_type = 'invalid_recordset_name' expected = True -class InvalidRecordSetLocation(Base): +class InvalidRecordSetLocation(DesignateException): error_code = 400 error_type = 'invalid_recordset_location' expected = True -class InvaildZoneTransfer(Base): +class InvaildZoneTransfer(DesignateException): error_code = 400 error_type = 'invalid_zone_transfer_request' -class InvalidTTL(Base): +class InvalidTTL(DesignateException): error_code = 400 error_type = 'invalid_ttl' -class ZoneHasSubZone(Base): +class ZoneHasSubZone(DesignateException): error_code = 400 error_type = 'zone_has_sub_zone' -class Forbidden(Base): +class Forbidden(DesignateException): error_code = 403 error_type = 'forbidden' expected = True @@ -257,7 +257,7 @@ class IncorrectZoneTransferKey(Forbidden): error_type = 'invalid_key' -class Duplicate(Base): +class Duplicate(DesignateException): expected = True error_code = 409 error_type = 'duplicate' @@ -343,7 +343,7 @@ class DuplicateZoneExport(Duplicate): error_type = 'duplicate_zone_export' -class MethodNotAllowed(Base): +class MethodNotAllowed(DesignateException): expected = True error_code = 405 error_type = 'method_not_allowed' @@ -365,7 +365,7 @@ class DuplicateZoneMaster(Duplicate): error_type = 'duplicate_zone_attribute' -class NotFound(Base): +class NotFound(DesignateException): expected = True error_code = 404 error_type = 'not_found' diff --git a/designate/rpc.py b/designate/rpc.py index c531661f2..df6c8d463 100644 --- a/designate/rpc.py +++ b/designate/rpc.py @@ -25,20 +25,22 @@ __all__ = [ 'get_notifier', ] +import functools from oslo_config import cfg import oslo_messaging as messaging from oslo_messaging.rpc import dispatcher as rpc_dispatcher -from oslo_messaging.rpc import server as rpc_server from oslo_serialization import jsonutils +import threading import designate.context import designate.exceptions from designate import objects CONF = cfg.CONF -TRANSPORT = None -NOTIFIER = None +EXPECTED_EXCEPTION = threading.local() NOTIFICATION_TRANSPORT = None +NOTIFIER = None +TRANSPORT = None # NOTE: Additional entries to designate.exceptions goes here. CONF.register_opts([ @@ -162,16 +164,6 @@ class RequestContextSerializer(messaging.Serializer): return designate.context.DesignateContext.from_dict(context) -class RPCDispatcher(rpc_dispatcher.RPCDispatcher): - def dispatch(self, *args, **kwds): - try: - return super(RPCDispatcher, self).dispatch(*args, **kwds) - except designate.exceptions.Base as e: - if e.expected: - raise rpc_dispatcher.ExpectedException() - raise - - def get_transport_url(url_str=None): return messaging.TransportURL.parse(CONF, url_str) @@ -197,12 +189,13 @@ def get_server(target, endpoints, serializer=None): serializer = DesignateObjectSerializer() serializer = RequestContextSerializer(serializer) access_policy = rpc_dispatcher.DefaultRPCAccessPolicy - dispatcher = RPCDispatcher(endpoints, serializer, access_policy) - return rpc_server.RPCServer( + return messaging.get_rpc_server( TRANSPORT, target, - dispatcher=dispatcher, + endpoints, executor='eventlet', + serializer=serializer, + access_policy=access_policy ) @@ -234,3 +227,27 @@ def create_transport(url): return messaging.get_rpc_transport(CONF, url=url, allowed_remote_exmods=exmods) + + +def expected_exceptions(): + def outer(f): + @functools.wraps(f) + def exception_wrapper(self, *args, **kwargs): + if not hasattr(EXPECTED_EXCEPTION, 'depth'): + EXPECTED_EXCEPTION.depth = 0 + EXPECTED_EXCEPTION.depth += 1 + + # We only want to wrap the first function wrapped. + if EXPECTED_EXCEPTION.depth > 1: + return f(self, *args, **kwargs) + + try: + return f(self, *args, **kwargs) + except designate.exceptions.DesignateException as e: + if e.expected: + raise rpc_dispatcher.ExpectedException() + raise + finally: + EXPECTED_EXCEPTION.depth = 0 + return exception_wrapper + return outer diff --git a/designate/tests/test_central/test_service.py b/designate/tests/test_central/test_service.py index c4f03762c..e4995bd24 100644 --- a/designate/tests/test_central/test_service.py +++ b/designate/tests/test_central/test_service.py @@ -28,6 +28,7 @@ from oslo_log import log as logging from oslo_db import exception as db_exception from oslo_versionedobjects import exception as ovo_exc from oslo_messaging.notify import notifier +from oslo_messaging.rpc import dispatcher as rpc_dispatcher from designate import exceptions from designate import objects @@ -325,10 +326,11 @@ class CentralServiceTest(CentralTestCase): self.central_service.delete_tld(self.admin_context, tld['id']) # Fetch the tld again, ensuring an exception is raised - self.assertRaises( - exceptions.TldNotFound, - self.central_service.get_tld, - self.admin_context, tld['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_tld, + self.admin_context, tld['id']) + + self.assertEqual(exceptions.TldNotFound, exc.exc_info[0]) # TsigKey Tests def test_create_tsigkey(self): @@ -404,8 +406,11 @@ class CentralServiceTest(CentralTestCase): self.central_service.delete_tsigkey(self.admin_context, tsigkey['id']) # Fetch the tsigkey again, ensuring an exception is raised - with testtools.ExpectedException(exceptions.TsigKeyNotFound): - self.central_service.get_tsigkey(self.admin_context, tsigkey['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_tsigkey, + self.admin_context, tsigkey['id']) + + self.assertEqual(exceptions.TsigKeyNotFound, exc.exc_info[0]) # Tenant Tests def test_count_tenants(self): @@ -430,8 +435,11 @@ class CentralServiceTest(CentralTestCase): # Set the policy to reject the authz self.policy({'count_tenants': '!'}) - with testtools.ExpectedException(exceptions.Forbidden): - self.central_service.count_tenants(self.get_context()) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.count_tenants, + self.get_context()) + + self.assertEqual(exceptions.Forbidden, exc.exc_info[0]) # Zone Tests @mock.patch.object(notifier.Notifier, "info") @@ -510,8 +518,10 @@ class CentralServiceTest(CentralTestCase): self.create_zone() - with testtools.ExpectedException(exceptions.OverQuota): - self.create_zone() + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_zone) + + self.assertEqual(exceptions.OverQuota, exc.exc_info[0]) def test_create_subzone(self): # Create the Parent Zone using fixture 0 @@ -593,9 +603,11 @@ class CentralServiceTest(CentralTestCase): values['name'] = 'www.%s' % parent_zone['name'] # Attempt to create the subzone - with testtools.ExpectedException(exceptions.IllegalChildZone): - self.central_service.create_zone( - context, objects.Zone.from_dict(values)) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.create_zone, + context, objects.Zone.from_dict(values)) + + self.assertEqual(exceptions.IllegalChildZone, exc.exc_info[0]) def test_create_superzone_failure(self): context = self.get_admin_context() @@ -620,9 +632,11 @@ class CentralServiceTest(CentralTestCase): context.tenant = '2' # Attempt to create the zone - with testtools.ExpectedException(exceptions.IllegalParentZone): - self.central_service.create_zone( - context, objects.Zone.from_dict(zone_values)) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.create_zone, + context, objects.Zone.from_dict(zone_values)) + + self.assertEqual(exceptions.IllegalParentZone, exc.exc_info[0]) def test_create_blacklisted_zone_success(self): # Create blacklisted zone using default values @@ -656,10 +670,12 @@ class CentralServiceTest(CentralTestCase): email='info@blacklisted.com' ) - with testtools.ExpectedException(exceptions.InvalidZoneName): - # Create a zone - self.central_service.create_zone( - self.admin_context, objects.Zone.from_dict(values)) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.create_zone, + self.admin_context, + objects.Zone.from_dict(values)) + + self.assertEqual(exceptions.InvalidZoneName, exc.exc_info[0]) def _test_create_zone_fail(self, values, exception): @@ -687,10 +703,13 @@ class CentralServiceTest(CentralTestCase): ) # There is no TLD for net so it should fail - with testtools.ExpectedException(exceptions.InvalidZoneName): - # Create an invalid zone - self.central_service.create_zone( - self.admin_context, objects.Zone.from_dict(values)) + # Create an invalid zone + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.create_zone, + self.admin_context, + objects.Zone.from_dict(values)) + + self.assertEqual(exceptions.InvalidZoneName, exc.exc_info[0]) def test_create_zone_invalid_ttl_fail(self): self.policy({'use_low_ttl': '!'}) @@ -905,8 +924,11 @@ class CentralServiceTest(CentralTestCase): zone.name = 'example.net.' # Perform the update - with testtools.ExpectedException(exceptions.BadRequest): - self.central_service.update_zone(self.admin_context, zone) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.update_zone, + self.admin_context, zone) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def test_update_zone_deadlock_retry(self): # Create a zone @@ -1005,8 +1027,11 @@ class CentralServiceTest(CentralTestCase): # Set the policy to reject the authz self.policy({'count_zones': '!'}) - with testtools.ExpectedException(exceptions.Forbidden): - self.central_service.count_zones(self.get_context()) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.count_zones, + self.get_context()) + + self.assertEqual(exceptions.Forbidden, exc.exc_info[0]) def _fetch_all_zones(self): """Fetch all zones including deleted ones @@ -1356,8 +1381,11 @@ class CentralServiceTest(CentralTestCase): def test_xfr_zone_invalid_type(self): zone = self.create_zone() - with testtools.ExpectedException(exceptions.BadRequest): - self.central_service.xfr_zone(self.admin_context, zone.id) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.xfr_zone, + self.admin_context, zone.id) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) # RecordSet Tests def test_create_recordset(self): @@ -1424,8 +1452,11 @@ class CentralServiceTest(CentralTestCase): self.create_recordset(zone) - with testtools.ExpectedException(exceptions.OverQuota): - self.create_recordset(zone) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_recordset, + zone) + + self.assertEqual(exceptions.OverQuota, exc.exc_info[0]) def test_create_invalid_recordset_location_cname_at_apex(self): zone = self.create_zone() @@ -1436,11 +1467,13 @@ class CentralServiceTest(CentralTestCase): ) # Attempt to create a CNAME record at the apex - with testtools.ExpectedException(exceptions.InvalidRecordSetLocation): - self.central_service.create_recordset( - self.admin_context, - zone['id'], - recordset=objects.RecordSet.from_dict(values)) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.create_recordset, + self.admin_context, + zone['id'], + recordset=objects.RecordSet.from_dict(values)) + + self.assertEqual(exceptions.InvalidRecordSetLocation, exc.exc_info[0]) def test_create_invalid_recordset_location_cname_sharing(self): zone = self.create_zone() @@ -1452,11 +1485,13 @@ class CentralServiceTest(CentralTestCase): ) # Attempt to create a CNAME record alongside another record - with testtools.ExpectedException(exceptions.InvalidRecordSetLocation): - self.central_service.create_recordset( - self.admin_context, - zone['id'], - recordset=objects.RecordSet.from_dict(values)) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.create_recordset, + self.admin_context, + zone['id'], + recordset=objects.RecordSet.from_dict(values)) + + self.assertEqual(exceptions.InvalidRecordSetLocation, exc.exc_info[0]) def test_create_invalid_recordset_location_wrong_zone(self): zone = self.create_zone() @@ -1468,11 +1503,13 @@ class CentralServiceTest(CentralTestCase): ) # Attempt to create a record in the incorrect zone - with testtools.ExpectedException(exceptions.InvalidRecordSetLocation): - self.central_service.create_recordset( - self.admin_context, - zone['id'], - recordset=objects.RecordSet.from_dict(values)) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.create_recordset, + self.admin_context, + zone['id'], + recordset=objects.RecordSet.from_dict(values)) + + self.assertEqual(exceptions.InvalidRecordSetLocation, exc.exc_info[0]) def test_create_invalid_recordset_ttl(self): self.policy({'use_low_ttl': '!'}) @@ -1487,11 +1524,11 @@ class CentralServiceTest(CentralTestCase): ) # Attempt to create a A record under the TTL - with testtools.ExpectedException(exceptions.InvalidTTL): - self.central_service.create_recordset( - self.admin_context, - zone['id'], - recordset=objects.RecordSet.from_dict(values)) + self.assertRaises(exceptions.InvalidTTL, + self.central_service.create_recordset, + self.admin_context, + zone['id'], + recordset=objects.RecordSet.from_dict(values)) def test_create_recordset_no_min_ttl(self): self.policy({'use_low_ttl': '!'}) @@ -1549,9 +1586,12 @@ class CentralServiceTest(CentralTestCase): expected = self.create_recordset(zone) # Ensure we get a 404 if we use the incorrect zone_id - with testtools.ExpectedException(exceptions.RecordSetNotFound): - self.central_service.get_recordset( - self.admin_context, other_zone['id'], expected['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_recordset, + self.admin_context, other_zone['id'], + expected['id']) + + self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0]) def test_find_recordsets(self): zone = self.create_zone() @@ -1843,9 +1883,12 @@ class CentralServiceTest(CentralTestCase): self.admin_context, zone['id'], recordset['id']) # Fetch the recordset again, ensuring an exception is raised - with testtools.ExpectedException(exceptions.RecordSetNotFound): - self.central_service.get_recordset( - self.admin_context, zone['id'], recordset['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_recordset, + self.admin_context, zone['id'], + recordset['id']) + + self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0]) # Fetch the zone again to verify serial number increased updated_zone = self.central_service.get_zone(self.admin_context, @@ -1869,9 +1912,12 @@ class CentralServiceTest(CentralTestCase): increment_serial=False) # Fetch the record again, ensuring an exception is raised - with testtools.ExpectedException(exceptions.RecordSetNotFound): - self.central_service.get_recordset( - self.admin_context, zone['id'], recordset['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_recordset, + self.admin_context, zone['id'], + recordset['id']) + + self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0]) # Ensure the zones serial number was not updated zone_after = self.central_service.get_zone( @@ -1887,9 +1933,12 @@ class CentralServiceTest(CentralTestCase): recordset = self.create_recordset(zone) # Ensure we get a 404 if we use the incorrect zone_id - with testtools.ExpectedException(exceptions.RecordSetNotFound): - self.central_service.delete_recordset( - self.admin_context, other_zone['id'], recordset['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.delete_recordset, + self.admin_context, other_zone['id'], + recordset['id']) + + self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0]) def test_count_recordsets(self): # in the beginning, there should be nothing @@ -1910,8 +1959,11 @@ class CentralServiceTest(CentralTestCase): # Set the policy to reject the authz self.policy({'count_recordsets': '!'}) - with testtools.ExpectedException(exceptions.Forbidden): - self.central_service.count_recordsets(self.get_context()) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.count_recordsets, + self.get_context()) + + self.assertEqual(exceptions.Forbidden, exc.exc_info[0]) # Record Tests def test_create_record(self): @@ -1942,8 +1994,11 @@ class CentralServiceTest(CentralTestCase): self.create_record(zone, recordset) - with testtools.ExpectedException(exceptions.OverQuota): - self.create_record(zone, recordset) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_record, + zone, recordset) + + self.assertEqual(exceptions.OverQuota, exc.exc_info[0]) def test_create_record_over_zone_quota(self): self.config(quota_zone_records=1) @@ -1960,10 +2015,12 @@ class CentralServiceTest(CentralTestCase): ]) ) - with testtools.ExpectedException(exceptions.OverQuota): - # Persist the Object - recordset = self.central_service.create_recordset( - self.admin_context, zone.id, recordset=recordset) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.create_recordset, + self.admin_context, zone.id, + recordset=recordset) + + self.assertEqual(exceptions.OverQuota, exc.exc_info[0]) def test_create_record_over_recordset_quota(self): self.config(quota_recordset_records=1) @@ -1974,8 +2031,11 @@ class CentralServiceTest(CentralTestCase): self.create_record(zone, recordset) - with testtools.ExpectedException(exceptions.OverQuota): - self.create_record(zone, recordset) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_record, + zone, recordset) + + self.assertEqual(exceptions.OverQuota, exc.exc_info[0]) def test_create_record_without_incrementing_serial(self): zone = self.create_zone() @@ -2020,11 +2080,13 @@ class CentralServiceTest(CentralTestCase): # Create a record expected = self.create_record(zone, recordset) - # Ensure we get a 404 if we use the incorrect zone_id - with testtools.ExpectedException(exceptions.RecordNotFound): - self.central_service.get_record( - self.admin_context, other_zone['id'], recordset['id'], - expected['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_record, + self.admin_context, other_zone['id'], + recordset['id'], + expected['id']) + + self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) def test_get_record_incorrect_recordset_id(self): zone = self.create_zone() @@ -2034,11 +2096,14 @@ class CentralServiceTest(CentralTestCase): # Create a record expected = self.create_record(zone, recordset) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_record, + self.admin_context, zone['id'], + other_recordset['id'], + expected['id']) + # Ensure we get a 404 if we use the incorrect recordset_id - with testtools.ExpectedException(exceptions.RecordNotFound): - self.central_service.get_record( - self.admin_context, zone['id'], other_recordset['id'], - expected['id']) + self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) def test_find_records(self): zone = self.create_zone() @@ -2162,8 +2227,11 @@ class CentralServiceTest(CentralTestCase): record.zone_id = other_zone.id # Ensure we get a BadRequest if we change the zone_id - with testtools.ExpectedException(exceptions.BadRequest): - self.central_service.update_record(self.admin_context, record) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.update_record, + self.admin_context, record) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def test_update_record_immutable_recordset_id(self): zone = self.create_zone() @@ -2177,8 +2245,11 @@ class CentralServiceTest(CentralTestCase): record.recordset_id = other_recordset.id # Ensure we get a BadRequest if we change the recordset_id - with testtools.ExpectedException(exceptions.BadRequest): - self.central_service.update_record(self.admin_context, record) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.update_record, + self.admin_context, record) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def test_delete_record(self): zone = self.create_zone() @@ -2260,10 +2331,13 @@ class CentralServiceTest(CentralTestCase): record = self.create_record(zone, recordset) # Ensure we get a 404 if we use the incorrect zone_id - with testtools.ExpectedException(exceptions.RecordNotFound): - self.central_service.delete_record( - self.admin_context, other_zone['id'], recordset['id'], - record['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.delete_record, + self.admin_context, other_zone['id'], + recordset['id'], + record['id']) + + self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) def test_delete_record_incorrect_recordset_id(self): zone = self.create_zone() @@ -2274,10 +2348,13 @@ class CentralServiceTest(CentralTestCase): record = self.create_record(zone, recordset) # Ensure we get a 404 if we use the incorrect recordset_id - with testtools.ExpectedException(exceptions.RecordNotFound): - self.central_service.delete_record( - self.admin_context, zone['id'], other_recordset['id'], - record['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.delete_record, + self.admin_context, zone['id'], + other_recordset['id'], + record['id']) + + self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) def test_count_records(self): # in the beginning, there should be nothing @@ -2299,8 +2376,11 @@ class CentralServiceTest(CentralTestCase): # Set the policy to reject the authz self.policy({'count_records': '!'}) - with testtools.ExpectedException(exceptions.Forbidden): - self.central_service.count_records(self.get_context()) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.count_records, + self.get_context()) + + self.assertEqual(exceptions.Forbidden, exc.exc_info[0]) def test_get_floatingip_no_record(self): context = self.get_context(tenant='a') @@ -2351,9 +2431,11 @@ class CentralServiceTest(CentralTestCase): fip = self.network_api.fake.allocate_floatingip(context.tenant) self.network_api.fake.deallocate_floatingip(fip['id']) - with testtools.ExpectedException(exceptions.NotFound): - self.central_service.get_floatingip( - context, fip['region'], fip['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_floatingip, + context, fip['region'], fip['id']) + + self.assertEqual(exceptions.NotFound, exc.exc_info[0]) def test_get_floatingip_deallocated_and_invalidate(self): context_a = self.get_context(tenant='a') @@ -2384,9 +2466,11 @@ class CentralServiceTest(CentralTestCase): self.network_api.fake.deallocate_floatingip(fip['id']) - with testtools.ExpectedException(exceptions.NotFound): - self.central_service.get_floatingip( - context_a, fip['region'], fip['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_floatingip, + context_a, fip['region'], fip['id']) + + self.assertEqual(exceptions.NotFound, exc.exc_info[0]) # Ensure that the record is still in DB (No invalidation) self.central_service.find_record(elevated_a, criterion) @@ -2408,8 +2492,11 @@ class CentralServiceTest(CentralTestCase): # Ensure that the old record for tenant a for the fip now owned by # tenant b is gone - with testtools.ExpectedException(exceptions.RecordNotFound): - self.central_service.find_record(elevated_a, criterion) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.find_record, + elevated_a, criterion) + + self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) def test_list_floatingips_no_allocations(self): context = self.get_context(tenant='a') @@ -2481,7 +2568,7 @@ class CentralServiceTest(CentralTestCase): self.network_api.fake.deallocate_floatingip(fip['id']) fips = self.central_service.list_floatingips(context_a) - assert(len(fips) == 0) + self.assertEqual(len(fips), 0) # Ensure that the record is still in DB (No invalidation) self.central_service.find_record(elevated_a, criterion) @@ -2503,8 +2590,11 @@ class CentralServiceTest(CentralTestCase): # Ensure that the old record for tenant a for the fip now owned by # tenant b is gone - with testtools.ExpectedException(exceptions.RecordNotFound): - self.central_service.find_record(elevated_a, criterion) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.find_record, + elevated_a, criterion) + + self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) def test_set_floatingip(self): context = self.get_context(tenant='a') @@ -2609,9 +2699,11 @@ class CentralServiceTest(CentralTestCase): # If one attempts to assign a de-allocated FIP or not-owned it should # fail with BadRequest - with testtools.ExpectedException(exceptions.NotFound): - fixture = self.central_service.update_floatingip( - context, fip['region'], fip['id'], fixture) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.update_floatingip, + context, fip['region'], fip['id'], fixture) + + self.assertEqual(exceptions.NotFound, exc.exc_info[0]) def test_unset_floatingip(self): context = self.get_context(tenant='a') @@ -2726,9 +2818,13 @@ class CentralServiceTest(CentralTestCase): blacklist['id']) # Try to fetch the blacklist to verify an exception is raised - with testtools.ExpectedException(exceptions.BlacklistNotFound): - self.central_service.get_blacklist(self.admin_context, - blacklist['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_blacklist, + self.admin_context, + blacklist['id'] + ) + + self.assertEqual(exceptions.BlacklistNotFound, exc.exc_info[0]) # SOA recordset tests def test_create_SOA(self): @@ -3005,8 +3101,11 @@ class CentralServiceTest(CentralTestCase): self.central_service.delete_pool(self.admin_context, pool['id']) # Verify that the pool has been deleted - with testtools.ExpectedException(exceptions.PoolNotFound): - self.central_service.get_pool(self.admin_context, pool['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_pool, + self.admin_context, pool['id']) + + self.assertEqual(exceptions.PoolNotFound, exc.exc_info[0]) def test_update_status_delete_zone(self): # Create a zone @@ -3029,8 +3128,11 @@ class CentralServiceTest(CentralTestCase): self.admin_context, zone['id'], "SUCCESS", zone_serial) # Fetch the zone again, ensuring an exception is raised - with testtools.ExpectedException(exceptions.ZoneNotFound): - self.central_service.get_zone(self.admin_context, zone['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_zone, + self.admin_context, zone['id']) + + self.assertEqual(exceptions.ZoneNotFound, exc.exc_info[0]) def test_update_status_delete_last_record(self): zone = self.create_zone() @@ -3050,10 +3152,13 @@ class CentralServiceTest(CentralTestCase): self.admin_context, zone['id'], "SUCCESS", zone_serial) # Fetch the record again, ensuring an exception is raised - with testtools.ExpectedException(exceptions.RecordSetNotFound): - self.central_service.get_record( - self.admin_context, zone['id'], recordset['id'], - record['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_record, + self.admin_context, zone['id'], + recordset['id'], + record['id']) + + self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0]) @mock.patch.object(notifier.Notifier, "info") def test_update_status_send_notification(self, mock_notifier): @@ -3114,10 +3219,12 @@ class CentralServiceTest(CentralTestCase): self.admin_context, zone['id'], "SUCCESS", zone_serial) # Fetch the record again, ensuring an exception is raised - with testtools.ExpectedException(exceptions.RecordSetNotFound): - self.central_service.get_record( - self.admin_context, zone['id'], recordset['id'], - record['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_record, + self.admin_context, zone['id'], + recordset['id'], record['id']) + + self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0]) # Ensure the zones serial number was not updated new_zone_serial = self.central_service.get_zone( @@ -3180,9 +3287,14 @@ class CentralServiceTest(CentralTestCase): def test_create_zone_transfer_request_duplicate(self): zone = self.create_zone() self.create_zone_transfer_request(zone) - with testtools.ExpectedException( - exceptions.DuplicateZoneTransferRequest): - self.create_zone_transfer_request(zone) + + exc = self.assertRaises( + rpc_dispatcher.ExpectedException, + self.create_zone_transfer_request, + zone) + + self.assertEqual(exceptions.DuplicateZoneTransferRequest, + exc.exc_info[0]) def test_create_scoped_zone_transfer_request(self): zone = self.create_zone() @@ -3225,9 +3337,11 @@ class CentralServiceTest(CentralTestCase): self.central_service.get_zone_transfer_request( tenant_1_context, zt_request.id) - with testtools.ExpectedException(exceptions.Forbidden): - self.central_service.get_zone_transfer_request( - tenant_3_context, zt_request.id) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_zone_transfer_request, + tenant_3_context, zt_request.id) + + self.assertEqual(exceptions.Forbidden, exc.exc_info[0]) def test_update_zone_transfer_request(self): zone = self.create_zone() @@ -3250,11 +3364,13 @@ class CentralServiceTest(CentralTestCase): self.central_service.delete_zone_transfer_request( self.admin_context, zone_transfer_request.id) - with testtools.ExpectedException( - exceptions.ZoneTransferRequestNotFound): - self.central_service.get_zone_transfer_request( - self.admin_context, - zone_transfer_request.id) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_zone_transfer_request, + self.admin_context, + zone_transfer_request.id) + + self.assertEqual(exceptions.ZoneTransferRequestNotFound, + exc.exc_info[0]) def test_create_zone_transfer_accept(self): tenant_1_context = self.get_context(tenant=1) @@ -3380,10 +3496,12 @@ class CentralServiceTest(CentralTestCase): zone_transfer_accept.key = 'WRONG KEY' zone_transfer_accept.zone_id = zone.id - with testtools.ExpectedException(exceptions.IncorrectZoneTransferKey): - zone_transfer_accept = \ - self.central_service.create_zone_transfer_accept( - tenant_2_context, zone_transfer_accept) + exc = self.assertRaises( + rpc_dispatcher.ExpectedException, + self.central_service.create_zone_transfer_accept, + tenant_2_context, zone_transfer_accept) + + self.assertEqual(exceptions.IncorrectZoneTransferKey, exc.exc_info[0]) def test_create_zone_tarnsfer_accept_out_of_tenant_scope(self): tenant_1_context = self.get_context(tenant=1) @@ -3405,10 +3523,13 @@ class CentralServiceTest(CentralTestCase): zone_transfer_accept.key = zone_transfer_request.key zone_transfer_accept.zone_id = zone.id - with testtools.ExpectedException(exceptions.Forbidden): - zone_transfer_accept = \ - self.central_service.create_zone_transfer_accept( - tenant_3_context, zone_transfer_accept) + exc = self.assertRaises( + rpc_dispatcher.ExpectedException, + self.central_service.create_zone_transfer_accept, + tenant_3_context, zone_transfer_accept + ) + + self.assertEqual(exceptions.Forbidden, exc.exc_info[0]) # Zone Import Tests def test_create_zone_import(self): @@ -3517,7 +3638,8 @@ class CentralServiceTest(CentralTestCase): zone_import['id']) # Fetch the zone_import again, ensuring an exception is raised - self.assertRaises( - exceptions.ZoneImportNotFound, - self.central_service.get_zone_import, - context, zone_import['id']) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.central_service.get_zone_import, + context, zone_import['id']) + + self.assertEqual(exceptions.ZoneImportNotFound, exc.exc_info[0]) diff --git a/designate/tests/test_storage/__init__.py b/designate/tests/test_storage/__init__.py index 8f096b304..5a42a3f38 100644 --- a/designate/tests/test_storage/__init__.py +++ b/designate/tests/test_storage/__init__.py @@ -19,6 +19,7 @@ import mock import testtools from oslo_config import cfg from oslo_log import log as logging +from oslo_messaging.rpc import dispatcher as rpc_dispatcher from designate import exceptions from designate import objects @@ -375,8 +376,11 @@ class StorageTestCase(object): values = self.get_tsigkey_fixture(1) values['name'] = tsigkey_one['name'] - with testtools.ExpectedException(exceptions.DuplicateTsigKey): - self.create_tsigkey(**values) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_tsigkey, + **values) + + self.assertEqual(exceptions.DuplicateTsigKey, exc.exc_info[0]) def test_find_tsigkeys(self): actual = self.storage.find_tsigkeys(self.admin_context) @@ -599,8 +603,10 @@ class StorageTestCase(object): # Create the Initial Zone self.create_zone() - with testtools.ExpectedException(exceptions.DuplicateZone): - self.create_zone() + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_zone) + + self.assertEqual(exceptions.DuplicateZone, exc.exc_info[0]) def test_find_zones(self): self.config(quota_zones=20) @@ -880,9 +886,11 @@ class StorageTestCase(object): # Create the First RecordSet self.create_recordset(zone) - with testtools.ExpectedException(exceptions.DuplicateRecordSet): - # Attempt to create the second/duplicate recordset - self.create_recordset(zone) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_recordset, + zone) + + self.assertEqual(exceptions.DuplicateRecordSet, exc.exc_info[0]) def test_create_recordset_with_records(self): zone = self.create_zone() @@ -1308,9 +1316,11 @@ class StorageTestCase(object): # Create the First Record self.create_record(zone, recordset) - with testtools.ExpectedException(exceptions.DuplicateRecord): - # Attempt to create the second/duplicate record - self.create_record(zone, recordset) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_record, + zone, recordset) + + self.assertEqual(exceptions.DuplicateRecord, exc.exc_info[0]) def test_find_records(self): zone = self.create_zone() @@ -1603,9 +1613,11 @@ class StorageTestCase(object): # Create the First Tld self.create_tld(fixture=0) - with testtools.ExpectedException(exceptions.DuplicateTld): - # Attempt to create the second/duplicate Tld - self.create_tld(fixture=0) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_tld, + fixture=0) + + self.assertEqual(exceptions.DuplicateTld, exc.exc_info[0]) def test_find_tlds(self): @@ -1759,8 +1771,11 @@ class StorageTestCase(object): # Create the initial Blacklist self.create_blacklist(fixture=0) - with testtools.ExpectedException(exceptions.DuplicateBlacklist): - self.create_blacklist(fixture=0) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_blacklist, + fixture=0) + + self.assertEqual(exceptions.DuplicateBlacklist, exc.exc_info[0]) def test_find_blacklists(self): # Verify that there are no blacklists created @@ -1935,8 +1950,11 @@ class StorageTestCase(object): self.create_pool(fixture=0) # Create the second pool and should get exception - with testtools.ExpectedException(exceptions.DuplicatePool): - self.create_pool(fixture=0) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.create_pool, + fixture=0) + + self.assertEqual(exceptions.DuplicatePool, exc.exc_info[0]) def test_find_pools(self): # Verify that there are no pools, except for default pool diff --git a/designate/tests/unit/test_central/test_basic.py b/designate/tests/unit/test_central/test_basic.py index 2d6db3ef4..eb5a78cbc 100644 --- a/designate/tests/unit/test_central/test_basic.py +++ b/designate/tests/unit/test_central/test_basic.py @@ -14,25 +14,26 @@ # License for the specific language governing permissions and limitations # under the License. -import six import unittest +import fixtures +import mock +import six +import testtools from mock import Mock from mock import patch from oslo_config import cfg from oslo_config import fixture as cfg_fixture from oslo_log import log as logging +from oslo_messaging.rpc import dispatcher as rpc_dispatcher from oslotest import base -import fixtures -import mock -import testtools +import designate.central.service from designate import exceptions from designate import objects from designate.central.service import Service from designate.tests import TestCase from designate.tests.fixtures import random_seed -import designate.central.service LOG = logging.getLogger(__name__) @@ -317,7 +318,7 @@ class CentralServiceTestCase(CentralBasic): assert self.service.quota self.assertTrue(designate.central.service.quota.get_quota.called) - def test__is_valid_ttl(self): + def test_is_valid_ttl(self): self.CONF.set_override('min_ttl', 10, 'service:central') self.service._is_valid_ttl(self.context, 20) @@ -330,17 +331,18 @@ class CentralServiceTestCase(CentralBasic): designate.central.service.policy.check = mock.Mock( side_effect=exceptions.Forbidden ) + with testtools.ExpectedException(exceptions.InvalidTTL): self.service._is_valid_ttl(self.context, 3) - def test__update_soa_secondary(self): + def test_update_soa_secondary(self): ctx = mock.Mock() mock_zone = RoObject(type='SECONDARY') self.service._update_soa(ctx, mock_zone) self.assertFalse(ctx.elevated.called) - def test__update_soa(self): + def test_update_soa(self): class MockZone(dict): type = 'PRIMARY' pool_id = 1 @@ -352,14 +354,16 @@ class CentralServiceTestCase(CentralBasic): self.context.elevated = mock.Mock() self.service._update_zone_in_storage = mock.Mock() - self.service.storage.get_pool = mock.Mock(return_value=MockPool()) + self.service.storage.get_pool = mock.Mock( + return_value=MockPool()) self.service.find_recordset = mock.Mock(return_value=mock_soa) self.service._build_soa_record = mock.Mock() self.service._update_recordset_in_storage = mock.Mock() self.service._update_soa(self.context, Mockzone()) - self.assertTrue(self.service._update_recordset_in_storage.called) + self.assertTrue( + self.service._update_recordset_in_storage.called) self.assertTrue(self.context.elevated.called) def test_count_zones(self): @@ -375,38 +379,44 @@ class CentralServiceTestCase(CentralBasic): ) def test_validate_new_recordset(self): - self.service._is_valid_recordset_name = mock.Mock() - self.service._is_valid_recordset_placement = mock.Mock() - self.service._is_valid_recordset_placement_subzone = mock.Mock() - self.service._is_valid_ttl = mock.Mock() + central_service = self.central_service + + central_service._is_valid_recordset_name = mock.Mock() + central_service._is_valid_recordset_placement = mock.Mock() + central_service._is_valid_recordset_placement_subzone = mock.Mock() + central_service._is_valid_ttl = mock.Mock() MockRecordSet.id = None - self.service._validate_recordset( + central_service._validate_recordset( self.context, Mockzone, MockRecordSet ) - assert self.service._is_valid_recordset_name.called - assert self.service._is_valid_recordset_placement.called - assert self.service._is_valid_recordset_placement_subzone.called - assert self.service._is_valid_ttl.called + self.assertTrue(central_service._is_valid_recordset_name.called) + self.assertTrue(central_service._is_valid_recordset_placement.called) + self.assertTrue( + central_service._is_valid_recordset_placement_subzone.called) + self.assertTrue(central_service._is_valid_ttl.called) def test_validate_existing_recordset(self): - self.service._is_valid_recordset_name = mock.Mock() - self.service._is_valid_recordset_placement = mock.Mock() - self.service._is_valid_recordset_placement_subzone = mock.Mock() - self.service._is_valid_ttl = mock.Mock() + central_service = self.central_service + + central_service._is_valid_recordset_name = mock.Mock() + central_service._is_valid_recordset_placement = mock.Mock() + central_service._is_valid_recordset_placement_subzone = mock.Mock() + central_service._is_valid_ttl = mock.Mock() MockRecordSet.obj_get_changes = Mock(return_value={'ttl': 3600}) - self.service._validate_recordset( + central_service._validate_recordset( self.context, Mockzone, MockRecordSet ) - assert self.service._is_valid_recordset_name.called - assert self.service._is_valid_recordset_placement.called - assert self.service._is_valid_recordset_placement_subzone.called - assert self.service._is_valid_ttl.called + self.assertTrue(central_service._is_valid_recordset_name.called) + self.assertTrue(central_service._is_valid_recordset_placement.called) + self.assertTrue( + central_service._is_valid_recordset_placement_subzone.called) + self.assertTrue(central_service._is_valid_ttl.called) def test_create_recordset_in_storage(self): self.service._enforce_recordset_quota = Mock() @@ -422,26 +432,28 @@ class CentralServiceTestCase(CentralBasic): self.assertFalse(self.service._update_zone_in_storage.called) def test_create_recordset_with_records_in_storage(self): - self.service._enforce_recordset_quota = mock.Mock() - self.service._enforce_record_quota = mock.Mock() - self.service._is_valid_recordset_name = mock.Mock() - self.service._is_valid_recordset_placement = mock.Mock() - self.service._is_valid_recordset_placement_subzone = mock.Mock() - self.service._is_valid_ttl = mock.Mock() + central_service = self.central_service - self.service.storage.create_recordset = mock.Mock(return_value='rs') - self.service._update_zone_in_storage = mock.Mock() + central_service._enforce_recordset_quota = mock.Mock() + central_service._enforce_record_quota = mock.Mock() + central_service._is_valid_recordset_name = mock.Mock() + central_service._is_valid_recordset_placement = mock.Mock() + central_service._is_valid_recordset_placement_subzone = mock.Mock() + central_service._is_valid_ttl = mock.Mock() + + central_service.storage.create_recordset = mock.Mock(return_value='rs') + central_service._update_zone_in_storage = mock.Mock() recordset = Mock() recordset.obj_attr_is_set.return_value = True recordset.records = [MockRecord()] - rs, zone = self.service._create_recordset_in_storage( + rs, zone = central_service._create_recordset_in_storage( self.context, Mockzone(), recordset ) - assert self.service._enforce_record_quota.called - assert self.service._update_zone_in_storage.called + self.assertTrue(central_service._enforce_record_quota.called) + self.assertTrue(central_service._update_zone_in_storage.called) def test_create_recordset_checking_DBDeadLock(self): self.service._enforce_recordset_quota = mock.Mock() @@ -469,17 +481,19 @@ class CentralServiceTestCase(CentralBasic): assert self.service._update_zone_in_storage.called assert self.service.storage.create_recordset.called - def test__create_soa(self): - self.service._create_recordset_in_storage = Mock( + def test_create_soa(self): + central_service = self.central_service + + central_service._create_recordset_in_storage = Mock( return_value=(None, None) ) - self.service._build_soa_record = Mock( + central_service._build_soa_record = Mock( return_value='example.org. foo.bar 1 60 5 999 1' ) zone = Mockzone() - self.service._create_soa(self.context, zone) + central_service._create_soa(self.context, zone) - ctx, md, rset = self.service._create_recordset_in_storage.call_args[0] + _, _, rset = central_service._create_recordset_in_storage.call_args[0] self.assertEqual('example.org.', rset.name) self.assertEqual('SOA', rset.type) @@ -487,7 +501,7 @@ class CentralServiceTestCase(CentralBasic): self.assertEqual(1, len(rset.records.objects)) self.assertTrue(rset.records.objects[0].managed) - def test__create_zone_in_storage(self): + def test_create_zone_in_storage(self): self.service._create_soa = Mock() self.service._create_ns = Mock() self.service.get_zone_ns_records = Mock( @@ -509,7 +523,7 @@ class CentralServiceTestCase(CentralBasic): @unittest.expectedFailure # FIXME def test_create_zone_forbidden(self): - assert not self.service.storage.count_zones.called + self.assertFalse(self.service.storage.count_zones.called) designate.central.service.policy.check = mock.Mock(return_value=None) self.service._enforce_zone_quota = mock.Mock(return_value=None) self.service._is_valid_zone_name = mock.Mock(return_value=None) @@ -525,8 +539,12 @@ class CentralServiceTestCase(CentralBasic): # self.assertEqual('', parent_zone) self.service.check_for_tlds = False - with testtools.ExpectedException(exceptions.Forbidden): - self.service.create_zone(self.context, Mockzone()) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.create_zone, + self.context, Mockzone()) + + self.assertEqual(exceptions.Forbidden, exc.exc_info[0]) # TODO(Federico) add more create_zone tests assert parent_zone @@ -559,37 +577,40 @@ class CentralZoneTestCase(CentralBasic): self.service.storage.find_tlds = storage_find_tlds self.service.storage.find_tld = storage_find_tld - def test__is_valid_zone_name_valid(self): + def test_is_valid_zone_name_valid(self): self.service._is_blacklisted_zone_name = Mock() self.service._is_valid_zone_name(self.context, 'valid.org.') - def test__is_valid_zone_name_invalid(self): + def test_is_valid_zone_name_invalid(self): self.service._is_blacklisted_zone_name = Mock() with testtools.ExpectedException(exceptions.InvalidZoneName): - self.service._is_valid_zone_name(self.context, 'example^org.') + self.service._is_valid_zone_name(self.context, + 'example^org.') - def test__is_valid_zone_name_invalid_2(self): + def test_is_valid_zone_name_invalid_2(self): self.service._is_blacklisted_zone_name = Mock() with testtools.ExpectedException(exceptions.InvalidZoneName): - self.service._is_valid_zone_name(self.context, 'example.tld.') + self.service._is_valid_zone_name(self.context, + 'example.tld.') - def test__is_valid_zone_name_invalid_same_as_tld(self): + def test_is_valid_zone_name_invalid_same_as_tld(self): self.service._is_blacklisted_zone_name = Mock() with testtools.ExpectedException(exceptions.InvalidZoneName): self.service._is_valid_zone_name(self.context, 'com.com.') - def test__is_valid_zone_name_invalid_tld(self): + def test_is_valid_zone_name_invalid_tld(self): self.service._is_blacklisted_zone_name = Mock() with testtools.ExpectedException(exceptions.InvalidZoneName): self.service._is_valid_zone_name(self.context, 'tld.') - def test__is_valid_zone_name_blacklisted(self): + def test_is_valid_zone_name_blacklisted(self): self.service._is_blacklisted_zone_name = Mock( side_effect=exceptions.InvalidZoneName) with testtools.ExpectedException(exceptions.InvalidZoneName): - self.service._is_valid_zone_name(self.context, 'valid.com.') + self.service._is_valid_zone_name(self.context, + 'valid.com.') - def test__is_blacklisted_zone_name(self): + def test_is_blacklisted_zone_name(self): self.service.storage.find_blacklists.return_value = [ RoObject(pattern='a'), RoObject(pattern='b') ] @@ -605,28 +626,27 @@ class CentralZoneTestCase(CentralBasic): expected ) - def test__is_valid_recordset_name(self): + def test_is_valid_recordset_name(self): zone = RoObject(name='example.org.') self.service._is_valid_recordset_name(self.context, zone, 'foo..example.org.') - def test__is_valid_recordset_name_no_dot(self): + def test_is_valid_recordset_name_no_dot(self): zone = RoObject(name='example.org.') with testtools.ExpectedException(ValueError): self.service._is_valid_recordset_name(self.context, zone, 'foo.example.org') - def test__is_valid_recordset_name_too_long(self): + def test_is_valid_recordset_name_too_long(self): zone = RoObject(name='example.org.') designate.central.service.cfg.CONF['service:central'].\ max_recordset_name_len = 255 rs_name = 'a' * 255 + '.org.' with testtools.ExpectedException(exceptions.InvalidRecordSetName) as e: - self.service._is_valid_recordset_name(self.context, zone, - rs_name) + self.service._is_valid_recordset_name(self.context, zone, rs_name) self.assertEqual(six.text_type(e), 'Name too long') - def test__is_valid_recordset_name_wrong_zone(self): + def test_is_valid_recordset_name_wrong_zone(self): zone = RoObject(name='example.org.') with testtools.ExpectedException(exceptions.InvalidRecordSetLocation): self.service._is_valid_recordset_name(self.context, zone, @@ -634,8 +654,8 @@ class CentralZoneTestCase(CentralBasic): def test_is_valid_recordset_placement_cname(self): zone = RoObject(name='example.org.') - with testtools.ExpectedException(exceptions.InvalidRecordSetLocation) \ - as e: + with testtools.ExpectedException( + exceptions.InvalidRecordSetLocation) as e: self.service._is_valid_recordset_placement( self.context, zone, @@ -651,8 +671,8 @@ class CentralZoneTestCase(CentralBasic): self.service.storage.find_recordsets.return_value = [ RoObject(id=CentralZoneTestCase.recordset__id) ] - with testtools.ExpectedException(exceptions.InvalidRecordSetLocation) \ - as e: + with testtools.ExpectedException( + exceptions.InvalidRecordSetLocation) as e: self.service._is_valid_recordset_placement( self.context, zone, @@ -669,8 +689,8 @@ class CentralZoneTestCase(CentralBasic): RoObject(), RoObject() ] - with testtools.ExpectedException(exceptions.InvalidRecordSetLocation) \ - as e: + with testtools.ExpectedException( + exceptions.InvalidRecordSetLocation) as e: self.service._is_valid_recordset_placement( self.context, zone, @@ -692,7 +712,7 @@ class CentralZoneTestCase(CentralBasic): ) self.assertTrue(ret) - def test__is_valid_recordset_placement_subzone(self): + def test_is_valid_recordset_placement_subzone(self): zone = RoObject(name='example.org.', id=CentralZoneTestCase.zone__id) self.service._is_valid_recordset_placement_subzone( self.context, @@ -700,9 +720,10 @@ class CentralZoneTestCase(CentralBasic): 'example.org.' ) - def test__is_valid_recordset_placement_subzone_2(self): + def test_is_valid_recordset_placement_subzone_2(self): zone = RoObject(name='example.org.', id=CentralZoneTestCase.zone__id) - self.service._is_valid_recordset_name = Mock(side_effect=Exception) + self.service._is_valid_recordset_name = Mock( + side_effect=Exception) self.service.storage.find_zones.return_value = [ RoObject(name='foo.example.org.') ] @@ -712,7 +733,7 @@ class CentralZoneTestCase(CentralBasic): 'bar.example.org.' ) - def test__is_valid_recordset_placement_subzone_failing(self): + def test_is_valid_recordset_placement_subzone_failing(self): zone = RoObject(name='example.org.', id=CentralZoneTestCase.zone__id) self.service._is_valid_recordset_name = Mock() self.service.storage.find_zones.return_value = [ @@ -738,20 +759,23 @@ class CentralZoneTestCase(CentralBasic): recordset ) - def test__is_superzone(self): - self.service.storage.find_zones = Mock() - self.service._is_superzone(self.context, 'example.org.', '1') - _class_self_, crit = self.service.storage.find_zones.call_args[0] + def test_is_superzone(self): + central_service = self.central_service + + central_service.storage.find_zones = Mock() + central_service._is_superzone(self.context, 'example.org.', '1') + _, crit = self.service.storage.find_zones.call_args[0] self.assertEqual({'name': '%.example.org.', 'pool_id': '1'}, crit) @patch('designate.central.service.utils.increment_serial') - def FIXME_test__increment_zone_serial(self, utils_inc_ser): + def FIXME_test_increment_zone_serial(self, utils_inc_ser): fixtures.MockPatch('designate.central.service.utils.increment_serial') zone = RoObject(serial=1) self.service._increment_zone_serial(self.context, zone) - def test__create_ns(self): - self.service._create_recordset_in_storage = Mock(return_value=(0, 0)) + def test_create_ns(self): + self.service._create_recordset_in_storage = Mock( + return_value=(0, 0)) self.service._create_ns( self.context, RoObject(type='PRIMARY', name='example.org.'), @@ -765,16 +789,17 @@ class CentralZoneTestCase(CentralBasic): self.assertEqual(3, len(rset.records)) self.assertTrue(rset.records[0].managed) - def test__create_ns_skip(self): + def test_create_ns_skip(self): self.service._create_recordset_in_storage = Mock() self.service._create_ns( self.context, RoObject(type='SECONDARY', name='example.org.'), [], ) - self.assertFalse(self.service._create_recordset_in_storage.called) + self.assertFalse( + self.service._create_recordset_in_storage.called) - def test__add_ns_creation(self): + def test_add_ns_creation(self): self.service._create_ns = Mock() self.service.find_recordsets = Mock( @@ -789,7 +814,7 @@ class CentralZoneTestCase(CentralBasic): ctx, zone, records = self.service._create_ns.call_args[0] self.assertTrue(len(records), 1) - def test__add_ns(self): + def test_add_ns(self): self.service._update_recordset_in_storage = Mock() recordsets = [ @@ -810,7 +835,7 @@ class CentralZoneTestCase(CentralBasic): self.assertTrue(rset.records[0].managed) self.assertEqual('bar', rset.records[0].data.name) - def test__add_ns_with_other_ns_rs(self): + def test_add_ns_with_other_ns_rs(self): self.service._update_recordset_in_storage = Mock() recordsets = [ @@ -859,12 +884,13 @@ class CentralZoneTestCase(CentralBasic): ) ) - with testtools.ExpectedException(exceptions.NoServersConfigured): - self.service.create_zone( - self.context, - objects.Zone(tenant_id='1', name='example.com.', ttl=60, - pool_id=CentralZoneTestCase.pool__id) - ) + z = objects.Zone(tenant_id='1', + name='example.com.', ttl=60, + pool_id=CentralZoneTestCase.pool__id) + + self.assertRaises(exceptions.NoServersConfigured, + self.service.create_zone, + self.context, z) def test_create_zone(self): self.service._enforce_zone_quota = Mock() @@ -897,8 +923,6 @@ class CentralZoneTestCase(CentralBasic): ) ) - # self.service.create_zone = unwrap(self.service.create_zone) - out = self.service.create_zone( self.context, objects.Zone( @@ -917,7 +941,8 @@ class CentralZoneTestCase(CentralBasic): name='foo', tenant_id='2', ) - self.service.get_zone(self.context, CentralZoneTestCase.zone__id) + self.service.get_zone(self.context, + CentralZoneTestCase.zone__id) n, ctx, target = designate.central.service.policy.check.call_args[0] self.assertEqual(CentralZoneTestCase.zone__id, target['zone_id']) self.assertEqual('foo', target['zone_name']) @@ -942,7 +967,7 @@ class CentralZoneTestCase(CentralBasic): self.context = RoObject(tenant='t') self.service.storage.find_zones = Mock() self.service.find_zones(self.context) - assert self.service.storage.find_zones.called + self.assertTrue(self.service.storage.find_zones.called) pcheck, ctx, target = \ designate.central.service.policy.check.call_args[0] self.assertEqual('find_zones', pcheck) @@ -951,7 +976,7 @@ class CentralZoneTestCase(CentralBasic): self.context = RoObject(tenant='t') self.service.storage.find_zone = Mock() self.service.find_zone(self.context) - assert self.service.storage.find_zone.called + self.assertTrue(self.service.storage.find_zone.called) pcheck, ctx, target = \ designate.central.service.policy.check.call_args[0] self.assertEqual('find_zone', pcheck) @@ -963,9 +988,11 @@ class CentralZoneTestCase(CentralBasic): tenant_id='2', ) self.service.storage.count_zones.return_value = 2 - with testtools.ExpectedException(exceptions.ZoneHasSubZone): - self.service.delete_zone(self.context, - CentralZoneTestCase.zone__id) + + self.assertRaises(exceptions.ZoneHasSubZone, + self.service.delete_zone, + self.context, + CentralZoneTestCase.zone__id) pcheck, ctx, target = \ designate.central.service.policy.check.call_args[0] @@ -985,11 +1012,12 @@ class CentralZoneTestCase(CentralBasic): ]) self.context.abandon = True self.service.storage.count_zones.return_value = 0 - self.service.delete_zone(self.context, CentralZoneTestCase.zone__id) - assert self.service.storage.delete_zone.called - assert not self.service.pool_manager_api.delete_zone.called - pcheck, ctx, target = \ - designate.central.service.policy.check.call_args[0] + self.service.delete_zone(self.context, + CentralZoneTestCase.zone__id) + self.assertTrue(self.service.storage.delete_zone.called) + self.assertFalse( + self.service.pool_manager_api.delete_zone.called) + pcheck, _, _ = designate.central.service.policy.check.call_args[0] self.assertEqual('abandon_zone', pcheck) def test_delete_zone(self): @@ -1006,18 +1034,19 @@ class CentralZoneTestCase(CentralBasic): self.service.storage.count_zones.return_value = 0 out = self.service.delete_zone(self.context, CentralZoneTestCase.zone__id) - assert not self.service.storage.delete_zone.called - assert self.service.zone_api.delete_zone.called - assert designate.central.service.policy.check.called + self.assertFalse(self.service.storage.delete_zone.called) + self.assertTrue(self.service.zone_api.delete_zone.called) + self.assertTrue(designate.central.service.policy.check.called) ctx, deleted_dom = \ self.service.zone_api.delete_zone.call_args[0] + self.assertEqual('foo', deleted_dom.name) self.assertEqual('foo', out.name) pcheck, ctx, target = \ designate.central.service.policy.check.call_args[0] self.assertEqual('delete_zone', pcheck) - def test__delete_zone_in_storage(self): + def test_delete_zone_in_storage(self): self.service._delete_zone_in_storage( self.context, RwObject(action='', status=''), @@ -1026,7 +1055,7 @@ class CentralZoneTestCase(CentralBasic): self.assertEqual('DELETE', d.action) self.assertEqual('PENDING', d.status) - def test__xfr_zone_secondary(self): + def test_xfr_zone_secondary(self): self.service.storage.get_zone.return_value = RoObject( name='example.org.', tenant_id='2', @@ -1037,23 +1066,30 @@ class CentralZoneTestCase(CentralBasic): with fx_mdns_api: self.service.mdns_api.get_serial_number.return_value = \ "SUCCESS", 2, 1 - self.service.xfr_zone(self.context, CentralZoneTestCase.zone__id) - assert self.service.mdns_api.perform_zone_xfr.called + self.service.xfr_zone( + self.context, CentralZoneTestCase.zone__id) + self.assertTrue( + self.service.mdns_api.perform_zone_xfr.called) - assert designate.central.service.policy.check.called + self.assertTrue(designate.central.service.policy.check.called) self.assertEqual( 'xfr_zone', designate.central.service.policy.check.call_args[0][0] ) - def test__xfr_zone_not_secondary(self): + def test_xfr_zone_not_secondary(self): self.service.storage.get_zone.return_value = RoObject( name='example.org.', tenant_id='2', type='PRIMARY' ) - with testtools.ExpectedException(exceptions.BadRequest): - self.service.xfr_zone(self.context, CentralZoneTestCase.zone__id) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.xfr_zone, + self.context, + CentralZoneTestCase.zone__id) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def test_count_report(self): self.service.count_zones = Mock(return_value=1) @@ -1099,11 +1135,13 @@ class CentralZoneTestCase(CentralBasic): self.service.count_zones = Mock(return_value=1) self.service.count_records = Mock(return_value=2) self.service.count_tenants = Mock(return_value=3) - with testtools.ExpectedException(exceptions.ReportNotFound): - self.service.count_report( - self.context, - criterion='bogus' - ) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.count_report, + self.context, + criterion='bogus') + + self.assertEqual(exceptions.ReportNotFound, exc.exc_info[0]) def _test_touch_zone(self, worker_enabled=True): if not worker_enabled: @@ -1127,7 +1165,7 @@ class CentralZoneTestCase(CentralBasic): self.service.touch_zone(self.context, CentralZoneTestCase.zone__id) - assert designate.central.service.policy.check.called + self.assertTrue(designate.central.service.policy.check.called) self.assertEqual( 'touch_zone', designate.central.service.policy.check.call_args[0][0] @@ -1146,12 +1184,14 @@ class CentralZoneTestCase(CentralBasic): self.service.storage.get_recordset.return_value = RoObject( zone_id=CentralZoneTestCase.zone__id_2 ) - with testtools.ExpectedException(exceptions.RecordSetNotFound): - self.service.get_recordset( - self.context, - CentralZoneTestCase.zone__id, - CentralZoneTestCase.recordset__id - ) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.get_recordset, + self.context, + CentralZoneTestCase.zone__id, + CentralZoneTestCase.recordset__id) + + self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0]) def test_get_recordset(self): self.service.storage.get_zone.return_value = RoObject( @@ -1159,11 +1199,12 @@ class CentralZoneTestCase(CentralBasic): name='example.org.', tenant_id='2', ) - self.service.storage.get_recordset.return_value = objects.RecordSet( - zone_id=CentralZoneTestCase.zone__id_2, - zone_name='example.org.', - id=CentralZoneTestCase.recordset__id - ) + self.service.storage.get_recordset.return_value = ( + objects.RecordSet( + zone_id=CentralZoneTestCase.zone__id_2, + zone_name='example.org.', + id=CentralZoneTestCase.recordset__id + )) self.service.get_recordset( self.context, CentralZoneTestCase.zone__id_2, @@ -1185,7 +1226,7 @@ class CentralZoneTestCase(CentralBasic): self.context = Mock() self.context.tenant = 't' self.service.find_recordsets(self.context) - assert self.service.storage.find_recordsets.called + self.assertTrue(self.service.storage.find_recordsets.called) n, ctx, target = designate.central.service.policy.check.call_args[0] self.assertEqual('find_recordsets', n) self.assertEqual({'tenant_id': 't'}, target) @@ -1194,7 +1235,7 @@ class CentralZoneTestCase(CentralBasic): self.context = Mock() self.context.tenant = 't' self.service.find_recordset(self.context) - assert self.service.storage.find_recordset.called + self.assertTrue(self.service.storage.find_recordset.called) n, ctx, target = designate.central.service.policy.check.call_args[0] self.assertEqual('find_recordset', n) self.assertEqual({'tenant_id': 't'}, target) @@ -1205,16 +1246,28 @@ class CentralZoneTestCase(CentralBasic): recordset.obj_get_original_value.return_value = '1' recordset.obj_get_changes.return_value = ['tenant_id', 'foo'] - with testtools.ExpectedException(exceptions.BadRequest): - self.service.update_recordset(self.context, recordset) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.update_recordset, + self.context, + recordset) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) recordset.obj_get_changes.return_value = ['zone_id', 'foo'] - with testtools.ExpectedException(exceptions.BadRequest): - self.service.update_recordset(self.context, recordset) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.update_recordset, + self.context, + recordset) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) recordset.obj_get_changes.return_value = ['type', 'foo'] - with testtools.ExpectedException(exceptions.BadRequest): - self.service.update_recordset(self.context, recordset) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.update_recordset, + self.context, + recordset) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def test_update_recordset_action_delete(self): self.service.storage.get_zone.return_value = RoObject( @@ -1222,8 +1275,13 @@ class CentralZoneTestCase(CentralBasic): ) recordset = Mock() recordset.obj_get_changes.return_value = ['foo'] - with testtools.ExpectedException(exceptions.BadRequest): - self.service.update_recordset(self.context, recordset) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.update_recordset, + self.context, + recordset) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def test_update_recordset_action_fail_on_managed(self): self.service.storage.get_zone.return_value = RoObject( @@ -1237,8 +1295,13 @@ class CentralZoneTestCase(CentralBasic): recordset.managed = True self.context = Mock() self.context.edit_managed_records = False - with testtools.ExpectedException(exceptions.BadRequest): - self.service.update_recordset(self.context, recordset) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.update_recordset, + self.context, + recordset) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def _test_update_recordset(self, worker_enabled=True): if not worker_enabled: @@ -1260,13 +1323,15 @@ class CentralZoneTestCase(CentralBasic): self.service._update_recordset_in_storage = Mock( return_value=('x', 'y') ) + if worker_enabled: with fx_worker: self.service.update_recordset(self.context, recordset) else: with fx_pool_manager: self.service.update_recordset(self.context, recordset) - assert self.service._update_recordset_in_storage.called + self.assertTrue( + self.service._update_recordset_in_storage.called) n, ctx, target = designate.central.service.policy.check.call_args[0] self.assertEqual('update_recordset', n) @@ -1320,10 +1385,10 @@ class CentralZoneTestCase(CentralBasic): 90, self.service._is_valid_ttl.call_args[0][1] ) - assert self.service.storage.update_recordset.called - assert self.service._update_zone_in_storage.called + self.assertTrue(self.service.storage.update_recordset.called) + self.assertTrue(self.service._update_zone_in_storage.called) - def test__update_recordset_in_storage_2(self): + def test_update_recordset_in_storage_2(self): recordset = Mock() recordset.name = 'n' recordset.type = 't' @@ -1361,9 +1426,9 @@ class CentralZoneTestCase(CentralBasic): self.service._is_valid_recordset_placement_subzone. call_args[0][2] ) - assert not self.service._update_zone_in_storage.called - assert self.service.storage.update_recordset.called - assert self.service._enforce_record_quota.called + self.assertFalse(self.service._update_zone_in_storage.called) + self.assertTrue(self.service.storage.update_recordset.called) + self.assertTrue(self.service._enforce_record_quota.called) def test_delete_recordset_not_found(self): self.service.storage.get_zone.return_value = RoObject( @@ -1380,10 +1445,14 @@ class CentralZoneTestCase(CentralBasic): ) self.context = Mock() self.context.edit_managed_records = False - with testtools.ExpectedException(exceptions.RecordSetNotFound): - self.service.delete_recordset(self.context, - CentralZoneTestCase.zone__id_2, - CentralZoneTestCase.recordset__id) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.delete_recordset, + self.context, + CentralZoneTestCase.zone__id_2, + CentralZoneTestCase.recordset__id) + + self.assertEqual(exceptions.RecordSetNotFound, exc.exc_info[0]) def test_delete_recordset_action_delete(self): self.service.storage.get_zone.return_value = RoObject( @@ -1400,10 +1469,14 @@ class CentralZoneTestCase(CentralBasic): ) self.context = Mock() self.context.edit_managed_records = False - with testtools.ExpectedException(exceptions.BadRequest): - self.service.delete_recordset(self.context, - CentralZoneTestCase.zone__id_2, - CentralZoneTestCase.recordset__id) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.delete_recordset, + self.context, + CentralZoneTestCase.zone__id_2, + CentralZoneTestCase.recordset__id) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def test_delete_recordset_managed(self): self.service.storage.get_zone.return_value = RoObject( @@ -1420,10 +1493,14 @@ class CentralZoneTestCase(CentralBasic): ) self.context = Mock() self.context.edit_managed_records = False - with testtools.ExpectedException(exceptions.BadRequest): - self.service.delete_recordset(self.context, - CentralZoneTestCase.zone__id_2, - CentralZoneTestCase.recordset__id) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.delete_recordset, + self.context, + CentralZoneTestCase.zone__id_2, + CentralZoneTestCase.recordset__id) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def _test_delete_recordset(self, worker_enabled=True): if not worker_enabled: @@ -1452,20 +1529,24 @@ class CentralZoneTestCase(CentralBasic): self.service._delete_recordset_in_storage = Mock( return_value=(mock_rs, mock_zone) ) + if worker_enabled: with fx_worker: self.service.delete_recordset(self.context, CentralZoneTestCase.zone__id_2, CentralZoneTestCase.recordset__id) - assert self.service.zone_api.update_zone.called + self.assertTrue( + self.service.zone_api.update_zone.called) else: with fx_pool_manager: self.service.delete_recordset(self.context, CentralZoneTestCase.zone__id_2, CentralZoneTestCase.recordset__id) - assert self.service.zone_api.update_zone.called + self.assertTrue( + self.service.zone_api.update_zone.called) - assert self.service._delete_recordset_in_storage.called + self.assertTrue( + self.service._delete_recordset_in_storage.called) def test_delete_recordset_worker(self): self._test_delete_recordset(worker_enabled=True) @@ -1488,15 +1569,15 @@ class CentralZoneTestCase(CentralBasic): ) ]) ) - assert self.service.storage.update_recordset.called - assert self.service.storage.delete_recordset.called + self.assertTrue(self.service.storage.update_recordset.called) + self.assertTrue(self.service.storage.delete_recordset.called) rs = self.service.storage.update_recordset.call_args[0][1] self.assertEqual(1, len(rs.records)) self.assertEqual('DELETE', rs.records[0].action) self.assertEqual('PENDING', rs.records[0].status) self.assertEqual(1, rs.records[0].serial) - def test__delete_recordset_in_storage_no_increment_serial(self): + def test_delete_recordset_in_storage_no_increment_serial(self): self.service._update_zone_in_storage = Mock() self.service._delete_recordset_in_storage( self.context, @@ -1510,9 +1591,9 @@ class CentralZoneTestCase(CentralBasic): ]), increment_serial=False, ) - assert self.service.storage.update_recordset.called - assert self.service.storage.delete_recordset.called - assert not self.service._update_zone_in_storage.called + self.assertTrue(self.service.storage.update_recordset.called) + self.assertTrue(self.service.storage.delete_recordset.called) + self.assertFalse(self.service._update_zone_in_storage.called) def test_count_recordset(self): self.service.count_recordsets(self.context) @@ -1532,13 +1613,14 @@ class CentralZoneTestCase(CentralBasic): tenant_id='2', type='foo', ) - with testtools.ExpectedException(exceptions.BadRequest): - self.service.create_record( - self.context, - CentralZoneTestCase.zone__id, - CentralZoneTestCase.recordset__id, - RoObject(), - ) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.create_record, + self.context, + CentralZoneTestCase.zone__id, + CentralZoneTestCase.recordset__id, + RoObject()) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def _test_create_record(self, worker_enabled=True): if not worker_enabled: @@ -1564,7 +1646,8 @@ class CentralZoneTestCase(CentralBasic): CentralZoneTestCase.zone__id, CentralZoneTestCase.recordset__id, RoObject()) - assert self.service.zone_api.update_zone.called + self.assertTrue( + self.service.zone_api.update_zone.called) else: with fx_pool_manager: self.service.create_record( @@ -1572,7 +1655,8 @@ class CentralZoneTestCase(CentralBasic): CentralZoneTestCase.zone__id, CentralZoneTestCase.recordset__id, RoObject()) - assert self.service.zone_api.update_zone.called + self.assertTrue( + self.service.zone_api.update_zone.called) n, ctx, target = designate.central.service.policy.check.call_args[0] self.assertEqual('create_record', n) @@ -1603,7 +1687,9 @@ class CentralZoneTestCase(CentralBasic): ), increment_serial=False ) - ctx, did, rid, record = self.service.storage.create_record.call_args[0] + create_record = self.service.storage.create_record + + ctx, did, rid, record = create_record.call_args[0] self.assertEqual(CentralZoneTestCase.zone__id, did) self.assertEqual(CentralZoneTestCase.recordset__id, rid) self.assertEqual('CREATE', record.action) @@ -1617,11 +1703,15 @@ class CentralZoneTestCase(CentralBasic): self.service.storage.get_recordset.return_value = RoObject( zone_id=CentralZoneTestCase.recordset__id ) - with testtools.ExpectedException(exceptions.RecordNotFound): - self.service.get_record(self.context, - CentralZoneTestCase.zone__id_2, - CentralZoneTestCase.recordset__id, - CentralZoneTestCase.record__id) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.get_record, + self.context, + CentralZoneTestCase.zone__id_2, + CentralZoneTestCase.recordset__id, + CentralZoneTestCase.record__id) + + self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) def test_get_record_not_found_2(self): self.service.storage.get_zone.return_value = RoObject( @@ -1639,11 +1729,15 @@ class CentralZoneTestCase(CentralBasic): zone_id=CentralZoneTestCase.zone__id_2, recordset_id=CentralZoneTestCase.recordset__id ) - with testtools.ExpectedException(exceptions.RecordNotFound): - self.service.get_record(self.context, - CentralZoneTestCase.zone__id_2, - CentralZoneTestCase.recordset__id, - CentralZoneTestCase.record__id) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.get_record, + self.context, + CentralZoneTestCase.zone__id_2, + CentralZoneTestCase.recordset__id, + CentralZoneTestCase.record__id) + + self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) def test_get_record(self): self.service.storage.get_zone.return_value = RoObject( @@ -1690,24 +1784,36 @@ class CentralZoneTestCase(CentralBasic): record.obj_get_original_value.return_value = 1 record.obj_get_changes.return_value = ['tenant_id', 'foo'] - with testtools.ExpectedException(exceptions.BadRequest): - self.service.update_record(self.context, record) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.update_record, + self.context, record) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) record.obj_get_changes.return_value = ['zone_id', 'foo'] - with testtools.ExpectedException(exceptions.BadRequest): - self.service.update_record(self.context, record) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.update_record, + self.context, record) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) record.obj_get_changes.return_value = ['recordset_id', 'foo'] - with testtools.ExpectedException(exceptions.BadRequest): - self.service.update_record(self.context, record) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.update_record, + self.context, record) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def test_update_record_action_delete(self): self.service.storage.get_zone.return_value = RoObject( action='DELETE', ) record = Mock() - with testtools.ExpectedException(exceptions.BadRequest): - self.service.update_record(self.context, record) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.update_record, + self.context, record) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def test_update_record_action_fail_on_managed(self): self.service.storage.get_zone.return_value = RoObject( @@ -1724,8 +1830,12 @@ class CentralZoneTestCase(CentralBasic): record.obj_get_changes.return_value = ['foo'] self.context = Mock() self.context.edit_managed_records = False - with testtools.ExpectedException(exceptions.BadRequest): - self.service.update_record(self.context, record) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.update_record, + self.context, record) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def _test_update_record(self, worker_enabled=True): if not worker_enabled: @@ -1755,7 +1865,7 @@ class CentralZoneTestCase(CentralBasic): else: with fx_pool_manager: self.service.update_record(self.context, record) - assert self.service._update_record_in_storage.called + self.assertTrue(self.service._update_record_in_storage.called) n, ctx, target = designate.central.service.policy.check.call_args[0] self.assertEqual('update_record', n) @@ -1795,8 +1905,12 @@ class CentralZoneTestCase(CentralBasic): self.service.storage.get_zone.return_value = RoObject( action='DELETE', ) - with testtools.ExpectedException(exceptions.BadRequest): - self.service.delete_record(self.context, 1, 2, 3) + + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.delete_record, + self.context, 1, 2, 3) + + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) def test_delete_record_not_found(self): self.service.storage.get_zone.return_value = RoObject( @@ -1809,12 +1923,16 @@ class CentralZoneTestCase(CentralBasic): self.service.storage.get_recordset.return_value = RoObject( id=CentralZoneTestCase.recordset__id_2, ) + # zone.id != record.zone_id - with testtools.ExpectedException(exceptions.RecordNotFound): - self.service.delete_record(self.context, - CentralZoneTestCase.zone__id, - CentralZoneTestCase.recordset__id, - CentralZoneTestCase.record__id) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.delete_record, + self.context, + CentralZoneTestCase.zone__id, + CentralZoneTestCase.recordset__id, + CentralZoneTestCase.record__id) + + self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) self.service.storage.get_record.return_value = RoObject( id=CentralZoneTestCase.record__id, @@ -1822,11 +1940,14 @@ class CentralZoneTestCase(CentralBasic): recordset_id=CentralZoneTestCase.recordset__id_3, ) # recordset.id != record.recordset_id - with testtools.ExpectedException(exceptions.RecordNotFound): - self.service.delete_record(self.context, - CentralZoneTestCase.zone__id, - CentralZoneTestCase.recordset__id, - CentralZoneTestCase.record__id) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.delete_record, + self.context, + CentralZoneTestCase.zone__id, + CentralZoneTestCase.recordset__id, + CentralZoneTestCase.record__id) + + self.assertEqual(exceptions.RecordNotFound, exc.exc_info[0]) def _test_delete_record(self, worker_enabled=True): if not worker_enabled: @@ -1907,13 +2028,16 @@ class CentralZoneTestCase(CentralBasic): self.context.edit_managed_records = False with fx_pool_manager: - with testtools.ExpectedException(exceptions.BadRequest): - self.service.delete_record(self.context, - CentralZoneTestCase.zone__id_2, - CentralZoneTestCase.recordset__id_2, - CentralZoneTestCase.record__id_2) + exc = self.assertRaises(rpc_dispatcher.ExpectedException, + self.service.delete_record, + self.context, + CentralZoneTestCase.zone__id_2, + CentralZoneTestCase.recordset__id_2, + CentralZoneTestCase.record__id_2) - def test__delete_record_in_storage(self): + self.assertEqual(exceptions.BadRequest, exc.exc_info[0]) + + def test_delete_record_in_storage(self): self.service._delete_record_in_storage( self.context, RoObject(serial=2), @@ -1951,7 +2075,8 @@ class CentralZoneTestCase(CentralBasic): tenant_id='tid', ) - self.service.sync_zone(self.context, CentralZoneTestCase.zone__id) + self.service.sync_zone(self.context, + CentralZoneTestCase.zone__id) t, ctx, target = designate.central.service.policy.check.call_args[0] self.assertEqual('diagnostics_sync_zone', t) @@ -1969,9 +2094,10 @@ class CentralZoneTestCase(CentralBasic): name='n', ) - self.service.sync_record(self.context, CentralZoneTestCase.zone__id, - CentralZoneTestCase.recordset__id, - CentralZoneTestCase.record__id) + self.service.sync_record( + self.context, CentralZoneTestCase.zone__id, + CentralZoneTestCase.recordset__id, + CentralZoneTestCase.record__id) t, ctx, target = designate.central.service.policy.check.call_args[0] self.assertEqual('diagnostics_sync_record', t) @@ -1997,7 +2123,7 @@ class CentralZoneTestCase(CentralBasic): self.assertFalse(r['status']) self.assertFalse(r['storage']) - def test__determine_floatingips(self): + def test_determine_floatingips(self): self.context = Mock() self.context.tenant = 'tnt' self.service.find_records = Mock(return_value=[ @@ -2005,11 +2131,12 @@ class CentralZoneTestCase(CentralBasic): ]) fips = {} - data, invalid = self.service._determine_floatingips(self.context, fips) + data, invalid = self.service._determine_floatingips( + self.context, fips) self.assertEqual({}, data) self.assertEqual([], invalid) - def test__determine_floatingips_with_data(self): + def test_determine_floatingips_with_data(self): self.context = Mock() self.context.tenant = 2 self.service.find_records = Mock(return_value=[ @@ -2021,14 +2148,16 @@ class CentralZoneTestCase(CentralBasic): 'k': {'address': 1}, 'k2': {'address': 2}, } - data, invalid = self.service._determine_floatingips(self.context, fips) + data, invalid = self.service._determine_floatingips( + self.context, fips) self.assertEqual(1, len(invalid)) self.assertEqual(1, invalid[0].managed_tenant_id) self.assertEqual(data['k'], ({'address': 1}, None)) - def test__generate_soa_refresh_interval(self): + def test_generate_soa_refresh_interval(self): + central_service = self.central_service with random_seed(42): - refresh_time = self.service._generate_soa_refresh_interval() + refresh_time = central_service._generate_soa_refresh_interval() self.assertEqual(3563, refresh_time) @@ -2047,24 +2176,25 @@ class IsSubzoneTestCase(CentralBasic): self.service.storage.find_zone = find_zone - def test__is_subzone_false(self): + def test_is_subzone_false(self): r = self.service._is_subzone(self.context, 'com', CentralZoneTestCase.pool__id) self.assertFalse(r) - def FIXME_test__is_subzone_false2(self): + def FIXME_test_is_subzone_false2(self): r = self.service._is_subzone(self.context, 'com.', CentralZoneTestCase.pool__id) self.assertEqual('com.', r) - def FIXME_test__is_subzone_false3(self): + def FIXME_test_is_subzone_false3(self): r = self.service._is_subzone(self.context, 'example.com.', CentralZoneTestCase.pool__id) self.assertEqual('example.com.', r) - def test__is_subzone_false4(self): - r = self.service._is_subzone(self.context, 'foo.a.b.example.com.', - CentralZoneTestCase.pool__id) + def test_is_subzone_false4(self): + r = self.service._is_subzone( + self.context, 'foo.a.b.example.com.', + CentralZoneTestCase.pool__id) self.assertEqual('example.com.', r) @@ -2123,8 +2253,9 @@ class CentralZoneExportTests(CentralBasic): tenant_id='t' ) - out = self.service.get_zone_export(self.context, - CentralZoneTestCase.zone_export__id) + out = self.service.get_zone_export( + self.context, + CentralZoneTestCase.zone_export__id) n, ctx, target = designate.central.service.policy.check.call_args[0] @@ -2145,7 +2276,7 @@ class CentralZoneExportTests(CentralBasic): self.service.find_zone_exports(self.context) - assert self.service.storage.find_zone_exports.called + self.assertTrue(self.service.storage.find_zone_exports.called) pcheck, ctx, target = \ designate.central.service.policy.check.call_args[0] self.assertEqual('find_zone_exports', pcheck) @@ -2164,10 +2295,11 @@ class CentralZoneExportTests(CentralBasic): ) ) - out = self.service.delete_zone_export(self.context, - CentralZoneTestCase.zone_export__id) + out = self.service.delete_zone_export( + self.context, + CentralZoneTestCase.zone_export__id) - assert self.service.storage.delete_zone_export.called + self.assertTrue(self.service.storage.delete_zone_export.called) self.assertEqual(CentralZoneTestCase.zone__id, out.zone_id) self.assertEqual('PENDING', out.status) @@ -2175,7 +2307,7 @@ class CentralZoneExportTests(CentralBasic): self.assertIsNone(out.message) self.assertEqual('t', out.tenant_id) - assert designate.central.service.policy.check.called + self.assertTrue(designate.central.service.policy.check.called) pcheck, ctx, target = \ designate.central.service.policy.check.call_args[0] self.assertEqual('delete_zone_export', pcheck) @@ -2184,14 +2316,14 @@ class CentralZoneExportTests(CentralBasic): class CentralStatusTests(CentralBasic): - def test__update_zone_or_record_status_no_zone(self): + def test_update_zone_or_record_status_no_zone(self): zone = RwObject( action='UPDATE', status='SUCCESS', serial=0, ) - dom, deleted = self.service.\ - _update_zone_or_record_status(zone, 'NO_ZONE', 0) + dom, deleted = self.service._update_zone_or_record_status( + zone, 'NO_ZONE', 0) self.assertEqual(dom.action, 'CREATE') self.assertEqual(dom.status, 'ERROR') diff --git a/designate/worker/service.py b/designate/worker/service.py index 213c5f36a..1604fc275 100644 --- a/designate/worker/service.py +++ b/designate/worker/service.py @@ -21,6 +21,7 @@ import oslo_messaging as messaging from designate import backend from designate import exceptions +from designate import rpc from designate import service from designate import storage from designate.central import rpcapi as central_api @@ -141,6 +142,7 @@ class Service(service.RPCService, service.Service): notify_target)) return self.executor.run(all_tasks) + @rpc.expected_exceptions() def create_zone(self, context, zone): """ :param context: Security context information. @@ -149,6 +151,7 @@ class Service(service.RPCService, service.Service): """ self._do_zone_action(context, zone) + @rpc.expected_exceptions() def update_zone(self, context, zone): """ :param context: Security context information. @@ -157,6 +160,7 @@ class Service(service.RPCService, service.Service): """ self._do_zone_action(context, zone) + @rpc.expected_exceptions() def delete_zone(self, context, zone): """ :param context: Security context information. @@ -165,6 +169,7 @@ class Service(service.RPCService, service.Service): """ self._do_zone_action(context, zone) + @rpc.expected_exceptions() def recover_shard(self, context, begin, end): """ :param begin: the beginning of the shards to recover @@ -175,6 +180,7 @@ class Service(service.RPCService, service.Service): self.executor, context, begin, end )) + @rpc.expected_exceptions() def start_zone_export(self, context, zone, export): """ :param zone: Zone to be exported