Merge "Support odoo in health check"
This commit is contained in:
commit
010ebf5aa9
|
@ -22,6 +22,13 @@ class BaseDriver(object):
|
|||
def __init__(self, conf):
|
||||
self.conf = conf
|
||||
|
||||
def is_healthy(self):
|
||||
"""Check if the ERP back end is healthy or not
|
||||
|
||||
:returns True if the ERP is healthy, otherwise False
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def get_products(self, regions=[]):
|
||||
"""List products based o given regions
|
||||
|
||||
|
|
|
@ -72,6 +72,14 @@ class OdooDriver(driver.BaseDriver):
|
|||
|
||||
self.product_catagory_mapping = {}
|
||||
|
||||
def is_healthy(self):
|
||||
try:
|
||||
self.odoo.db.list()
|
||||
return True
|
||||
except Exception as e:
|
||||
LOG.exception(e)
|
||||
return False
|
||||
|
||||
@cache.memoize
|
||||
def get_products(self, regions=[]):
|
||||
self.product_catagory_mapping.clear()
|
||||
|
|
|
@ -13,25 +13,33 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from datetime import datetime
|
||||
from datetime import timedelta
|
||||
|
||||
import enum
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
from distil.db import api as db_api
|
||||
from distil.erp import utils as erp_utils
|
||||
from distil.common import openstack
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
@enum.unique
|
||||
class Status(enum.IntEnum):
|
||||
"""Enum of status for a checking item."""
|
||||
OK = 1
|
||||
FAIL = 0
|
||||
|
||||
|
||||
def get_health():
|
||||
"""Get health status of distil.
|
||||
|
||||
Currently, we only check usage collection to achieve feature parity with
|
||||
current monitoring requirements.
|
||||
|
||||
In future, we could add running status for ERP system, etc.
|
||||
Check usage collection and connection to ERP.
|
||||
"""
|
||||
result = {}
|
||||
|
||||
|
@ -49,13 +57,25 @@ def get_health():
|
|||
|
||||
if failed_count == 0:
|
||||
result['usage_collection'] = {
|
||||
'status': 'OK',
|
||||
'status': Status.OK.name,
|
||||
'msg': 'Tenant usage successfully collected and up-to-date.'
|
||||
}
|
||||
else:
|
||||
result['usage_collection'] = {
|
||||
'status': 'FAIL',
|
||||
'status': Status.FAIL.name,
|
||||
'msg': 'Failed to collect usage for %s projects.' % failed_count
|
||||
}
|
||||
|
||||
try:
|
||||
erp_driver = erp_utils.load_erp_driver(CONF)
|
||||
if erp_driver.is_healthy():
|
||||
result['erp_backend'] = {"status": Status.OK.name,
|
||||
"msg": "ERP backend works."}
|
||||
return result
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
result['erp_backend'] = {"status": Status.FAIL.name,
|
||||
"msg": "ERP backend doesn't work."}
|
||||
|
||||
return result
|
||||
|
|
|
@ -368,3 +368,15 @@ class TestOdooDriver(base.DistilTestCase):
|
|||
"type": "Cloud Trial Credit",
|
||||
"start_date": "2017-02-14 02:12:40"}],
|
||||
credits)
|
||||
|
||||
@mock.patch('odoorpc.ODOO')
|
||||
def test_is_healthy(self, mock_odoo):
|
||||
odoodriver = odoo.OdooDriver(self.conf)
|
||||
odoodriver.odoo.db.list.return_value = ["A", "B"]
|
||||
self.assertTrue(odoodriver.is_healthy())
|
||||
|
||||
@mock.patch('odoorpc.ODOO')
|
||||
def test_is_healthy_false(self, mock_odoo):
|
||||
odoodriver = odoo.OdooDriver(self.conf)
|
||||
odoodriver.odoo.db.list.side_effect = Exception("Odoo Error!")
|
||||
self.assertFalse(odoodriver.is_healthy())
|
||||
|
|
|
@ -17,12 +17,17 @@ from datetime import timedelta
|
|||
|
||||
import mock
|
||||
|
||||
from distil.erp import utils as erp_utils
|
||||
from distil.db.sqlalchemy import api as db_api
|
||||
from distil.service.api.v2 import health
|
||||
from distil.tests.unit import base
|
||||
|
||||
|
||||
class HealthTest(base.DistilWithDbTestCase):
|
||||
def setUp(self):
|
||||
super(HealthTest, self).setUp()
|
||||
erp_utils._ERP_DRIVER = None
|
||||
|
||||
@mock.patch('distil.common.openstack.get_projects')
|
||||
def test_get_health_ok(self, mock_get_projects):
|
||||
mock_get_projects.return_value = [
|
||||
|
@ -85,3 +90,22 @@ class HealthTest(base.DistilWithDbTestCase):
|
|||
|
||||
self.assertEqual('FAIL', ret['usage_collection'].get('status'))
|
||||
self.assertIn('2', ret['usage_collection'].get('msg'))
|
||||
|
||||
@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):
|
||||
new = mock.MagicMock()
|
||||
new.db.list.side_effect = Exception('Boom!')
|
||||
mock_odoo.return_value = new
|
||||
# mock_odoo.side_effect = ValueError
|
||||
ret = health.get_health()
|
||||
|
||||
self.assertEqual('FAIL', ret['erp_backend'].get('status'))
|
||||
|
||||
@mock.patch('odoorpc.ODOO')
|
||||
@mock.patch('distil.common.openstack.get_projects')
|
||||
def test_get_health_with_erp_backend(self, mock_get_projects, mock_odoo):
|
||||
ret = health.get_health()
|
||||
|
||||
self.assertEqual('OK', ret['erp_backend'].get('status'))
|
||||
|
|
|
@ -12,6 +12,7 @@ SQLAlchemy<1.1.0,>=1.0.10 # MIT
|
|||
keystonemiddleware!=4.1.0,>=4.0.0 # Apache-2.0
|
||||
keystoneauth1>=2.1.0 # Apache-2.0
|
||||
retrying>=1.2.3,!=1.3.0 # Apache-2.0
|
||||
enum34;python_version=='2.7' or python_version=='2.6' or python_version=='3.3' # BSD
|
||||
|
||||
python-cinderclient>=1.6.0 # Apache-2.0
|
||||
python-keystoneclient!=1.8.0,!=2.1.0,>=1.6.0 # Apache-2.0
|
||||
|
|
Loading…
Reference in New Issue