def test_bool_validation(): c = config.Config() t.eq(c.preload_app, False) c.set("preload_app", True) t.eq(c.preload_app, True) c.set("preload_app", "true") t.eq(c.preload_app, True) c.set("preload_app", "false") t.eq(c.preload_app, False) t.raises(ValueError, c.set, "preload_app", "zilch") t.raises(TypeError, c.set, "preload_app", 4)
def test_bool_validation(): c = config.Config() assert c.preload_app is False c.set("preload_app", True) assert c.preload_app is True c.set("preload_app", "true") assert c.preload_app is True c.set("preload_app", "false") assert c.preload_app is False pytest.raises(ValueError, c.set, "preload_app", "zilch") pytest.raises(TypeError, c.set, "preload_app", 4)
def test_bool_validation(): c = config.Config() t.eq(c.debug, False) c.set("debug", True) t.eq(c.debug, True) c.set("debug", "true") t.eq(c.debug, True) c.set("debug", "false") t.eq(c.debug, False) t.raises(ValueError, c.set, "debug", "zilch") t.raises(TypeError, c.set, "debug", 4)
def load_config(self): self.cfg = config.Config() self.cfg.set('paste', self.cfgurl) self.cfg.set('logconfig', self.cfgpath) cfg = paste_config(self.cfg, self.cfgurl, self.relpath, self.settings) for k, v in cfg.items(): self.cfg.set(k.lower(), v) default_config = config.get_default_config_file() if default_config is not None: self.load_config_from_file(default_config)
def test_pos_int_validation(): c = config.Config() assert c.workers == 1 c.set("workers", 4) assert c.workers == 4 c.set("workers", "5") assert c.workers == 5 c.set("workers", "0xFF") assert c.workers == 255 c.set("workers", True) assert c.workers == 1 # Yes. That's right... pytest.raises(ValueError, c.set, "workers", -21) pytest.raises(TypeError, c.set, "workers", c)
def test_pos_int_validation(): c = config.Config() t.eq(c.workers, 1) c.set("workers", 4) t.eq(c.workers, 4) c.set("workers", "5") t.eq(c.workers, 5) c.set("workers", "0xFF") t.eq(c.workers, 255) c.set("workers", True) t.eq(c.workers, 1) # Yes. That's right... t.raises(ValueError, c.set, "workers", -21) t.raises(TypeError, c.set, "workers", c)
def test_init(self, mocker): from bigchaindb.log.configs import (DEFAULT_SOCKET_LOGGING_ADDR as expected_socket_address) from bigchaindb.log.loggers import HttpServerLogger from gunicorn import config logger_config = config.Config() logger = HttpServerLogger(logger_config) assert len(logger.access_log.handlers) == 1 assert len(logger.error_log.handlers) == 1 assert isinstance(logger.access_log.handlers[0], SocketHandler) assert isinstance(logger.error_log.handlers[0], SocketHandler) assert logger.access_log.handlers[0].address == expected_socket_address assert logger.error_log.handlers[0].address == expected_socket_address
def test_property_access(): c = config.Config() for s in config.KNOWN_SETTINGS: getattr(c, s.name) # Class was loaded assert c.worker_class == SyncWorker # logger class was loaded assert c.logger_class == glogging.Logger # Workers defaults to 1 assert c.workers == 1 c.set("workers", 3) assert c.workers == 3 # Address is parsed assert c.address == [(AF_INET, SOCK_STREAM, 6, '', ('127.0.0.1', 8000))] \ or c.address == [(AF_INET6, SOCK_STREAM, 6, '', ('::1', 8000, 0, 0)), \ (AF_INET, SOCK_STREAM, 6, '', ('127.0.0.1', 8000))] # User and group defaults assert os.geteuid() == c.uid assert os.getegid() == c.gid # Proc name assert "gunicorn" == c.proc_name # Not a config property pytest.raises(AttributeError, getattr, c, 'foo') # Force to be not an error class Baz(object): def get(self): return 3.14 c.settings["foo"] = Baz() assert c.foo == 3.14 # Attempt to set a cfg not via c.set pytest.raises(AttributeError, setattr, c, "proc_name", "baz") # No setting for name pytest.raises(AttributeError, c.set, "baz", "bar")
def load_config(self): self.cfg = gconfig.Config(self.usage, prog=self.prog) settings = { 'bind': '%s:%s' % (cfg.CONF.bind_host, cfg.CONF.bind_port), 'workers': cfg.CONF.api_workers, 'worker_connections': cfg.CONF.gunicorn.worker_connections, 'worker_class': cfg.CONF.gunicorn.worker_class, 'proc_name': 'neutron-server', 'accesslog': cfg.CONF.gunicorn.access_log, 'errorlog': cfg.CONF.gunicorn.error_log, 'limit_request_line': cfg.CONF.gunicorn.limit_request_line, 'loglevel': cfg.CONF.gunicorn.loglevel, 'access_log_format': ' '.join(( '%(h)s', '%(l)s', '%(u)s', '%(t)s', '"%(r)s"', '%(s)s', '%(b)s', '"%(f)s"', '"%(a)s"', '%(T)s', '%(D)s', '"%({X_USER_ID}i)s"', '"%({X_TENANT_ID}i)s"', '%({X-Forward-For}i)s', )), } for k, v in settings.iteritems(): self.cfg.set(k.lower(), v)
def test_post_request(): c = config.Config() def post_request_4(worker, req, environ, resp): return 4 def post_request_3(worker, req, environ): return 3 def post_request_2(worker, req): return 2 c.set("post_request", post_request_4) assert c.post_request(1, 2, 3, 4) == 4 c.set("post_request", post_request_3) assert c.post_request(1, 2, 3, 4) == 3 c.set("post_request", post_request_2) assert c.post_request(1, 2, 3, 4) == 2
def test_post_request(): c = config.Config() def post_request_4(worker, req, environ, resp): return 4 def post_request_3(worker, req, environ): return 3 def post_request_2(worker, req): return 2 c.set("post_request", post_request_4) t.eq(4, c.post_request(1, 2, 3, 4)) c.set("post_request", post_request_3) t.eq(3, c.post_request(1, 2, 3, 4)) c.set("post_request", post_request_2) t.eq(2, c.post_request(1, 2, 3, 4))
def test_property_access(): c = config.Config() for s in config.KNOWN_SETTINGS: getattr(c, s.name) # Class was loaded t.eq(c.worker_class, SyncWorker) # Workers defaults to 1 t.eq(c.workers, 1) c.set("workers", 3) t.eq(c.workers, 3) # Address is parsed t.eq(c.address, [("127.0.0.1", 8000)]) # User and group defaults t.eq(os.geteuid(), c.uid) t.eq(os.getegid(), c.gid) # Proc name t.eq("gunicorn", c.proc_name) # Not a config property t.raises(AttributeError, getattr, c, "foo") # Force to be not an error class Baz(object): def get(self): return 3.14 c.settings["foo"] = Baz() t.eq(c.foo, 3.14) # Attempt to set a cfg not via c.set t.raises(AttributeError, setattr, c, "proc_name", "baz") # No setting for name t.raises(AttributeError, c.set, "baz", "bar")
def read_user_configuration(default_configuration, command_line_parser): configuration = default_configuration args, unknown_args = command_line_parser.parse_known_args() if args.configuration_file: file_configuration = {} with open(args.configuration_file, "r") as file: exec(file.read(), {}, file_configuration) # Configuration file overloads default configuration update(configuration, file_configuration) # Command line configuration overloads all configuration gunicorn_parser = config.Config().parser() configuration = update(configuration, vars(args)) configuration = update(configuration, vars(gunicorn_parser.parse_args(unknown_args))) if configuration['args']: command_line_parser.print_help() log.error('Unexpected positional argument {}'.format(configuration['args'])) sys.exit(1) return configuration
def load_config(self): self.cfg = gconfig.Config(self.usage, prog=self.prog) settings = {'bind': '%s:%s' % (BIND_HOST, BIND_PORT), 'workers': WORKERS, 'worker_connections': WORKER_CONNECTIONS, 'worker_class': WORKER_CLASS, 'proc_name': PROC_NAME, 'limit_request_line': LIMIT_LINE_REQUEST, 'loglevel': LOG_LEVEL, 'access_log_format': ' '.join(('%(h)s', '%(l)s', '%(u)s', '%(t)s', '"%(r)s"', '%(s)s', '%(b)s', '"%(f)s"', '"%(a)s"', '%(T)s', '%(D)s',))} for k, v in settings.items(): self.cfg.set(k.lower(), v)
def test_statsd_socket_changes_logger(): c = config.Config() assert c.logger_class == glogging.Logger c.set('statsd_socket', '/var/run/sock') assert c.logger_class == statsd.Statsd
def test_statsd_host_changes_logger(): c = config.Config() assert c.logger_class == glogging.Logger c.set('statsd_host', 'localhost:12345') assert c.logger_class == statsd.Statsd
def test_repr(): c = config.Config() c.set("workers", 5) assert "with value 5" in repr(c.settings['workers'])
def test_str_validation(): c = config.Config() assert c.proc_name == "gunicorn" c.set("proc_name", " foo ") assert c.proc_name == "foo" pytest.raises(TypeError, c.set, "proc_name", 2)
def test_str_validation(): c = config.Config() t.eq(c.proc_name, "gunicorn") c.set("proc_name", " foo ") t.eq(c.proc_name, "foo") t.raises(TypeError, c.set, "proc_name", 2)
def test_ssl_version_bad(ssl_version): c = config.Config() with pytest.raises(ValueError) as exc: c.set("ssl_version", ssl_version) assert 'Valid options' in str(exc.value) assert "TLSv" in str(exc.value)
def test_defaults(): c = config.Config() for s in config.KNOWN_SETTINGS: assert c.settings[s.name].validator( s.default) == c.settings[s.name].get()
def test_defaults(): c = config.Config() for s in config.KNOWN_SETTINGS: t.eq(c.settings[s.name].validator(s.default), c.settings[s.name].get())