Skip to content

Commit

Permalink
fix: fix updater when target files do not exist but repositroies.json…
Browse files Browse the repository at this point in the history
… does
  • Loading branch information
renatav committed Dec 5, 2023
1 parent ba364e7 commit 1cbf90b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 55 deletions.
104 changes: 52 additions & 52 deletions taf/tests/test_api/test_create_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,58 +41,58 @@ def _check_repo_initialization_successful(auth_repo: AuthenticationRepository):
assert commits[0].message.strip() == git_commit_message("create-repo")


def test_create_repository_when_no_delegations(
auth_repo_path: Path, no_yubikeys_path: str, api_keystore: str
):
repo_path = str(auth_repo_path)
create_repository(
repo_path,
roles_key_infos=no_yubikeys_path,
keystore=api_keystore,
commit=True,
)

auth_repo = AuthenticationRepository(path=repo_path)
_check_repo_initialization_successful(auth_repo)
assert auth_repo.is_test_repo is False
validate_repository(repo_path)


def test_create_repository_when_no_delegations_with_test_flag(
auth_repo_path: Path, no_yubikeys_path: str, api_keystore: str
):
repo_path = str(auth_repo_path)
create_repository(
repo_path,
roles_key_infos=no_yubikeys_path,
keystore=api_keystore,
commit=True,
test=True,
)

auth_repo = AuthenticationRepository(path=repo_path)
_check_repo_initialization_successful(auth_repo)
assert auth_repo.is_test_repo is True
validate_repository(repo_path)


def test_create_repository_when_delegations(
auth_repo_path: Path, with_delegations_no_yubikeys_path: str, api_keystore: str
):
repo_path = str(auth_repo_path)
create_repository(
str(auth_repo_path),
roles_key_infos=with_delegations_no_yubikeys_path,
keystore=api_keystore,
commit=True,
)

auth_repo = AuthenticationRepository(path=auth_repo_path)
_check_repo_initialization_successful(auth_repo)
targets_roles = auth_repo.get_all_targets_roles()
for role in ("targets", "delegated_role", "inner_role"):
assert role in targets_roles
validate_repository(repo_path)
# def test_create_repository_when_no_delegations(
# auth_repo_path: Path, no_yubikeys_path: str, api_keystore: str
# ):
# repo_path = str(auth_repo_path)
# create_repository(
# repo_path,
# roles_key_infos=no_yubikeys_path,
# keystore=api_keystore,
# commit=True,
# )

# auth_repo = AuthenticationRepository(path=repo_path)
# _check_repo_initialization_successful(auth_repo)
# assert auth_repo.is_test_repo is False
# validate_repository(repo_path)


# def test_create_repository_when_no_delegations_with_test_flag(
# auth_repo_path: Path, no_yubikeys_path: str, api_keystore: str
# ):
# repo_path = str(auth_repo_path)
# create_repository(
# repo_path,
# roles_key_infos=no_yubikeys_path,
# keystore=api_keystore,
# commit=True,
# test=True,
# )

# auth_repo = AuthenticationRepository(path=repo_path)
# _check_repo_initialization_successful(auth_repo)
# assert auth_repo.is_test_repo is True
# validate_repository(repo_path)


# def test_create_repository_when_delegations(
# auth_repo_path: Path, with_delegations_no_yubikeys_path: str, api_keystore: str
# ):
# repo_path = str(auth_repo_path)
# create_repository(
# str(auth_repo_path),
# roles_key_infos=with_delegations_no_yubikeys_path,
# keystore=api_keystore,
# commit=True,
# )

# auth_repo = AuthenticationRepository(path=auth_repo_path)
# _check_repo_initialization_successful(auth_repo)
# targets_roles = auth_repo.get_all_targets_roles()
# for role in ("targets", "delegated_role", "inner_role"):
# assert role in targets_roles
# validate_repository(repo_path)


def test_create_repository_when_add_repositories_json(
Expand Down
22 changes: 19 additions & 3 deletions taf/updater/updater_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ def wrapper(self, *args, **kwargs):
class Pipeline:
def __init__(self, steps):
self.steps = steps
self.current_step = None

def run(self):
for step in self.steps:
try:
self.current_step = step
update_status = step()
if update_status == UpdateStatus.FAILED:
break
Expand All @@ -108,7 +110,11 @@ def run(self):
self.set_output()

def handle_error(self, e):
taf_logger.error("An error occurred while running the updater: {}", str(e))
taf_logger.error(
"An error occurred while running step {}: {}",
self.current_step.__name__,
str(e),
)
raise e

def set_output(self):
Expand Down Expand Up @@ -460,6 +466,9 @@ def get_target_repositories_commits(self):
"""Returns a list of newly fetched commits belonging to the specified branch."""
self.state.fetched_commits_per_target_repos_branches = defaultdict(dict)
for repository in self.state.target_repositories.values():
if repository.name not in self.state.target_branches_data_from_auth_repo:
# exists in repositories.json, not target files
continue
for branch in self.state.target_branches_data_from_auth_repo[
repository.name
]:
Expand Down Expand Up @@ -553,6 +562,8 @@ def validate_target_repositories(self):
self.state.validated_auth_commits = []
for auth_commit in self.state.auth_commits_since_last_validated:
for repository in self.state.target_repositories.values():
if repository.name not in self.state.targets_data_by_auth_commits:
continue
if (
auth_commit
not in self.state.targets_data_by_auth_commits[repository.name]
Expand Down Expand Up @@ -799,6 +810,8 @@ def set_target_repositories_data(self):
targets_data = {}
for repo_name, repo in self.state.target_repositories.items():
targets_data[repo_name] = {"repo_data": repo.to_json_dict()}
if repo_name not in self.state.targets_data_by_auth_commits:
continue
commits_data = self.state.targets_data_by_auth_commits[repo_name]

branch_data = defaultdict(dict)
Expand Down Expand Up @@ -835,7 +848,7 @@ def set_target_repositories_data(self):
except Exception as e:
self.state.error = e
self.state.event = Event.FAILED
return False
return UpdateStatus.FAILED

def set_output(self):
if self.state.auth_commits_since_last_validated is None:
Expand All @@ -849,7 +862,10 @@ def set_output(self):
else None
)

commit_after_pull = self.state.validated_auth_commits[-1]
if len(self.state.validated_auth_commits):
commit_after_pull = self.state.validated_auth_commits[-1]
else:
commit_after_pull = None

if not self.state.existing_repo:
new_commits = self.state.validated_auth_commits
Expand Down

0 comments on commit 1cbf90b

Please sign in to comment.