-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathtest_validate_pr.py
More file actions
327 lines (260 loc) · 11.8 KB
/
Copy pathtest_validate_pr.py
File metadata and controls
327 lines (260 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python3
"""Black-box regression tests for validate-pr cherry-pick matching."""
import os
import pathlib
import shutil
import subprocess
import sys
import tempfile
import unittest
VALIDATE_PR = pathlib.Path(__file__).with_name("validate-pr")
SUBJECT = "fixture: insert payload"
UPSTREAM_SOB = "Signed-off-by: Upstream Author <upstream@example.com>"
LOCAL_SOB = "Signed-off-by: Local Author <local@example.com>"
class ValidatePrPatchIdTest(unittest.TestCase):
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self.addCleanup(self._tmp.cleanup)
self.repo = pathlib.Path(self._tmp.name) / "repo"
self.repo.mkdir()
self.real_git = shutil.which("git")
self.assertIsNotNone(self.real_git)
self._git("init")
self._set_identity("Fixture Author", "fixture@example.com")
self._write_fixture([
"first-before",
"anchor",
"first-after",
"spacer-1",
"spacer-2",
"spacer-3",
"spacer-4",
"second-before",
"anchor",
"second-after",
])
self.base = self._commit("fixture: base", LOCAL_SOB)
def _relocate_repo(self, name):
relocated = self.repo.parent / name
self.repo.rename(relocated)
self.repo = relocated
def _git(self, *args, input_text=None):
result = subprocess.run(
["git", *args],
cwd=self.repo,
input=input_text,
text=True,
capture_output=True,
)
if result.returncode:
self.fail(
"git {} failed ({}):\n{}{}".format(
" ".join(args), result.returncode,
result.stdout, result.stderr))
return result.stdout.strip()
def _set_identity(self, name, email):
self._git("config", "user.name", name)
self._git("config", "user.email", email)
def _write_fixture(self, lines):
(self.repo / "fixture.txt").write_text("\n".join(lines) + "\n")
def _read_fixture(self):
return (self.repo / "fixture.txt").read_text().splitlines()
def _use_identical_occurrence_fixture(self):
block = [
"same-before-4",
"same-before-3",
"same-before-2",
"same-before-1",
"anchor",
"same-after-1",
"same-after-2",
"same-after-3",
"same-after-4",
]
separator = ["separator-{}".format(i) for i in range(1, 8)]
self._write_fixture(["prefix", *block, *separator, *block, "suffix"])
self.base = self._commit("fixture: duplicate context", LOCAL_SOB)
def _commit(self, subject, body):
self._git("add", "fixture.txt")
self._git("commit", "-F", "-",
input_text="{}\n\n{}\n".format(subject, body))
return self._git("rev-parse", "HEAD")
def _patch_id(self, commit):
patch = self._git("show", commit)
output = self._git("patch-id", "--stable", input_text=patch)
return output.split()[0]
def _build_case(self, change, context_parent=True):
self._git("checkout", "-b", "upstream-topic", self.base)
self._set_identity("Upstream Author", "upstream@example.com")
lines = self._read_fixture()
lines.insert(lines.index("anchor") + 1, "payload")
self._write_fixture(lines)
upstream = self._commit(SUBJECT, UPSTREAM_SOB)
self._git("update-ref", "refs/remotes/upstream/linux", upstream)
self._git("checkout", "-b", "local", self.base)
self._set_identity("Local Author", "local@example.com")
if context_parent:
lines = self._read_fixture()
lines[0] = "local-first-before"
self._write_fixture(lines)
self._commit("fixture: adjust local context", LOCAL_SOB)
parent = self._git("rev-parse", "HEAD")
if change == "exact":
self._git("cherry-pick", "-x", "--signoff", upstream)
else:
lines = self._read_fixture()
anchors = [i for i, line in enumerate(lines) if line == "anchor"]
if change == "context":
lines.insert(anchors[0] + 1, "payload")
elif change == "mutated":
lines.insert(anchors[0] + 1, "payload-mutated")
elif change == "wrong-occurrence":
lines.insert(anchors[1] + 1, "payload")
else:
self.fail("unknown fixture change: {}".format(change))
self._write_fixture(lines)
self._commit(
SUBJECT,
"{}\n\n(cherry picked from commit {})\n{}".format(
UPSTREAM_SOB, upstream, LOCAL_SOB))
local = self._git("rev-parse", "HEAD")
return parent, local
def _fake_git_env(self, mode):
fake_bin = self.repo / "fake-bin"
fake_bin.mkdir(exist_ok=True)
wrapper = fake_bin / "git"
wrapper.write_text("""#!/usr/bin/env python3
import os
import subprocess
import sys
real_git = os.environ["VALIDATE_PR_REAL_GIT"]
mode = os.environ["VALIDATE_PR_FAKE_GIT_MODE"]
args = sys.argv[1:]
if mode == "show-failure" and args and args[0] == "show":
result = subprocess.run([real_git, *args], capture_output=True)
sys.stdout.buffer.write(result.stdout)
sys.stderr.buffer.write(result.stderr)
sys.exit(1)
if (args[:2] == ["patch-id", "--stable"] and
mode in ("patch-id-malformed", "patch-id-extra-line")):
sys.stdin.buffer.read()
if mode == "patch-id-malformed":
sys.stdout.write("not-a-patch-id not-a-commit-id\\n")
sys.exit(0)
if mode == "patch-id-extra-line":
sys.stdout.write("{} {}\\n{} {}\\n".format(
"a" * 40, "b" * 40, "c" * 40, "d" * 40))
sys.exit(0)
if (mode == "rev-parse-invalid-utf8" and
args[:3] == ["rev-parse", "--git-path", "objects"]):
sys.stdout.buffer.write(b"\\xff\\n")
sys.exit(0)
if mode == "merge-tree-failure" and args and args[0] == "merge-tree":
sys.stderr.write("synthetic merge-tree failure\\n")
sys.exit(2)
if mode == "merge-tree-extra-line" and args and args[0] == "merge-tree":
result = subprocess.run([real_git, *args], capture_output=True)
sys.stdout.buffer.write(result.stdout)
if result.stdout and not result.stdout.endswith(b"\\n"):
sys.stdout.buffer.write(b"\\n")
sys.stdout.buffer.write(b"unexpected-extra-line\\n")
sys.stderr.buffer.write(result.stderr)
sys.exit(result.returncode)
os.execv(real_git, [real_git, *args])
""")
wrapper.chmod(0o755)
env = os.environ.copy()
env["PATH"] = str(fake_bin) + os.pathsep + env.get("PATH", "")
env["VALIDATE_PR_REAL_GIT"] = self.real_git
env["VALIDATE_PR_FAKE_GIT_MODE"] = mode
return env
def _validate(self, parent, local, git_mode=None):
return subprocess.run(
[sys.executable, str(VALIDATE_PR),
"{}..{}".format(parent, local), "upstream", "linux",
"--no-update"],
cwd=self.repo,
env=self._fake_git_env(git_mode) if git_mode else None,
text=True,
capture_output=True,
)
@staticmethod
def _output(result):
return "stdout:\n{}\nstderr:\n{}".format(
result.stdout, result.stderr)
def _patch_id_status(self, result, local):
marker = "│ {} │".format(local[:12])
for line in result.stdout.splitlines():
if marker in line:
cells = line.split("│")
self.assertGreaterEqual(len(cells), 6, self._output(result))
return cells[3].strip()
self.fail("no digest row for {}:\n{}".format(
local[:12], self._output(result)))
def test_accepts_context_only_replay(self):
parent, local = self._build_case("context")
result = self._validate(parent, local)
self.assertEqual(result.returncode, 0, self._output(result))
self.assertEqual(self._patch_id_status(result, local), "context")
def test_accepts_context_only_replay_with_colon_in_object_path(self):
self._relocate_repo("repo:colon")
parent, local = self._build_case("context")
result = self._validate(parent, local)
self.assertEqual(result.returncode, 0, self._output(result))
self.assertEqual(self._patch_id_status(result, local), "context")
def test_accepts_exact_cherry_pick(self):
parent, local = self._build_case("exact", context_parent=False)
result = self._validate(parent, local)
self.assertEqual(result.returncode, 0, self._output(result))
self.assertEqual(self._patch_id_status(result, local), "match")
def test_rejects_git_show_failure_with_patch_output(self):
parent, local = self._build_case("exact", context_parent=False)
result = self._validate(parent, local, "show-failure")
self.assertEqual(result.returncode, 1, self._output(result))
self.assertIn("patch-ID mismatch with upstream", result.stdout)
def test_rejects_malformed_patch_id_output(self):
parent, local = self._build_case("exact", context_parent=False)
result = self._validate(parent, local, "patch-id-malformed")
self.assertEqual(result.returncode, 1, self._output(result))
self.assertIn("patch-ID mismatch with upstream", result.stdout)
def test_rejects_merge_tree_output_with_extra_line(self):
parent, local = self._build_case("context")
result = self._validate(parent, local, "merge-tree-extra-line")
self.assertEqual(result.returncode, 1, self._output(result))
self.assertIn("patch-ID mismatch with upstream", result.stdout)
def test_reports_replay_environment_failure(self):
parent, local = self._build_case("exact", context_parent=False)
result = self._validate(parent, local, "rev-parse-invalid-utf8")
self.assertEqual(result.returncode, 1, self._output(result))
self.assertIn("unable to verify patch replay", result.stderr)
def test_reports_merge_tree_failure(self):
parent, local = self._build_case("exact", context_parent=False)
result = self._validate(parent, local, "merge-tree-failure")
self.assertEqual(result.returncode, 1, self._output(result))
self.assertIn("synthetic merge-tree failure", result.stderr)
def test_rejects_multiple_patch_id_output_lines(self):
parent, local = self._build_case("exact", context_parent=False)
result = self._validate(parent, local, "patch-id-extra-line")
self.assertEqual(result.returncode, 1, self._output(result))
self.assertIn("patch-ID mismatch with upstream", result.stdout)
def test_rejects_mutated_payload(self):
parent, local = self._build_case("mutated")
result = self._validate(parent, local)
self.assertEqual(result.returncode, 1, self._output(result))
self.assertIn("patch-ID mismatch with upstream", result.stdout)
def test_rejects_same_change_at_different_occurrence(self):
parent, local = self._build_case("wrong-occurrence")
result = self._validate(parent, local)
self.assertEqual(result.returncode, 1, self._output(result))
self.assertIn("patch-ID mismatch with upstream", result.stdout)
def test_rejects_equal_patch_id_at_different_occurrence(self):
self._use_identical_occurrence_fixture()
parent, local = self._build_case(
"wrong-occurrence", context_parent=False)
upstream = self._git("rev-parse", "refs/remotes/upstream/linux")
self.assertEqual(self._patch_id(local), self._patch_id(upstream))
result = self._validate(parent, local)
self.assertEqual(result.returncode, 1, self._output(result))
self.assertIn("patch-ID mismatch with upstream", result.stdout)
if __name__ == "__main__":
unittest.main()