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 # Send with max coverage
143 self.client.SendRequest(req2, coverage=-1)
144 self.assertEquals(self.client._socket.send_count,
145 constants.CONFD_DEFAULT_REQ_COVERAGE + len(self.mc_list))
146 self.assert_(self.client._socket.last_address in self.mc_list)
149 def testClientExpire(self):
150 req = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
151 self.client.SendRequest(req)
152 # Make a couple of seconds pass ;)
153 self.mock_time.increase(2)
154 # Now sending the second request
155 req2 = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
156 self.client.SendRequest(req2)
157 self.mock_time.increase(constants.CONFD_CLIENT_EXPIRE_TIMEOUT - 1)
158 # First request should be expired, second one should not
159 self.client.ExpireRequests()
160 self.assertEquals(self.callback.call_count, 1)
161 self.assertEquals(self.callback.last_up.type, confd.client.UPCALL_EXPIRE)
162 self.assertEquals(self.callback.last_up.salt, req.rsalt)
163 self.assertEquals(self.callback.last_up.orig_request, req)
164 self.mock_time.increase(3)
165 self.assertEquals(self.callback.call_count, 1)
166 self.client.ExpireRequests()
167 self.assertEquals(self.callback.call_count, 2)
168 self.assertEquals(self.callback.last_up.type, confd.client.UPCALL_EXPIRE)
169 self.assertEquals(self.callback.last_up.salt, req2.rsalt)
170 self.assertEquals(self.callback.last_up.orig_request, req2)
172 def testClientCascadeExpire(self):
173 req = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
174 self.client.SendRequest(req)
175 self.mock_time.increase(constants.CONFD_CLIENT_EXPIRE_TIMEOUT +1)
176 req2 = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
177 self.client.SendRequest(req2)
178 self.assertEquals(self.callback.call_count, 1)
180 def testUpdatePeerList(self):
181 new_peers = ['1.2.3.4', '1.2.3.5']
182 self.client.UpdatePeerList(new_peers)
183 self.assertEquals(self.client._peers, new_peers)
184 req = confd.client.ConfdClientRequest(type=constants.CONFD_REQ_PING)
185 self.client.SendRequest(req)
186 self.assertEquals(self.client._socket.send_count, len(new_peers))
187 self.assert_(self.client._socket.last_address in new_peers)
190 if __name__ == '__main__':
191 testutils.GanetiTestProgram()