def test_sync_apps_happy_flow(self): SyncAppsCommand(ARGS).execute() assert self.mock_manager.method_calls == [ call.GitRepoApiFactory.create(ARGS, "TEAM_ORGA", "TEAM_REPO",), call.GitRepoApiFactory.create(ARGS, "ROOT_ORGA", "ROOT_REPO",), call.GitRepo(self.team_config_git_repo_api_mock), call.GitRepo(self.root_config_git_repo_api_mock), call.GitRepo_team.get_clone_url(), call.logging.info("Team config repository: %s", "https://team.config.repo.git"), call.GitRepo_root.get_clone_url(), call.logging.info("Root config repository: %s", "https://root.config.repo.git"), call.GitRepo_team.clone(), call.GitRepo_team.get_full_file_path("."), call.os.listdir("/tmp/team-config-repo/."), call.os.path.join("/tmp/team-config-repo/.", "my-app"), call.os.path.isdir("/tmp/team-config-repo/./my-app"), call.logging.info("Found %s app(s) in apps repository: %s", 1, "my-app"), call.logging.info("Searching apps repository in root repository's 'apps/' directory..."), call.GitRepo_root.clone(), call.GitRepo_root.get_full_file_path("bootstrap/values.yaml"), call.yaml_file_load("/tmp/root-config-repo/bootstrap/values.yaml"), call.GitRepo_team.get_clone_url(), call.logging.info("Analyzing %s in root repository", "apps/team-non-prod.yaml"), call.GitRepo_root.get_full_file_path("apps/team-non-prod.yaml"), call.yaml_file_load("/tmp/root-config-repo/apps/team-non-prod.yaml"), call.logging.info("Found apps repository in %s", "apps/team-non-prod.yaml"), call.logging.info("Analyzing %s in root repository", "apps/other-team-non-prod.yaml"), call.GitRepo_root.get_full_file_path("apps/other-team-non-prod.yaml"), call.yaml_file_load("/tmp/root-config-repo/apps/other-team-non-prod.yaml"), call.logging.info("Sync applications in root repository's %s.", "apps/team-non-prod.yaml"), call.merge_yaml_element("/tmp/root-config-repo/apps/team-non-prod.yaml", "applications", {"my-app": {}}), call.GitRepo_team.get_author_from_last_commit(), call.GitRepo_root.commit("GIT_USER", "GIT_EMAIL", "author updated apps/team-non-prod.yaml"), call.GitRepo_root.push(), ]
def test_sync_apps_already_up_to_date(self): self.yaml_file_load_mock.side_effect = lambda file_path: { "/tmp/root-config-repo/bootstrap/values.yaml": { "bootstrap": [{"name": "team-non-prod"}, {"name": "other-team-non-prod"}], }, "/tmp/root-config-repo/apps/team-non-prod.yaml": { "repository": "https://team.config.repo.git", "applications": {"my-app": None}, # my-app already exists }, "/tmp/root-config-repo/apps/other-team-non-prod.yaml": { "repository": "https://other-team.config.repo.git", "applications": {}, }, }[file_path] SyncAppsCommand(ARGS).execute() assert self.mock_manager.method_calls == [ call.GitRepoApiFactory.create(ARGS, "TEAM_ORGA", "TEAM_REPO",), call.GitRepoApiFactory.create(ARGS, "ROOT_ORGA", "ROOT_REPO",), call.GitRepo(self.team_config_git_repo_api_mock), call.GitRepo(self.root_config_git_repo_api_mock), call.GitRepo_team.get_clone_url(), call.logging.info("Team config repository: %s", "https://team.config.repo.git"), call.GitRepo_root.get_clone_url(), call.logging.info("Root config repository: %s", "https://root.config.repo.git"), call.GitRepo_team.clone(), call.GitRepo_team.get_full_file_path("."), call.os.listdir("/tmp/team-config-repo/."), call.os.path.join("/tmp/team-config-repo/.", "my-app"), call.os.path.isdir("/tmp/team-config-repo/./my-app"), call.logging.info("Found %s app(s) in apps repository: %s", 1, "my-app"), call.logging.info("Searching apps repository in root repository's 'apps/' directory..."), call.GitRepo_root.clone(), call.GitRepo_root.get_full_file_path("bootstrap/values.yaml"), call.yaml_file_load("/tmp/root-config-repo/bootstrap/values.yaml"), call.GitRepo_team.get_clone_url(), call.logging.info("Analyzing %s in root repository", "apps/team-non-prod.yaml"), call.GitRepo_root.get_full_file_path("apps/team-non-prod.yaml"), call.yaml_file_load("/tmp/root-config-repo/apps/team-non-prod.yaml"), call.logging.info("Found apps repository in %s", "apps/team-non-prod.yaml"), call.logging.info("Analyzing %s in root repository", "apps/other-team-non-prod.yaml"), call.GitRepo_root.get_full_file_path("apps/other-team-non-prod.yaml"), call.yaml_file_load("/tmp/root-config-repo/apps/other-team-non-prod.yaml"), call.logging.info("Root repository already up-to-date. I'm done here."), ]
def test_file_not_found(self): self.yaml_file_load_mock.side_effect = FileNotFoundError("file not found") with pytest.raises(GitOpsException) as ex: load_gitops_config(git_api_config=self.git_api_config, organisation="ORGA", repository_name="REPO") self.assertEqual(str(ex.value), "No such file: .gitops.config.yaml") assert self.mock_manager.method_calls == [ call.GitRepoApiFactory.create(self.git_api_config, "ORGA", "REPO"), call.GitRepo(self.git_repo_api_mock), call.GitRepo.clone(), call.GitRepo.get_full_file_path(".gitops.config.yaml"), call.yaml_file_load("/repo-dir/.gitops.config.yaml"), ]
def test_happy_flow(self): gitops_config = load_gitops_config( git_api_config=self.git_api_config, organisation="ORGA", repository_name="REPO" ) assert gitops_config == self.gitops_config_mock assert self.mock_manager.method_calls == [ call.GitRepoApiFactory.create(self.git_api_config, "ORGA", "REPO"), call.GitRepo(self.git_repo_api_mock), call.GitRepo.clone(), call.GitRepo.get_full_file_path(".gitops.config.yaml"), call.yaml_file_load("/repo-dir/.gitops.config.yaml"), call.GitOpsConfig.from_yaml({"dummy": "gitopsconfig"}), ]