Standardized Service unit tests
A lot of our unit tests don't need to setup a proper RPC connection. This patch mocks the RPC path for all these tests and standardizes some of the tests. Change-Id: Ibdc1d495c8aa15c3e29293aabd2a9641bf12c897
This commit is contained in:
parent
b68451c076
commit
db5766e883
|
@ -14,6 +14,7 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from designateclient import exceptions
|
||||
|
@ -26,6 +27,7 @@ from designate import context
|
|||
from designate import objects
|
||||
from designate.tests import fixtures
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
|
|
@ -10,11 +10,11 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
from unittest import mock
|
||||
|
||||
import oslotest.base
|
||||
|
||||
|
||||
from designate.central import service
|
||||
from designate.common import profiler
|
||||
import designate.conf
|
||||
|
@ -24,16 +24,21 @@ from designate.objects import zone
|
|||
from designate import policy
|
||||
from designate import rpc
|
||||
|
||||
|
||||
CONF = designate.conf.CONF
|
||||
|
||||
|
||||
class CentralTestCase(oslotest.base.BaseTestCase):
|
||||
@mock.patch.object(policy, 'init')
|
||||
@mock.patch.object(rpc, 'init')
|
||||
@mock.patch.object(rpc, 'initialized')
|
||||
@mock.patch.object(profiler, 'setup_profiler')
|
||||
def setUp(self, mock_setup_profiler, mock_rpc_init, mock_policy_init):
|
||||
def setUp(self, mock_setup_profiler, mock_rpc_initialized,
|
||||
mock_rpc_init, mock_policy_init):
|
||||
super().setUp()
|
||||
|
||||
mock_rpc_initialized.return_value = False
|
||||
|
||||
self.storage = mock.Mock()
|
||||
|
||||
self.service = service.Service()
|
||||
|
@ -108,10 +113,14 @@ class CentralTestCase(oslotest.base.BaseTestCase):
|
|||
class ZoneAndRecordStatusTestCase(oslotest.base.BaseTestCase):
|
||||
@mock.patch.object(policy, 'init')
|
||||
@mock.patch.object(rpc, 'init')
|
||||
@mock.patch.object(rpc, 'initialized')
|
||||
@mock.patch.object(profiler, 'setup_profiler')
|
||||
def setUp(self, mock_setup_profiler, mock_rpc_init, mock_policy_init):
|
||||
def setUp(self, mock_setup_profiler, mock_rpc_initialized,
|
||||
mock_rpc_init, mock_policy_init):
|
||||
super().setUp()
|
||||
|
||||
mock_rpc_initialized.return_value = False
|
||||
|
||||
self.service = service.Service()
|
||||
|
||||
mock_setup_profiler.assert_called()
|
||||
|
|
|
@ -13,17 +13,19 @@
|
|||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
from unittest import mock
|
||||
|
||||
import dns
|
||||
from oslo_config import fixture as cfg_fixture
|
||||
from oslo_messaging import conffixture as messaging_fixture
|
||||
import oslotest.base
|
||||
|
||||
import designate.conf
|
||||
from designate import exceptions
|
||||
from designate.mdns import handler
|
||||
from designate import objects
|
||||
from designate import rpc
|
||||
from designate.tests import fixtures
|
||||
from designate.worker import rpcapi as worker_rpcapi
|
||||
|
||||
|
@ -37,16 +39,14 @@ class MdnsHandleTest(oslotest.base.BaseTestCase):
|
|||
self.stdlog = fixtures.StandardLogging()
|
||||
self.useFixture(self.stdlog)
|
||||
self.useFixture(cfg_fixture.Config(CONF))
|
||||
|
||||
self.context = mock.Mock()
|
||||
self.storage = mock.Mock()
|
||||
self.tg = mock.Mock()
|
||||
self.handler = handler.RequestHandler(self.storage, self.tg)
|
||||
self.messaging_conf = messaging_fixture.ConfFixture(CONF)
|
||||
self.messaging_conf.transport_url = 'fake:/'
|
||||
self.messaging_conf.response_timeout = 5
|
||||
self.useFixture(self.messaging_conf)
|
||||
self.useFixture(fixtures.RPCFixture(CONF))
|
||||
|
||||
self.handler = handler.RequestHandler(self.storage, self.tg)
|
||||
|
||||
@mock.patch.object(rpc, 'get_client', mock.Mock())
|
||||
def test_worker_api(self):
|
||||
self.assertIsNone(self.handler._worker_api)
|
||||
self.assertIsInstance(self.handler.worker_api,
|
||||
|
@ -55,6 +55,7 @@ class MdnsHandleTest(oslotest.base.BaseTestCase):
|
|||
self.assertIsInstance(self.handler.worker_api,
|
||||
worker_rpcapi.WorkerAPI)
|
||||
|
||||
@mock.patch.object(rpc, 'get_client', mock.Mock())
|
||||
@mock.patch.object(dns.resolver.Resolver, 'resolve')
|
||||
def test_notify(self, mock_query):
|
||||
self.storage.find_zone.return_value = objects.Zone(
|
||||
|
|
|
@ -13,15 +13,17 @@
|
|||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from oslo_config import fixture as cfg_fixture
|
||||
import oslotest.base
|
||||
|
||||
import designate.conf
|
||||
from designate import dnsmiddleware
|
||||
from designate.mdns import handler
|
||||
from designate.mdns import service
|
||||
from designate import policy
|
||||
from designate import rpc
|
||||
import designate.service
|
||||
from designate import storage
|
||||
from designate.tests import fixtures
|
||||
|
@ -33,40 +35,40 @@ CONF = designate.conf.CONF
|
|||
|
||||
class MdnsServiceTest(oslotest.base.BaseTestCase):
|
||||
@mock.patch.object(storage, 'get_storage', mock.Mock())
|
||||
def setUp(self):
|
||||
@mock.patch.object(policy, 'init')
|
||||
@mock.patch.object(rpc, 'init')
|
||||
@mock.patch.object(rpc, 'initialized')
|
||||
def setUp(self, mock_rpc_initialized, mock_rpc_init, mock_policy_init):
|
||||
super().setUp()
|
||||
self.stdlog = fixtures.StandardLogging()
|
||||
self.useFixture(self.stdlog)
|
||||
|
||||
conf = self.useFixture(cfg_fixture.Config(CONF))
|
||||
conf.conf([], project='designate')
|
||||
mock_rpc_initialized.return_value = False
|
||||
|
||||
self.service = service.Service()
|
||||
|
||||
mock_policy_init.assert_called_once()
|
||||
mock_rpc_initialized.assert_called_once()
|
||||
mock_rpc_init.assert_called_once()
|
||||
|
||||
@mock.patch.object(designate.service.DNSService, 'start')
|
||||
def test_service_start(self, mock_dns_start):
|
||||
self.service.start()
|
||||
|
||||
self.assertTrue(mock_dns_start.called)
|
||||
mock_dns_start.assert_called()
|
||||
|
||||
def test_service_stop(self):
|
||||
self.service.dns_service.stop = mock.Mock()
|
||||
|
||||
self.service.stop()
|
||||
|
||||
self.assertTrue(self.service.dns_service.stop.called)
|
||||
self.service.dns_service.stop.assert_called()
|
||||
|
||||
self.assertIn('Stopping mdns service', self.stdlog.logger.output)
|
||||
|
||||
def test_service_name(self):
|
||||
self.assertEqual('mdns', self.service.service_name)
|
||||
|
||||
@mock.patch.object(handler, 'RequestHandler')
|
||||
@mock.patch.object(designate.service.DNSService, 'start')
|
||||
@mock.patch.object(designate.utils, 'cache_result')
|
||||
def test_dns_application(self, mock_cache_result, mock_dns_start,
|
||||
mock_request_handler):
|
||||
|
||||
def test_dns_application(self):
|
||||
app = self.service.dns_application
|
||||
|
||||
self.assertIsInstance(app, dnsmiddleware.DNSMiddleware)
|
||||
|
|
|
@ -14,9 +14,6 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""
|
||||
Unit-test Producer service
|
||||
"""
|
||||
|
||||
from unittest import mock
|
||||
|
||||
|
@ -25,7 +22,9 @@ import oslotest.base
|
|||
|
||||
import designate.conf
|
||||
from designate import exceptions
|
||||
from designate import policy
|
||||
from designate.producer import service
|
||||
from designate import rpc
|
||||
import designate.service
|
||||
from designate.tests import fixtures
|
||||
|
||||
|
@ -34,24 +33,31 @@ CONF = designate.conf.CONF
|
|||
|
||||
|
||||
@mock.patch.object(service.rpcapi.CentralAPI, 'get_instance', mock.Mock())
|
||||
class ProducerTest(oslotest.base.BaseTestCase):
|
||||
def setUp(self):
|
||||
conf = self.useFixture(cfg_fixture.Config(CONF))
|
||||
conf.conf([], project='designate')
|
||||
|
||||
class ProducerServiceTest(oslotest.base.BaseTestCase):
|
||||
@mock.patch.object(policy, 'init')
|
||||
@mock.patch.object(rpc, 'get_client')
|
||||
@mock.patch.object(rpc, 'init')
|
||||
@mock.patch.object(rpc, 'initialized')
|
||||
def setUp(self, mock_rpc_initialized, mock_rpc_init, mock_rpc_get_client,
|
||||
mock_policy_init):
|
||||
super().setUp()
|
||||
|
||||
self.useFixture(cfg_fixture.Config(CONF))
|
||||
self.stdlog = fixtures.StandardLogging()
|
||||
self.useFixture(self.stdlog)
|
||||
|
||||
mock_rpc_initialized.return_value = False
|
||||
|
||||
self.tg = mock.Mock()
|
||||
self.service = service.Service()
|
||||
self.service.coordination = mock.Mock()
|
||||
self.service.rpc_server = mock.Mock()
|
||||
self.service.tg = self.tg
|
||||
self.service._storage = mock.Mock()
|
||||
self.service._quota = mock.Mock()
|
||||
self.service._quota.limit_check = mock.Mock()
|
||||
|
||||
mock_policy_init.assert_called_once()
|
||||
mock_rpc_initialized.assert_called_once()
|
||||
mock_rpc_init.assert_called_once()
|
||||
|
||||
@mock.patch.object(rpc, 'get_notifier', mock.Mock())
|
||||
@mock.patch.object(service.coordination, 'Partitioner')
|
||||
@mock.patch.object(designate.service.RPCService, 'start')
|
||||
def test_service_start(self, mock_rpc_start, mock_partitioner):
|
||||
|
@ -98,6 +104,11 @@ class ProducerTest(oslotest.base.BaseTestCase):
|
|||
def test_service_name(self):
|
||||
self.assertEqual('producer', self.service.service_name)
|
||||
|
||||
@mock.patch.object(policy, 'init', mock.Mock())
|
||||
@mock.patch.object(rpc, 'get_client', mock.Mock())
|
||||
@mock.patch.object(rpc, 'init', mock.Mock())
|
||||
@mock.patch.object(rpc, 'initialized', mock.Mock(return_value=False))
|
||||
@mock.patch.object(rpc, 'get_notifier', mock.Mock())
|
||||
def test_producer_rpc_topic(self):
|
||||
CONF.set_override('topic', 'test-topic', 'service:producer')
|
||||
|
||||
|
|
|
@ -9,6 +9,8 @@
|
|||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.mport threading
|
||||
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from oslo_config import fixture as cfg_fixture
|
||||
|
@ -16,6 +18,8 @@ import oslotest.base
|
|||
|
||||
import designate.conf
|
||||
from designate.notification_handler import fake
|
||||
from designate import policy
|
||||
from designate import rpc
|
||||
from designate.sink import service
|
||||
from designate.tests import fixtures
|
||||
from designate.tests import test_notification_handler
|
||||
|
@ -27,8 +31,16 @@ CONF = designate.conf.CONF
|
|||
class TestSinkNotification(oslotest.base.BaseTestCase,
|
||||
test_notification_handler.NotificationHandlerMixin):
|
||||
|
||||
def setUp(self):
|
||||
@mock.patch.object(policy, 'init')
|
||||
@mock.patch.object(rpc, 'get_client')
|
||||
@mock.patch.object(rpc, 'init')
|
||||
@mock.patch.object(rpc, 'initialized')
|
||||
def setUp(self, mock_rpc_initialized, mock_rpc_init, mock_rpc_get_client,
|
||||
mock_policy_init):
|
||||
super().setUp()
|
||||
|
||||
mock_rpc_initialized.return_value = False
|
||||
|
||||
self.stdlog = fixtures.StandardLogging()
|
||||
self.useFixture(cfg_fixture.Config(CONF))
|
||||
self.useFixture(self.stdlog)
|
||||
|
@ -42,11 +54,14 @@ class TestSinkNotification(oslotest.base.BaseTestCase,
|
|||
'allowed_event_types', ['compute.instance.create.end'],
|
||||
'handler:fake'
|
||||
)
|
||||
CONF([], project='designate')
|
||||
|
||||
self.context = mock.Mock()
|
||||
self.service = service.Service()
|
||||
|
||||
mock_policy_init.assert_called_once()
|
||||
mock_rpc_initialized.assert_called_once()
|
||||
mock_rpc_init.assert_called_once()
|
||||
|
||||
def test_notification(self):
|
||||
event_type = 'compute.instance.create.end'
|
||||
fixture = self.get_notification_fixture('nova', event_type)
|
||||
|
@ -71,8 +86,13 @@ class TestSinkNotification(oslotest.base.BaseTestCase,
|
|||
self.stdlog.logger.output
|
||||
)
|
||||
|
||||
@mock.patch.object(policy, 'init', mock.Mock())
|
||||
@mock.patch.object(rpc, 'get_client', mock.Mock())
|
||||
@mock.patch.object(rpc, 'init', mock.Mock())
|
||||
@mock.patch.object(rpc, 'initialized', mock.Mock(return_value=False))
|
||||
def test_notification_without_handler(self):
|
||||
CONF.set_override('enabled_notification_handlers', [], 'service:sink')
|
||||
|
||||
self.service = service.Service()
|
||||
|
||||
event_type = 'compute.instance.create.end'
|
||||
|
|
|
@ -9,6 +9,8 @@
|
|||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
import errno
|
||||
import socket
|
||||
from unittest import mock
|
||||
|
@ -23,16 +25,20 @@ from designate.mdns import handler
|
|||
from designate import policy
|
||||
from designate import rpc
|
||||
from designate import service as designate_service
|
||||
from designate.tests import fixtures
|
||||
from designate import utils
|
||||
|
||||
|
||||
CONF = designate.conf.CONF
|
||||
|
||||
|
||||
class TestBaseService(oslotest.base.BaseTestCase):
|
||||
def tearDown(self):
|
||||
designate_service._launcher = None
|
||||
super().tearDown()
|
||||
@mock.patch.object(designate_service, '_launcher', None)
|
||||
class BaseServiceTest(oslotest.base.BaseTestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
||||
self.stdlog = fixtures.StandardLogging()
|
||||
self.useFixture(self.stdlog)
|
||||
|
||||
@mock.patch.object(service, 'launch')
|
||||
def test_serve(self, mock_service_launch):
|
||||
|
@ -43,8 +49,8 @@ class TestBaseService(oslotest.base.BaseTestCase):
|
|||
mock.ANY, server, workers=None, restart_method='mutate'
|
||||
)
|
||||
|
||||
@mock.patch.object(service, 'launch')
|
||||
def test_serve_twice(self, _):
|
||||
@mock.patch.object(service, 'launch', mock.Mock())
|
||||
def test_serve_twice(self):
|
||||
server = mock.Mock()
|
||||
designate_service.serve(server)
|
||||
self.assertRaisesRegex(
|
||||
|
@ -80,6 +86,11 @@ class TestBaseService(oslotest.base.BaseTestCase):
|
|||
|
||||
mock_rpc_cleanup.assert_called()
|
||||
|
||||
self.assertIn(
|
||||
'Caught KeyboardInterrupt, shutting down now',
|
||||
self.stdlog.logger.output
|
||||
)
|
||||
|
||||
|
||||
@mock.patch.object(policy, 'init')
|
||||
@mock.patch.object(rpc, 'init')
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.mport threading
|
||||
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from oslo_config import fixture as cfg_fixture
|
||||
|
@ -24,6 +26,8 @@ from designate.central import rpcapi as central_rpcapi
|
|||
import designate.conf
|
||||
from designate import exceptions
|
||||
from designate import objects
|
||||
from designate import policy
|
||||
from designate import rpc
|
||||
import designate.service
|
||||
from designate import storage
|
||||
import designate.tests
|
||||
|
@ -35,17 +39,29 @@ from designate.worker import service
|
|||
CONF = designate.conf.CONF
|
||||
|
||||
|
||||
class TestService(oslotest.base.BaseTestCase):
|
||||
def setUp(self):
|
||||
class WorkerServiceTest(oslotest.base.BaseTestCase):
|
||||
@mock.patch.object(policy, 'init')
|
||||
@mock.patch.object(rpc, 'get_client')
|
||||
@mock.patch.object(rpc, 'init')
|
||||
@mock.patch.object(rpc, 'initialized')
|
||||
def setUp(self, mock_rpc_initialized, mock_rpc_init, mock_rpc_get_client,
|
||||
mock_policy_init):
|
||||
super().setUp()
|
||||
|
||||
mock_rpc_initialized.return_value = False
|
||||
|
||||
self.stdlog = fixtures.StandardLogging()
|
||||
self.useFixture(self.stdlog)
|
||||
conf = self.useFixture(cfg_fixture.Config(CONF))
|
||||
conf.conf([], project='designate')
|
||||
self.useFixture(cfg_fixture.Config(CONF))
|
||||
|
||||
self.context = mock.Mock()
|
||||
self.zone = mock.Mock()
|
||||
self.service = service.Service()
|
||||
|
||||
mock_policy_init.assert_called_once()
|
||||
mock_rpc_initialized.assert_called_once()
|
||||
mock_rpc_init.assert_called_once()
|
||||
|
||||
@mock.patch.object(designate.service.RPCService, 'start')
|
||||
def test_service_start(self, mock_rpc_start):
|
||||
self.service.start()
|
||||
|
@ -61,6 +77,10 @@ class TestService(oslotest.base.BaseTestCase):
|
|||
def test_service_name(self):
|
||||
self.assertEqual('worker', self.service.service_name)
|
||||
|
||||
@mock.patch.object(policy, 'init', mock.Mock())
|
||||
@mock.patch.object(rpc, 'get_client', mock.Mock())
|
||||
@mock.patch.object(rpc, 'init', mock.Mock())
|
||||
@mock.patch.object(rpc, 'initialized', mock.Mock(return_value=False))
|
||||
def test_worker_rpc_topic(self):
|
||||
CONF.set_override('topic', 'test-topic', 'service:worker')
|
||||
|
||||
|
@ -69,6 +89,7 @@ class TestService(oslotest.base.BaseTestCase):
|
|||
self.assertEqual('test-topic', self.service.rpc_topic)
|
||||
self.assertEqual('worker', self.service.service_name)
|
||||
|
||||
@mock.patch.object(rpc, 'get_client', mock.Mock())
|
||||
def test_central_api(self):
|
||||
self.assertIsNone(self.service._central_api)
|
||||
self.assertIsInstance(self.service.central_api,
|
||||
|
|
Loading…
Reference in New Issue