Skip to content

CBSE-24138: Make the unit suite runnable on Windows - #375

Open
celticht32 wants to merge 1 commit into
couchbase:mainfrom
celticht32:fix/windows-unit-suite
Open

celticht32 wants to merge 1 commit into
couchbase:mainfrom
celticht32:fix/windows-unit-suite

Conversation

@celticht32

@celticht32 celticht32 commented Nov 17, 2026 •

Copy link
Copy Markdown

Related Issue

Resolves: CBSE-24138 (https://couchbasecloud.atlassian.net/browse/CBSE-24138)

Server(s) Affected

  • Operational (operational)
  • Operational Insights (operational-insights)
  • Shared machinery (all servers)

Almost entirely a test change. The one change under src/ is in the audit sink (see below).

What does this change do?

The unit suite could not run on Windows. It failed during collection, before any test executed, because three modules import Unix-only standard library modules at module scope:

File Imports
tests/unit/test_sdk_isolation.py resource
tests/unit/operational/test_reference_tools_unit.py resource
tests/unit/test_audit_integration_points.py pwd

A collection error aborts the whole run, so a contributor on Windows had no way to verify a change before opening a pull request. The imports are now guarded and only the tests that cannot work on Windows are skipped: two tests that assert POSIX uid-to-passwd mapping. Everything else runs on both platforms.

Two further changes make the whole suite pass on Windows:

  • tests/unit/test_workers_smoke.py: the test could not stop its server on Windows. Popen.send_signal(SIGINT) is not supported there, and the console script is a launcher whose process id is not the Python process's. It now ends the process tree with taskkill /T and skips the supervisor pid assertion on Windows. The check that the supervisor and each worker write their own log files still runs.
  • src/cb_mcp/audit/sink.py: the sink opened its log in text mode, so on Windows every \n was written as \r\n while rotation counted the bytes it meant to write. Measured over the same 20 records: 1920 counted and 1920 on disk on Linux, 1920 counted and 1940 on disk on Windows, so rotation fired late. The four open() calls now use newline="", so records are written exactly as emitted on every platform. There is no change on Linux or macOS. Tracked in AuditSink writes CRLF on Windows, so its rotation size count is wrong #383.

Why is this change needed?

A contributor on Windows cannot verify a change before opening a pull request, and two audit-sink rotation tests failed there because of a real defect in the sink.

Evidence of Testing

# Windows
.venv\Scripts\python.exe -m pytest tests/unit -q
2256 passed, 2 skipped

# Linux
pytest tests/unit -q
2258 passed
ruff format --check / ruff check: clean

The 2 Windows skips are test_local_identity_ignores_the_caller_controlled_environment and test_local_identity_falls_back_to_the_numeric_uid_without_a_passwd_entry, both POSIX uid-to-passwd mapping. They run on Linux, and CI is ubuntu-latest throughout.

No cluster runs: nothing here touches cluster-facing code. Manual verification with an MCP client: not applicable.

Worth considering separately: every job in .github/workflows runs on ubuntu-latest, so nothing prevents the next Unix-only import landing the same way. A windows-latest entry on the test matrix would. Out of scope here.

Compatibility Considerations

The audit sink now writes log lines with a bare newline on Windows, as it already did elsewhere. No tool, flag, environment variable or dependency changes. One commit on main 45e8b32; no dependency on other PRs.

Checklist

  • Linked to an issue (required for new tools)
  • Uses the server's SDK (no cluster calls)
  • Works on every deployment the server targets (no deployment-specific behaviour)
  • No changes to cb_mcp.core contracts / managed MCP interfaces (or discussed first)
  • Unit tests added/updated
  • Integration tests added/updated (not applicable where no cluster-facing code changes)
  • Read-only mode, tool annotations, and audit classification handled
  • Results are bounded and no new cross-call state was added
  • Evidence of testing included above (manual MCP-client run pending)
  • Docs updated (README, DOCKER.md, server.json): not needed for this change
  • Lint and pre-commit pass

@celticht32
celticht32 force-pushed the fix/windows-unit-suite branch 2 times, most recently from 1412361 to 10a89fd Compare November 19, 2026 15:39
@celticht32
celticht32 marked this pull request as ready for review November 20, 2026 00:10
Copilot AI balanced review requested due to automatic review settings November 20, 2026 00:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are blocking approval.

No open issues 🎉

What changed in this PR

This test-only PR makes the unit suite collect and run on Windows without changing server behavior. Existing Windows failures remain outside its scope.

Changes:

  • Preserve the parent environment for SDK-isolation subprocesses while explicitly setting PYTHONPATH.
  • Guard the Unix-only pwd import and skip the two POSIX identity tests on Windows.
  • Replace RSS measurements with cross-platform tracemalloc measurements and tighten the streaming-memory threshold.
File Description
tests/​unit/​test_sdk_isolation.py Inherits the environment so probe subprocesses can start on Windows.
tests/​unit/​test_audit_integration_points.py Guards pwd and adds Windows skips for POSIX-specific tests.
tests/​unit/​operational/​test_reference_tools_unit.py Uses tracemalloc to check streaming memory usage across platforms.

Review effort: Balanced

@celticht32
celticht32 force-pushed the fix/windows-unit-suite branch 2 times, most recently from 69138f6 to 0de546f Compare November 23, 2026 16:27
@celticht32

Copy link
Copy Markdown
Author

Updated: rebased onto current main (45e8b32) and addressed the review comments, with a reply on each thread. No code change, rebase only. Unit tests: 2258 passed on Linux, ruff format --check and ruff check clean.

@celticht32

Copy link
Copy Markdown
Author

Updated this PR. It now makes the full unit suite pass on Windows, not just most of it. The audit sink part is tracked in #383.

Added since the last push:

  • tests/unit/test_workers_smoke.py: on Windows the test could not stop its server. Popen.send_signal(SIGINT) is not supported there, and the console script is a launcher whose process id is not the Python process's. It now ends the whole process tree with taskkill /T, and skips the supervisor pid assertion on Windows. The check that one supervisor and each worker write their own log files still runs.
  • src/cb_mcp/audit/sink.py: the sink opened its log in text mode, so on Windows every \n was written as \r\n. Rotation counts the bytes it meant to write, so the file on disk was one byte longer per line than counted, and rotation fired late. The four open() calls now use newline="", so records are written exactly as emitted on every platform. This is what made the two test_audit_sink rotation tests fail on Windows. There is no change on Linux or macOS.

Verification: pytest tests/unit on Windows gives 2256 passed, 2 skipped. On Linux, 2258 passed. ruff format and ruff check are clean. The branch is one commit on current main.

Four independent defects, all in tests, none touching src. The first three make
the unit suite unrunnable on Windows, so a contributor on that platform cannot
verify anything before opening a pull request. Each is a module-scope import or
a hardcoded POSIX value, and because a collection error is fatal, any one of
them takes the whole suite down rather than the file it is in. The fourth makes
one test fail there for reasons that are not the product's.

test_audit_integration_points.py imports pwd at module scope. pwd is Unix-only.
cb_mcp/audit/identity.py already guards its own import of it and has a Windows
path through getpass, so the product is fine and only the test is not. The
import is guarded the same way the source guards it, and the two tests of the
twenty-nine that genuinely assert POSIX uid-to-passwd mapping are skipped by
platform, because there is no Windows behaviour to assert instead. The other
twenty-seven then run there.

test_reference_tools_unit.py imports resource at module scope, also Unix-only.
Its one use was measuring peak RSS for the streaming invariant, which
tracemalloc measures on every platform, so the measurement moves there rather
than being skipped off Unix. That also measures the right quantity: peak RSS
counts pages the allocator has not returned to the operating system, so it
moves for reasons unrelated to whether records were materialised, and it can
stay flat when they were. Against allocation the separation is stark, 0.007 MB
when streaming against 11.8 MB with records held, so the growth bound tightens
from 10.0 MB to 2.0 MB. The old bound sat close enough to the broken value that
a smaller dataset could have slipped under it.

test_sdk_isolation.py builds its probe subprocess environment by hand with a
literal PATH of /usr/bin:/bin. That is not a valid PATH on Windows, so the
probe interpreter cannot start and all nine tests in the file fail. What the
probe needs to control is PYTHONPATH, which is still set explicitly; the rest
of the environment is now inherited.

test_workers_smoke.py fails on Windows in two ways that are both in the test.
Its fixture stops the server with send_signal(SIGINT), which Windows Python
rejects for a child process (ValueError: Unsupported signal), so teardown errors
and the kill that follows never runs. And it compares the pid of the process it
started with the pids in the log file names. On Windows the console script is a
launcher that starts the real Python process, so the test holds the launcher's
pid while the logs carry the Python process's, and the check fails after every
earlier assertion has passed. Teardown now ends the whole process tree with
taskkill /T on Windows and keeps SIGINT elsewhere. The supervisor-pid check is
skipped on Windows only; the check that exactly one supervisor and one process
per worker each wrote their own log files still runs everywhere.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants