Coverage for manila/tests/test_coordination.py: 98%
76 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright 2015 Intel
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16from unittest import mock
18import ddt
19from tooz import coordination as tooz_coordination
20from tooz import locking as tooz_locking
22from manila import coordination
23from manila import test
26class Locked(Exception):
27 pass
30class MockToozLock(tooz_locking.Lock):
31 active_locks = set()
33 def acquire(self, blocking=True):
34 if self.name not in self.active_locks:
35 self.active_locks.add(self.name)
36 return True
37 elif not blocking: 37 ↛ 38line 37 didn't jump to line 38 because the condition on line 37 was never true
38 return False
39 else:
40 raise Locked
42 def release(self):
43 self.active_locks.remove(self.name)
46@ddt.ddt
47class CoordinatorTestCase(test.TestCase):
49 def setUp(self):
50 super(CoordinatorTestCase, self).setUp()
51 self.get_coordinator = self.mock_object(tooz_coordination,
52 'get_coordinator')
54 def test_coordinator_start(self):
55 crd = self.get_coordinator.return_value
57 agent = coordination.Coordinator()
58 agent.start()
60 self.assertTrue(self.get_coordinator.called)
61 self.assertTrue(crd.start.called)
62 self.assertTrue(agent.started)
64 def test_coordinator_stop(self):
65 crd = self.get_coordinator.return_value
67 agent = coordination.Coordinator()
68 agent.start()
70 self.assertIsNotNone(agent.coordinator)
71 agent.stop()
73 self.assertTrue(crd.stop.called)
74 self.assertIsNone(agent.coordinator)
75 self.assertFalse(agent.started)
77 def test_coordinator_lock(self):
78 crd = self.get_coordinator.return_value
79 crd.get_lock.side_effect = lambda n: MockToozLock(n)
81 agent1 = coordination.Coordinator()
82 agent1.start()
83 agent2 = coordination.Coordinator()
84 agent2.start()
86 lock_string = 'lock'
87 expected_lock = lock_string.encode('ascii')
89 self.assertNotIn(expected_lock, MockToozLock.active_locks)
90 with agent1.get_lock(lock_string):
91 self.assertIn(expected_lock, MockToozLock.active_locks)
92 self.assertRaises(Locked, agent1.get_lock(lock_string).acquire)
93 self.assertRaises(Locked, agent2.get_lock(lock_string).acquire)
94 self.assertNotIn(expected_lock, MockToozLock.active_locks)
96 def test_coordinator_offline(self):
97 crd = self.get_coordinator.return_value
98 crd.start.side_effect = tooz_coordination.ToozConnectionError('err')
100 agent = coordination.Coordinator()
101 self.assertRaises(tooz_coordination.ToozError, agent.start)
102 self.assertFalse(agent.started)
105@mock.patch.object(coordination.LOCK_COORDINATOR, 'get_lock')
106class CoordinationTestCase(test.TestCase):
107 def test_lock(self, get_lock):
108 with coordination.Lock('lock'):
109 self.assertTrue(get_lock.called)
111 def test_synchronized(self, get_lock):
112 @coordination.synchronized('lock-{f_name}-{foo.val}-{bar[val]}')
113 def func(foo, bar):
114 pass
116 foo = mock.Mock()
117 foo.val = 7
118 bar = mock.MagicMock()
119 bar.__getitem__.return_value = 8
120 func(foo, bar)
121 get_lock.assert_called_with('lock-func-7-8')