From f2513a09b8161a52a2e2f0696a34a8dfce41dcdf Mon Sep 17 00:00:00 2001 From: Kiall Mac Innes Date: Sat, 29 Jun 2013 14:59:18 +0100 Subject: [PATCH] Ensure all backend methods respect deleted domains Change-Id: Iabaf990309e7f9950e61a763176d3d4ada90a8c4 --- designate/storage/impl_sqlalchemy/__init__.py | 7 ++++++- designate/tests/test_storage/__init__.py | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/designate/storage/impl_sqlalchemy/__init__.py b/designate/storage/impl_sqlalchemy/__init__.py index 88a9c02a8..a18e45129 100644 --- a/designate/storage/impl_sqlalchemy/__init__.py +++ b/designate/storage/impl_sqlalchemy/__init__.py @@ -270,6 +270,7 @@ class SQLAlchemyStorage(base.Storage): def get_tenants(self, context): query = self.session.query(models.Domain.tenant_id, func.count(models.Domain.id)) + query = self._apply_deleted_criteria(context, models.Domain, query) query = query.group_by(models.Domain.tenant_id) return [{'id': t[0], 'domain_count': t[1]} for t in query.all()] @@ -277,6 +278,7 @@ class SQLAlchemyStorage(base.Storage): def get_tenant(self, context, tenant_id): query = self.session.query(models.Domain.name) query = query.filter(models.Domain.tenant_id == tenant_id) + query = self._apply_deleted_criteria(context, models.Domain, query) result = query.all() @@ -289,7 +291,10 @@ class SQLAlchemyStorage(base.Storage): def count_tenants(self, context): # tenants are the owner of domains, count the number of unique tenants # select count(distinct tenant_id) from domains - return self.session.query(distinct(models.Domain.tenant_id)).count() + query = self.session.query(distinct(models.Domain.tenant_id)) + query = self._apply_deleted_criteria(context, models.Domain, query) + + return query.count() # Domain Methods def _find_domains(self, context, criterion, one=False): diff --git a/designate/tests/test_storage/__init__.py b/designate/tests/test_storage/__init__.py index 1afcd7273..8ca685bbc 100644 --- a/designate/tests/test_storage/__init__.py +++ b/designate/tests/test_storage/__init__.py @@ -464,14 +464,18 @@ class StorageTestCase(TestCase): def test_get_tenants(self): # create 3 domains in 2 tenants self.create_domain(fixture=0, values={'tenant_id': 'One'}) - self.create_domain(fixture=1, values={'tenant_id': 'One'}) + _, domain = self.create_domain(fixture=1, values={'tenant_id': 'One'}) self.create_domain(fixture=2, values={'tenant_id': 'Two'}) + # Delete one of the domains. + self.storage.delete_domain(self.admin_context, domain['id']) + + # Ensure we get accurate results result = self.storage.get_tenants(self.admin_context) expected = [{ 'id': 'One', - 'domain_count': 2 + 'domain_count': 1 }, { 'id': 'Two', 'domain_count': 1 @@ -483,6 +487,10 @@ class StorageTestCase(TestCase): # create 2 domains in a tenant _, domain_1 = self.create_domain(fixture=0, values={'tenant_id': 1}) _, domain_2 = self.create_domain(fixture=1, values={'tenant_id': 1}) + _, domain_3 = self.create_domain(fixture=2, values={'tenant_id': 1}) + + # Delete one of the domains. + self.storage.delete_domain(self.admin_context, domain_3['id']) result = self.storage.get_tenant(self.admin_context, 1) @@ -499,6 +507,10 @@ class StorageTestCase(TestCase): # create 2 domains with 2 tenants self.create_domain(fixture=0, values={'tenant_id': 1}) self.create_domain(fixture=1, values={'tenant_id': 2}) + _, domain = self.create_domain(fixture=2, values={'tenant_id': 2}) + + # Delete one of the domains. + self.storage.delete_domain(self.admin_context, domain['id']) tenants = self.storage.count_tenants(self.admin_context) self.assertEqual(tenants, 2)