コード例 #1
0
    def test_parse_cmd_args(self):
        description = "description"
        descr = 'test_app'
        test_path = 'test_path'
        pid = str(12345)

        args = ["-c", test_path]
        result = utils.parse_cmd_args(args, description)
        self.assertEqual(
            result,
            argparse.Namespace(config=test_path, daemon=False, pidfile=None))

        args = ["-c", test_path, "-d"]
        result = utils.parse_cmd_args(args, description)
        self.assertEqual(
            result,
            argparse.Namespace(config=test_path, daemon=True, pidfile=None))

        args = ["-c", test_path, "-P", pid]
        result = utils.parse_cmd_args(args, description)
        self.assertEqual(
            result,
            argparse.Namespace(config=test_path, daemon=False, pidfile=pid))

        args = ["-c", test_path, "-d", "-P", pid]
        result = utils.parse_cmd_args(args, description)
        self.assertEqual(
            result,
            argparse.Namespace(config=test_path, daemon=True, pidfile=pid))
        pass
コード例 #2
0
 def test_parse_cmd_args_without_params(self):
     """
     нет параметров командной строки, нет обязательного параметра
     :return:
     """
     with mock.patch('sys.exit', mock.Mock()) as sys_exit:
         utils.parse_cmd_args([])
         assert mock.call(0) not in sys_exit.call_args_list
コード例 #3
0
 def test_parse_cmd_args_exist(self):
     """
     Test correct work with existing cmd params
     """
     app_description = "this is description"
     parameters = ['-d', '--config', '-P', app_description]
     parser = Mock()
     with patch('argparse.ArgumentParser', Mock(return_value=parser)):
         utils.parse_cmd_args(parameters)
         parser.parse_args.assert_called_once_with(args=parameters)
コード例 #4
0
 def test_parse_cmd_args_exist(self):
     """
     Test correct work with existing cmd params
     """
     app_description = "this is description"
     parameters = ['-d', '--config', '-P', app_description]
     parser = Mock()
     with patch('argparse.ArgumentParser', Mock(return_value=parser)):
         utils.parse_cmd_args(parameters)
         parser.parse_args.assert_called_once_with(args=parameters)
コード例 #5
0
 def test_parse_cmd_args_with_params(self):
     """
     сутствуют все аргументы командной строки
     :return:
     """
     my_parser = mock.Mock()
     daemon_param = '-d'
     config_param = '-c'
     pid_param = '-P'
     config_path = 'some path'
     pidfile = "pidfile"
     args = [config_param, config_path, daemon_param, pid_param, pidfile]
     with mock.patch('source.lib.utils.argparse.ArgumentParser', mock.Mock(return_value=my_parser)):
         utils.parse_cmd_args(args)
         my_parser.parse_args.assert_called_once_with(args=args)
コード例 #6
0
 def test_mixed_name_argument_parsed_without_daemon_flag(self):
     path = '/test/path'
     pid = 1
     description = 'description'
     args = ['-c', path, '--pid', str(pid)]
     result = utils.parse_cmd_args(args, description)
     self.assertEqual(result, argparse.Namespace(config=path, daemon=False, pidfile=str(pid)))
コード例 #7
0
 def test_all_short_name_argument_parsed(self):
     path = '/test/path'
     pid = 1
     description = 'description'
     args = ['-c', path, '-d', '-P', str(pid)]
     result = utils.parse_cmd_args(args, description)
     self.assertEqual(result, argparse.Namespace(config=path, daemon=True, pidfile=str(pid)))
コード例 #8
0
def main(argv):
    """
    Точка входа в приложение.

    В случае возникновения ошибки в приложении, оно засыпает на config.SLEEP_ON_FAIL секунд.

    :param argv: агрументы командной строки.
    :type argv: list
    """
    args = utils.parse_cmd_args(argv[1:])
    config = utils.prepare(args)

    dictConfig(config.LOGGING)
    current_thread().name = 'pusher.main'
    install_signal_handlers()
    patch_all()

    while run_application:
        try:
            main_loop(config)
        except Exception as exc:
            logger.error(
                'Error in main loop. Go to sleep on {} second(s).'.format(config.SLEEP_ON_FAIL)
            )
            logger.exception(exc)

            sleep(config.SLEEP_ON_FAIL)
    else:
        logger.info('Stop application loop in main.')

    return exit_code
コード例 #9
0
def main(argv):
    args = utils.parse_cmd_args(argv[1:])
    config = utils.prepare(args)
    dictConfig(config.LOGGING)

    main_loop(config)

    return config.EXIT_CODE
コード例 #10
0
    def test_parse_cmd_args(self):
        import argparse

        descr = 'test_app'
        config_path = '/file/path'
        pid = 42
        args = '%s -c %s -d -P %d' % (descr, config_path, pid)
        obj = utils.parse_cmd_args(args.split(' ')[1:], descr)
        self.assertEqual(obj, argparse.Namespace(config=config_path, daemon=True, pidfile=str(pid)))
コード例 #11
0
 def test_mixed_name_argument_parsed_without_daemon_flag(self):
     path = '/test/path'
     pid = 1
     description = 'description'
     args = ['-c', path, '--pid', str(pid)]
     result = utils.parse_cmd_args(args, description)
     self.assertEqual(
         result,
         argparse.Namespace(config=path, daemon=False, pidfile=str(pid)))
コード例 #12
0
 def test_all_short_name_argument_parsed(self):
     path = '/test/path'
     pid = 1
     description = 'description'
     args = ['-c', path, '-d', '-P', str(pid)]
     result = utils.parse_cmd_args(args, description)
     self.assertEqual(
         result,
         argparse.Namespace(config=path, daemon=True, pidfile=str(pid)))
コード例 #13
0
 def test_parse_cmd_args(self):
     description = "description"
     descr = 'test_app'
     test_path = 'test_path'
     pid = str(12345)
     
     args = ["-c", test_path]
     result = utils.parse_cmd_args(args, description)
     self.assertEqual(result, argparse.Namespace(config=test_path, daemon=False, pidfile=None))
     
     args = ["-c", test_path, "-d"]
     result = utils.parse_cmd_args(args, description)
     self.assertEqual(result, argparse.Namespace(config=test_path, daemon=True, pidfile=None))
     
     args = ["-c", test_path,"-P", pid]
     result = utils.parse_cmd_args(args, description)
     self.assertEqual(result, argparse.Namespace(config=test_path, daemon=False, pidfile=pid))
     
     args = ["-c", test_path, "-d", "-P", pid]
     result = utils.parse_cmd_args(args, description)
     self.assertEqual(result, argparse.Namespace(config=test_path, daemon=True, pidfile=pid))
     pass
コード例 #14
0
def main(argv):
    """
    Точка входа в приложение.

    В случае возникновения ошибки в приложении, оно засыпает на config.SLEEP_ON_FAIL секунд.

    :param argv: агрументы командной строки.
    :type argv: list
    """
    args = parse_cmd_args(argv[1:])
    if args.daemon:
        daemonize()

    if args.pidfile:
        create_pidfile(args.pidfile)

    config = load_config_from_pyfile(
        os.path.realpath(os.path.expanduser(args.config))
    )

    patch_all()

    dictConfig(config.LOGGING)

    current_thread().name = 'pusher.main'

    install_signal_handlers()

    while run:
        try:
            main_loop(config)

        except Exception as exc:
            logger.error(
                'Error in main loop. Go to sleep on {} second(s).'.format(config.SLEEP_ON_FAIL)
            )
            logger.exception(exc)

            sleep(config.SLEEP_ON_FAIL)

    logger.info('Stop application loop in main.')

    return exit_code
コード例 #15
0
 def test_parse_cmd_args(self):
     args = ["-c", "config", "-d"]
     parsed_args = parse_cmd_args(args)
     import argparse
     self.assertEqual(argparse.Namespace(config='config', daemon=True, pidfile=None), parsed_args)
コード例 #16
0
 def test_parse_cmd_args(self):
     mock_args = 'args'
     with mock.patch('source.lib.utils.argparse.ArgumentParser.parse_args', mock.Mock()) as mock_parse:
         utils.parse_cmd_args(mock_args)
     mock_parse.assert_called_once()