<s> """<STR_LIT>""" <EOL> import socket <EOL> import unittest <EOL> from ganeti import confd <EOL> from ganeti import constants <EOL> from ganeti import errors <EOL> import ganeti . confd . client <EOL> import testutils <EOL> class ResettableMock ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . Reset ( ) <EOL> def Reset ( self ) : <EOL> pass <EOL> class MockLogger ( ResettableMock ) : <EOL> def Reset ( self ) : <EOL> self . debug_count = <NUM_LIT:0> <EOL> self . warn_count = <NUM_LIT:0> <EOL> self . error_count = <NUM_LIT:0> <EOL> def debug ( string ) : <EOL> self . debug_count += <NUM_LIT:1> <EOL> def warning ( string ) : <EOL> self . warn_count += <NUM_LIT:1> <EOL> def error ( string ) : <EOL> self . error_count += <NUM_LIT:1> <EOL> class MockConfdAsyncUDPClient ( ResettableMock ) : <EOL> def Reset ( self ) : <EOL> self . send_count = <NUM_LIT:0> <EOL> self . last_address = '<STR_LIT>' <EOL> self . last_port = - <NUM_LIT:1> <EOL> self . last_sent = '<STR_LIT>' <EOL> def enqueue_send ( self , address , port , payload ) : <EOL> self . send_count += <NUM_LIT:1> <EOL> self . last_payload = payload <EOL> self . last_port = port <EOL> self . last_address = address <EOL> class MockCallback ( ResettableMock ) : <EOL> def Reset ( self ) : <EOL> self . call_count = <NUM_LIT:0> <EOL> self . last_up = None <EOL> def __call__ ( self , up ) : <EOL> """<STR_LIT>""" <EOL> self . call_count += <NUM_LIT:1> <EOL> self . last_up = up <EOL> class MockTime ( ResettableMock ) : <EOL> def Reset ( self ) : <EOL> self . mytime = <NUM_LIT> <EOL> def time ( self ) : <EOL> return self . mytime <EOL> def increase ( self , delta ) : <EOL> self . mytime += delta <EOL> class _BaseClientTest : <EOL> """<STR_LIT>""" <EOL> mc_list = None <EOL> new_peers = None <EOL> family = None <EOL> def setUp ( self ) : <EOL> self . mock_time = MockTime ( ) <EOL> confd . client . time = self . mock_time <EOL> confd . client . ConfdAsyncUDPClient = MockConfdAsyncUDPClient <EOL> self . logger = MockLogger ( ) <EOL> hmac_key = "<STR_LIT>" <EOL> self . callback = MockCallback ( ) <EOL> self . client = confd . client . ConfdClient ( hmac_key , self . mc_list , <EOL> self . callback , logger = self . logger ) <EOL> def testRequest ( self ) : <EOL> req1 = confd . client . ConfdClientRequest ( type = constants . CONFD_REQ_PING ) <EOL> req2 = confd . client . ConfdClientRequest ( type = constants . CONFD_REQ_PING ) <EOL> self . assertNotEqual ( req1 . rsalt , req2 . rsalt ) <EOL> self . assertEqual ( req1 . protocol , constants . CONFD_PROTOCOL_VERSION ) <EOL> self . assertEqual ( req2 . protocol , constants . CONFD_PROTOCOL_VERSION ) <EOL> self . assertRaises ( errors . ConfdClientError , confd . client . ConfdClientRequest , <EOL> type = - <NUM_LIT> ) <EOL> def testClientSend ( self ) : <EOL> req = confd . client . ConfdClientRequest ( type = constants . CONFD_REQ_PING ) <EOL> self . client . SendRequest ( req ) <EOL> self . assertRaises ( errors . ConfdClientError , self . client . SendRequest , req ) <EOL> req2 = confd . client . ConfdClientRequest ( type = constants . CONFD_REQ_PING ) <EOL> self . assertRaises ( errors . ConfdClientError , self . client . SendRequest , <EOL> req2 , coverage = <NUM_LIT:15> ) <EOL> self . assertEquals ( self . client . _socket . send_count , <EOL> constants . CONFD_DEFAULT_REQ_COVERAGE ) <EOL> self . client . SendRequest ( req2 , coverage = - <NUM_LIT:1> ) <EOL> self . assertEquals ( self . client . _socket . send_count , <EOL> constants . CONFD_DEFAULT_REQ_COVERAGE + len ( self . mc_list ) ) <EOL> self . assert_ ( self . client . _socket . last_address in self . mc_list ) <EOL> def testClientExpire ( self ) : <EOL> req = confd . client . ConfdClientRequest ( type = constants . CONFD_REQ_PING ) <EOL> self . client . SendRequest ( req ) <EOL> self . mock_time . increase ( <NUM_LIT:2> ) <EOL> req2 = confd . client . ConfdClientRequest ( type = constants . CONFD_REQ_PING ) <EOL> self . client . SendRequest ( req2 ) <EOL> self . mock_time . increase ( constants . CONFD_CLIENT_EXPIRE_TIMEOUT - <NUM_LIT:1> ) <EOL> self . client . ExpireRequests ( ) <EOL> self . assertEquals ( self . callback . call_count , <NUM_LIT:1> ) <EOL> self . assertEquals ( self . callback . last_up . type , confd . client . UPCALL_EXPIRE ) <EOL> self . assertEquals ( self . callback . last_up . salt , req . rsalt ) <EOL> self . assertEquals ( self . callback . last_up . orig_request , req ) <EOL> self . mock_time . increase ( <NUM_LIT:3> ) <EOL> self . assertEquals ( self . callback . call_count , <NUM_LIT:1> ) <EOL> self . client . ExpireRequests ( ) <EOL> self . assertEquals ( self . callback . call_count , <NUM_LIT:2> ) <EOL> self . assertEquals ( self . callback . last_up . type , confd . client . UPCALL_EXPIRE ) <EOL> self . assertEquals ( self . callback . last_up . salt , req2 . rsalt ) <EOL> self . assertEquals ( self . callback . last_up . orig_request , req2 ) <EOL> def testClientCascadeExpire ( self ) : <EOL> req = confd . client . ConfdClientRequest ( type = constants . CONFD_REQ_PING ) <EOL> self . client . SendRequest ( req ) <EOL> self . mock_time . increase ( constants . CONFD_CLIENT_EXPIRE_TIMEOUT + <NUM_LIT:1> ) <EOL> req2 = confd . client . ConfdClientRequest ( type = constants . CONFD_REQ_PING ) <EOL> self . client . SendRequest ( req2 ) <EOL> self . assertEquals ( self . callback . call_count , <NUM_LIT:1> ) <EOL> def testUpdatePeerList ( self ) : <EOL> self . client . UpdatePeerList ( self . new_peers ) <EOL> self . assertEquals ( self . client . _peers , self . new_peers ) <EOL> req = confd . client . ConfdClientRequest ( type = constants . CONFD_REQ_PING ) <EOL> self . client . SendRequest ( req ) <EOL> self . assertEquals ( self . client . _socket . send_count , len ( self . new_peers ) ) <EOL> self . assert_ ( self . client . _socket . last_address in self . new_peers ) <EOL> def testSetPeersFamily ( self ) : <EOL> self . client . _SetPeersAddressFamily ( ) <EOL> self . assertEquals ( self . client . _family , self . family ) <EOL> mixed_peers = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . client . UpdatePeerList ( mixed_peers ) <EOL> self . assertRaises ( errors . ConfdClientError , <EOL> self . client . _SetPeersAddressFamily ) <EOL> class TestIP4Client ( unittest . TestCase , _BaseClientTest ) : <EOL> """<STR_LIT>""" <EOL> mc_list = [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> new_peers = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> family = socket . AF_INET <EOL> def setUp ( self ) : <EOL> unittest . TestCase . setUp ( self ) <EOL> _BaseClientTest . setUp ( self ) <EOL> class TestIP6Client ( unittest . TestCase , _BaseClientTest ) : <EOL> """<STR_LIT>""" <EOL> mc_list = [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> new_peers = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> family = socket . AF_INET6 <EOL> def setUp ( self ) : <EOL> unittest . TestCase . setUp ( self ) <EOL> _BaseClientTest . setUp ( self ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> testutils . GanetiTestProgram ( ) </s>
