Ejemplo n.º 1
0
def test_config_interfaces(test_conf_dir):
    conf_filename = os.path.join(test_conf_dir, 'example.conf')
    parser = ArgumentParser()
    parser.add_argument('-f', dest='config')
    config = IniConfig(importlib.import_module('tests.settings1'),
                       parser.parse_args(['-f', conf_filename]))

    interfaces = sorted(config.interfaces, key=lambda x: x.name)

    assert len(interfaces) == 3

    assert interfaces[0].name == 'fastrpc'
    assert interfaces[0].host == '192.168.1.0'
    assert interfaces[0].port == 4445
    assert interfaces[0].unix_socket is None
    assert len(interfaces[0].urls) == 0

    assert interfaces[1].name == 'http'
    assert interfaces[1].host == ''
    assert interfaces[1].port == 4444
    assert interfaces[1].unix_socket is None
    assert len(interfaces[1].urls) == 2

    assert interfaces[2].name == 'unix'
    assert interfaces[2].host is None
    assert interfaces[2].port is None
    assert interfaces[2].unix_socket == '/tmp/tornadoini.socket'
    assert len(interfaces[2].urls) == 3
Ejemplo n.º 2
0
def test_config_interfaces_fail_when_neither_tcp_nor_unix(test_conf_dir):
    conf_filename = os.path.join(test_conf_dir, 'example6.conf')
    with pytest.raises(ValueError) as e:
        parser = ArgumentParser()
        parser.add_argument('-f', dest='config')
        config = IniConfig(importlib.import_module('tests.settings6'),
                           parser.parse_args(['-f', conf_filename]))
        _ = config.interfaces
    assert "Interface MUST listen either on TCP or UNIX socket" in str(e)
Ejemplo n.º 3
0
def test_config_interfaces_both_tcp_and_unix(test_conf_dir):
    conf_filename = os.path.join(test_conf_dir, 'example5.conf')
    parser = ArgumentParser()
    parser.add_argument('-f', dest='config')
    config = IniConfig(importlib.import_module('tests.settings5'),
                       parser.parse_args(['-f', conf_filename]))

    interface = config.interfaces[0]

    assert interface.name == 'http_both_tcp_and_unix'
    assert interface.host == ''
    assert interface.port == 4444
    assert interface.unix_socket == '/tmp/tornadoini.socket'
Ejemplo n.º 4
0
def test_config_interface_fail_when_neither_tcp_nor_unix():
    config = Config(importlib.import_module('tests.settings6'),
                    ArgumentParser())

    with pytest.raises(ValueError) as e:
        _ = config.interfaces
    assert "Interface MUST listen either on TCP or UNIX socket" in str(e)
Ejemplo n.º 5
0
def test_config_interfaces_both_tcp_and_unix():
    config = Config(importlib.import_module('tests.settings5'),
                    ArgumentParser())

    interface = config.interfaces[0]

    assert interface.name == 'http_both_tcp_and_unix'
    assert interface.host == ''
    assert interface.port == 4443
    assert interface.unix_socket == '/tmp/tornado.socket'
Ejemplo n.º 6
0
def test_config_interfaces():
    config = Config(importlib.import_module('tests.settings1'),
                    ArgumentParser())

    interfaces = sorted(config.interfaces, key=lambda x: x.name)

    assert len(interfaces) == 2

    assert interfaces[0].name == 'fastrpc'
    assert interfaces[0].host == '192.168.1.0'
    assert interfaces[0].port == 4445
    assert len(interfaces[0].urls) == 0

    assert interfaces[1].name == 'http'
    assert interfaces[1].host == ''
    assert interfaces[1].port == 4443
    assert len(interfaces[1].urls) == 2
Ejemplo n.º 7
0
def test_get_tornado_apps():
    config = Config(
        importlib.import_module('tests.settings1'),
        ArgumentParser()
    )
    context = config.context_class.from_config(config)

    apps = sorted(
        get_tornado_apps(context), key=lambda x: x.settings['interface'].name)

    assert len(apps) == 2

    assert isinstance(apps[0].settings['context'], Context)
    assert isinstance(apps[0].settings['interface'], Config.Interface)
    assert apps[0].settings['interface'].name == 'fastrpc'

    assert isinstance(apps[1].settings['context'], Context)
    assert isinstance(apps[1].settings['interface'], Config.Interface)
    assert apps[1].settings['interface'].name == 'http'
Ejemplo n.º 8
0
def main(args=None):
    """
    Run management command handled from command line.
    """
    # Base command line parser. Help is not allowed because command
    # line is parsed in two stages - in the first stage is found setting
    # module of the application, in the second stage are found management
    # command's arguments.
    parser = ArgumentParser(add_help=False)
    parser.add_argument('-s',
                        '--settings',
                        dest='settings',
                        action='store',
                        type=str,
                        default=None,
                        help=_('application settings module'))

    # Get settings module
    try:
        settings = get_app_settings(parser, args)
    except ImportError as exc:
        parser.error(_("Invalid application settings module: {}").format(exc))

    # Get management commands and add their arguments into command
    # line parser
    commands = get_management_commands(settings)
    subparsers = parser.add_subparsers(dest='action', help=_('specify action'))
    for command_cls in six.itervalues(commands):
        subparser = subparsers.add_parser(command_cls.name,
                                          help=command_cls.help)
        for command_args, kwargs in command_cls.arguments:
            subparser.add_argument(*command_args, **kwargs)

    # Get config class and add its arguments into command line parser
    if settings:
        config_cls = get_config_class(settings)
        if not issubclass(config_cls, Config):
            raise TypeError("Config class must be subclass of the "
                            "shelter.core.config.Config")
        for config_args, kwargs in config_cls.arguments:
            parser.add_argument(*config_args, **kwargs)
    else:
        config_cls = Config

    # Add help argument and parse command line
    parser.add_argument('-h',
                        '--help',
                        action='help',
                        help=_('show this help message and exit'))
    cmdline_args = parser.parse_args(args)
    if not cmdline_args.action:
        parser.error(_('No action'))

    # Run management command
    command_cls = commands[cmdline_args.action]
    if command_cls.settings_required and not settings:
        parser.error(
            _("Settings module is not defined. You must either set "
              "'SHELTER_SETTINGS_MODULE' environment variable or "
              "'-s/--settings' command line argument."))
    try:
        config = config_cls(settings, cmdline_args)
    except ImproperlyConfiguredError as exc:
        parser.error(str(exc))
    command = command_cls(config)
    try:
        command()
    except Exception:
        traceback.print_exc(file=sys.stderr)
        sys.stderr.flush()
        sys_exit(1)
    sys_exit(0)
Ejemplo n.º 9
0
def test_config_context_class_user():
    config = Config(importlib.import_module('tests.settings2'),
                    ArgumentParser())
    assert config.context_class is not Context
    assert config.context_class is ContextTest
Ejemplo n.º 10
0
def test_config_context_class_default():
    config = Config(importlib.import_module('tests.settings1'),
                    ArgumentParser())
    assert config.context_class is Context