Skip to content

Commit 47813d1

Browse files
authored
Opt selected Linux CMake unittests into report upload
Summary: Add an off-by-default upload input, gated to noneditable Linux CMake jobs. Enable only pull unittest and trunk unittest-release, with actions:read propagated for job-ID lookup. Keep editable, Buck, backend, macOS and Windows calls unopted. AI-assisted implementation. Reviewed By: shoumikhin Differential Revision: D129316990 Pull Request resolved: #24877
1 parent 3cef345 commit 47813d1

4 files changed

Lines changed: 121 additions & 3 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
import unittest
8+
from pathlib import Path
9+
10+
import yaml
11+
12+
13+
ROOT = Path(__file__).resolve().parents[3]
14+
WORKFLOWS = ROOT / ".github/workflows"
15+
REUSABLE = "./.github/workflows/_unittest.yml"
16+
17+
18+
def load_workflow(name):
19+
return yaml.safe_load((WORKFLOWS / name).read_text())
20+
21+
22+
class TorchciWorkflowTest(unittest.TestCase):
23+
def test_reporting_is_off_by_default_and_linux_only(self):
24+
workflow = load_workflow("_unittest.yml")
25+
triggers = workflow.get("on", workflow.get(True))
26+
report_input = triggers["workflow_call"]["inputs"]["upload-test-run-reports"]
27+
self.assertEqual(report_input["type"], "boolean")
28+
self.assertIs(report_input["default"], False)
29+
jobs = workflow["jobs"]
30+
self.assertEqual(
31+
jobs["linux"]["with"]["upload-test-run-reports"],
32+
"${{ inputs.upload-test-run-reports && inputs.build-tool == 'cmake' "
33+
"&& inputs.editable != 'true' }}",
34+
)
35+
for platform in ("macos", "windows"):
36+
self.assertNotIn("upload-test-run-reports", jobs[platform]["with"])
37+
for job in load_workflow("_test_backend.yml")["jobs"].values():
38+
self.assertNotIn("upload-test-run-reports", job.get("with", {}))
39+
40+
def test_linux_does_not_lower_the_callers_actions_permission(self):
41+
workflow = load_workflow("_unittest.yml")
42+
self.assertNotIn("permissions", workflow)
43+
self.assertNotIn("permissions", workflow["jobs"]["linux"])
44+
self.assertEqual(
45+
workflow["jobs"]["linux"]["uses"],
46+
"pytorch/test-infra/.github/workflows/linux_job_v3.yml@main",
47+
)
48+
49+
def test_all_four_callers_have_the_intended_opt_in_and_permission(self):
50+
callers = {}
51+
for path in WORKFLOWS.glob("*.yml"):
52+
for name, job in load_workflow(path.name).get("jobs", {}).items():
53+
if job.get("uses") == REUSABLE:
54+
callers[(path.name, name)] = job
55+
selected = {("pull.yml", "unittest"), ("trunk.yml", "unittest-release")}
56+
self.assertEqual(
57+
set(callers),
58+
selected
59+
| {("pull.yml", "unittest-editable"), ("pull.yml", "unittest-buck")},
60+
)
61+
for key, job in callers.items():
62+
with self.subTest(caller=key):
63+
opted_in = key in selected
64+
self.assertEqual(
65+
job["with"].get("upload-test-run-reports", False), opted_in
66+
)
67+
permissions = {"id-token": "write", "contents": "read"}
68+
if opted_in:
69+
permissions["actions"] = "read"
70+
self.assertEqual(job["with"]["build-tool"], "cmake")
71+
self.assertNotIn("editable", job["with"])
72+
self.assertEqual(job["permissions"], permissions)
73+
74+
def test_no_other_job_gains_actions_read(self):
75+
selected = {("pull.yml", "unittest"), ("trunk.yml", "unittest-release")}
76+
for name in ("pull.yml", "trunk.yml"):
77+
for key, job in load_workflow(name)["jobs"].items():
78+
if (name, key) not in selected:
79+
self.assertNotIn("actions", job.get("permissions", {}))
80+
81+
def test_image_resolution_and_linux_entrypoint_are_preserved(self):
82+
jobs = load_workflow("_unittest.yml")["jobs"]
83+
self.assertEqual(jobs["linux"]["needs"], "docker-image")
84+
self.assertEqual(
85+
jobs["docker-image"]["uses"], "./.github/workflows/_docker-image.yml"
86+
)
87+
script = jobs["linux"]["with"]["script"]
88+
self.assertIn('.ci/scripts/unittest-linux.sh --build-tool "', script)
89+
self.assertIn(' --build-mode "${{ inputs.build-mode }}"', script)
90+
self.assertIn(' --editable "${{ inputs.editable }}"', script)
91+
92+
def test_editable_and_buck_cannot_opt_in_even_with_a_true_input(self):
93+
expression = load_workflow("_unittest.yml")["jobs"]["linux"]["with"][
94+
"upload-test-run-reports"
95+
][3:-2].strip()
96+
for enabled in (False, True):
97+
for tool in ("cmake", "buck2"):
98+
for editable in ("false", "true"):
99+
with self.subTest(enabled=enabled, tool=tool, editable=editable):
100+
evaluated = (
101+
expression.replace(
102+
"inputs.upload-test-run-reports", repr(enabled)
103+
)
104+
.replace("inputs.build-tool", repr(tool))
105+
.replace("inputs.editable", repr(editable))
106+
.replace("&&", "and")
107+
)
108+
self.assertEqual(
109+
eval(evaluated, {"__builtins__": {}}, {}),
110+
enabled and tool == "cmake" and editable == "false",
111+
)

‎.github/workflows/_unittest.yml‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ on:
2828
required: false
2929
type: number
3030
default: 90
31+
upload-test-run-reports:
32+
required: false
33+
type: boolean
34+
default: false
35+
description: Upload Linux CMake pytest test-run reports to S3.
3136

3237
jobs:
3338
docker-image:
@@ -37,15 +42,13 @@ jobs:
3742
linux:
3843
needs: docker-image
3944
uses: pytorch/test-infra/.github/workflows/linux_job_v3.yml@main
40-
permissions:
41-
id-token: write
42-
contents: read
4345
with:
4446
runner: mt-l-x86iavx512-8-64
4547
docker-image: ${{ needs.docker-image.outputs.docker-registry }}/${{ inputs.docker-image }}-${{ needs.docker-image.outputs.ci-docker-hash }}
4648
submodules: 'recursive'
4749
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
4850
timeout: ${{ inputs.linux-timeout }}
51+
upload-test-run-reports: ${{ inputs.upload-test-run-reports && inputs.build-tool == 'cmake' && inputs.editable != 'true' }}
4952
script: |
5053
set -eux
5154
.ci/scripts/unittest-linux.sh --build-tool "${{ inputs.build-tool }}" --build-mode "${{ inputs.build-mode }}" --editable "${{ inputs.editable }}"

‎.github/workflows/pull.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,11 @@ jobs:
894894
unittest:
895895
uses: ./.github/workflows/_unittest.yml
896896
permissions:
897+
actions: read
897898
id-token: write
898899
contents: read
899900
with:
901+
upload-test-run-reports: true
900902
build-mode: Debug
901903
build-tool: cmake
902904
docker-image: ci-image:executorch-ubuntu-22.04-clang12

‎.github/workflows/trunk.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,11 @@ jobs:
992992
unittest-release:
993993
uses: ./.github/workflows/_unittest.yml
994994
permissions:
995+
actions: read
995996
id-token: write
996997
contents: read
997998
with:
999+
upload-test-run-reports: true
9981000
build-mode: Release
9991001
build-tool: cmake
10001002
docker-image: ci-image:executorch-ubuntu-22.04-clang12

0 commit comments

Comments
 (0)