ironic-inspector/ironic_inspector/test/unit/test_locking.py
Kaifeng Wang 293b0c7c15 Split API and conductor services
This patch splits API and conductor services for ironic-inspector.
Previous patch utilized lock from tooz coordinator, this patch adds
a coordinator wrapper for easier usage and further introduces group
interfaces.

Each conductor service will join a predefined group to mark it's
availability, on each request, API service will query members from
the group and randomly choose on of them, create desiginated topic
and deliver request to it.

The feature is tested with the memcached, file backend of tooz.
Other backends are not fully tested but may work as well, please
refer to tooz documentation for driver compatibilities[1].

[1] https://docs.openstack.org/tooz/latest/user/compatibility.html

Story: 2001842
Task: 30376

Change-Id: I419176cd6d44d74c066db275ef008fe8bb6ef37a
2019-08-12 15:29:55 +08:00

110 lines
3.5 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT 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 mock
from oslo_config import cfg
from ironic_inspector.common import coordination
from ironic_inspector.common import locking
from ironic_inspector.test import base as test_base
CONF = cfg.CONF
@mock.patch.object(locking, 'InternalLock', autospec=True)
@mock.patch.object(locking, 'ToozLock', autospec=True)
class TestGetLock(test_base.NodeTest):
def test_get_lock_internal(self, mock_tooz, mock_internal):
locking.get_lock(self.node.uuid)
mock_internal.assert_called_once_with(self.node.uuid)
mock_tooz.assert_not_called()
@mock.patch.object(coordination, 'get_coordinator', autospec=True)
def test_get_lock_tooz(self, mock_get_coord, mock_tooz, mock_internal):
CONF.set_override('standalone', False)
mock_lock = mock.Mock()
coordinator = mock.Mock()
coordinator.get_lock.return_value = mock_lock
mock_get_coord.return_value = coordinator
locking.get_lock(self.node.uuid)
mock_tooz.assert_called_once_with(mock_lock)
mock_internal.assert_not_called()
class TestInternalLock(test_base.NodeTest):
def setUp(self):
super(TestInternalLock, self).setUp()
self.mock_lock = mock.MagicMock()
self.mock_lock.acquire.return_value = True
@mock.patch.object(locking, 'lockutils', autospec=True)
def test_init_lock(self, mock_lockutils):
locking.InternalLock(self.node.uuid)
mock_lockutils.internal_lock.assert_called_with(
'node-%s' % self.node.uuid, mock.ANY)
def test_acquire(self):
lock = locking.InternalLock(self.node.uuid)
lock._lock = self.mock_lock
lock.acquire()
self.mock_lock.acquire.assert_called_once_with(blocking=True)
def test_release(self):
lock = locking.ToozLock(self.mock_lock)
self.mock_lock._locked = True
lock.release()
self.mock_lock.release.assert_called_once_with()
def test_context(self):
lock = locking.InternalLock(self.node.uuid)
lock._lock = self.mock_lock
with lock:
self.mock_lock.acquire.assert_called_once_with()
self.mock_lock.release.assert_called_once_with()
class TestToozLock(test_base.NodeTest):
def setUp(self):
super(TestToozLock, self).setUp()
self.mock_lock = mock.MagicMock()
self.mock_lock.acquire.return_value = True
self.mock_lock.acquired = False
def test_acquire(self):
lock = locking.ToozLock(self.mock_lock)
lock.acquire()
self.mock_lock.acquire.assert_called_once_with(blocking=True)
def test_release(self):
self.mock_lock.acquired = True
lock = locking.ToozLock(self.mock_lock)
lock.release()
self.mock_lock.release.assert_called_once_with()
def test_context(self):
lock = locking.ToozLock(self.mock_lock)
with lock:
self.mock_lock.acquire.assert_called_once_with()
self.mock_lock.release.assert_called_once_with()