def test_cli_custom_argument_parser_cli(self): parser = get_argument_parser() parser.add_argument("some_parameter") cli_args = ["param", "--bg-host", "the_host"] parsed_args = parser.parse_args(cli_args) config = load_config(cli_args=cli_args, argument_parser=parser) assert config.bg_host == "the_host" assert parsed_args.some_parameter == "param" assert "some_parameter" not in config
def main(): raw_req = YAML(typ="safe").load(Path("templates/echo.yaml")) req = {"_" + k: v for k, v in raw_req.items() if k != "parameters"} req.update(raw_req.get("parameters", {})) futures = [] sys_client = SystemClient(**load_config(), blocking=False) start = datetime.now() for _ in range(250_000): futures.append(sys_client.send_bg_request(**req))
def __init__(self, client=None, system=None, logger=None, **kwargs): self._client = None self._instance = None self._admin_processor = None self._request_processor = None self._shutdown_event = threading.Event() # Need to set up logging before loading config self._custom_logger = False self._logger = self._setup_logging(logger=logger, **kwargs) # Now that logging is configured we can load the real config self._config = load_config(**kwargs) # If global config has already been set that's a warning global CONFIG if len(CONFIG): self._logger.warning( "Global CONFIG object is not empty! If multiple plugins are running in " "this process please ensure any [System|Easy|Rest]Clients are passed " "connection information as kwargs as auto-discovery may be incorrect." ) CONFIG = Box(self._config.to_dict(), default_box=True) # Now set up the system self._system = self._setup_system(system, kwargs) # Make sure this is set after self._system if client: self.client = client # Now that the config is loaded we can create the EasyClient self._ez_client = EasyClient(logger=self._logger, **self._config) # With the EasyClient we can determine if this is an old garden self._legacy = self._legacy_garden() if not self._legacy: # Namespace setup depends on self._system and self._ez_client self._setup_namespace() # And with _system and _ez_client we can ask for the real logging config self._initialize_logging()
def _setup_logging(self, logger=None, **kwargs): """Set up logging configuration and get a logger for the Plugin This method will configure Python-wide logging for the process if it has not already been configured. Whether or not logging has been configured is determined by the root handler count - if there aren't any then it's assumed logging has not already been configured. The configuration applied (again, if no configuration has already happened) is a stream handler with elevated log levels for libraries that are verbose. The overall level will be loaded as a configuration option, so it can be set as a keyword argument, command line option, or environment variable. A logger to be used by the Plugin will be returned. If the ``logger`` keyword parameter is given then that logger will be used, otherwise a logger will be generated from the standard ``logging`` module. Finally, if a the ``logger`` keyword parameter is supplied it's assumed that logging is already configured and no further configuration will be applied. Args: logger: A custom logger **kwargs: Will be used to load the bootstrap config Returns: A logger for the Plugin """ if logger or len(logging.root.handlers) != 0: self._custom_logger = True else: # log_level is the only bootstrap config item boot_config = load_config(bootstrap=True, **kwargs) logging.config.dictConfig(default_config(level=boot_config.log_level)) self._custom_logger = False return logger or logging.getLogger(__name__)
def test_ignore_cli(self, monkeypatch): cli_args = ["filename", "--bg-host", "the_host"] monkeypatch.setattr(argparse, "_sys", Mock(argv=cli_args)) with pytest.raises(ValidationError): load_config(cli_args=False)
def test_cli_from_sys(self, monkeypatch): cli_args = ["filename", "--bg-host", "the_host"] monkeypatch.setattr(argparse, "_sys", Mock(argv=cli_args)) config = load_config() assert config.bg_host == "the_host"
def test_cli_from_arg(self): cli_args = ["--bg-host", "the_host"] config = load_config(cli_args=cli_args) assert config.bg_host == "the_host"
def test_cli(self): cli_args = ["--metadata", '{"foo": "bar"}'] assert load_config(cli_args=cli_args).metadata == '{"foo": "bar"}'
def test_env(self): os.environ["BG_METADATA"] = '{"foo": "bar"}' assert load_config().metadata == '{"foo": "bar"}'
def test_kwarg_str(self): assert load_config( metadata='{"foo": "bar"}').metadata == '{"foo": "bar"}'
def test_kwarg_dict(self): assert load_config(metadata={ "foo": "bar" }).metadata == '{"foo": "bar"}'
def test_ignore_environment(self, monkeypatch): os.environ["BG_HOST"] = "the_host" with pytest.raises(ValidationError): load_config(environment=False)
def test_environment(self): os.environ["BG_HOST"] = "the_host" config = load_config() assert config.bg_host == "the_host"