Mock monascastatsd in tests

Add mixin class to mock monascastatsd to avoid error about connection
to the daemons. Add new 2 classes which inherits from mixin class
and oslotest, falcon depending on the test.

Change-Id: Ib44357d9355400f603f76bf2a99fc89c05eae831
This commit is contained in:
Artur Basiak 2017-06-22 07:56:44 +02:00
parent 07fe9de96c
commit 35e67f300a
13 changed files with 45 additions and 39 deletions

View File

@ -13,8 +13,11 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import falcon import falcon
from falcon import testing
import mock
from oslo_config import fixture as oo_cfg from oslo_config import fixture as oo_cfg
from oslo_context import fixture as oo_ctx from oslo_context import fixture as oo_ctx
from oslotest import base as os_test
from monasca_log_api.api.core import request from monasca_log_api.api.core import request
@ -27,6 +30,21 @@ def mock_context(test):
return test.useFixture(oo_ctx.ClearRequestContext()) return test.useFixture(oo_ctx.ClearRequestContext())
class DisableStatsdMixin(object):
def setUp(self):
super(DisableStatsdMixin, self).setUp()
self.statsd_patch = mock.patch('monascastatsd.Connection')
self.statsd_check = self.statsd_patch.start()
class BaseTestCase(DisableStatsdMixin, os_test.BaseTestCase):
pass
class TestBase(DisableStatsdMixin, testing.TestBase):
pass
class MockedAPI(falcon.API): class MockedAPI(falcon.API):
"""MockedAPI """MockedAPI

View File

@ -13,7 +13,6 @@
# under the License. # under the License.
import falcon import falcon
from falcon import testing
import mock import mock
import simplejson as json import simplejson as json
@ -24,7 +23,7 @@ from monasca_log_api.tests import base
ENDPOINT = '/healthcheck' ENDPOINT = '/healthcheck'
class TestHealthChecks(testing.TestBase): class TestHealthChecks(base.TestBase):
def before(self): def before(self):
self.conf = base.mock_config(self) self.conf = base.mock_config(self)
self.resource = healthchecks.HealthChecks() self.resource = healthchecks.HealthChecks()

View File

@ -13,7 +13,6 @@
# under the License. # under the License.
import mock import mock
from oslotest import base as os_test
from monasca_common.kafka_lib import client from monasca_common.kafka_lib import client
@ -21,7 +20,7 @@ from monasca_log_api.healthcheck import kafka_check as kc
from monasca_log_api.tests import base from monasca_log_api.tests import base
class KafkaCheckLogicTest(os_test.BaseTestCase): class KafkaCheckLogicTest(base.BaseTestCase):
mock_kafka_url = 'localhost:1234' mock_kafka_url = 'localhost:1234'
mocked_topics = ['test_1', 'test_2'] mocked_topics = ['test_1', 'test_2']

View File

@ -21,7 +21,6 @@ import ujson
import unittest import unittest
import mock import mock
from oslotest import base as os_test
from monasca_log_api.reference.common import log_publisher from monasca_log_api.reference.common import log_publisher
from monasca_log_api.reference.common import model from monasca_log_api.reference.common import model
@ -40,7 +39,7 @@ def _generate_unique_message(size):
return rand(size) return rand(size)
class TestSendMessage(os_test.BaseTestCase): class TestSendMessage(base.BaseTestCase):
def setUp(self): def setUp(self):
self.conf = base.mock_config(self) self.conf = base.mock_config(self)
@ -193,7 +192,7 @@ class TestSendMessage(os_test.BaseTestCase):
@mock.patch( @mock.patch(
'monasca_log_api.reference.common.log_publisher.producer' 'monasca_log_api.reference.common.log_publisher.producer'
'.KafkaProducer') '.KafkaProducer')
class TestTruncation(os_test.BaseTestCase): class TestTruncation(base.BaseTestCase):
EXTRA_CHARS_SIZE = len(bytearray(ujson.dumps({ EXTRA_CHARS_SIZE = len(bytearray(ujson.dumps({
'log': { 'log': {
'message': None 'message': None

View File

@ -14,9 +14,7 @@
# under the License. # under the License.
import falcon import falcon
from falcon import testing
import mock import mock
import unittest
from monasca_log_api.api import exceptions as log_api_exceptions from monasca_log_api.api import exceptions as log_api_exceptions
from monasca_log_api.api import headers from monasca_log_api.api import headers
@ -32,7 +30,7 @@ def _init_resource(test):
return resource return resource
class TestLogsVersion(unittest.TestCase): class TestLogsVersion(base.TestBase):
@mock.patch('monasca_log_api.reference.common.log_publisher.LogPublisher') @mock.patch('monasca_log_api.reference.common.log_publisher.LogPublisher')
@mock.patch('monasca_log_api.reference.v2.common.service.LogCreator') @mock.patch('monasca_log_api.reference.v2.common.service.LogCreator')
def test_should_return_v2_as_version(self, _, __): def test_should_return_v2_as_version(self, _, __):
@ -40,7 +38,7 @@ class TestLogsVersion(unittest.TestCase):
self.assertEqual('v2.0', logs_resource.version) self.assertEqual('v2.0', logs_resource.version)
class TestLogs(testing.TestBase): class TestLogs(base.TestBase):
api_class = base.MockedAPI api_class = base.MockedAPI

View File

@ -14,10 +14,9 @@
import random import random
import string import string
import unittest
import falcon import falcon
from falcon import testing
import mock import mock
import ujson as json import ujson as json
@ -66,7 +65,7 @@ def _generate_v3_payload(log_count):
return v3_body, v3_logs return v3_body, v3_logs
class TestLogsVersion(unittest.TestCase): class TestLogsVersion(base.TestBase):
@mock.patch('monasca_log_api.reference.v3.common.' @mock.patch('monasca_log_api.reference.v3.common.'
'bulk_processor.BulkProcessor') 'bulk_processor.BulkProcessor')
@ -78,7 +77,7 @@ class TestLogsVersion(unittest.TestCase):
@mock.patch('monasca_log_api.reference.common.log_publisher.producer.' @mock.patch('monasca_log_api.reference.common.log_publisher.producer.'
'KafkaProducer') 'KafkaProducer')
@mock.patch('monasca_log_api.monitoring.client.monascastatsd.Connection') @mock.patch('monasca_log_api.monitoring.client.monascastatsd.Connection')
class TestLogsMonitoring(testing.TestBase): class TestLogsMonitoring(base.TestBase):
api_class = base.MockedAPI api_class = base.MockedAPI
@ -207,7 +206,7 @@ class TestLogsMonitoring(testing.TestBase):
size_gauge.mock_calls[0][2]['value']) size_gauge.mock_calls[0][2]['value'])
class TestLogs(testing.TestBase): class TestLogs(base.TestBase):
api_class = base.MockedAPI api_class = base.MockedAPI

View File

@ -14,9 +14,8 @@
import mock import mock
from oslotest import base
from monasca_log_api.monitoring import client from monasca_log_api.monitoring import client
from monasca_log_api.tests import base
class TestMonitoring(base.BaseTestCase): class TestMonitoring(base.BaseTestCase):

View File

@ -14,14 +14,13 @@
from falcon import testing from falcon import testing
from mock import mock from mock import mock
from oslotest import base as os_test
from monasca_log_api.api.core import request from monasca_log_api.api.core import request
from monasca_log_api.reference.common import validation from monasca_log_api.reference.common import validation
from monasca_log_api.tests import base from monasca_log_api.tests import base
class TestRequest(os_test.BaseTestCase): class TestRequest(base.BaseTestCase):
def setUp(self): def setUp(self):
super(TestRequest, self).setUp() super(TestRequest, self).setUp()

View File

@ -15,9 +15,8 @@
import mock import mock
from webob import response from webob import response
from oslotest import base
from monasca_log_api.middleware import role_middleware as rm from monasca_log_api.middleware import role_middleware as rm
from monasca_log_api.tests import base
class SideLogicTestEnsureLowerRoles(base.BaseTestCase): class SideLogicTestEnsureLowerRoles(base.BaseTestCase):

View File

@ -19,7 +19,6 @@ import unittest
from falcon import errors from falcon import errors
from falcon import testing from falcon import testing
import mock import mock
from oslotest import base as os_test
from monasca_log_api.api import exceptions from monasca_log_api.api import exceptions
from monasca_log_api.reference.common import validation from monasca_log_api.reference.common import validation
@ -27,7 +26,7 @@ from monasca_log_api.reference.v2.common import service as common_service
from monasca_log_api.tests import base from monasca_log_api.tests import base
class IsDelegate(os_test.BaseTestCase): class IsDelegate(base.BaseTestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(IsDelegate, self).__init__(*args, **kwargs) super(IsDelegate, self).__init__(*args, **kwargs)
@ -50,7 +49,7 @@ class IsDelegate(os_test.BaseTestCase):
self.assertFalse(validation.validate_is_delegate(roles)) self.assertFalse(validation.validate_is_delegate(roles))
class ParseDimensions(os_test.BaseTestCase): class ParseDimensions(base.BaseTestCase):
def test_should_fail_for_empty_dimensions(self): def test_should_fail_for_empty_dimensions(self):
self.assertRaises(exceptions.HTTPUnprocessableEntity, self.assertRaises(exceptions.HTTPUnprocessableEntity,
common_service.parse_dimensions, common_service.parse_dimensions,
@ -84,7 +83,7 @@ class ParseDimensions(os_test.BaseTestCase):
common_service.parse_dimensions(dimensions)) common_service.parse_dimensions(dimensions))
class ParseApplicationType(os_test.BaseTestCase): class ParseApplicationType(base.BaseTestCase):
def test_should_return_none_for_none(self): def test_should_return_none_for_none(self):
self.assertIsNone(common_service.parse_application_type(None)) self.assertIsNone(common_service.parse_application_type(None))
@ -106,7 +105,7 @@ class ParseApplicationType(os_test.BaseTestCase):
common_service.parse_application_type(app_type)) common_service.parse_application_type(app_type))
class ApplicationTypeValidations(os_test.BaseTestCase): class ApplicationTypeValidations(base.BaseTestCase):
def test_should_pass_for_empty_app_type(self): def test_should_pass_for_empty_app_type(self):
validation.validate_application_type() validation.validate_application_type()
validation.validate_application_type('') validation.validate_application_type('')
@ -139,7 +138,7 @@ class ApplicationTypeValidations(os_test.BaseTestCase):
validation.validate_application_type(r_app_type) validation.validate_application_type(r_app_type)
class DimensionsValidations(os_test.BaseTestCase): class DimensionsValidations(base.BaseTestCase):
@unittest.expectedFailure @unittest.expectedFailure
def test_should_fail_for_none_dimensions(self): def test_should_fail_for_none_dimensions(self):
validation.validate_dimensions(None) validation.validate_dimensions(None)
@ -230,7 +229,7 @@ class DimensionsValidations(os_test.BaseTestCase):
validation.validate_dimensions(dimensions) validation.validate_dimensions(dimensions)
class ContentTypeValidations(os_test.BaseTestCase): class ContentTypeValidations(base.BaseTestCase):
def test_should_pass_text_plain(self): def test_should_pass_text_plain(self):
content_type = 'text/plain' content_type = 'text/plain'
allowed_types = ['text/plain'] allowed_types = ['text/plain']
@ -277,7 +276,7 @@ class ContentTypeValidations(os_test.BaseTestCase):
) )
class PayloadSizeValidations(os_test.BaseTestCase): class PayloadSizeValidations(base.BaseTestCase):
def setUp(self): def setUp(self):
super(PayloadSizeValidations, self).setUp() super(PayloadSizeValidations, self).setUp()
self.conf = base.mock_config(self) self.conf = base.mock_config(self)
@ -334,7 +333,7 @@ class PayloadSizeValidations(os_test.BaseTestCase):
) )
class LogMessageValidations(os_test.BaseTestCase): class LogMessageValidations(base.BaseTestCase):
def test_should_pass_message_in_log_property(self): def test_should_pass_message_in_log_property(self):
log_object = { log_object = {
'message': 'some messages', 'message': 'some messages',
@ -361,7 +360,7 @@ class LogMessageValidations(os_test.BaseTestCase):
validation.validate_log_message, {}) validation.validate_log_message, {})
class LogsCreatorNewLog(os_test.BaseTestCase): class LogsCreatorNewLog(base.BaseTestCase):
def setUp(self): def setUp(self):
super(LogsCreatorNewLog, self).setUp() super(LogsCreatorNewLog, self).setUp()
self.instance = common_service.LogCreator() self.instance = common_service.LogCreator()
@ -415,7 +414,7 @@ class LogsCreatorNewLog(os_test.BaseTestCase):
)) ))
class LogCreatorNewEnvelope(os_test.BaseTestCase): class LogCreatorNewEnvelope(base.BaseTestCase):
def setUp(self): def setUp(self):
super(LogCreatorNewEnvelope, self).setUp() super(LogCreatorNewEnvelope, self).setUp()
self.instance = common_service.LogCreator() self.instance = common_service.LogCreator()

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from falcon import testing
import mock import mock
import ujson as json import ujson as json
@ -22,7 +21,7 @@ from monasca_log_api.reference.v3 import logs as v3_logs
from monasca_log_api.tests import base from monasca_log_api.tests import base
class SameV2V3Output(testing.TestBase): class SameV2V3Output(base.TestBase):
api_class = base.MockedAPI api_class = base.MockedAPI

View File

@ -12,8 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from oslotest import base from monasca_log_api.tests import base
from monasca_log_api import version from monasca_log_api import version

View File

@ -13,17 +13,17 @@
# under the License. # under the License.
import falcon import falcon
from falcon import testing
import ujson as json import ujson as json
from monasca_log_api.reference import versions from monasca_log_api.reference import versions
from monasca_log_api.tests import base
def _get_versioned_url(version_id): def _get_versioned_url(version_id):
return '/version/%s' % version_id return '/version/%s' % version_id
class TestVersions(testing.TestBase): class TestVersions(base.TestBase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.versions = None self.versions = None
super(TestVersions, self).__init__(*args, **kwargs) super(TestVersions, self).__init__(*args, **kwargs)