4 # Copyright (C) 2009 Google Inc.
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.
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.
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
22 """Script for unittesting the confd client module"""
27 from ganeti import confd
28 from ganeti import constants
29 from ganeti import errors
31 import ganeti.confd.client
36 class ResettableMock(object):
37 def __init__(self, *args, **kwargs):
44 class MockLogger(ResettableMock):
59 class MockConfdAsyncUDPClient(ResettableMock):
62 self.last_address = ''
66 def enqueue_send(self, address, port, payload):
68 self.last_payload = payload
70 self.last_address = address
72 class MockCallback(ResettableMock):
77 def __call__(self, up):
80 @type up: L{ConfdUpcallPayload}
81 @param up: upper callback
88 class MockTime(ResettableMock):
90 self.mytime = 1254213006.5175071
95 def increase(self, delta):
99 class TestClient(unittest.TestCase):
103 self.mock_time = MockTime()
104 confd.client.time = self.mock_time
105 confd.client.ConfdAsyncUDPClient = MockConfdAsyncUDPClient
106 self.logger = MockLogger()
107 hmac_key = "mykeydata"
108 self.mc_list = ['10.0.0.1',
118 self.callback = MockCallback()
119 self.client = confd.client.ConfdClient(hmac_key, self.mc_list,
120 self.callback, logger=self.logger)
122 def testRequest(self):
123 req1 = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
124 req2 = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
125 self.assertNotEqual(req1.rsalt, req2.rsalt)
126 self.assertEqual(req1.protocol, constants.CONFD_PROTOCOL_VERSION)
127 self.assertEqual(req2.protocol, constants.CONFD_PROTOCOL_VERSION)
128 self.assertRaises(errors.ConfdClientError, confd.client.ConfdClientRequest,
131 def testClientSend(self):
132 req = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
133 self.client.SendRequest(req)
134 # Cannot send the same request twice
135 self.assertRaises(errors.ConfdClientError, self.client.SendRequest, req)
136 req2 = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
137 # Coverage is too big
138 self.assertRaises(errors.ConfdClientError, self.client.SendRequest,
140 self.assertEquals(self.client._socket.send_count,
141 constants.CONFD_DEFAULT_REQ_COVERAGE)
142 self.assert_(self.client._socket.last_address in self.mc_list)
145 def testClientExpire(self):
146 req = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
147 self.client.SendRequest(req)
148 # Make a couple of seconds pass ;)
149 self.mock_time.increase(2)
150 # Now sending the second request
151 req2 = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
152 self.client.SendRequest(req2)
153 self.mock_time.increase(constants.CONFD_CLIENT_EXPIRE_TIMEOUT - 1)
154 # First request should be expired, second one should not
155 self.client.ExpireRequests()
156 self.assertEquals(self.callback.call_count, 1)
157 self.assertEquals(self.callback.last_up.type, confd.client.UPCALL_EXPIRE)
158 self.assertEquals(self.callback.last_up.salt, req.rsalt)
159 self.assertEquals(self.callback.last_up.orig_request, req)
160 self.mock_time.increase(3)
161 self.assertEquals(self.callback.call_count, 1)
162 self.client.ExpireRequests()
163 self.assertEquals(self.callback.call_count, 2)
164 self.assertEquals(self.callback.last_up.type, confd.client.UPCALL_EXPIRE)
165 self.assertEquals(self.callback.last_up.salt, req2.rsalt)
166 self.assertEquals(self.callback.last_up.orig_request, req2)
168 def testClientCascadeExpire(self):
169 req = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
170 self.client.SendRequest(req)
171 self.mock_time.increase(constants.CONFD_CLIENT_EXPIRE_TIMEOUT +1)
172 req2 = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
173 self.client.SendRequest(req2)
174 self.assertEquals(self.callback.call_count, 1)
176 def testUpdatePeerList(self):
177 new_peers = ['1.2.3.4', '1.2.3.5']
178 self.client.UpdatePeerList(new_peers)
179 self.assertEquals(self.client._peers, new_peers)
180 req = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
181 self.client.SendRequest(req)
182 self.assertEquals(self.client._socket.send_count, len(new_peers))
183 self.assert_(self.client._socket.last_address in new_peers)
186 if __name__ == '__main__':
187 testutils.GanetiTestProgram()