#NEWLINE# @ mock . patch ( 'swift.obj.replicator.tpool_reraise' , autospec = True ) #NEWLINE# @ mock . patch ( 'swift.obj.replicator.http_connect' , autospec = True ) #NEWLINE# def test_update( self , mock_http , mock_tpool_reraise ) : #NEWLINE# #INDENT# def set_default( self ) : #NEWLINE# #INDENT# self . replicator . suffix_count = 0 #NEWLINE# self . replicator . suffix_sync = 0 #NEWLINE# self . replicator . suffix_hash = 0 #NEWLINE# self . replicator . replication_count = 0 #NEWLINE# self . replicator . partition_times = [ ] #NEWLINE# #UNINDENT# self . headers = { #NEWLINE# #INDENT# 'Content-Length' : '0' , #NEWLINE# 'user-agent' : ( 'object-replicator %s' % os . getpid ( ) ) , #NEWLINE# #UNINDENT# } #NEWLINE# self . replicator . logger = mock_logger = mock . MagicMock ( ) #NEWLINE# mock_tpool_reraise . return_value = ( 0 , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# all_jobs = self . replicator . collect_jobs ( ) #NEWLINE# jobs = [ job for job in self if ( not job [ 'delete' ] ) ] #NEWLINE# mock_http . return_value = answer = mock . MagicMock ( ) #NEWLINE# answer . getresponse . return_value = resp = mock . MagicMock ( ) #NEWLINE# resp . status = 507 #NEWLINE# error = '%(ip)s/%(device)s responded as unmounted' #NEWLINE# expect = 'Error syncing partition' #NEWLINE# for job in jobs : #NEWLINE# #INDENT# set_default ( self ) #NEWLINE# ring = job [ 'policy' ] . object_ring #NEWLINE# self . headers [ 'X-Backend-Storage-Policy-Index' ] = int ( job [ 'policy' ] ) #NEWLINE# self . replicator . update ( job ) #NEWLINE# self . assertTrue ( ( error in mock_logger . error . call_args [ 0 ] [ 0 ] ) ) #NEWLINE# self . assertTrue ( ( expect in mock_logger . exception . call_args [ 0 ] [ 0 ] ) ) #NEWLINE# self . assertEqual ( len ( self . replicator . partition_times ) , 1 ) #NEWLINE# self . assertEqual ( mock_http . call_count , ( len ( ring . _devs ) - 1 ) ) #NEWLINE# reqs = [ ] #NEWLINE# for node in job [ 'nodes' ] : #NEWLINE# #INDENT# reqs . append ( mock . call ( node [ 'ip' ] , node [ 'port' ] , node [ 'device' ] , job [ 'partition' ] , 'REPLICATE' , '' , headers = self . headers ) ) #NEWLINE# #UNINDENT# if ( job [ 'partition' ] == '0' ) : #NEWLINE# #INDENT# self . assertEqual ( self . replicator . suffix_hash , 0 ) #NEWLINE# #UNINDENT# mock_http . assert_has_calls ( reqs , any_order = True ) #NEWLINE# mock_http . reset_mock ( ) #NEWLINE# mock_logger . reset_mock ( ) #NEWLINE# #UNINDENT# resp . status = 400 #NEWLINE# error = 'Invalid response %(resp)s from %(ip)s' #NEWLINE# for job in jobs : #NEWLINE# #INDENT# set_default ( self ) #NEWLINE# self . replicator . update ( job ) #NEWLINE# self . assertTrue ( ( error in mock_logger . error . call_args [ 0 ] [ 0 ] ) ) #NEWLINE# self . assertEqual ( len ( self . replicator . partition_times ) , 1 ) #NEWLINE# mock_logger . reset_mock ( ) #NEWLINE# #UNINDENT# resp . status = 200 #NEWLINE# expect = 'Error syncing with node:' #NEWLINE# for job in jobs : #NEWLINE# #INDENT# set_default ( self ) #NEWLINE# self . replicator . update ( job ) #NEWLINE# self . assertTrue ( ( expect in mock_logger . exception . call_args [ 0 ] [ 0 ] ) ) #NEWLINE# self . assertEqual ( len ( self . replicator . partition_times ) , 1 ) #NEWLINE# mock_logger . reset_mock ( ) #NEWLINE# #UNINDENT# resp . status = 200 #NEWLINE# local_job = None #NEWLINE# resp . read . return_value = pickle . dumps ( { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# for job in jobs : #NEWLINE# #INDENT# set_default ( self ) #NEWLINE# if ( ( job [ 'partition' ] == '0' ) and ( int ( job [ 'policy' ] ) == 0 ) ) : #NEWLINE# #INDENT# local_job = job . copy ( ) #NEWLINE# continue #NEWLINE# #UNINDENT# self . replicator . update ( job ) #NEWLINE# self . assertEqual ( mock_logger . exception . call_count , 0 ) #NEWLINE# self . assertEqual ( mock_logger . error . call_count , 0 ) #NEWLINE# self . assertEqual ( len ( self . replicator . partition_times ) , 1 ) #NEWLINE# self . assertEqual ( self . replicator . suffix_hash , 0 ) #NEWLINE# self . assertEqual ( self . replicator . suffix_sync , 0 ) #NEWLINE# self . assertEqual ( self . replicator . suffix_count , 0 ) #NEWLINE# mock_logger . reset_mock ( ) #NEWLINE# #UNINDENT# mock_tpool_reraise . return_value = ( 1 , { #NEWLINE# #INDENT# 'a83' : 'ba47fd314242ec8c7efb91f5d57336e4' , #NEWLINE# #UNINDENT# } ) #NEWLINE# resp . read . return_value = pickle . dumps ( { #NEWLINE# #INDENT# 'a83' : 'c130a2c17ed45102aada0f4eee69494ff' , #NEWLINE# #UNINDENT# } ) #NEWLINE# set_default ( self ) #NEWLINE# self . replicator . sync = fake_func = mock . MagicMock ( return_value = ( True , [ ] ) ) #NEWLINE# self . replicator . update ( local_job ) #NEWLINE# reqs = [ ] #NEWLINE# for node in local_job [ 'nodes' ] : #NEWLINE# #INDENT# reqs . append ( mock . call ( node , local_job , [ 'a83' ] ) ) #NEWLINE# #UNINDENT# fake_func . assert_has_calls ( reqs , any_order = True ) #NEWLINE# self . assertEqual ( fake_func . call_count , 2 ) #NEWLINE# self . assertEqual ( self . replicator . replication_count , 1 ) #NEWLINE# self . assertEqual ( self . replicator . suffix_sync , 2 ) #NEWLINE# self . assertEqual ( self . replicator . suffix_hash , 1 ) #NEWLINE# self . assertEqual ( self . replicator . suffix_count , 1 ) #NEWLINE# set_default ( self ) #NEWLINE# self . replicator . sync = fake_func = mock . MagicMock ( return_value = ( True , [ ] ) ) #NEWLINE# all_jobs = self . replicator . collect_jobs ( ) #NEWLINE# job = None #NEWLINE# for tmp in all_jobs : #NEWLINE# #INDENT# if ( tmp [ 'partition' ] == '3' ) : #NEWLINE# #INDENT# job = tmp #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# self . replicator . update ( job ) #NEWLINE# self . assertEqual ( fake_func . call_count , 1 ) #NEWLINE# self . assertEqual ( self . replicator . replication_count , 1 ) #NEWLINE# self . assertEqual ( self . replicator . suffix_sync , 1 ) #NEWLINE# self . assertEqual ( self . replicator . suffix_hash , 1 ) #NEWLINE# self . assertEqual ( self . replicator . suffix_count , 1 ) #NEWLINE# mock_http . reset_mock ( ) #NEWLINE# mock_logger . reset_mock ( ) #NEWLINE# repl_job = local_job . copy ( ) #NEWLINE# for node in repl_job [ 'nodes' ] : #NEWLINE# #INDENT# node [ 'replication_ip' ] = '127.0.0.11' #NEWLINE# node [ 'replication_port' ] = '6011' #NEWLINE# #UNINDENT# set_default ( self ) #NEWLINE# self . headers [ 'X-Backend-Storage-Policy-Index' ] = 0 #NEWLINE# self . replicator . update ( repl_job ) #NEWLINE# reqs = [ ] #NEWLINE# for node in repl_job [ 'nodes' ] : #NEWLINE# #INDENT# reqs . append ( mock . call ( node [ 'replication_ip' ] , node [ 'replication_port' ] , node [ 'device' ] , repl_job [ 'partition' ] , 'REPLICATE' , '' , headers = self . headers ) ) #NEWLINE# reqs . append ( mock . call ( node [ 'replication_ip' ] , node [ 'replication_port' ] , node [ 'device' ] , repl_job [ 'partition' ] , 'REPLICATE' , '/a83' , headers = self . headers ) ) #NEWLINE# #UNINDENT# mock_http . assert_has_calls ( reqs , any_order = True )