Skip to content

Commit

Permalink
test: set up library with target repos, test adding new target when o…
Browse files Browse the repository at this point in the history
…n filesystem
  • Loading branch information
renatav committed Sep 28, 2023
1 parent 1aa06d4 commit e243e53
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 16 deletions.
4 changes: 4 additions & 0 deletions taf/tests/test_api/test_create_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _check_repo_initialization_successful(auth_repo):
assert targets_dir.is_dir() is True
commits = auth_repo.list_commits()
assert len(commits) == 1
assert commits[0].message.strip() == "Initial commit"


def test_create_repository_when_no_delegations(
Expand Down Expand Up @@ -116,7 +117,10 @@ def test_create_repository_when_add_repositories_json(
for role in ("targets", "delegated_role", "inner_role"):
assert role in targets_roles
target_files = auth_repo.all_target_files()
signed_target_files = auth_repo.get_signed_target_files()
for target_file in ("repositories.json", "mirrors.json"):
assert target_file in target_files
assert target_file in signed_target_files
assert auth_repo.get_role_from_target_paths([target_file]) == "targets"

validate_repository(repo_path)
95 changes: 79 additions & 16 deletions taf/tests/test_api/test_update_repository.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import shutil
import uuid
import taf.repositoriesdb as repositoriesdb
from taf.auth_repo import AuthenticationRepository
from taf.git import GitRepository
from pytest import fixture
from taf.api.repository import create_repository
from taf.api.targets import add_target_repo
Expand All @@ -10,45 +13,105 @@
from tuf.repository_tool import METADATA_DIRECTORY_NAME, TARGETS_DIRECTORY_NAME


AUTH_REPO_NAME = "auth"


@fixture(scope="module")
def auth_repo_path():
def library():
random_name = str(uuid.uuid4())
path = CLIENT_DIR_PATH / random_name / "auth"
yield path
shutil.rmtree(path.parent, onerror=on_rm_error)
root_dir = CLIENT_DIR_PATH / random_name
# create an initialize some target repositories
# their content is not important
auth_path = root_dir / AUTH_REPO_NAME
auth_path.mkdir(exist_ok=True, parents=True)
targets = ("target1", "target2", "target3", "new_target")
for target in targets:
target_repo_path = root_dir / target
target_repo_path.mkdir()
target_repo = GitRepository(path=target_repo_path)
target_repo.init_repo()
target_repo.commit_empty("Initial commit")
yield root_dir
shutil.rmtree(root_dir, onerror=on_rm_error)


def test_setup_auth_repo_when_add_repositories_json(
auth_repo_path,
library,
with_delegations_no_yubikeys_path,
api_keystore,
repositories_json_template,
mirrors_json_path,
):
repo_path = str(auth_repo_path)
namespace = auth_repo_path.parent.name
copy_repositories_json(repositories_json_template, namespace, auth_repo_path)
copy_mirrors_json(mirrors_json_path, namespace, auth_repo_path)
repo_path = library / "auth"
namespace = library.name
copy_repositories_json(repositories_json_template, namespace, repo_path)
copy_mirrors_json(mirrors_json_path, namespace, repo_path)

create_repository(
repo_path,
str(repo_path),
roles_key_infos=with_delegations_no_yubikeys_path,
keystore=api_keystore,
commit=True,
commit_msg="Initial commit",
)


def test_add_target_repository_when_not_on_filesystem(auth_repo_path, api_keystore):
path = str(auth_repo_path)
namespace = auth_repo_path.parent.name
def test_add_target_repository_when_not_on_filesystem(library, api_keystore):
repo_path = str(library / "auth")
namespace = library.name
target_repo_name = f"{namespace}/target4"
commit_msg = "Add new target repository"
add_target_repo(
path,
str(repo_path),
None,
f"{namespace}/target4",
target_repo_name,
"delegated_role",
None,
api_keystore,
commit_msg="Add new target repository",
commit_msg=commit_msg,
push=False,
)
auth_repo = AuthenticationRepository(path=repo_path)
# verify repositories.json was updated and that changes were committed
# then validate the repository
repositories_json = repositoriesdb.load_repositories_json(auth_repo)
repositories = repositories_json["repositories"]
assert target_repo_name in repositories
commits = auth_repo.list_commits()
assert len(commits) == 2
assert commits[0].message.strip() == commit_msg

def test_add_target_repository_when_on_filesystem(library, api_keystore):
repo_path = str(library / "auth")
namespace = library.name
target_repo_name = f"{namespace}/new_target"
commit_msg = "Add another new target repository"
add_target_repo(
repo_path,
None,
target_repo_name,
"delegated_role",
None,
api_keystore,
commit_msg=commit_msg,
push=False,
)
auth_repo = AuthenticationRepository(path=repo_path)
# verify repositories.json was updated and that changes were committed
# then validate the repository
repositories_json = repositoriesdb.load_repositories_json(auth_repo)
repositories = repositories_json["repositories"]
assert target_repo_name in repositories
commits = auth_repo.list_commits()
assert len(commits) == 3
assert commits[0].message.strip() == commit_msg
signed_target_files = auth_repo.get_signed_target_files()
assert target_repo_name in signed_target_files
target_repo_path = library.parent / target_repo_name
target_repo = GitRepository(path=target_repo_path)
target_repo_head_sha = target_repo.head_commit_sha()
auth_repo_head_sha = auth_repo.head_commit_sha()
targets = auth_repo.targets_at_revisions(auth_repo_head_sha)
target_content = targets[auth_repo_head_sha][target_repo_name]
assert target_repo_head_sha == target_content["commit"]

0 comments on commit e243e53

Please sign in to comment.