Skip to content

Live tests for the Python and asyncio docs code samples - #241

Merged
techwritermat merged 20 commits into
masterfrom
docs-snippets-live-tests
Nov 25, 2026
Merged

techwritermat merged 20 commits into
masterfrom
docs-snippets-live-tests

Conversation

@techwritermat

@techwritermat techwritermat commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds docs-snippets/, a pytest suite that runs the code samples of the Python and asyncio SDK docs against the real PubNub service and asserts the documented effect. The docs embed the snippet regions with EmbeddedCode from this repo.

What is covered

  • 311 snippet ids in 193 steps: 183 live ids in 109 sync steps, 125 live ids in 84 asyncio steps, 3 compile-only
  • Areas: publish, signal, fire, subscribe (new and legacy), presence, message persistence, message actions, channel groups, files, mobile push, App Context, Access Manager (v2 and v3), encryption, configuration, misc
  • Not covered: signature blocks, response tables, interactive examples. The Python SDK has no DataSync API, so there is no DataSync role

How it stays honest

  • Assertions sit outside the snippet markers. A marker lint enforces it, so the region stays what the docs show
  • A step fails when its snippet did not reach snippet.end or when it made no assertion
  • Every step has its own run-suffixed names, so a client that lingers in presence cannot affect the next step
  • No skips, no xfail, no fixed sleeps (eventually for reads after writes), subset runs are refused in CI
  • Keys and tokens are masked in output and reports, the janitor revokes application-level Access Manager grants and removes leftovers
  • coverage.json lists every snippet id, python -m harness.cli manifest --check fails when it is stale
  • python -m harness.audit lists assertions per step, one log file per step is written to report/<shard>/steps/

techwritermat and others added 2 commits November 12, 2026 11:00
The "Cancel workflow runs for commit on error" step (fast-jobs-failure)
cancels every workflow of the commit when one fast job fails, so an unrelated
failure cancelled the first attempt of other workflows. The JavaScript SDK
removed the same step in PR 539.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Adds docs-snippets/, a pytest suite that runs the code samples of the Python
and asyncio docs against the real PubNub service and asserts the documented
effect. The docs embed the snippet regions with EmbeddedCode.

* 311 snippet ids in 193 steps (sync and asyncio), roles open and pam
* assertions sit outside the snippet markers, a marker lint enforces it
* honesty checks: each step must reach snippet.end and make an assertion
* run-suffixed names per step, key and token redaction, janitor, no skips
* coverage.json manifest with a --check mode
* separate workflow: dry job on Python 3.9 to 3.13, live shards in series
  under one concurrency group, nightly, fork PRs skip live
* setup.py excludes docs-snippets from the package

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pubnub-snyk-universal-prd

pubnub-snyk-universal-prd Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

techwritermat and others added 2 commits November 12, 2026 11:18
The staged copy under docs-snippets/.run/<id>/ cannot find the checkout from its
own harness package, so pytest failed with "No module named 'pubnub'" wherever
the package is not installed (the CI runner installs only the dev requirements).
The run now sets PYTHONPATH to the repository root. The key status line prints
only the roles the shard needs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The presence service resolves a channel group some seconds after it was
created and answers 400 "No valid channels specified" until then. The read
after the write now retries for up to 45 seconds with the same assertion.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
techwritermat and others added 15 commits November 20, 2026 09:43
- Updated lint job names in live integration workflows to remove reference to port map.
- Modified lint function in cli.py to conditionally check for base availability before executing port map checks, providing a fallback message for shallow CI checkouts.
- Added base_available function in port_map.py to verify if the base commit exists in the current checkout.
…lity

- Replaced instances of aeventually with asettled in various test files to ensure consistent state checks.
- Updated related imports to include asettled where necessary.
- Enhanced test reliability by ensuring that state checks wait for settled conditions before proceeding.
- Added a new `present` function to ensure user IDs are occupants of specified channels during presence tests.
- Updated `seed_parallel_async` to include a concurrency parameter, allowing controlled message publishing with error handling.
- Modified presence tests to utilize the new `present` function for improved reliability in state checks.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- Refactored comments in `live-integration.yml` for clarity on failure alerts.
- Improved documentation in `conftest.py` regarding the offline guard feature.
- Removed unused `port-map` functionality from `cli.py` and related files.
- Updated README and other documentation files for consistency and clarity.
- Enhanced test descriptions and fixed minor issues in various test files.
- Deleted obsolete files related to endpoint tier moves and port text changes.

These changes streamline the testing process and improve the overall documentation for the live tier tests.
- Replaced the `seed_parallel` function with `seed_many` in multiple test files to publish messages sequentially, ensuring unique timetokens for each publish.
- Updated relevant test cases to reflect the change in message seeding method, enhancing reliability in message history checks.

These changes improve the accuracy of message publishing tests by preventing duplicate timetokens during concurrent operations.
Take master's Ruff setup (#243) for the validations workflow and the lint
command, and restore the cross-workflow cancel step in the Tests workflow, so
the only differences from master outside tests/live are the live tier's own
files. The test command keeps --ignore=tests/live/ because the live tier needs
keys and runs in its own workflow.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
flake8 is no longer installed (requirements-dev.txt follows master). The live
workflow now runs `ruff check tests/live`, the README says so, and the encryption
page drops two imports that the samples import themselves and uses a Ruff noqa
for the blank lines that the published sample keeps.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- Introduced a new `test_objects.py` file for asyncio tests, covering various metadata operations including UUID and channel management.
- Updated `coverage.json` to include new test cases for the asyncio API reference.
- Modified `groups.py` to reference the new asyncio test file.
- Cleaned up `snippet-exceptions.json` by removing obsolete entries related to the asyncio docset.

These changes enhance the testing framework for asyncio operations, ensuring comprehensive coverage and improved organization of test cases.
- Reformatted the `await pubnub.set_channel_members` call in the `test_set_channel_members_builder` function to enhance code clarity.
- This change improves the overall readability of the test case without altering its functionality.
- Updated the `fetch` function in `file_sharing.py` to enforce HTTPS URL fetching, raising a `ValueError` for non-HTTPS URLs.
- Improved error handling in `clients.py` by adding debug logging for leave request failures.
- Refactored test cases in `test_file_sharing.py` to utilize the updated `fetch` function for URL validation and response checks.
- Adjusted payload structures in `test_encryption.py` to improve clarity and consistency in encryption tests.

These changes enhance security and debugging capabilities in the test suite.
- Modified the `role` in `coverage.json` for `initialize_access_manager` to include `pam`.
- Updated `test_initialize_access_manager` in both asyncio and sync test files to read the secret key from the `PUBNUB_SECRET_KEY` environment variable, ensuring compatibility with the PAM keyset.

These changes enhance the security and flexibility of the access manager tests by integrating PAM keyset support.
- Modified `test_initialize_access_manager` in both asyncio and sync test files to read the secret key from the `PUBNUB_SECRET_KEY` environment variable, enhancing security and flexibility in the tests.
- Introduced a new helper function in `extractor.py` to commit documentation changes, improving the process of tracking documentation updates.

These changes ensure that the tests align with best practices for secret management and streamline documentation handling.

pubnub = PubNubAsyncio(pnconfig)
# snippet.end
a.eq(pnconfig.secret_key, environ['PUBNUB_SECRET_KEY'], 'the sample sets the secret key from the environment')

@jguz-pubnub jguz-pubnub Nov 25, 2026 •

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.

If this assertion ever fails, a.eq prints both values in the message, so the real PAM secret key would end up in the CI log. Can you verify this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've switched both assertions to a.ok(pnconfig.secret_key == environ['PUBNUB_SECRET_KEY'], ...), so a failure only prints the message and no key value. I also checked the other live tests and found no other a.eq on a secret. The recent CI logs show no failing run for this assertion, and GitHub masks the key in them, but I didn't want to depend on that masking.


pubnub = PubNub(pnconfig)
# snippet.end
a.eq(pnconfig.secret_key, environ['PUBNUB_SECRET_KEY'], 'the sample sets the secret key from the environment')

@jguz-pubnub jguz-pubnub Nov 25, 2026 •

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.

If this assertion ever fails, a.eq prints both values in the message, so the real PAM secret key would end up in the CI log. Can you verify this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

as above

- Updated the `test_initialize_access_manager` in both asyncio and sync test files to replace `a.eq` with `a.ok` for asserting the secret key from the environment variable. This change enhances security by preventing the secret key from being logged in CI outputs.

These modifications improve the security practices in the test suite while maintaining functionality.
@techwritermat
techwritermat merged commit b0a1f6a into master Nov 25, 2026
49 of 65 checks passed
@techwritermat
techwritermat deleted the docs-snippets-live-tests branch November 25, 2026 12:01
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