Skip to content

Commit bdcffd5

Browse files
committed
test: address review feedback on the macOS cross-user test scaffolding
Seven changes, all to test/CI scaffolding; no change to the shim or to _subprocess.py behaviour. 1. Delete test/openjd/sessions_v0/test_sudo.py. It duplicated the pre-existing TestFindChildProcessIdPgrep in test_linux_sudo.py case for case (5 of 5), and the existing versions are better: the no-match case drives real pgrep against a real childless process instead of mocking the return, the error case is parametrized over three exit codes and asserts the message content, and the adjacent TestFindSudoChildProcessGroupId covers the late-child race the fix exists for. Nothing was lost; full suite still 936 passed. 2. Move test_setsid_shim_creates_new_process_group out of TestLoggingSubprocessMacOSSetsid into a new POSIX-scoped TestSetsidShimBehavior. It was macOS-named but gated on is_posix(), so it ran on Linux inside a macOS class. Kept POSIX rather than narrowed to macOS: the shim is portable stdlib (os.getpgrp/getpid/setsid/execvp, no platform branch) and macOS is only where it is *required*, so running it on Linux too catches a broken shim string on faster runners. Whether macOS selects the shim is a separate concern already covered by test_builds_setsid_shim_command_on_macos. 3. Add a class-level macOS skipif to TestMacOSShimInterpreter. Unlike the shim string, _macos_shim_interpreter() really is macOS-only selection logic. 4. Extract the workflow's ~80 lines of inline provisioning into scripts/run_macos_sudo_tests.sh, mirroring scripts/run_sudo_tests.sh so the job is reproducible on a developer's Mac. Because macOS cannot be containerized, the script is the counterpart to the Linux Dockerfile rather than to the docker command: it provisions, runs and then removes the users, groups, sudoers file, python symlink and temp root it created. Teardown is best-effort throughout so a partial provision is still removable, and --keep / --cleanup-only cover the CI and recovery cases. 5. Add cross-user-test / cross-user-test-macos hatch scripts so neither platform's cross-user suite requires remembering a script path. 6. Expand the Python matrix from ['3.11','3.13'] to 3.9-3.14, matching code_quality.yml and requires-python >=3.9. 7. Drop the paths: filter so the job runs on every PR. The cross-user path can break from more places than a four-file list can enumerate, and a filtered job that misses one reads as a pass. Also drops the workflow's final "run remaining tests" step: code_quality.yml already runs the whole suite on macos-latest across this same matrix, so that step only duplicated it. This job now covers the cross-user tests alone. Signed-off-by: andychoquette <78888816+andychoquette@users.noreply.github.com>
1 parent 843cd05 commit bdcffd5

5 files changed

Lines changed: 263 additions & 212 deletions

File tree

‎.github/workflows/macos_cross_user_test.yml‎

Lines changed: 22 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,24 @@ name: macOS Cross-User Tests
66
# process launch as another user, new-process-group creation, signalling, and
77
# process-tree termination.
88
#
9-
# The runner's passwordless sudo is used to provision the same user/group layout
10-
# that testing_containers/localuser_sudo_environment/Dockerfile creates for Linux:
11-
# runner -- runs the pytests (member of the shared group)
12-
# openjd-target -- the impersonated user (member of the shared group)
13-
# openjd-disjoint -- a user with no group in common (temp-dir permission tests)
9+
# The provisioning, the test run and the teardown all live in
10+
# scripts/run_macos_sudo_tests.sh, so a developer can reproduce this job on their
11+
# own Mac with one command (`hatch run cross-user-test-macos`). This job is
12+
# deliberately a thin wrapper around that script: anything it did that the script
13+
# does not would be something a developer cannot reproduce.
14+
#
15+
# This job covers ONLY the cross-user tests. The rest of the suite already runs on
16+
# macos-latest across the same Python matrix in code_quality.yml, so re-running it
17+
# here would duplicate that coverage.
18+
#
19+
# Runs on every PR rather than behind a paths filter: the cross-user path can be
20+
# broken from more places than a file list can enumerate (session setup, tempdir
21+
# handling, signalling), and a filtered job that misses those reads as a pass.
1422

1523
on:
1624
workflow_dispatch:
1725
pull_request:
1826
branches: [ mainline, release ]
19-
paths:
20-
- 'src/openjd/sessions/_subprocess.py'
21-
- 'src/openjd/sessions/_linux/_sudo.py'
22-
- 'src/openjd/sessions/_os_checker.py'
23-
- 'src/openjd/sessions/_tempdir.py'
24-
- '.github/workflows/macos_cross_user_test.yml'
25-
26-
env:
27-
OPENJD_TEST_SUDO_TARGET_USER: openjd-target
28-
OPENJD_TEST_SUDO_SHARED_GROUP: openjd-shared
29-
OPENJD_TEST_SUDO_DISJOINT_USER: openjd-disjoint
30-
OPENJD_TEST_SUDO_DISJOINT_GROUP: openjd-disjointgrp
31-
# Hatch's default data dir is under ~/Library, which other users cannot traverse.
32-
# The impersonation tests execute the hatch venv's python as the target user, so
33-
# the venv must live somewhere world-traversable.
34-
HATCH_DATA_DIR: /opt/hatch
35-
# macOS's default per-user temp dir (/var/folders/<hash>/T, mode 700) is not
36-
# traversable by the impersonated user, and /var is a symlink to /private/var
37-
# (which TempDir resolves but gettempdir() does not). Use a dedicated
38-
# world-writable, already-resolved temp root instead, which matches the /tmp
39-
# semantics the impersonation tests get on Linux. Created during provisioning
40-
# owned by runner:staff because BSD filesystems give new files the GROUP OF THE
41-
# PARENT DIRECTORY (not the creator's gid), and the same-user TempDir test
42-
# asserts the created directory has the creating process's gid.
43-
TMPDIR: /private/tmp/openjd-tests
4427

4528
jobs:
4629
macos-cross-user:
@@ -50,120 +33,21 @@ jobs:
5033
strategy:
5134
fail-fast: false
5235
matrix:
53-
python-version: ['3.11', '3.13']
36+
# Matches code_quality.yml: requires-python is >=3.9, and the boundary
37+
# versions are where an interpreter-specific difference in the setsid shim
38+
# or in sys._base_executable resolution would surface.
39+
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13', '3.14']
5440
steps:
5541
- uses: actions/checkout@v4
5642

5743
- uses: actions/setup-python@v5
5844
with:
5945
python-version: ${{ matrix.python-version }}
6046

61-
- name: Provision test users and groups
62-
run: |
63-
set -euxo pipefail
64-
65-
# Groups
66-
sudo dseditgroup -o create "${OPENJD_TEST_SUDO_SHARED_GROUP}"
67-
sudo dseditgroup -o create "${OPENJD_TEST_SUDO_DISJOINT_GROUP}"
68-
69-
# Target user: impersonated by the tests; shares a group with runner
70-
sudo sysadminctl -addUser "${OPENJD_TEST_SUDO_TARGET_USER}" \
71-
-fullName "OpenJD Test Target" -password "OpenJD-ci-test-1!" -shell /bin/zsh
72-
sudo createhomedir -c -u "${OPENJD_TEST_SUDO_TARGET_USER}" > /dev/null
73-
sudo dseditgroup -o edit -a "${OPENJD_TEST_SUDO_TARGET_USER}" -t user "${OPENJD_TEST_SUDO_SHARED_GROUP}"
74-
# Linux useradd gives every user a self-named group, and
75-
# test_cleanup_posix_user chowns to "user:user"; macOS does not, so
76-
# create the self-named group explicitly. runner must NOT be a member.
77-
sudo dseditgroup -o create "${OPENJD_TEST_SUDO_TARGET_USER}"
78-
sudo dseditgroup -o edit -a "${OPENJD_TEST_SUDO_TARGET_USER}" -t user "${OPENJD_TEST_SUDO_TARGET_USER}"
79-
80-
# Disjoint user: NO group in common with runner
81-
sudo sysadminctl -addUser "${OPENJD_TEST_SUDO_DISJOINT_USER}" \
82-
-fullName "OpenJD Test Disjoint" -password "OpenJD-ci-test-1!" -shell /bin/zsh
83-
sudo createhomedir -c -u "${OPENJD_TEST_SUDO_DISJOINT_USER}" > /dev/null
84-
sudo dseditgroup -o edit -a "${OPENJD_TEST_SUDO_DISJOINT_USER}" -t user "${OPENJD_TEST_SUDO_DISJOINT_GROUP}"
85-
86-
# The test-running user joins the shared group (matches the Docker layout)
87-
sudo dseditgroup -o edit -a runner -t user "${OPENJD_TEST_SUDO_SHARED_GROUP}"
88-
89-
# Passwordless sudo from runner to the target user (and itself), mirroring
90-
# the hostuser rule in the Linux test container
91-
echo "runner ALL=(${OPENJD_TEST_SUDO_TARGET_USER},runner) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/openjd-cross-user-tests
92-
sudo chmod 440 /etc/sudoers.d/openjd-cross-user-tests
93-
sudo visudo -cf /etc/sudoers.d/openjd-cross-user-tests
94-
95-
# test_basic_operation runs a bare `python` as the target user via
96-
# `sudo -i`; macOS ships python3 only, so provide the alias.
97-
sudo mkdir -p /usr/local/bin
98-
sudo ln -sf /usr/bin/python3 /usr/local/bin/python
99-
100-
# Flush Directory Services caches so the new users/groups resolve
101-
sudo dscacheutil -flushcache
102-
103-
# World-writable temp root for the tests (see TMPDIR at the top of the file)
104-
sudo mkdir -p "${TMPDIR}"
105-
sudo chown runner:staff "${TMPDIR}"
106-
sudo chmod 1777 "${TMPDIR}"
107-
108-
- name: Verify provisioning
109-
run: |
110-
set -euxo pipefail
111-
id "${OPENJD_TEST_SUDO_TARGET_USER}"
112-
id "${OPENJD_TEST_SUDO_DISJOINT_USER}"
113-
id runner
114-
# The isolation invariant the tests rely on: runner and the target user
115-
# share OPENJD_TEST_SUDO_SHARED_GROUP; the disjoint user shares nothing.
116-
id -Gn runner | tr ' ' '\n' | grep -qx "${OPENJD_TEST_SUDO_SHARED_GROUP}"
117-
id -Gn "${OPENJD_TEST_SUDO_TARGET_USER}" | tr ' ' '\n' | grep -qx "${OPENJD_TEST_SUDO_SHARED_GROUP}"
118-
if id -Gn "${OPENJD_TEST_SUDO_DISJOINT_USER}" | tr ' ' '\n' | grep -qx "${OPENJD_TEST_SUDO_SHARED_GROUP}"; then
119-
echo "disjoint user must not be in the shared group" && exit 1
120-
fi
121-
# Cross-user execution works at all
122-
sudo -u "${OPENJD_TEST_SUDO_TARGET_USER}" -i /usr/bin/true
123-
sudo -u "${OPENJD_TEST_SUDO_TARGET_USER}" -i python -c 'import getpass; print("bare python runs as", getpass.getuser())'
124-
12547
- name: Install hatch
126-
run: |
127-
sudo mkdir -p "${HATCH_DATA_DIR}"
128-
sudo chown runner "${HATCH_DATA_DIR}"
129-
pip install hatch
130-
131-
- name: Create test environment
132-
run: |
133-
set -euxo pipefail
134-
hatch env create
135-
# The target user executes the venv python and reads test support files;
136-
# make the venv and the workspace world-readable/traversable.
137-
chmod -R o+rX "${HATCH_DATA_DIR}" "${GITHUB_WORKSPACE}"
138-
139-
- name: Report which interpreter the setsid shim resolves to
140-
# NOTE: no braces in this inline script -- hatch run applies its own
141-
# {...} template substitution to the arguments it receives.
142-
run: |
143-
hatch run python -c "
144-
from openjd.sessions._subprocess import _macos_shim_interpreter, _MACOS_FALLBACK_SHIM_INTERPRETER
145-
picked = _macos_shim_interpreter()
146-
branch = 'FALLBACK' if picked == _MACOS_FALLBACK_SHIM_INTERPRETER else 'BASE-INTERPRETER'
147-
print('shim interpreter:', picked, '(' + branch + ' branch)')
148-
"
149-
150-
- name: Run cross-user impersonation tests
151-
run: |
152-
set -euxo pipefail
153-
# -rxX lists (x)failed and (X)passed-unexpectedly tests in the summary so the
154-
# next step can assert nothing silently xfailed back to a no-op.
155-
hatch run test -- test/openjd/sessions_v0/test_subprocess.py test/openjd/sessions_v0/test_tempdir.py \
156-
--no-cov -rxX 2>&1 | tee pytest-cross-user.log
157-
158-
- name: Assert impersonation tests actually ran
159-
run: |
160-
set -euxo pipefail
161-
# If the OPENJD_TEST_SUDO_* wiring regresses, the impersonation tests xfail
162-
# with this message instead of failing the job -- catch that here.
163-
if grep -q "Must define environment vars OPENJD_TEST_SUDO" pytest-cross-user.log; then
164-
echo "Impersonation tests were skipped (env vars not picked up); provisioning is broken."
165-
exit 1
166-
fi
48+
run: pip install hatch
16749

168-
- name: Run remaining tests
169-
run: hatch run test -- --ignore test/openjd/sessions_v0/test_subprocess.py --ignore test/openjd/sessions_v0/test_tempdir.py --no-cov
50+
- name: Provision, run cross-user tests, and tear down
51+
# --keep skips the teardown: the runner is throwaway, so leaving the
52+
# environment in place costs nothing and keeps a failed run inspectable.
53+
run: bash scripts/run_macos_sudo_tests.sh --keep

‎hatch.toml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ pre-install-commands = [
77
sync = "pip install -r requirements-testing.txt"
88
test = "pytest --cov-config pyproject.toml {args}"
99
typing = "mypy {args:src test}"
10+
# Cross-user (jobRunAsUser impersonation) tests. These need a provisioned
11+
# user/group/sudoers environment, so they go through the platform's setup script
12+
# rather than pytest alone: Linux uses a throwaway container, macOS provisions the
13+
# host and cleans up after itself.
14+
cross-user-test = "bash scripts/run_sudo_tests.sh {args}"
15+
cross-user-test-macos = "bash scripts/run_macos_sudo_tests.sh {args}"
1016
style = [
1117
"ruff check {args:.}",
1218
"black --check --diff {args:.}",

0 commit comments

Comments
 (0)