<s> from hashlib import md5 <EOL> import unittest <EOL> import uuid <EOL> import shutil <EOL> import random <EOL> from collections import defaultdict <EOL> from test . probe . common import ECProbeTest <EOL> from swift . common import direct_client <EOL> from swift . common . storage_policy import EC_POLICY <EOL> from swift . common . manager import Manager <EOL> from swift . obj . reconstructor import _get_partners <EOL> from swiftclient import client <EOL> class Body ( object ) : <EOL> def __init__ ( self , total = <NUM_LIT> * <NUM_LIT:2> ** <NUM_LIT:20> ) : <EOL> self . total = total <EOL> self . hasher = md5 ( ) <EOL> self . size = <NUM_LIT:0> <EOL> self . chunk = '<STR_LIT:test>' * <NUM_LIT:16> * <NUM_LIT:2> ** <NUM_LIT:10> <EOL> @ property <EOL> def etag ( self ) : <EOL> return self . hasher . hexdigest ( ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> if self . size > self . total : <EOL> raise StopIteration ( ) <EOL> self . size += len ( self . chunk ) <EOL> self . hasher . update ( self . chunk ) <EOL> return self . chunk <EOL> def __next__ ( self ) : <EOL> return next ( self ) <EOL> class TestReconstructorRebuild ( ECProbeTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestReconstructorRebuild , self ) . setUp ( ) <EOL> self . container_name = '<STR_LIT>' % uuid . uuid4 ( ) <EOL> self . object_name = '<STR_LIT>' % uuid . uuid4 ( ) <EOL> self . assertEqual ( self . policy . policy_type , EC_POLICY ) <EOL> self . reconstructor = Manager ( [ "<STR_LIT>" ] ) <EOL> def proxy_get ( self ) : <EOL> headers , body = client . get_object ( self . url , self . token , <EOL> self . container_name , <EOL> self . object_name , <EOL> resp_chunk_size = <NUM_LIT:64> * <NUM_LIT:2> ** <NUM_LIT:10> ) <EOL> resp_checksum = md5 ( ) <EOL> for chunk in body : <EOL> resp_checksum . update ( chunk ) <EOL> return resp_checksum . hexdigest ( ) <EOL> def direct_get ( self , node , part ) : <EOL> req_headers = { '<STR_LIT>' : int ( self . policy ) } <EOL> headers , data = direct_client . direct_get_object ( <EOL> node , part , self . account , self . container_name , <EOL> self . object_name , headers = req_headers , <EOL> resp_chunk_size = <NUM_LIT:64> * <NUM_LIT:2> ** <NUM_LIT:20> ) <EOL> hasher = md5 ( ) <EOL> for chunk in data : <EOL> hasher . update ( chunk ) <EOL> return hasher . hexdigest ( ) <EOL> def _check_node ( self , node , part , etag , headers_post ) : <EOL> fragment_archive_etag = self . direct_get ( node , part ) <EOL> part_dir = self . storage_dir ( '<STR_LIT:object>' , node , part = part ) <EOL> shutil . rmtree ( part_dir , True ) <EOL> try : <EOL> self . direct_get ( node , part ) <EOL> except direct_client . DirectClientException as err : <EOL> self . assertEqual ( err . http_status , <NUM_LIT> ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' % <EOL> ( node , ) ) <EOL> self . assertEqual ( etag , self . proxy_get ( ) ) <EOL> self . reconstructor . once ( ) <EOL> self . assertEqual ( fragment_archive_etag , <EOL> self . direct_get ( node , part ) ) <EOL> meta = client . head_object ( self . url , self . token , <EOL> self . container_name , <EOL> self . object_name ) <EOL> for key in headers_post : <EOL> self . assertTrue ( key in meta ) <EOL> self . assertEqual ( meta [ key ] , headers_post [ key ] ) <EOL> def _format_node ( self , node ) : <EOL> return '<STR_LIT>' % ( node [ '<STR_LIT>' ] , node [ '<STR_LIT:index>' ] ) <EOL> def test_main ( self ) : <EOL> headers = { '<STR_LIT>' : self . policy . name } <EOL> client . put_container ( self . url , self . token , self . container_name , <EOL> headers = headers ) <EOL> contents = Body ( ) <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' } <EOL> headers_post = { '<STR_LIT>' : '<STR_LIT>' } <EOL> etag = client . put_object ( self . url , self . token , <EOL> self . container_name , <EOL> self . object_name , <EOL> contents = contents , headers = headers ) <EOL> client . post_object ( self . url , self . token , self . container_name , <EOL> self . object_name , headers = headers_post ) <EOL> del headers_post [ '<STR_LIT>' ] <EOL> opart , onodes = self . object_ring . get_nodes ( <EOL> self . account , self . container_name , self . object_name ) <EOL> single_node = [ random . choice ( onodes ) ] <EOL> adj_nodes = [ onodes [ <NUM_LIT:0> ] , onodes [ - <NUM_LIT:1> ] ] <EOL> far_nodes = [ onodes [ <NUM_LIT:0> ] , onodes [ - <NUM_LIT:2> ] ] <EOL> test_list = [ single_node , adj_nodes , far_nodes ] <EOL> for node_list in test_list : <EOL> for onode in node_list : <EOL> try : <EOL> self . _check_node ( onode , opart , etag , headers_post ) <EOL> except AssertionError as e : <EOL> self . fail ( <EOL> str ( e ) + '<STR_LIT>' % ( <EOL> self . _format_node ( onode ) , <EOL> [ self . _format_node ( n ) for n in node_list ] ) ) <EOL> def test_rebuild_partner_down ( self ) : <EOL> headers = { '<STR_LIT>' : self . policy . name } <EOL> client . put_container ( self . url , self . token , self . container_name , <EOL> headers = headers ) <EOL> contents = Body ( ) <EOL> client . put_object ( self . url , self . token , <EOL> self . container_name , <EOL> self . object_name , <EOL> contents = contents ) <EOL> opart , onodes = self . object_ring . get_nodes ( <EOL> self . account , self . container_name , self . object_name ) <EOL> group_nodes_by_config = defaultdict ( list ) <EOL> for n in onodes : <EOL> group_nodes_by_config [ self . config_number ( n ) ] . append ( n ) <EOL> for config_number , node_list in group_nodes_by_config . items ( ) : <EOL> if len ( node_list ) == <NUM_LIT:1> : <EOL> break <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> primary_node = node_list [ <NUM_LIT:0> ] <EOL> partner_node = random . choice ( _get_partners ( <EOL> primary_node [ '<STR_LIT:index>' ] , onodes ) ) <EOL> device_path = self . device_dir ( '<STR_LIT:object>' , partner_node ) <EOL> self . kill_drive ( device_path ) <EOL> failed_primary = [ n for n in onodes if n [ '<STR_LIT:id>' ] not in <EOL> ( primary_node [ '<STR_LIT:id>' ] , partner_node [ '<STR_LIT:id>' ] ) ] [ <NUM_LIT:0> ] <EOL> failed_primary_etag = self . direct_get ( failed_primary , opart ) <EOL> part_dir = self . storage_dir ( '<STR_LIT:object>' , failed_primary , part = opart ) <EOL> shutil . rmtree ( part_dir , True ) <EOL> self . reconstructor . once ( number = self . config_number ( primary_node ) ) <EOL> self . assertEqual ( failed_primary_etag , <EOL> self . direct_get ( failed_primary , opart ) ) <EOL> self . revive_drive ( device_path ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
