Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.locking_unittest.py @ 3b7ed473

History | View | Annotate | Download (24.8 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2006, 2007 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 0.0510-1301, USA.
20

    
21

    
22
"""Script for unittesting the locking module"""
23

    
24

    
25
import os
26
import unittest
27
import time
28
import Queue
29

    
30
from ganeti import locking
31
from ganeti import errors
32
from threading import Thread
33

    
34

    
35
class TestSharedLock(unittest.TestCase):
36
  """SharedLock tests"""
37

    
38
  def setUp(self):
39
    self.sl = locking.SharedLock()
40
    # helper threads use the 'done' queue to tell the master they finished.
41
    self.done = Queue.Queue(0)
42

    
43
  def testSequenceAndOwnership(self):
44
    self.assert_(not self.sl._is_owned())
45
    self.sl.acquire(shared=1)
46
    self.assert_(self.sl._is_owned())
47
    self.assert_(self.sl._is_owned(shared=1))
48
    self.assert_(not self.sl._is_owned(shared=0))
49
    self.sl.release()
50
    self.assert_(not self.sl._is_owned())
51
    self.sl.acquire()
52
    self.assert_(self.sl._is_owned())
53
    self.assert_(not self.sl._is_owned(shared=1))
54
    self.assert_(self.sl._is_owned(shared=0))
55
    self.sl.release()
56
    self.assert_(not self.sl._is_owned())
57
    self.sl.acquire(shared=1)
58
    self.assert_(self.sl._is_owned())
59
    self.assert_(self.sl._is_owned(shared=1))
60
    self.assert_(not self.sl._is_owned(shared=0))
61
    self.sl.release()
62
    self.assert_(not self.sl._is_owned())
63

    
64
  def testBooleanValue(self):
65
    # semaphores are supposed to return a true value on a successful acquire
66
    self.assert_(self.sl.acquire(shared=1))
67
    self.sl.release()
68
    self.assert_(self.sl.acquire())
69
    self.sl.release()
70

    
71
  def testDoubleLockingStoE(self):
72
    self.sl.acquire(shared=1)
73
    self.assertRaises(AssertionError, self.sl.acquire)
74

    
75
  def testDoubleLockingEtoS(self):
76
    self.sl.acquire()
77
    self.assertRaises(AssertionError, self.sl.acquire, shared=1)
78

    
79
  def testDoubleLockingStoS(self):
80
    self.sl.acquire(shared=1)
81
    self.assertRaises(AssertionError, self.sl.acquire, shared=1)
82

    
83
  def testDoubleLockingEtoE(self):
84
    self.sl.acquire()
85
    self.assertRaises(AssertionError, self.sl.acquire)
86

    
87
  # helper functions: called in a separate thread they acquire the lock, send
88
  # their identifier on the done queue, then release it.
89
  def _doItSharer(self):
90
    try:
91
      self.sl.acquire(shared=1)
92
      self.done.put('SHR')
93
      self.sl.release()
94
    except errors.LockError:
95
      self.done.put('ERR')
96

    
97
  def _doItExclusive(self):
98
    try:
99
      self.sl.acquire()
100
      self.done.put('EXC')
101
      self.sl.release()
102
    except errors.LockError:
103
      self.done.put('ERR')
104

    
105
  def _doItDelete(self):
106
    try:
107
      self.sl.delete()
108
      self.done.put('DEL')
109
    except errors.LockError:
110
      self.done.put('ERR')
111

    
112
  def testSharersCanCoexist(self):
113
    self.sl.acquire(shared=1)
114
    Thread(target=self._doItSharer).start()
115
    self.assert_(self.done.get(True, 1))
116
    self.sl.release()
117

    
118
  def testExclusiveBlocksExclusive(self):
119
    self.sl.acquire()
120
    Thread(target=self._doItExclusive).start()
121
    # give it a bit of time to check that it's not actually doing anything
122
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
123
    self.sl.release()
124
    self.assert_(self.done.get(True, 1))
125

    
126
  def testExclusiveBlocksDelete(self):
127
    self.sl.acquire()
128
    Thread(target=self._doItDelete).start()
129
    # give it a bit of time to check that it's not actually doing anything
130
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
131
    self.sl.release()
132
    self.assert_(self.done.get(True, 1))
133

    
134
  def testExclusiveBlocksSharer(self):
135
    self.sl.acquire()
136
    Thread(target=self._doItSharer).start()
137
    time.sleep(0.05)
138
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
139
    self.sl.release()
140
    self.assert_(self.done.get(True, 1))
141

    
142
  def testSharerBlocksExclusive(self):
143
    self.sl.acquire(shared=1)
144
    Thread(target=self._doItExclusive).start()
145
    time.sleep(0.05)
146
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
147
    self.sl.release()
148
    self.assert_(self.done.get(True, 1))
149

    
150
  def testSharerBlocksDelete(self):
151
    self.sl.acquire(shared=1)
152
    Thread(target=self._doItDelete).start()
153
    time.sleep(0.05)
154
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
155
    self.sl.release()
156
    self.assert_(self.done.get(True, 1))
157

    
158
  def testWaitingExclusiveBlocksSharer(self):
159
    self.sl.acquire(shared=1)
160
    # the lock is acquired in shared mode...
161
    Thread(target=self._doItExclusive).start()
162
    # ...but now an exclusive is waiting...
163
    time.sleep(0.05)
164
    Thread(target=self._doItSharer).start()
165
    # ...so the sharer should be blocked as well
166
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
167
    self.sl.release()
168
    # The exclusive passed before
169
    self.assertEqual(self.done.get(True, 1), 'EXC')
170
    self.assertEqual(self.done.get(True, 1), 'SHR')
171

    
172
  def testWaitingSharerBlocksExclusive(self):
173
    self.sl.acquire()
174
    # the lock is acquired in exclusive mode...
175
    Thread(target=self._doItSharer).start()
176
    # ...but now a sharer is waiting...
177
    time.sleep(0.05)
178
    Thread(target=self._doItExclusive).start()
179
    # ...the exclusive is waiting too...
180
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
181
    self.sl.release()
182
    # The sharer passed before
183
    self.assertEqual(self.done.get(True, 1), 'SHR')
184
    self.assertEqual(self.done.get(True, 1), 'EXC')
185

    
186
  def testNoNonBlocking(self):
187
    self.assertRaises(NotImplementedError, self.sl.acquire, blocking=0)
188
    self.assertRaises(NotImplementedError, self.sl.delete, blocking=0)
189
    self.sl.acquire()
190
    self.sl.delete(blocking=0) # Fine, because the lock is already acquired
191

    
192
  def testDelete(self):
193
    self.sl.delete()
194
    self.assertRaises(errors.LockError, self.sl.acquire)
195
    self.assertRaises(errors.LockError, self.sl.acquire, shared=1)
196
    self.assertRaises(errors.LockError, self.sl.delete)
197

    
198
  def testNoDeleteIfSharer(self):
199
    self.sl.acquire(shared=1)
200
    self.assertRaises(AssertionError, self.sl.delete)
201

    
202
  def testDeletePendingSharersExclusiveDelete(self):
203
    self.sl.acquire()
204
    Thread(target=self._doItSharer).start()
205
    Thread(target=self._doItSharer).start()
206
    time.sleep(0.05)
207
    Thread(target=self._doItExclusive).start()
208
    Thread(target=self._doItDelete).start()
209
    time.sleep(0.05)
210
    self.sl.delete()
211
    # The two threads who were pending return both ERR
212
    self.assertEqual(self.done.get(True, 1), 'ERR')
213
    self.assertEqual(self.done.get(True, 1), 'ERR')
214
    self.assertEqual(self.done.get(True, 1), 'ERR')
215
    self.assertEqual(self.done.get(True, 1), 'ERR')
216

    
217
  def testDeletePendingDeleteExclusiveSharers(self):
218
    self.sl.acquire()
219
    Thread(target=self._doItDelete).start()
220
    Thread(target=self._doItExclusive).start()
221
    time.sleep(0.05)
222
    Thread(target=self._doItSharer).start()
223
    Thread(target=self._doItSharer).start()
224
    time.sleep(0.05)
225
    self.sl.delete()
226
    # The two threads who were pending return both ERR
227
    self.assertEqual(self.done.get(True, 1), 'ERR')
228
    self.assertEqual(self.done.get(True, 1), 'ERR')
229
    self.assertEqual(self.done.get(True, 1), 'ERR')
230
    self.assertEqual(self.done.get(True, 1), 'ERR')
231

    
232

    
233
class TestLockSet(unittest.TestCase):
234
  """LockSet tests"""
235

    
236
  def setUp(self):
237
    self.resources = ['one', 'two', 'three']
238
    self.ls = locking.LockSet(self.resources)
239
    # helper threads use the 'done' queue to tell the master they finished.
240
    self.done = Queue.Queue(0)
241

    
242
  def testResources(self):
243
    self.assertEquals(self.ls._names(), set(self.resources))
244
    newls = locking.LockSet()
245
    self.assertEquals(newls._names(), set())
246

    
247
  def testAcquireRelease(self):
248
    self.assert_(self.ls.acquire('one'))
249
    self.assertEquals(self.ls._list_owned(), set(['one']))
250
    self.ls.release()
251
    self.assertEquals(self.ls._list_owned(), set())
252
    self.assertEquals(self.ls.acquire(['one']), set(['one']))
253
    self.assertEquals(self.ls._list_owned(), set(['one']))
254
    self.ls.release()
255
    self.assertEquals(self.ls._list_owned(), set())
256
    self.ls.acquire(['one', 'two', 'three'])
257
    self.assertEquals(self.ls._list_owned(), set(['one', 'two', 'three']))
258
    self.ls.release('one')
259
    self.assertEquals(self.ls._list_owned(), set(['two', 'three']))
260
    self.ls.release(['three'])
261
    self.assertEquals(self.ls._list_owned(), set(['two']))
262
    self.ls.release()
263
    self.assertEquals(self.ls._list_owned(), set())
264
    self.assertEquals(self.ls.acquire(['one', 'three']), set(['one', 'three']))
265
    self.assertEquals(self.ls._list_owned(), set(['one', 'three']))
266
    self.ls.release()
267
    self.assertEquals(self.ls._list_owned(), set())
268

    
269
  def testNoDoubleAcquire(self):
270
    self.ls.acquire('one')
271
    self.assertRaises(AssertionError, self.ls.acquire, 'one')
272
    self.assertRaises(AssertionError, self.ls.acquire, ['two'])
273
    self.assertRaises(AssertionError, self.ls.acquire, ['two', 'three'])
274
    self.ls.release()
275
    self.ls.acquire(['one', 'three'])
276
    self.ls.release('one')
277
    self.assertRaises(AssertionError, self.ls.acquire, ['two'])
278
    self.ls.release('three')
279

    
280
  def testNoWrongRelease(self):
281
    self.assertRaises(AssertionError, self.ls.release)
282
    self.ls.acquire('one')
283
    self.assertRaises(AssertionError, self.ls.release, 'two')
284

    
285
  def testAddRemove(self):
286
    self.ls.add('four')
287
    self.assertEquals(self.ls._list_owned(), set())
288
    self.assert_('four' in self.ls._names())
289
    self.ls.add(['five', 'six', 'seven'], acquired=1)
290
    self.assert_('five' in self.ls._names())
291
    self.assert_('six' in self.ls._names())
292
    self.assert_('seven' in self.ls._names())
293
    self.assertEquals(self.ls._list_owned(), set(['five', 'six', 'seven']))
294
    self.assertEquals(self.ls.remove(['five', 'six']), ['five', 'six'])
295
    self.assert_('five' not in self.ls._names())
296
    self.assert_('six' not in self.ls._names())
297
    self.assertEquals(self.ls._list_owned(), set(['seven']))
298
    self.ls.add('eight', acquired=1, shared=1)
299
    self.assert_('eight' in self.ls._names())
300
    self.assertEquals(self.ls._list_owned(), set(['seven', 'eight']))
301
    self.ls.remove('seven')
302
    self.assert_('seven' not in self.ls._names())
303
    self.assertEquals(self.ls._list_owned(), set(['eight']))
304
    self.ls.release()
305
    self.ls.remove(['two'])
306
    self.assert_('two' not in self.ls._names())
307
    self.ls.acquire('three')
308
    self.assertEquals(self.ls.remove(['three']), ['three'])
309
    self.assert_('three' not in self.ls._names())
310
    self.assertEquals(self.ls.remove('three'), [])
311
    self.assertEquals(self.ls.remove(['one', 'three', 'six']), ['one'])
312
    self.assert_('one' not in self.ls._names())
313

    
314
  def testRemoveNonBlocking(self):
315
    self.assertRaises(NotImplementedError, self.ls.remove, 'one', blocking=0)
316
    self.ls.acquire('one')
317
    self.assertEquals(self.ls.remove('one', blocking=0), ['one'])
318
    self.ls.acquire(['two', 'three'])
319
    self.assertEquals(self.ls.remove(['two', 'three'], blocking=0),
320
                      ['two', 'three'])
321

    
322
  def testNoDoubleAdd(self):
323
    self.assertRaises(errors.LockError, self.ls.add, 'two')
324
    self.ls.add('four')
325
    self.assertRaises(errors.LockError, self.ls.add, 'four')
326

    
327
  def testNoWrongRemoves(self):
328
    self.ls.acquire(['one', 'three'], shared=1)
329
    # Cannot remove 'two' while holding something which is not a superset
330
    self.assertRaises(AssertionError, self.ls.remove, 'two')
331
    # Cannot remove 'three' as we are sharing it
332
    self.assertRaises(AssertionError, self.ls.remove, 'three')
333

    
334
  def testAcquireSetLock(self):
335
    # acquire the set-lock exclusively
336
    self.assertEquals(self.ls.acquire(None), set(['one', 'two', 'three']))
337
    # I can still add/remove elements...
338
    self.assertEquals(self.ls.remove(['two', 'three']), ['two', 'three'])
339
    self.assert_(self.ls.add('six'))
340
    self.ls.release()
341
    # share the set-lock
342
    self.assertEquals(self.ls.acquire(None, shared=1), set(['one', 'six']))
343
    # adding new elements is not possible
344
    self.assertRaises(AssertionError, self.ls.add, 'five')
345
    self.ls.release()
346

    
347
  def _doLockSet(self, set, shared):
348
    try:
349
      self.ls.acquire(set, shared=shared)
350
      self.done.put('DONE')
351
      self.ls.release()
352
    except errors.LockError:
353
      self.done.put('ERR')
354

    
355
  def _doAddSet(self, set):
356
    try:
357
      self.ls.add(set, acquired=1)
358
      self.done.put('DONE')
359
      self.ls.release()
360
    except errors.LockError:
361
      self.done.put('ERR')
362

    
363
  def _doRemoveSet(self, set):
364
    self.done.put(self.ls.remove(set))
365

    
366
  def testConcurrentSharedAcquire(self):
367
    self.ls.acquire(['one', 'two'], shared=1)
368
    Thread(target=self._doLockSet, args=(['one', 'two'], 1)).start()
369
    self.assertEqual(self.done.get(True, 1), 'DONE')
370
    Thread(target=self._doLockSet, args=(['one', 'two', 'three'], 1)).start()
371
    self.assertEqual(self.done.get(True, 1), 'DONE')
372
    Thread(target=self._doLockSet, args=('three', 1)).start()
373
    self.assertEqual(self.done.get(True, 1), 'DONE')
374
    Thread(target=self._doLockSet, args=(['one', 'two'], 0)).start()
375
    Thread(target=self._doLockSet, args=(['two', 'three'], 0)).start()
376
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
377
    self.ls.release()
378
    self.assertEqual(self.done.get(True, 1), 'DONE')
379
    self.assertEqual(self.done.get(True, 1), 'DONE')
380

    
381
  def testConcurrentExclusiveAcquire(self):
382
    self.ls.acquire(['one', 'two'])
383
    Thread(target=self._doLockSet, args=('three', 1)).start()
384
    self.assertEqual(self.done.get(True, 1), 'DONE')
385
    Thread(target=self._doLockSet, args=('three', 0)).start()
386
    self.assertEqual(self.done.get(True, 1), 'DONE')
387
    Thread(target=self._doLockSet, args=(['one', 'two'], 0)).start()
388
    Thread(target=self._doLockSet, args=(['one', 'two'], 1)).start()
389
    Thread(target=self._doLockSet, args=('one', 0)).start()
390
    Thread(target=self._doLockSet, args=('one', 1)).start()
391
    Thread(target=self._doLockSet, args=(['two', 'three'], 0)).start()
392
    Thread(target=self._doLockSet, args=(['two', 'three'], 1)).start()
393
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
394
    self.ls.release()
395
    self.assertEqual(self.done.get(True, 1), 'DONE')
396
    self.assertEqual(self.done.get(True, 1), 'DONE')
397
    self.assertEqual(self.done.get(True, 1), 'DONE')
398
    self.assertEqual(self.done.get(True, 1), 'DONE')
399
    self.assertEqual(self.done.get(True, 1), 'DONE')
400
    self.assertEqual(self.done.get(True, 1), 'DONE')
401

    
402
  def testConcurrentRemove(self):
403
    self.ls.add('four')
404
    self.ls.acquire(['one', 'two', 'four'])
405
    Thread(target=self._doLockSet, args=(['one', 'four'], 0)).start()
406
    Thread(target=self._doLockSet, args=(['one', 'four'], 1)).start()
407
    Thread(target=self._doLockSet, args=(['one', 'two'], 0)).start()
408
    Thread(target=self._doLockSet, args=(['one', 'two'], 1)).start()
409
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
410
    self.ls.remove('one')
411
    self.ls.release()
412
    self.assertEqual(self.done.get(True, 1), 'ERR')
413
    self.assertEqual(self.done.get(True, 1), 'ERR')
414
    self.assertEqual(self.done.get(True, 1), 'ERR')
415
    self.assertEqual(self.done.get(True, 1), 'ERR')
416
    self.ls.add(['five', 'six'], acquired=1)
417
    Thread(target=self._doLockSet, args=(['three', 'six'], 1)).start()
418
    Thread(target=self._doLockSet, args=(['three', 'six'], 0)).start()
419
    Thread(target=self._doLockSet, args=(['four', 'six'], 1)).start()
420
    Thread(target=self._doLockSet, args=(['four', 'six'], 0)).start()
421
    self.ls.remove('five')
422
    self.ls.release()
423
    self.assertEqual(self.done.get(True, 1), 'DONE')
424
    self.assertEqual(self.done.get(True, 1), 'DONE')
425
    self.assertEqual(self.done.get(True, 1), 'DONE')
426
    self.assertEqual(self.done.get(True, 1), 'DONE')
427
    self.ls.acquire(['three', 'four'])
428
    Thread(target=self._doRemoveSet, args=(['four', 'six'], )).start()
429
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
430
    self.ls.remove('four')
431
    self.assertEqual(self.done.get(True, 1), ['six'])
432
    Thread(target=self._doRemoveSet, args=(['two'])).start()
433
    self.assertEqual(self.done.get(True, 1), ['two'])
434
    self.ls.release()
435

    
436
  def testConcurrentSharedSetLock(self):
437
    # share the set-lock...
438
    self.ls.acquire(None, shared=1)
439
    # ...another thread can share it too
440
    Thread(target=self._doLockSet, args=(None, 1)).start()
441
    self.assertEqual(self.done.get(True, 1), 'DONE')
442
    # ...or just share some elements
443
    Thread(target=self._doLockSet, args=(['one', 'three'], 1)).start()
444
    self.assertEqual(self.done.get(True, 1), 'DONE')
445
    # ...but not add new ones or remove any
446
    Thread(target=self._doAddSet, args=(['nine'])).start()
447
    Thread(target=self._doRemoveSet, args=(['two'], )).start()
448
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
449
    # this just releases the set-lock
450
    self.ls.release([])
451
    self.assertEqual(self.done.get(True, 1), 'DONE')
452
    # release the lock on the actual elements so remove() can proceed too
453
    self.ls.release()
454
    self.assertEqual(self.done.get(True, 1), ['two'])
455

    
456
  def testConcurrentExclusiveSetLock(self):
457
    # acquire the set-lock...
458
    self.ls.acquire(None, shared=0)
459
    # ...no one can do anything else
460
    Thread(target=self._doLockSet, args=(None, 1)).start()
461
    Thread(target=self._doLockSet, args=(None, 0)).start()
462
    Thread(target=self._doLockSet, args=(['three'], 0)).start()
463
    Thread(target=self._doLockSet, args=(['two'], 1)).start()
464
    Thread(target=self._doAddSet, args=(['nine'])).start()
465
    self.ls.release()
466
    self.assertEqual(self.done.get(True, 1), 'DONE')
467
    self.assertEqual(self.done.get(True, 1), 'DONE')
468
    self.assertEqual(self.done.get(True, 1), 'DONE')
469
    self.assertEqual(self.done.get(True, 1), 'DONE')
470
    self.assertEqual(self.done.get(True, 1), 'DONE')
471

    
472

    
473
class TestGanetiLockManager(unittest.TestCase):
474

    
475
  def setUp(self):
476
    self.nodes=['n1', 'n2']
477
    self.instances=['i1', 'i2', 'i3']
478
    self.GL = locking.GanetiLockManager(nodes=self.nodes,
479
                                        instances=self.instances)
480
    self.done = Queue.Queue(0)
481

    
482
  def tearDown(self):
483
    # Don't try this at home...
484
    locking.GanetiLockManager._instance = None
485

    
486
  def testLockingConstants(self):
487
    # The locking library internally cheats by assuming its constants have some
488
    # relationships with each other. Check those hold true.
489
    for i in range(len(locking.LEVELS)):
490
      self.assertEqual(i, locking.LEVELS[i])
491

    
492
  def testDoubleGLFails(self):
493
    # We are not passing test=True, so instantiating a new one should fail
494
    self.assertRaises(AssertionError, locking.GanetiLockManager)
495

    
496
  def testLockNames(self):
497
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
498
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set(self.nodes))
499
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set(self.instances))
500
    self.assertEqual(self.GL._names(locking.LEVEL_CONFIG), set(['config']))
501

    
502
  def testInitAndResources(self):
503
    locking.GanetiLockManager._instance = None
504
    self.GL = locking.GanetiLockManager()
505
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
506
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set())
507
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set())
508
    self.assertEqual(self.GL._names(locking.LEVEL_CONFIG), set(['config']))
509

    
510
    locking.GanetiLockManager._instance = None
511
    self.GL = locking.GanetiLockManager(nodes=self.nodes)
512
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
513
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set(self.nodes))
514
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set())
515
    self.assertEqual(self.GL._names(locking.LEVEL_CONFIG), set(['config']))
516

    
517
    locking.GanetiLockManager._instance = None
518
    self.GL = locking.GanetiLockManager(instances=self.instances)
519
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
520
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set())
521
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set(self.instances))
522
    self.assertEqual(self.GL._names(locking.LEVEL_CONFIG), set(['config']))
523

    
524
  def testAcquireRelease(self):
525
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
526
    self.assertEquals(self.GL._list_owned(locking.LEVEL_CLUSTER), set(['BGL']))
527
    self.GL.acquire(locking.LEVEL_NODE, ['n1', 'n2'], shared=1)
528
    self.GL.release(locking.LEVEL_NODE)
529
    self.GL.acquire(locking.LEVEL_NODE, ['n1'])
530
    self.assertEquals(self.GL._list_owned(locking.LEVEL_NODE), set(['n1']))
531
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i1', 'i2'])
532
    self.GL.acquire(locking.LEVEL_CONFIG, ['config'])
533
    self.GL.release(locking.LEVEL_INSTANCE, ['i2'])
534
    self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), set(['i1']))
535
    self.GL.release(locking.LEVEL_NODE)
536
    self.GL.release(locking.LEVEL_INSTANCE)
537
    self.GL.release(locking.LEVEL_CONFIG)
538
    self.assertRaises(errors.LockError, self.GL.acquire,
539
                      locking.LEVEL_INSTANCE, ['i5'])
540
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i3'], shared=1)
541
    self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), set(['i3']))
542

    
543
  def testBGLDependency(self):
544
    self.assertRaises(AssertionError, self.GL.acquire,
545
                      locking.LEVEL_NODE, ['n1', 'n2'])
546
    self.assertRaises(AssertionError, self.GL.acquire,
547
                      locking.LEVEL_INSTANCE, ['i3'])
548
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
549
    self.GL.acquire(locking.LEVEL_NODE, ['n1'])
550
    self.assertRaises(AssertionError, self.GL.release,
551
                      locking.LEVEL_CLUSTER, ['BGL'])
552
    self.assertRaises(AssertionError, self.GL.release,
553
                      locking.LEVEL_CLUSTER)
554
    self.GL.release(locking.LEVEL_NODE)
555
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i1', 'i2'])
556
    self.assertRaises(AssertionError, self.GL.release,
557
                      locking.LEVEL_CLUSTER, ['BGL'])
558
    self.assertRaises(AssertionError, self.GL.release,
559
                      locking.LEVEL_CLUSTER)
560
    self.GL.release(locking.LEVEL_INSTANCE)
561
    self.GL.acquire(locking.LEVEL_CONFIG, ['config'])
562
    self.assertRaises(AssertionError, self.GL.release,
563
                      locking.LEVEL_CLUSTER)
564

    
565
  def testWrongOrder(self):
566
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
567
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i3'])
568
    self.assertRaises(AssertionError, self.GL.acquire,
569
                      locking.LEVEL_NODE, ['n1'])
570
    self.assertRaises(AssertionError, self.GL.acquire,
571
                      locking.LEVEL_INSTANCE, ['i2'])
572
    self.GL.acquire(locking.LEVEL_CONFIG, ['config'])
573
    self.assertRaises(AssertionError, self.GL.acquire,
574
                      locking.LEVEL_CONFIG, ['config'])
575
    self.GL.release(locking.LEVEL_INSTANCE)
576
    self.assertRaises(AssertionError, self.GL.acquire,
577
                      locking.LEVEL_NODE, ['n1'])
578
    self.assertRaises(AssertionError, self.GL.acquire,
579
                      locking.LEVEL_INSTANCE, ['i2'])
580
    self.assertRaises(AssertionError, self.GL.acquire,
581
                      locking.LEVEL_CONFIG, ['config'])
582

    
583
  # Helper function to run as a thread that shared the BGL and then acquires
584
  # some locks at another level.
585
  def _doLock(self, level, names, shared):
586
    try:
587
      self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
588
      self.GL.acquire(level, names, shared=shared)
589
      self.done.put('DONE')
590
      self.GL.release(level)
591
      self.GL.release(locking.LEVEL_CLUSTER)
592
    except errors.LockError:
593
      self.done.put('ERR')
594

    
595
  def testConcurrency(self):
596
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
597
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i1', 1)).start()
598
    self.assertEqual(self.done.get(True, 1), 'DONE')
599
    self.GL.acquire(locking.LEVEL_NODE, ['n1'])
600
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i3'])
601
    self.GL.acquire(locking.LEVEL_CONFIG, ['config'])
602
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i1', 1)).start()
603
    self.assertEqual(self.done.get(True, 1), 'DONE')
604
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i3', 1)).start()
605
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
606
    self.GL.release(locking.LEVEL_CONFIG)
607
    self.GL.release(locking.LEVEL_INSTANCE)
608
    self.assertEqual(self.done.get(True, 1), 'DONE')
609
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i2'], shared=1)
610
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i2', 1)).start()
611
    self.assertEqual(self.done.get(True, 1), 'DONE')
612
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i2', 0)).start()
613
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
614
    self.GL.release(locking.LEVEL_INSTANCE)
615
    self.assertEqual(self.done.get(True, 1), 'DONE')
616

    
617

    
618
if __name__ == '__main__':
619
  unittest.main()
620
  #suite = unittest.TestLoader().loadTestsFromTestCase(TestSharedLock)
621
  #unittest.TextTestRunner(verbosity=2).run(suite)