2021-11-29 12:25:26 +00:00
|
|
|
# Copyright 2020, New York University and the TUF contributors
|
|
|
|
|
# SPDX-License-Identifier: MIT OR Apache-2.0
|
2024-03-07 08:05:36 +00:00
|
|
|
"""Unit tests for 'examples' scripts."""
|
2021-11-29 12:25:26 +00:00
|
|
|
|
2024-11-29 10:29:32 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2021-11-29 12:25:26 +00:00
|
|
|
import glob
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
2021-12-07 13:19:38 +00:00
|
|
|
import sys
|
2021-11-29 12:25:26 +00:00
|
|
|
import tempfile
|
|
|
|
|
import unittest
|
|
|
|
|
from pathlib import Path
|
2024-11-04 04:21:23 +00:00
|
|
|
from typing import ClassVar
|
2021-11-29 12:25:26 +00:00
|
|
|
|
2021-12-07 13:19:38 +00:00
|
|
|
from tests import utils
|
|
|
|
|
|
2021-11-29 12:25:26 +00:00
|
|
|
|
|
|
|
|
class TestRepoExamples(unittest.TestCase):
|
2022-11-23 11:55:19 +00:00
|
|
|
"""Unit test class for 'manual_repo' scripts.
|
2021-11-29 12:25:26 +00:00
|
|
|
|
|
|
|
|
Provides a '_run_example_script' method to run (exec) a script located in
|
2022-11-23 11:55:19 +00:00
|
|
|
the 'manual_repo' directory.
|
2021-11-29 12:25:26 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2021-12-03 16:47:31 +00:00
|
|
|
repo_examples_dir: ClassVar[Path]
|
|
|
|
|
|
2021-11-29 12:25:26 +00:00
|
|
|
@classmethod
|
2021-12-03 16:47:31 +00:00
|
|
|
def setUpClass(cls) -> None:
|
2022-11-23 11:55:19 +00:00
|
|
|
"""Locate the example dir."""
|
2021-11-29 12:25:26 +00:00
|
|
|
base = Path(__file__).resolve().parents[1]
|
2022-11-23 11:55:19 +00:00
|
|
|
cls.repo_examples_dir = base / "examples" / "manual_repo"
|
2021-11-29 12:25:26 +00:00
|
|
|
|
2021-12-03 16:47:31 +00:00
|
|
|
def setUp(self) -> None:
|
2021-11-29 12:25:26 +00:00
|
|
|
"""Create and change into test dir.
|
|
|
|
|
NOTE: Test scripts are expected to create dirs/files in new CWD."""
|
|
|
|
|
self.original_cwd = os.getcwd()
|
|
|
|
|
self.base_test_dir = os.path.realpath(tempfile.mkdtemp())
|
|
|
|
|
os.chdir(self.base_test_dir)
|
|
|
|
|
|
2021-12-03 16:47:31 +00:00
|
|
|
def tearDown(self) -> None:
|
2021-11-29 12:25:26 +00:00
|
|
|
"""Change back to original dir and remove test dir, which may contain
|
|
|
|
|
dirs/files the test created at test-time CWD."""
|
|
|
|
|
os.chdir(self.original_cwd)
|
|
|
|
|
shutil.rmtree(self.base_test_dir)
|
|
|
|
|
|
2021-12-03 16:47:31 +00:00
|
|
|
def _run_script_and_assert_files(
|
2024-11-04 04:21:23 +00:00
|
|
|
self, script_name: str, filenames_created: list[str]
|
2021-12-03 16:47:31 +00:00
|
|
|
) -> None:
|
2025-03-10 20:48:43 +00:00
|
|
|
"""Run script in example dir and assert that it created the
|
2021-11-29 12:25:26 +00:00
|
|
|
files corresponding to the passed filenames inside a 'tmp*' test dir at
|
|
|
|
|
CWD."""
|
|
|
|
|
script_path = str(self.repo_examples_dir / script_name)
|
|
|
|
|
with open(script_path, "rb") as f:
|
|
|
|
|
exec(
|
|
|
|
|
compile(f.read(), script_path, "exec"),
|
|
|
|
|
{"__file__": script_path},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
test_dirs = glob.glob("tmp*")
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
len(test_dirs) == 1, f"expected 1 'tmp*' test dir, got {test_dirs}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
test_dir = test_dirs.pop()
|
|
|
|
|
for name in filenames_created:
|
|
|
|
|
metadata_path = Path(test_dir) / f"{name}"
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
metadata_path.exists(), f"missing '{metadata_path}' file"
|
|
|
|
|
)
|
|
|
|
|
|
2021-12-03 16:47:31 +00:00
|
|
|
def test_basic_repo(self) -> None:
|
2021-11-29 12:25:26 +00:00
|
|
|
"""Run 'basic_repo.py' and assert creation of metadata files."""
|
|
|
|
|
self._run_script_and_assert_files(
|
|
|
|
|
"basic_repo.py",
|
|
|
|
|
[
|
|
|
|
|
"1.python-scripts.json",
|
|
|
|
|
"1.root.json",
|
|
|
|
|
"1.snapshot.json",
|
|
|
|
|
"1.targets.json",
|
|
|
|
|
"2.root.json",
|
|
|
|
|
"2.snapshot.json",
|
|
|
|
|
"2.targets.json",
|
|
|
|
|
"timestamp.json",
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
2021-12-10 09:48:36 +00:00
|
|
|
def test_hashed_bin_delegation(self) -> None:
|
2021-11-23 16:07:11 +00:00
|
|
|
"""Run 'hashed_bin_delegation.py' and assert creation of metadata files."""
|
|
|
|
|
self._run_script_and_assert_files(
|
|
|
|
|
"hashed_bin_delegation.py",
|
|
|
|
|
[
|
2022-06-28 15:17:43 +00:00
|
|
|
"1.bins.json",
|
|
|
|
|
"1.00-07.json",
|
|
|
|
|
"1.08-0f.json",
|
|
|
|
|
"1.10-17.json",
|
|
|
|
|
"1.18-1f.json",
|
|
|
|
|
"1.20-27.json",
|
|
|
|
|
"1.28-2f.json",
|
|
|
|
|
"1.30-37.json",
|
|
|
|
|
"1.38-3f.json",
|
|
|
|
|
"1.40-47.json",
|
|
|
|
|
"1.48-4f.json",
|
|
|
|
|
"1.50-57.json",
|
|
|
|
|
"1.58-5f.json",
|
|
|
|
|
"1.60-67.json",
|
|
|
|
|
"1.68-6f.json",
|
|
|
|
|
"1.70-77.json",
|
|
|
|
|
"1.78-7f.json",
|
|
|
|
|
"1.80-87.json",
|
|
|
|
|
"1.88-8f.json",
|
|
|
|
|
"1.90-97.json",
|
|
|
|
|
"1.98-9f.json",
|
|
|
|
|
"1.a0-a7.json",
|
|
|
|
|
"1.a8-af.json",
|
|
|
|
|
"1.b0-b7.json",
|
|
|
|
|
"1.b8-bf.json",
|
|
|
|
|
"1.c0-c7.json",
|
|
|
|
|
"1.c8-cf.json",
|
|
|
|
|
"1.d0-d7.json",
|
|
|
|
|
"1.d8-df.json",
|
|
|
|
|
"1.e0-e7.json",
|
|
|
|
|
"1.e8-ef.json",
|
|
|
|
|
"1.f0-f7.json",
|
|
|
|
|
"1.f8-ff.json",
|
2021-11-23 16:07:11 +00:00
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
2022-06-24 11:12:46 +00:00
|
|
|
def test_succinct_hash_bin_delegation(self) -> None:
|
|
|
|
|
self._run_script_and_assert_files(
|
|
|
|
|
"succinct_hash_bin_delegations.py",
|
|
|
|
|
[
|
|
|
|
|
"1.targets.json",
|
|
|
|
|
"1.delegated_bin-00.json",
|
|
|
|
|
"1.delegated_bin-01.json",
|
|
|
|
|
"1.delegated_bin-02.json",
|
|
|
|
|
"1.delegated_bin-03.json",
|
|
|
|
|
"1.delegated_bin-04.json",
|
|
|
|
|
"1.delegated_bin-05.json",
|
|
|
|
|
"1.delegated_bin-06.json",
|
|
|
|
|
"1.delegated_bin-07.json",
|
|
|
|
|
"1.delegated_bin-08.json",
|
|
|
|
|
"1.delegated_bin-09.json",
|
|
|
|
|
"1.delegated_bin-0a.json",
|
|
|
|
|
"1.delegated_bin-0b.json",
|
|
|
|
|
"1.delegated_bin-0c.json",
|
|
|
|
|
"1.delegated_bin-0d.json",
|
|
|
|
|
"1.delegated_bin-0e.json",
|
|
|
|
|
"1.delegated_bin-0f.json",
|
|
|
|
|
"1.delegated_bin-10.json",
|
|
|
|
|
"1.delegated_bin-11.json",
|
|
|
|
|
"1.delegated_bin-12.json",
|
|
|
|
|
"1.delegated_bin-13.json",
|
|
|
|
|
"1.delegated_bin-14.json",
|
|
|
|
|
"1.delegated_bin-15.json",
|
|
|
|
|
"1.delegated_bin-16.json",
|
|
|
|
|
"1.delegated_bin-17.json",
|
|
|
|
|
"1.delegated_bin-18.json",
|
|
|
|
|
"1.delegated_bin-19.json",
|
|
|
|
|
"1.delegated_bin-1a.json",
|
|
|
|
|
"1.delegated_bin-1b.json",
|
|
|
|
|
"1.delegated_bin-1c.json",
|
|
|
|
|
"1.delegated_bin-1d.json",
|
|
|
|
|
"1.delegated_bin-1e.json",
|
|
|
|
|
"1.delegated_bin-1f.json",
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
2021-11-29 12:25:26 +00:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-12-07 13:19:38 +00:00
|
|
|
utils.configure_test_logging(sys.argv)
|
2021-11-29 12:25:26 +00:00
|
|
|
unittest.main()
|