def test_read_variables_from_yaml(
        test_output_dirs: OutputFolderForTests) -> None:
    """
    Test that variables are read from a yaml file correctly.
    """
    root = test_output_dirs.root_dir
    # this will return a dictionary of all variables in the yaml file
    yaml_path = root / "foo.yml"
    yaml_path.write_text("""variables:
  some_key: 'some_val'
  key2: 'val2'""")
    vars_dict = secrets_handling.read_all_settings(yaml_path)
    assert vars_dict == {"some_key": "some_val", "key2": "val2"}
    # YAML file missing "variables" key should raise key error
    fail_yaml_path = root / "error.yml"
    fail_yaml_path.write_text("""some_key: 'some_val'""")
    with pytest.raises(KeyError):
        secrets_handling.read_all_settings(fail_yaml_path)
    # Write a private settings file, and check if that is merged correctly.
    # Keys in the private settings file should have higher priority than those in the normal file.
    private_file = root / secrets_handling.PRIVATE_SETTINGS_FILE
    private_file.write_text("""variables:
  some_key: 'private_value'
  key42: 42
""")
    vars_with_private = secrets_handling.read_all_settings(yaml_path,
                                                           project_root=root)
    assert vars_with_private == \
           {
               "some_key": "private_value",
               "key42": 42,
               "key2": "val2"
           }
    # Providing no files should return an empty dictionary
    vars_from_no_file = secrets_handling.read_all_settings()
    assert vars_from_no_file == {}
    # Provide only a project root with a private file:
    private_only = secrets_handling.read_all_settings(project_root=root)
    assert private_only == \
           {
               "some_key": "private_value",
               "key42": 42,
           }
    # Invalid file name should raise an exception
    does_not_exist = "does_not_exist"
    with pytest.raises(FileNotFoundError) as ex:
        secrets_handling.read_all_settings(project_settings_file=root /
                                           does_not_exist)
    assert does_not_exist in str(ex)
Exemple #2
0
 def from_yaml(yaml_file_path: Path, project_root: Optional[Path]) -> AzureConfig:
     """
     Creates an AzureConfig object with default values, with the keys/secrets populated from values in the
      given YAML file. If a `project_root` folder is provided, a private settings file is read from there as well.
     :param yaml_file_path: Path to the YAML file that contains values to create the AzureConfig
     :param project_root: A folder in which to search for a private settings file.
     :return: AzureConfig with values populated from the yaml files.
     """
     config = AzureConfig(**read_all_settings(project_settings_file=yaml_file_path,
                                              project_root=project_root))
     if project_root:
         config.project_root = project_root
     return config
Exemple #3
0
def parse_args_and_add_yaml_variables(
        parser: ArgumentParser,
        yaml_config_file: Optional[Path] = None,
        project_root: Optional[Path] = None,
        fail_on_unknown_args: bool = False) -> ParserResult:
    """
    Reads arguments from sys.argv, modifies them with secrets from local YAML files,
    and parses them using the given argument parser.
    :param project_root: The root folder for the whole project. Only used to access a private settings file.
    :param parser: The parser to use.
    :param yaml_config_file: The path to the YAML file that contains values to supply into sys.argv.
    :param fail_on_unknown_args: If True, raise an exception if the parser encounters an argument that it does not
    recognize. If False, unrecognized arguments will be ignored, and added to the "unknown" field of the parser result.
    :return: The parsed arguments, and overrides
    """
    settings_from_yaml = read_all_settings(yaml_config_file,
                                           project_root=project_root)
    return parse_arguments(parser,
                           settings_from_yaml=settings_from_yaml,
                           fail_on_unknown_args=fail_on_unknown_args)