From d9e546368029e57f6ad1f522485151d12198d3c5 Mon Sep 17 00:00:00 2001 From: Feilong Wang Date: Tue, 24 Apr 2018 10:17:51 +1200 Subject: [PATCH] Support ignore project in /health API Currently /health API doesn't support the ignore_tenants config option, which is not consistent with the collector behaviour. This patch adds the support and related unit test. Change-Id: I72e5ed35bd50e629d06c6fe53715d056cc024241 --- distil/service/api/v2/health.py | 3 +- .../tests/unit/service/api/v2/test_health.py | 38 ++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/distil/service/api/v2/health.py b/distil/service/api/v2/health.py index f3620cd..5475ee6 100644 --- a/distil/service/api/v2/health.py +++ b/distil/service/api/v2/health.py @@ -44,7 +44,8 @@ def get_health(): result = {} projects_keystone = openstack.get_projects() - keystone_projects = [t['id'] for t in projects_keystone] + keystone_projects = [t['id'] for t in projects_keystone + if t['name'] not in CONF.collector.ignore_tenants] threshold = datetime.utcnow() - timedelta(days=1) diff --git a/distil/tests/unit/service/api/v2/test_health.py b/distil/tests/unit/service/api/v2/test_health.py index b62619d..2b165a6 100644 --- a/distil/tests/unit/service/api/v2/test_health.py +++ b/distil/tests/unit/service/api/v2/test_health.py @@ -93,8 +93,8 @@ class HealthTest(base.DistilWithDbTestCase): @mock.patch('odoorpc.ODOO') @mock.patch('distil.common.openstack.get_projects') - def test_get_health_with_erp_abackend_fail(self, mock_get_projects, - mock_odoo): + def test_get_health_with_erp_backend_fail(self, mock_get_projects, + mock_odoo): new = mock.MagicMock() new.db.list.side_effect = Exception('Boom!') mock_odoo.return_value = new @@ -109,3 +109,37 @@ class HealthTest(base.DistilWithDbTestCase): ret = health.get_health() self.assertEqual('OK', ret['erp_backend'].get('status')) + + @mock.patch('distil.common.openstack.get_projects') + def test_get_health_with_ignore_tenants(self, mock_get_projects): + self.override_config('collector', ignore_tenants=['project_2']) + mock_get_projects.return_value = [ + {'id': '111', 'name': 'project_1', 'description': ''}, + {'id': '222', 'name': 'project_2', 'description': ''}, + ] + + # Insert projects in the database. + project_1_collect = datetime.utcnow() - timedelta(days=2) + db_api.project_add( + { + 'id': '111', + 'name': 'project_1', + 'description': '', + }, + project_1_collect + ) + project_2_collect = datetime.utcnow() - timedelta(hours=25) + db_api.project_add( + { + 'id': '222', + 'name': 'project_2', + 'description': '', + }, + project_2_collect + ) + + ret = health.get_health() + + self.assertEqual('FAIL', ret['usage_collection'].get('status')) + self.assertIn('1', ret['usage_collection'].get('msg')) +