コード例 #1
0
class ServiceLocator:
    """
    Give the node information. 
    """
    def __init__(self, osd_dir, logger):
        """
        Constructor for UpdaterRing.
        """
        self.container_ring = ContainerRing(osd_dir, logger)
        self.account_ring = AccountRing(osd_dir, logger)
        self.logger = logger
        self.msg = GlobalVariables(self.logger)
        self.shift_param = 512

    def get_service_details(self, service_obj):
        node = {}
        node['ip'] = service_obj.get_ip()
        node['port'] = service_obj.get_port()
        return node

    def get_container_from_ring(self, account_hash, container_hash):
        """get container node info from ring"""
        #TODO: Needs to modify to get complete node info(i.e. fs, dir)
        comp_no = Calculation.evaluate(container_hash, self.shift_param) - 1
        node = self.get_service_details(\
            self.msg.get_container_map()[comp_no])
        node['fs'] = self.container_ring.get_filesystem()
        node['dir'] = self.get_cont_dir_by_hash(\
            account_hash, container_hash)
        return node

    def get_account_from_ring(self, account_hash):
        """get account node info from ring"""
        #TODO: Needs to modify to get complete node info(i.e. fs)
        comp_no = Calculation.evaluate(account_hash, self.shift_param) - 1
        node = self.get_service_details(\
            self.msg.get_account_map()[comp_no])
        node['fs'] = self.account_ring.get_filesystem()
        node['account'] = account_hash
        node['dir'] = self.get_acc_dir_by_hash(account_hash)
        return node

    def get_acc_dir_by_hash(self, key):
        return self.account_ring.get_directory_by_hash(key)

    def get_cont_dir_by_hash(self, acc_hash, cont_hash):
        return self.container_ring.get_directory_by_hash(acc_hash, cont_hash)
コード例 #2
0
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")