|
1 |
#
|
|
2 |
#
|
|
3 |
|
|
4 |
# Copyright (C) 2014 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 |
# 02110-1301, USA.
|
|
20 |
|
|
21 |
|
|
22 |
"""Support for mocking the wconfd calls"""
|
|
23 |
|
|
24 |
|
|
25 |
class MockClient(object):
|
|
26 |
"""Mock client calls to wconfd.
|
|
27 |
|
|
28 |
"""
|
|
29 |
def __init__(self, wconfdmock):
|
|
30 |
self.wconfdmock = wconfdmock
|
|
31 |
|
|
32 |
def TryUpdateLocks(self, _jid, _lockfile, req):
|
|
33 |
for lockrq in req:
|
|
34 |
if lockrq[1] == "release":
|
|
35 |
if lockrq[0] in self.wconfdmock.mylocks:
|
|
36 |
del self.wconfdmock.mylocks[lockrq[0]]
|
|
37 |
else:
|
|
38 |
self.wconfdmock.mylocks[lockrq[0]] = lockrq[1]
|
|
39 |
return []
|
|
40 |
|
|
41 |
def ListLocks(self, *_):
|
|
42 |
result = []
|
|
43 |
for lock in self.wconfdmock.mylocks:
|
|
44 |
result.append([lock, self.wconfdmock.mylocks[lock]])
|
|
45 |
return result
|
|
46 |
|
|
47 |
def FreeLocksLevel(self, _jid, _lockfile, level):
|
|
48 |
locks = self.wconfdmock.mylocks.keys()
|
|
49 |
for lock in locks:
|
|
50 |
if lock.startswith(level + "/"):
|
|
51 |
del self.wconfdmock.mylocks[lock]
|
|
52 |
|
|
53 |
def OpportunisticLockUnion(self, _jid, _lockfile, req):
|
|
54 |
for lockrq in req:
|
|
55 |
self.wconfdmock.mylocks[lockrq[0]] = lockrq[1]
|
|
56 |
return [lockrq[0] for lockrq in req]
|
|
57 |
|
|
58 |
|
|
59 |
class WConfdMock(object):
|
|
60 |
"""Mock calls to WConfD.
|
|
61 |
|
|
62 |
As at various points, LUs are tested in an integration-test
|
|
63 |
fashion, calling it through mcpu, which, in turn, calls wconfd,
|
|
64 |
this mock must be able to live under these circumstances. In particular,
|
|
65 |
it needs to keep track of locks requested and released, as both,
|
|
66 |
mcpu and the individual LUs do consistency checks on the locks they
|
|
67 |
own.
|
|
68 |
|
|
69 |
"""
|
|
70 |
def __init__(self):
|
|
71 |
self.mylocks = {}
|
|
72 |
|
|
73 |
def Client(self):
|
|
74 |
return MockClient(self)
|