Add validation of log property

Log property should have at least following forms
"message", "application_type" and "dimensions".
"application_type" and "dimensions" will be always set.
But "message" will not be always set.
So this validation checks log property has "message".

Change-Id: I9f7cfaf11906724a9cf555b77c61a690f44ddaaa
This commit is contained in:
Shinya Kawabata 2016-03-08 15:31:51 +09:00
parent c4557c1a0f
commit 73c7077177
3 changed files with 41 additions and 1 deletions

View File

@ -16,7 +16,7 @@ Create log.
* X-Auth-Token (string, required) - Keystone auth token
* Content-Type (string, required) - application/json; text/plain
* X-Application-Type (string(255), optional) - Type of application
* X-Dimensions ({string(255):string(255)}, optional) - A dictionary consisting of (key, value) pairs used to structure logs.
* X-Dimensions ({string(255):string(255)}, required) - A dictionary consisting of (key, value) pairs used to structure logs.
#### Path Parameters
None.

View File

@ -350,6 +350,30 @@ class EnvelopeSizeValidations(testing.TestBase):
)
class LogMessageValidations(testing.TestBase):
def test_should_pass_message_in_log_property(self):
log_object = {
'message': 'some messages',
'application_type': 'monasca-log-api',
'dimensions': {
'hostname': 'devstack'
}
}
common_service.Validations.validate_log_message(log_object)
@unittest.expectedFailure
def test_should_fail_pass_for_non_message_in_log_property(self):
log_object = {
'massage': 'some messages',
'application_type': 'monasca-log-api',
'dimensions': {
'hostname': 'devstack'
}
}
common_service.Validations.validate_log_message(log_object)
class LogsCreatorNewLog(unittest.TestCase):
def setUp(self):
self.instance = common_service.LogCreator()

View File

@ -262,6 +262,20 @@ class Validations(object):
description=u'Maximum allowed size is %d bytes' % max_size
)
@staticmethod
def validate_log_message(log_object):
"""Validates log property.
Log property should have message property.
Args:
log_object (dict): log property
"""
if 'message' not in log_object:
raise exceptions.HTTPUnprocessableEntity(
'Log property should have message'
)
class LogCreator(object):
"""Transforms logs,
@ -343,6 +357,8 @@ class LogCreator(object):
else:
log_object.update({'message': payload})
Validations.validate_log_message(log_object)
log_object.update({
'application_type': application_type,
'dimensions': dimensions