def test_yaml_file_load_yaml_exception(self): path = self._create_file("{ INVALID YAML") try: yaml_file_load(path) self.fail() except YAMLException as ex: self.assertEqual(f"Error parsing YAML file: {path}", str(ex))
def test_yaml_file_load_and_dump_roundtrip(self): input_content = "answer: #comment\n is: '42'\n" # comment should be preserved input_path = self._create_file(input_content) yaml = yaml_file_load(input_path) output_path = self._create_tmp_file_path() yaml_file_dump(yaml, output_path) output_content = self._read_file(output_path) self.assertEqual(output_content, input_content)
def __find_apps_config_from_repo( team_config_git_repo: GitRepo, root_config_git_repo: GitRepo ) -> Tuple[str, str, Set[str], Set[str], str]: apps_from_other_repos: Set[str] = set( ) # Set for all entries in .applications from each config repository found_app_config_file = None found_app_config_file_name = None found_apps_path = "applications" found_app_config_apps: Set[str] = set() bootstrap_entries = __get_bootstrap_entries(root_config_git_repo) team_config_git_repo_clone_url = team_config_git_repo.get_clone_url() for bootstrap_entry in bootstrap_entries: if "name" not in bootstrap_entry: raise GitOpsException( "Every bootstrap entry must have a 'name' property.") app_file_name = "apps/" + bootstrap_entry["name"] + ".yaml" logging.info("Analyzing %s in root repository", app_file_name) app_config_file = root_config_git_repo.get_full_file_path( app_file_name) try: app_config_content = yaml_file_load(app_config_file) except FileNotFoundError as ex: raise GitOpsException( f"File '{app_file_name}' not found in root repository." ) from ex if "config" in app_config_content: app_config_content = app_config_content["config"] found_apps_path = "config.applications" if "repository" not in app_config_content: raise GitOpsException( f"Cannot find key 'repository' in '{app_file_name}'") if app_config_content["repository"] == team_config_git_repo_clone_url: logging.info("Found apps repository in %s", app_file_name) found_app_config_file = app_config_file found_app_config_file_name = app_file_name found_app_config_apps = __get_applications_from_app_config( app_config_content) else: apps_from_other_repos.update( __get_applications_from_app_config(app_config_content)) if found_app_config_file is None or found_app_config_file_name is None: raise GitOpsException( f"Couldn't find config file for apps repository in root repository's 'apps/' directory" ) return ( found_app_config_file, found_app_config_file_name, found_app_config_apps, apps_from_other_repos, found_apps_path, )
def load_gitops_config(git_api_config: GitApiConfig, organisation: str, repository_name: str) -> GitOpsConfig: git_repo_api = GitRepoApiFactory.create(git_api_config, organisation, repository_name) with GitRepo(git_repo_api) as git_repo: git_repo.clone() gitops_config_file_path = git_repo.get_full_file_path( ".gitops.config.yaml") try: gitops_config_yaml = yaml_file_load(gitops_config_file_path) except FileNotFoundError as ex: raise GitOpsException("No such file: .gitops.config.yaml") from ex return GitOpsConfig.from_yaml(gitops_config_yaml)
def __get_bootstrap_entries(root_config_git_repo: GitRepo) -> Any: root_config_git_repo.clone() bootstrap_values_file = root_config_git_repo.get_full_file_path( "bootstrap/values.yaml") try: bootstrap_yaml = yaml_file_load(bootstrap_values_file) except FileNotFoundError as ex: raise GitOpsException( "File 'bootstrap/values.yaml' not found in root repository." ) from ex if "bootstrap" not in bootstrap_yaml: raise GitOpsException( "Cannot find key 'bootstrap' in 'bootstrap/values.yaml'") return bootstrap_yaml["bootstrap"]
def test_yaml_file_load_file_not_found(self): try: yaml_file_load("unknown") self.fail() except FileNotFoundError: pass
def test_yaml_file_load(self): path = self._create_file("answer: #comment\n is: '42'\n") self.assertEqual(yaml_file_load(path), {"answer": {"is": "42"}})