def test_config_defaults(self): c = ConfigDeserializer() assert c.dict() == { "inventory": { "plugin": "nornir.plugins.inventory.simple.SimpleInventory", "options": {}, "transform_function": "", }, "ssh": { "config_file": "~/.ssh/config" }, "logging": { "level": "debug", "file": "nornir.log", "format": DEFAULT_LOG_FORMAT, "to_console": False, "loggers": ["nornir"], }, "jinja2": { "filters": "" }, "core": { "num_workers": 20, "raise_on_error": False }, "user_defined": {}, }
def test_config_basic(self): c = ConfigDeserializer( core={"num_workers": 30}, logging={"file": ""}, user_defined={"my_opt": True}, ) assert c.dict() == { "inventory": { "plugin": "nornir.plugins.inventory.simple.SimpleInventory", "options": {}, "transform_function": "", "transform_function_options": {}, }, "ssh": { "config_file": "~/.ssh/config" }, "logging": { "enabled": None, "level": "INFO", "file": "", "format": DEFAULT_LOG_FORMAT, "to_console": False, "loggers": ["nornir"], }, "jinja2": { "filters": "" }, "core": { "num_workers": 30, "raise_on_error": False }, "user_defined": { "my_opt": True }, }
def test_order_of_resolution_code_is_higher_than_env(self): os.environ["NORNIR_CORE_NUM_WORKERS"] = "20" config = ConfigDeserializer.load_from_file(os.path.join( dir_path, "config.yaml"), core={"num_workers": 30}) os.environ.pop("NORNIR_CORE_NUM_WORKERS") assert config.core.num_workers == 30
def test_deserialize_basic(self): c = ConfigDeserializer.deserialize( core={"num_workers": 30}, user_defined={"my_opt": True}, logging={ "file": "", "level": "DEBUG" }, ssh={"config_file": "~/.ssh/alt_config"}, inventory={ "plugin": "nornir.plugins.inventory.ansible.AnsibleInventory" }, ) assert isinstance(c, Config) assert c.core.num_workers == 30 assert not c.core.raise_on_error assert c.user_defined == {"my_opt": True} assert c.logging.enabled is None assert c.logging.level == "DEBUG" assert c.logging.file == "" assert c.logging.format == DEFAULT_LOG_FORMAT assert not c.logging.to_console assert c.ssh.config_file == str(Path("~/.ssh/alt_config").expanduser()) assert c.inventory.plugin == AnsibleInventory assert c.inventory.options == {} assert c.inventory.transform_function is None assert c.inventory.transform_function_options == {}
def test_configuration_file_empty(self): config = ConfigDeserializer.load_from_file(os.path.join( dir_path, "empty.yaml"), user_defined={"asd": "qwe"}) assert config.user_defined["asd"] == "qwe" assert config.core.num_workers == 20 assert not config.core.raise_on_error assert config.inventory.plugin == SimpleInventory
def InitNornir( config_file: str = "", dry_run: bool = False, configure_logging: Optional[bool] = None, **kwargs: Dict[str, Any], ) -> Nornir: """ Arguments: config_file(str): Path to the configuration file (optional) dry_run(bool): Whether to simulate changes or not configure_logging: Whether to configure logging or not. This argument is being deprecated. Please use logging.enabled parameter in the configuration instead. **kwargs: Extra information to pass to the :obj:`nornir.core.configuration.Config` object Returns: :obj:`nornir.core.Nornir`: fully instantiated and configured """ register_default_connection_plugins() if callable(kwargs.get("inventory", {}).get("plugin", "")): kwargs["inventory"]["plugin"] = cls_to_string(kwargs["inventory"]["plugin"]) if callable(kwargs.get("inventory", {}).get("transform_function", "")): kwargs["inventory"]["transform_function"] = cls_to_string( kwargs["inventory"]["transform_function"] ) conf = Config.load_from_file(config_file, **kwargs) data = GlobalState(dry_run=dry_run) if configure_logging is not None: msg = ( "'configure_logging' argument is deprecated, please use " "'logging.enabled' parameter in the configuration instead: " "https://nornir.readthedocs.io/en/stable/configuration/index.html" ) warnings.warn(msg, DeprecationWarning) if conf.logging.enabled is None: if configure_logging is not None: conf.logging.enabled = configure_logging else: conf.logging.enabled = True conf.logging.configure() inv = conf.inventory.plugin.deserialize( transform_function=conf.inventory.transform_function, transform_function_options=conf.inventory.transform_function_options, config=conf, **conf.inventory.options, ) return Nornir(inventory=inv, config=conf, data=data)
def test_configuration_file_override_argument(self): config = ConfigDeserializer.load_from_file( os.path.join(dir_path, "config.yaml"), core={ "num_workers": 20, "raise_on_error": True }, ) assert config.core.num_workers == 20 assert config.core.raise_on_error
def build_configuration_parameters(app): """Create documentation for configuration parameters.""" env = Environment(loader=FileSystemLoader("{0}/_data_templates".format(BASEPATH))) template_file = env.get_template("configuration-parameters.j2") data = {} data["schema"] = Config.schema() rendered_template = template_file.render(**data) output_dir = "{0}/configuration/generated".format(BASEPATH) with open("{}/parameters.rst".format(output_dir), "w") as f: f.write(rendered_template)
def test_configuration_file_override_env(self): os.environ["NORNIR_CORE_NUM_WORKERS"] = "30" os.environ["NORNIR_CORE_RAISE_ON_ERROR"] = "1" os.environ["NORNIR_SSH_CONFIG_FILE"] = "/user/ssh_config" config = ConfigDeserializer.deserialize() assert config.core.num_workers == 30 assert config.core.raise_on_error assert config.ssh.config_file == "/user/ssh_config" os.environ.pop("NORNIR_CORE_NUM_WORKERS") os.environ.pop("NORNIR_CORE_RAISE_ON_ERROR") os.environ.pop("NORNIR_SSH_CONFIG_FILE")
def test_deserialize_defaults(self): c = ConfigDeserializer.deserialize() assert isinstance(c, Config) assert c.core.num_workers == 20 assert not c.core.raise_on_error assert c.user_defined == {} assert c.logging.level == logging.DEBUG assert c.logging.file == "nornir.log" assert c.logging.format == DEFAULT_LOG_FORMAT assert not c.logging.to_console assert c.logging.loggers == ["nornir"] assert c.ssh.config_file == "~/.ssh/config" assert c.inventory.plugin == SimpleInventory assert c.inventory.options == {} assert c.inventory.transform_function is None
def InitNornir(config_file="", dry_run=False, configure_logging=True, **kwargs): """ Arguments: config_file(str): Path to the configuration file (optional) dry_run(bool): Whether to simulate changes or not **kwargs: Extra information to pass to the :obj:`nornir.core.configuration.Config` object Returns: :obj:`nornir.core.Nornir`: fully instantiated and configured """ register_default_connection_plugins() if callable(kwargs.get("inventory", {}).get("plugin", "")): kwargs["inventory"]["plugin"] = cls_to_string( kwargs["inventory"]["plugin"]) if callable(kwargs.get("inventory", {}).get("transform_function", "")): kwargs["inventory"]["transform_function"] = cls_to_string( kwargs["inventory"]["transform_function"]) conf = Config.load_from_file(config_file, **kwargs) data = GlobalState(dry_run=dry_run) if configure_logging: conf.logging.configure() inv = conf.inventory.plugin.deserialize( transform_function=conf.inventory.transform_function, config=conf, **conf.inventory.options, ) return Nornir(inventory=inv, config=conf, data=data)
def test_jinja_filters(self): c = ConfigDeserializer.deserialize(jinja2={ "filters": "tests.core.deserializer.my_jinja_filters.jinja_filters" }) assert c.jinja2.filters == my_jinja_filters.jinja_filters()
def test_order_of_resolution_config_is_lowest(self): config = ConfigDeserializer.load_from_file( os.path.join(dir_path, "config.yaml")) assert config.core.num_workers == 10
def test_get_user_defined_from_file(self): config = ConfigDeserializer.load_from_file( os.path.join(dir_path, "config.yaml")) assert config.user_defined["asd"] == "qwe"
def test_configuration_bool_env(self): os.environ["NORNIR_CORE_RAISE_ON_ERROR"] = "0" config = ConfigDeserializer.deserialize() assert config.core.num_workers == 20 assert not config.core.raise_on_error
def test_jinja_filters_error(self): with pytest.raises(ModuleNotFoundError): ConfigDeserializer.deserialize(jinja2={"filters": "asdasd.asdasd"})
def test_configuration_file_normal(self): config = ConfigDeserializer.load_from_file( os.path.join(dir_path, "config.yaml")) assert config.core.num_workers == 10 assert not config.core.raise_on_error assert config.inventory.plugin == AnsibleInventory