wconfd_mock.py 2.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#
#

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


"""Support for mocking the wconfd calls"""


class MockClient(object):
  """Mock client calls to wconfd.

  """
  def __init__(self, wconfdmock):
    self.wconfdmock = wconfdmock

32
  def TryUpdateLocks(self, _cid, req):
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    for lockrq in req:
      if lockrq[1] == "release":
        if lockrq[0] in self.wconfdmock.mylocks:
          del self.wconfdmock.mylocks[lockrq[0]]
      else:
        self.wconfdmock.mylocks[lockrq[0]] = lockrq[1]
    return []

  def ListLocks(self, *_):
    result = []
    for lock in self.wconfdmock.mylocks:
      result.append([lock, self.wconfdmock.mylocks[lock]])
    return result

47
  def FreeLocksLevel(self, _cid, level):
48
49
50
51
52
    locks = self.wconfdmock.mylocks.keys()
    for lock in locks:
      if lock.startswith(level + "/"):
        del self.wconfdmock.mylocks[lock]

53
  def OpportunisticLockUnion(self, _cid, req):
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    for lockrq in req:
      self.wconfdmock.mylocks[lockrq[0]] = lockrq[1]
    return [lockrq[0] for lockrq in req]


class WConfdMock(object):
  """Mock calls to WConfD.

  As at various points, LUs are tested in an integration-test
  fashion, calling it through mcpu, which, in turn, calls wconfd,
  this mock must be able to live under these circumstances. In particular,
  it needs to keep track of locks requested and released, as both,
  mcpu and the individual LUs do consistency checks on the locks they
  own.

  """
  def __init__(self):
    self.mylocks = {}

  def Client(self):
    return MockClient(self)