Debian Codegraph @

tests/test_examples.py

raw
1 # Copyright 2020, New York University and the TUF contributors
2 # SPDX-License-Identifier: MIT OR Apache-2.0
3 """Unit tests for 'examples' scripts."""
4
5 from __future__ import annotations
6
7 import glob
8 import os
9 import shutil
10 import sys
11 import tempfile
12 import unittest
13 from pathlib import Path
14 from typing import ClassVar
15
16 from tests import utils
17
18
19 class TestRepoExamples(unittest.TestCase):
20 """Unit test class for 'manual_repo' scripts.
21
22 Provides a '_run_example_script' method to run (exec) a script located in
23 the 'manual_repo' directory.
24
25 """
26
27 repo_examples_dir: ClassVar[Path]
28
29 @classmethod
30 def setUpClass(cls) -> None:
31 """Locate the example dir."""
32 base = Path(__file__).resolve().parents[1]
33 cls.repo_examples_dir = base / "examples" / "manual_repo"
34
35 def setUp(self) -> None:
36 """Create and change into test dir.
37 NOTE: Test scripts are expected to create dirs/files in new CWD."""
38 self.original_cwd = os.getcwd()
39 self.base_test_dir = os.path.realpath(tempfile.mkdtemp())
40 os.chdir(self.base_test_dir)
41
42 def tearDown(self) -> None:
43 """Change back to original dir and remove test dir, which may contain
44 dirs/files the test created at test-time CWD."""
45 os.chdir(self.original_cwd)
46 shutil.rmtree(self.base_test_dir)
47
48 def _run_script_and_assert_files(
49 self, script_name: str, filenames_created: list[str]
50 ) -> None:
51 """Run script in example dir and assert that it created the
52 files corresponding to the passed filenames inside a 'tmp*' test dir at
53 CWD."""
54 script_path = str(self.repo_examples_dir / script_name)
55 with open(script_path, "rb") as f:
56 exec(
57 compile(f.read(), script_path, "exec"),
58 {"__file__": script_path},
59 )
60
61 test_dirs = glob.glob("tmp*")
62 self.assertTrue(
63 len(test_dirs) == 1, f"expected 1 'tmp*' test dir, got {test_dirs}"
64 )
65
66 test_dir = test_dirs.pop()
67 for name in filenames_created:
68 metadata_path = Path(test_dir) / f"{name}"
69 self.assertTrue(
70 metadata_path.exists(), f"missing '{metadata_path}' file"
71 )
72
73 def test_basic_repo(self) -> None:
74 """Run 'basic_repo.py' and assert creation of metadata files."""
75 self._run_script_and_assert_files(
76 "basic_repo.py",
77 [
78 "1.python-scripts.json",
79 "1.root.json",
80 "1.snapshot.json",
81 "1.targets.json",
82 "2.root.json",
83 "2.snapshot.json",
84 "2.targets.json",
85 "timestamp.json",
86 ],
87 )
88
89 def test_hashed_bin_delegation(self) -> None:
90 """Run 'hashed_bin_delegation.py' and assert creation of metadata files."""
91 self._run_script_and_assert_files(
92 "hashed_bin_delegation.py",
93 [
94 "1.bins.json",
95 "1.00-07.json",
96 "1.08-0f.json",
97 "1.10-17.json",
98 "1.18-1f.json",
99 "1.20-27.json",
100 "1.28-2f.json",
101 "1.30-37.json",
102 "1.38-3f.json",
103 "1.40-47.json",
104 "1.48-4f.json",
105 "1.50-57.json",
106 "1.58-5f.json",
107 "1.60-67.json",
108 "1.68-6f.json",
109 "1.70-77.json",
110 "1.78-7f.json",
111 "1.80-87.json",
112 "1.88-8f.json",
113 "1.90-97.json",
114 "1.98-9f.json",
115 "1.a0-a7.json",
116 "1.a8-af.json",
117 "1.b0-b7.json",
118 "1.b8-bf.json",
119 "1.c0-c7.json",
120 "1.c8-cf.json",
121 "1.d0-d7.json",
122 "1.d8-df.json",
123 "1.e0-e7.json",
124 "1.e8-ef.json",
125 "1.f0-f7.json",
126 "1.f8-ff.json",
127 ],
128 )
129
130 def test_succinct_hash_bin_delegation(self) -> None:
131 self._run_script_and_assert_files(
132 "succinct_hash_bin_delegations.py",
133 [
134 "1.targets.json",
135 "1.delegated_bin-00.json",
136 "1.delegated_bin-01.json",
137 "1.delegated_bin-02.json",
138 "1.delegated_bin-03.json",
139 "1.delegated_bin-04.json",
140 "1.delegated_bin-05.json",
141 "1.delegated_bin-06.json",
142 "1.delegated_bin-07.json",
143 "1.delegated_bin-08.json",
144 "1.delegated_bin-09.json",
145 "1.delegated_bin-0a.json",
146 "1.delegated_bin-0b.json",
147 "1.delegated_bin-0c.json",
148 "1.delegated_bin-0d.json",
149 "1.delegated_bin-0e.json",
150 "1.delegated_bin-0f.json",
151 "1.delegated_bin-10.json",
152 "1.delegated_bin-11.json",
153 "1.delegated_bin-12.json",
154 "1.delegated_bin-13.json",
155 "1.delegated_bin-14.json",
156 "1.delegated_bin-15.json",
157 "1.delegated_bin-16.json",
158 "1.delegated_bin-17.json",
159 "1.delegated_bin-18.json",
160 "1.delegated_bin-19.json",
161 "1.delegated_bin-1a.json",
162 "1.delegated_bin-1b.json",
163 "1.delegated_bin-1c.json",
164 "1.delegated_bin-1d.json",
165 "1.delegated_bin-1e.json",
166 "1.delegated_bin-1f.json",
167 ],
168 )
169
170
171 if __name__ == "__main__":
172 utils.configure_test_logging(sys.argv)
173 unittest.main()