class TestGlobalVariables(unittest.TestCase):
    @mock.patch("osd.accountUpdaterService.monitor.Req", mockReq)
    @mock.patch("osd.accountUpdaterService.monitor.Resp", mockResp)
    @mock.patch("osd.accountUpdaterService.monitor.ServiceInfo",
                mockServiceInfo)
    def setUp(self):
        self.global_var = GlobalVariables(logger)
        self.global_var.set_service_id("service_id")

    def test_load_gl_map(self):
        #self.assertTrue(self.global_var.load_gl_map())
        with mock.patch(
                'osd.accountUpdaterService.unitTests.mockReq.connector',
                return_value=None):
            self.assertFalse(self.global_var.load_gl_map())
        with mock.patch(
                'osd.accountUpdaterService.unitTests.mockStatus.get_status_code',
                return_value="Resp.FAILURE"):
            self.assertFalse(self.global_var.load_gl_map())

    def test_load_ownership(self):
        with mock.patch(
                'osd.accountUpdaterService.unitTests.mockReq.connector',
                return_value=None):
            self.global_var.load_ownership()
            self.assertEquals(self.global_var.get_ownershipList(), [])
        with mock.patch(
                'osd.accountUpdaterService.unitTests.mockStatus.get_status_code',
                return_value="Resp.FAILURE"):
            self.global_var.load_ownership()
            self.assertEquals(self.global_var.get_ownershipList(), [])
        self.global_var.load_ownership()
        self.assertEquals(self.global_var.get_ownershipList(), range(1, 513))

    def test_get_account_map(self):
        self.assertEquals(
            [obj.get_id() for obj in self.global_var.get_account_map()],
            acc_id)
        self.assertEquals(
            [obj.get_ip() for obj in self.global_var.get_account_map()],
            acc_ip)
        self.assertEquals(
            [obj.get_port() for obj in self.global_var.get_account_map()],
            acc_port)

    def test_get_container_map(self):
        self.assertEquals(
            [obj.get_id() for obj in self.global_var.get_container_map()],
            cont_id)
        self.assertEquals(
            [obj.get_ip() for obj in self.global_var.get_container_map()],
            cont_ip)
        self.assertEquals(
            [obj.get_port() for obj in self.global_var.get_container_map()],
            cont_port)

    def test_get_acc_updater_map(self):
        self.assertEquals(
            [obj.get_id() for obj in self.global_var.get_acc_updater_map()],
            au_id)
        self.assertEquals(
            [obj.get_ip() for obj in self.global_var.get_acc_updater_map()],
            au_ip)
        self.assertEquals(
            [obj.get_port() for obj in self.global_var.get_acc_updater_map()],
            au_port)

    def test_get_acc_updater_map_version(self):
        self.assertEquals(self.global_var.get_acc_updater_map_version(), "5.0")

    def test_get_global_map_version(self):
        self.assertEquals(self.global_var.get_global_map_version(), "5.0")

    def test_get_service_id(self):
        self.assertEquals(self.global_var.get_service_id(), "service_id")
Пример #2
0
class ConnectionCreator:
    """
    Established the connection.
    """
    def __init__(self, conf, logger):
        """
        Constructor for Communicator.
        """
        self.__service_locator = ServiceLocator(conf['osd_dir'], logger)
        self.__account_updater_timeout = AccountUpdaterTimeout()
        self.__html_header_builder = HtmlHeaderBuilder()
        self.__conn_timeout = int(conf['conn_timeout'])
        self.__node_timeout = int(conf['node_timeout'])
        self.conf = conf
        self.logger = logger
        self.msg = GlobalVariables(self.logger)

    def connect_container(self, method, account_name, container_name, \
                                                    container_path):
        self.logger.debug('Enter in connect_container')
        use_hub("selects")
        node = self.__service_locator.get_container_from_ring(\
            account_name, container_name)
        conn = None
        headers = None
        shift_param = 512
        with self.__account_updater_timeout.get_connection_timeout(\
            self.__conn_timeout):
            headers = self.__html_header_builder.get_headers(None)
            headers['x-updater-request'] = True
            headers['x-component-number'] = Calculation.evaluate(\
                  container_name, shift_param) - 1
            headers['x-global-map-version'] = self.msg.get_global_map_version()
            conn = httplib.HTTPConnection(node['ip'], node['port'])
            conn.request(method, container_path, '', headers)
        return conn

    def get_http_connection_instance(self, info, data):
        """
        :return conn: connection object to service.
        """
        self.logger.debug('Enter in get_http_connection_instance')
        headers = None
        conn = None
        shift_param = 512
        if info['flag']:
            node = self.__service_locator.get_container_from_ring(\
                        info['account_name'], data.keys()[0])
        else:
            node = self.__service_locator.get_account_from_ring(
                info['account_name'])
        headers = self.__html_header_builder.get_headers(None)
        info['entity_name'] = info['entity_name'].encode("utf-8")
        with self.__account_updater_timeout.get_connection_timeout(\
            self.__conn_timeout):
            self.logger.debug('Connecting to : node: %s, fs: %s, account: \
                %s, message body size: %s'                                           % (node, node['fs'], \
                info['account_name'], info['body_size']))
            headers['account'] = info['account_name']
            headers['filesystem'] = node['fs']
            headers['dir'] = node['dir']
            headers['x-component-number'] = Calculation.evaluate(\
                  info['account_name'], shift_param) - 1
            headers['x-global-map-version'] = self.msg.get_global_map_version()
            conn = httplib.HTTPConnection(node['ip'], node['port'])
            conn.request("PUT_CONTAINER_RECORD", '', data, headers)
        self.logger.debug('Exit from get_http_connection_instance')
        return conn

    def get_http_response_instance(self, conn):
        """
        Get the response from service.

        :return resp: response from service.
        """
        self.logger.debug('Enter in get_http_response_instance')
        with self.__account_updater_timeout.get_node_timeout(\
            self.__node_timeout):
            resp = conn.getresponse()
            return resp