Skip to content

Commit

Permalink
Merge pull request #140 from AdamKorcz/target-filesearch-test
Browse files Browse the repository at this point in the history
Add target search test
  • Loading branch information
jku authored Aug 22, 2024
2 parents f7d4604 + 6700236 commit d8ab40b
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions tuf_conformance/test_updater_delegation_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class TargetTest:
targetpath: str


@dataclass
class TargetTestCase:
targetpath: str
found: bool
visited_order: list[str] = field(default_factory=list)


@dataclass
class DelegationsTestCase:
"""A delegations graph as lists of delegations and target files
Expand All @@ -41,6 +48,7 @@ class DelegationsTestCase:

# DataSet is only here so type hints can be used.
DataSet = dict[str, DelegationsTestCase]
DataSetTarget = dict[str, TargetTestCase]


graphs: DataSet = {
Expand Down Expand Up @@ -194,3 +202,75 @@ def test_graph_traversal(
# "('root', 2), ('timestamp', None)" gets prepended
# in every case, so we compare from the 3rd item in the list.
assert repo.metadata_statistics[2:] == exp_calls


r"""
Create a single repository with the following delegations:
targets
*.doc, *md / \ release/*/*
A B
release/x/* / \ release/y/*.zip
C D
Test that Updater successfully finds the target files metadata,
traversing the delegations as expected.
"""
delegations_tree = DelegationsTestCase(
delegations=[
DelegationTester("targets", "A", paths=["*.doc", "*.md"]),
DelegationTester("targets", "B", paths=["releases/*/*"]),
DelegationTester("B", "C", paths=["releases/x/*"]),
DelegationTester("B", "D", paths=["releases/y/*.zip"]),
],
target_files=[
TargetTest("targets", b"targetfile content", "targetfile"),
TargetTest("A", b"README by A", "README.md"),
TargetTest("C", b"x release by C", "releases/x/x_v1"),
TargetTest("D", b"y release by D", "releases/y/y_v1.zip"),
TargetTest("D", b"z release by D", "releases/z/z_v1.zip"),
],
)

targets: DataSetTarget = {
"no delegations": TargetTestCase("targetfile", True, []),
"targetpath matches wildcard": TargetTestCase("README.md", True, ["A"]),
"targetpath with separators x": TargetTestCase("releases/x/x_v1", True, ["B", "C"]),
"targetpath with separators y": TargetTestCase(
"releases/y/y_v1.zip", True, ["B", "D"]
),
"targetpath is not delegated by all roles in the chain": TargetTestCase(
"releases/z/z_v1.zip", False, ["B"]
),
}


targets_ids = targets.keys()
targets_cases = targets.values()


@pytest.mark.parametrize("target", targets_cases, ids=targets_ids)
def test_targetfile_search(
client: ClientRunner, server: SimulatorServer, target: TargetTestCase
) -> None:
exp_calls = [(role, 1) for role in target.visited_order]

init_data, repo = server.new_test(client.test_name)
assert client.init_client(init_data) == 0
init_repo(repo, delegations_tree)

# Call explicitly refresh to simplify the expected_calls list
assert client.refresh(init_data) == 0
repo.metadata_statistics.clear()
if target.found:
assert client.download_target(init_data, target.targetpath) == 0
assert client.get_downloaded_target_bytes() == [
repo.artifacts[target.targetpath].data
]
else:
assert client.download_target(init_data, target.targetpath) == 1

# repo prepends [('root', 2), ('timestamp', None)]
# so we compare equality from the 2nd call to the 3rd-last
# call.
assert repo.metadata_statistics[2:] == exp_calls

0 comments on commit d8ab40b

Please sign in to comment.