class ContainerStatReader: """ Read the container stat info from container file. """ def __init__(self, container_name, container_path, account_name, conf, \ logger): """ Constructor for RecordReader. :param container_name: container name :param container_path: container path """ self.__container_name = container_name self.__container_path = container_path self.__account_name = account_name self.stat_info = {} self.conf = conf self.logger = logger self.msg = GlobalVariables(self.logger) self.retry_count = 3 #self.logger.debug("ContainerStatReader constructed") self.connection_creator = ConnectionCreator(self.conf, self.logger) self.__is_completed = False def __repr__(self): return "container : %s account: %s" %(self.__container_name, self.__account_name) def is_completed(self): return self.__is_completed def read_container_stat_info(self, account_map): """ Get the stat info of container using H-File interface. """ try: conn = None self.logger.debug("Enter in read_container_stat_info for container") conn = self.connection_creator.connect_container('HEAD', \ self.__account_name, self.__container_name, self.__container_path) resp = self.connection_creator.get_http_response_instance(conn) while resp.status != 204 and resp.status != 404 and self.retry_count != 0: self.retry_count -= 1 if resp.status == 301 or resp.status == 307 resp.status == 503: self.msg.load_gl_map() conn = self.connection_creator.connect_container('HEAD', \ self.__account_name, self.__container_name, self.__container_path) resp = self.connection_creator.get_http_response_instance(conn) if resp.status == 204: headers = dict(resp.getheaders()) self.stat_info[self.__container_name] = \ {'container' : headers['x-container'], \ 'put_timestamp' : headers['x-put-timestamp'] , \ 'object_count' : headers['x-container-object-count'], \ 'bytes_used' : headers['x-container-bytes-used'], \ 'delete_timestamp' : '0', 'deleted' : False} account_map[self.__account_name, self.__container_name] = \ "success" self.__is_completed = True self.logger.info("container stats for %s: %s" \ %(self.__container_name, self.stat_info[self.__container_name])) elif resp.status == 404: self.logger.debug("Container info file %s not found" \ % self.__container_path) self.stat_info[self.__container_name] = \ {'container' : '', \ 'put_timestamp' : '0', \ 'object_count' : 0, \ 'bytes_used' : 0, \ 'delete_timestamp' : '0', 'deleted' : True} account_map[self.__account_name, self.__container_name] = \ "success" self.__is_completed = True else: self.logger.warning("Could not read stats form container: %s" \ % self.__container_name) except Exception as ex: self.logger.error("While getting container stats for:%s, Error: %s" \ % (self.__container_name, ex))
class TestConnectionCreator(unittest.TestCase): @mock.patch("osd.common.ring.ring.Req", mockReq) @mock.patch("osd.common.ring.ring.Resp", mockResp) @mock.patch("osd.common.ring.ring.RingFileReadHandler", FakeRingFileReadHandler) @mock.patch("osd.common.ring.ring.ServiceInfo", mockServiceInfo) def setUp(self): self.conn_obj = ConnectionCreator(conf, logger) @mock.patch( 'osd.accountUpdaterService.communicator.httplib.HTTPConnection', FakeHTTPConnection) def test_connect_container(self): with mock.patch( 'osd.accountUpdaterService.communicator.ServiceLocator.get_container_from_ring', return_value={ 'ip': '192.168.123.13', 'port': '3128' }): conn = self.conn_obj.connect_container( "HEAD", "acc", "082287da66aa3c25b281e06ae721a6d0", "cont_path") self.assertTrue(isinstance(conn, FakeHTTPConnection)) @mock.patch( 'osd.accountUpdaterService.communicator.httplib.HTTPConnection', FakeHTTPConnection) def test_get_http_connection_instance(self): stat_info = {'cont': ''} info = { 'recovery_flag': False, 'method_name': 'PUT_CONTAINER_RECORD', 'entity_name': '', 'body_size': len(str(stat_info)), 'account_name': "e3891c4148ce912256a07a50b69a8f48", 'flag': True } with mock.patch( 'osd.accountUpdaterService.communicator.ServiceLocator.get_container_from_ring', return_value={ 'ip': '192.168.123.13', 'port': '3128', 'fs': 'OSP_01', 'dir': 'dir1' }): conn = self.conn_obj.get_http_connection_instance(info, stat_info) self.assertTrue(isinstance(conn, FakeHTTPConnection)) with mock.patch( 'osd.accountUpdaterService.communicator.ServiceLocator.get_account_from_ring', return_value={ 'ip': '192.168.123.13', 'port': '3128', 'fs': 'OSP_01', 'dir': 'dir1' }): info['flag'] = False conn = self.conn_obj.get_http_connection_instance(info, stat_info) self.assertTrue(isinstance(conn, FakeHTTPConnection)) def test_get_http_response_instance(self): conn = FakeConnection() resp = self.conn_obj.get_http_response_instance(conn) self.assertTrue(isinstance(resp, FakeResponse))