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 AccountUpdater(Daemon):
    """
    Update container information in account listings.
    """
    def __init__(self, conf, logger=None):
        """
        constructor for account updater
        :param conf: configuration of account-updater 
        """
        self.logger = logger or SimpleLogger(conf).get_logger_object()
        Daemon.__init__(self, conf, self.logger)
        libraryUtils.OSDLoggerImpl(
            "account-updater-monitoring").initialize_logger()
        create_recovery_file('account-updater-server')
        self.conf = conf
        self.__interval = int(conf.get('interval', 1800))
        self.__ll_port = int(conf.get('llport', 61014))
        self.__account_updater_port = int(\
            conf.get('account_updater_port', 61009))
        self.__service_id = gethostname() + "_" + str(self.__ll_port) + \
            "_account-updater-server"
        self.__param = self.__get_param(conf)
        self.msg = GlobalVariables(self.logger)
        self.msg.set_service_id(self.__service_id)
        self.walker_map = WalkerMap()
        self.reader_map = ReaderMap()

        # Start sending health to local leader
        self.logger.info("Loading health monitoring library")
        self.health_instance = healthMonitoring(self.__get_node_ip\
            (gethostname()), self.__account_updater_port, \
            self.__ll_port, self.__service_id)
        self.logger.info("Loaded health monitoring library")
        remove_recovery_file('account-updater-server')

        # load global map
        if not self.msg.load_gl_map():
            sys.exit(130)
        self.logger.info("Account updater started")

    def __get_node_ip(self, hostname):
        """
        Get internal node ip on which service is running
        """
        try:
            command = "grep -w " + hostname + " /etc/hosts | awk {'print $1'}"
            child = subprocess.Popen(command, stdout = subprocess.PIPE, \
                stderr = subprocess.PIPE, shell = True)
            std_out, std_err = child.communicate()
            return std_out.strip()
        except Exception as err:
            self.logger.error("Error occurred while getting ip of node:%s" %
                              err)
            return ""

    def __get_param(self, conf):
        """
        Getting parameters through config file
        :param conf: configuration file of account-updater
        """
        return {
            'filesystems': conf.get('filesystems', '/export'),
            'stat_file_location': conf.get('stat_file_location', \
                '/export/OSP_01/updater'),
            'file_expire_time': conf.get('file_expire_time', 900),
            'interval': conf.get('interval', 600),
            'conn_timeout': float(conf.get('conn_timeout', 10)),
            'node_timeout': float(conf.get('node_timeout', 10)),
            'osd_dir': conf.get('osd_dir', '/export/.osd_meta_config'),
            'reader_max_count': int(conf.get('reader_max_count', 10)),
            'max_update_files': int(conf.get('max_update_files', 10)),
            'max_files_stored_in_cache': int(conf.get('max_files_stored_in_cache', 100)),
            'updaterfile_expire_delta_time': int(conf.get('updaterfile_expire_delta_time', 1020))
            }

    def run_forever(self, *args, **kwargs):
        """
        Run the updator continuously.
        """
        try:
            self.logger.debug('Begin account update')

            # get account-updater server ownership
            self.get_ownership_obj = threading.Thread(
                target=self.msg.get_my_ownership)
            self.get_ownership_obj.setDaemon(True)
            self.get_ownership_obj.start()

            self.walker_obj = Walker(self.walker_map, self.__param,
                                     self.logger)
            self.walker_obj.setDaemon(True)
            self.walker_obj.start()
            self.logger.info("Walker Started")
            self.reader_obj = Reader(self.walker_map, self.reader_map, \
                                self.__param, self.logger)
            self.reader_obj.setDaemon(True)
            self.reader_obj.start()
            self.logger.info("Reader Started")
            self.account_sweeper = AccountSweep(self.__param, self.logger)
            self.account_sweeper.setDaemon(True)
            self.account_sweeper.start()
            self.logger.info("Account Sweeper Started")
            self.updater_obj = Updater(self.walker_map, self.reader_map, \
                           self.__param, self.logger)
            self.updater_obj.setDaemon(True)
            self.updater_obj.start()
            self.logger.info("Updater Started")
            self.container_sweeper = ContainerSweeper(self.walker_map, \
                           self.reader_map, self.__param, self.logger)
            self.container_sweeper.setDaemon(True)
            self.container_sweeper.start()
            self.logger.info("Container Sweeper Started")

            account_updater_server = ThreadedAccountUpdaterServer(\
                (self.__get_node_ip(gethostname()), \
                self.__account_updater_port), HttpListener)
            account_updater_server.serve_forever()
        except Exception as ex:
            self.logger.error("Exception occured: %s" % ex)