def test_user_logger(self):
        logger = logging.getLogger(__name__)

        with tempfile.NamedTemporaryFile() as log_file:
            log_handler = gateway_logging.user_logger(log_file.name)

            logger.addHandler(log_handler)
            test_log = 'Test log'

            logger.info(test_log)
            log_handler.close()

            # file has data
            log_content = log_file.read().decode()
            self.assertNotEqual(0, len(log_content))
            self.assertIn(test_log, log_content)
    def test_user_logger(self):

        logger = logging.getLogger(__name__)

        log_file = 'test_log_file.log'
        log_handler = gateway_logging.user_logger(log_file)

        logger.addHandler(log_handler)

        for i in range(0, 100):
            logger.info('Test log %d', i)
        log_handler.close()

        # file has data
        self.assertNotEquals(0, os.path.getsize(log_file))

        os.remove(log_file)
Ejemplo n.º 3
0
    def exp_start(self, user, exp_id,  # pylint: disable=R0913
                  firmware_path=None, profile_dict=None, timeout=0):
        """
        Start an experiment

        :param exp_id: experiment id
        :param user: user running the experiment
        :param firmware_path: path of the firmware file to use, can be None
        :param profile_dict: monitoring profile
        :param timeout: Experiment expiration timeout. On 0 no timeout.

        Experiment start steps

        1) Prepare Gateway: User experiment files and log:
        2) Prepare Control node: Start communication and power on open node
        3) Prepare Open node: Check OK, setup firmware and serial redirection
        4) Configure Control Node Profile and experiment
        5) Set Experiment expiration timer

        """
        if self.experiment_is_running:
            LOGGER.debug('Experiment running. Stop previous experiment')
            self.exp_stop()

        try:
            profile = self.board_cfg.profile_from_dict(profile_dict)
        except ValueError as err:
            LOGGER.error('%r', err)
            return 1
        if not elftarget.is_compatible_with_node(firmware_path,
                                                 self.open_node):
            LOGGER.error('Invalid firmware target, aborting experiment.')
            return 1

        ret_val = 0

        self.experiment_is_running = True
        self.exp_id = exp_id
        self.user = user

        if (self.board_cfg.robot_type == 'turtlebot2' or
                self.board_cfg.cn_class.TYPE == 'no'):  # pragma: no cover
            LOGGER.info('Create user exp folder')
            self._create_user_exp_folders(user, exp_id)

        self.exp_files = self.create_user_exp_files(self.board_cfg.node_id,
                                                    user, exp_id)

        # Create user log
        self.user_log_handler = gateway_logging.user_logger(
            self.exp_files['log'])
        LOGGER.addHandler(self.user_log_handler)
        LOGGER.info('Start experiment: %s-%i', user, exp_id)

        # Init ControlNode
        ret_val += self.control_node.start(self.exp_id, self.exp_files)

        # with Pycom boards, trigger 2 power-cycle to ensure REPL is correctly
        # started
        if self.open_node.TYPE == 'pycom':
            for _ in range(2):
                LOGGER.debug("Power cycle %s board", self.open_node.TYPE)
                ret_val += self.control_node.open_stop()
                ret_val += wait_no_tty(self.open_node.TTY, timeout=10)
                ret_val += self.control_node.open_start()
                ret_val += wait_tty(self.open_node.TTY, LOGGER, timeout=10)
        # Configure Open Node
        ret_val += self.open_node.setup(firmware_path)
        # Configure experiment and monitoring on ControlNode
        ret_val += self.control_node.start_experiment(profile)

        # nrf52dk and nrf52840dk needs a power cycle before their serial
        # becomes fully usable.
        if (firmware_path is not None and
                self._board_require_power_cycle(self.open_node.TYPE)):
            LOGGER.info("Power cycle node %s",
                        self.control_node.node_id.replace('_', '-'))
            ret_val += self.control_node.open_stop()
            ret_val += self.control_node.open_start()

        if timeout != 0:
            LOGGER.debug("Setting timeout to: %d", timeout)
            self.timeout_timer = Timer(timeout, self._timeout_exp_stop,
                                       args=(exp_id, user))
            self.timeout_timer.start()
        LOGGER.info("Start experiment succeeded")
        return ret_val
Ejemplo n.º 4
0
    def exp_start(self, user, exp_id,  # pylint: disable=R0913
                  firmware_path=None, profile_dict=None, timeout=0):
        """
        Start an experiment

        :param exp_id: experiment id
        :param user: user running the experiment
        :param firmware_path: path of the firmware file to use, can be None
        :param profile_dict: monitoring profile
        :param timeout: Experiment expiration timeout. On 0 no timeout.

        Experiment start steps

        1) Prepare Gateway: User experiment files and log:
        2) Prepare Control node: Start communication and power on open node
        3) Prepare Open node: Check OK, setup firmware and serial redirection
        4) Configure Control Node Profile and experiment
        5) Set Experiment expiration timer

        """
        if self.experiment_is_running:
            LOGGER.debug('Experiment running. Stop previous experiment')
            self.exp_stop()

        try:
            profile = self.board_cfg.profile_from_dict(profile_dict)
        except ValueError as err:
            LOGGER.error('%r', err)
            return 1
        if not elftarget.is_compatible_with_node(firmware_path,
                                                 self.open_node):
            LOGGER.error('Invalid firmware target, aborting experiment.')
            return 1

        ret_val = 0

        self.experiment_is_running = True
        self.exp_id = exp_id
        self.user = user

        if (self.board_cfg.robot_type == 'turtlebot2' or
                self.board_cfg.cn_class.TYPE == 'no'):  # pragma: no cover
            LOGGER.info('Create user exp folder')
            self._create_user_exp_folders(user, exp_id)

        self.exp_files = self.create_user_exp_files(self.board_cfg.node_id,
                                                    user, exp_id)

        # Create user log
        self.user_log_handler = gateway_logging.user_logger(
            self.exp_files['log'])
        LOGGER.addHandler(self.user_log_handler)
        LOGGER.info('Start experiment: %s-%i', user, exp_id)

        # Init ControlNode
        ret_val += self.control_node.start(self.exp_id, self.exp_files)
        # Configure Open Node
        ret_val += self.open_node.setup(firmware_path)
        # Configure experiment and monitoring on ControlNode
        ret_val += self.control_node.start_experiment(profile)

        if timeout != 0:
            LOGGER.debug("Setting timeout to: %d", timeout)
            self.timeout_timer = Timer(timeout, self._timeout_exp_stop,
                                       args=(exp_id, user))
            self.timeout_timer.start()
        LOGGER.info("Start experiment succeeded")
        return ret_val