Repository navigation
test: add a Hypothesis property test suite with a nightly run #2869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tonyandrewmeyer
wants to merge
12
commits into
canonical:property-tests
Choose a base branch
from
tonyandrewmeyer:rainy/hypothesis-pilot-trimmed-v2
base: property-tests
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
540b97d
test: add a Hypothesis property test suite in test/property
tonyandrewmeyer 662ab2f
ci: run the property tests on pull requests and nightly
tonyandrewmeyer 012b96f
test: use st.builds for the port strategies, matching the status ones
tonyandrewmeyer cfec9c2
ci: run the property tests on the property-tests branch
tonyandrewmeyer a514602
ci: name the property test job property, not test
tonyandrewmeyer df33050
test: test the unknown status's dropped message as its own property
tonyandrewmeyer d877cd6
test: test port-number validation rather than restating _to_ops
tonyandrewmeyer 3209da1
docs: don't call the ci property-test profile small
tonyandrewmeyer c9c26f7
ci: give the pull-request property run a 10-minute timeout
tonyandrewmeyer 3280a2f
test: keep Hypothesis's deadline for local property-test runs
tonyandrewmeyer f09785e
test: drop the box-drawing section headers from the property tests
tonyandrewmeyer 2b33ac9
docs: say that ops.testing property tests live in test/property too
tonyandrewmeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| name: ops Property Tests | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
| workflow_dispatch: | ||
| schedule: | ||
| - cron: '17 15 * * *' | ||
|
|
||
| permissions: {} | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| persist-credentials: false | ||
| - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 | ||
| - run: uv tool install tox --with tox-uv | ||
|
|
||
| # Pull requests and pushes get a small, derandomised run. The scheduled | ||
| # and manual runs get a large budget with fresh randomness, to look for | ||
| # inputs nobody has tried yet. | ||
| - name: Run property tests | ||
| run: tox -e property | ||
| env: | ||
| HYPOTHESIS_PROFILE: ${{ (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') && 'nightly' || 'ci' }} | ||
| timeout-minutes: 60 | ||
|
|
||
| open-issue-on-failure-if-scheduled: | ||
| if: ${{ failure() && github.event_name == 'schedule' }} | ||
| needs: [test] | ||
| permissions: | ||
| issues: write | ||
| actions: read # for ai-failure-enrich.yaml | ||
| secrets: | ||
| # For ai-failure-enrich.yaml -- can only be read in the ai-failure-triage environment. | ||
| OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} | ||
| uses: $/.github/workflows/notify-scheduled-failure.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,3 +55,4 @@ juju-crashdump*.tar.xz | |
|
|
||
| # Workshop | ||
| /.workshop.lock | ||
| .hypothesis/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Copyright 2026 Canonical Ltd. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Hypothesis settings profiles for the property tests. | ||
|
|
||
| The profile is chosen with the ``HYPOTHESIS_PROFILE`` environment variable: | ||
|
|
||
| * ``dev`` (the default): Hypothesis's usual budget, for running locally. | ||
| * ``ci``: a small, derandomised budget, so a pull request's result doesn't | ||
| depend on luck. | ||
| * ``nightly``: a large budget with fresh randomness, for the scheduled run. | ||
|
|
||
| Tests shouldn't set ``max_examples`` themselves, so that the profile decides | ||
| how hard each run looks. When the nightly run finds a failing example, add it | ||
| to the test with ``@example(...)`` so that every later run checks it. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| from hypothesis import settings | ||
|
|
||
| # Generation is cheap here, but CI machines are noisy, and a timing-based | ||
| # failure would say nothing about the code. | ||
| settings.register_profile('dev', deadline=None) | ||
| settings.register_profile('ci', deadline=None, derandomize=True, print_blob=True) | ||
| settings.register_profile('nightly', deadline=None, max_examples=10_000, print_blob=True) | ||
|
|
||
| settings.load_profile(os.environ.get('HYPOTHESIS_PROFILE', 'dev')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # Copyright 2026 Canonical Ltd. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Hypothesis property-based tests for scenario.state. | ||
|
|
||
| These test the _EntityStatus and Port conversions between scenario and ops. | ||
| Both are pure in-memory round-trips with a wide input space and a simple | ||
| invariant, which is where property-based testing earns its keep. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from hypothesis import given | ||
| from hypothesis import strategies as st | ||
| from scenario.state import ( | ||
| ActiveStatus, | ||
| BlockedStatus, | ||
| ErrorStatus, | ||
| ICMPPort, | ||
| MaintenanceStatus, | ||
| TCPPort, | ||
| UDPPort, | ||
| UnknownStatus, | ||
| WaitingStatus, | ||
| _EntityStatus, | ||
| ) | ||
|
|
||
| # ── strategies ────────────────────────────────────────────────────────────── | ||
| # | ||
| # Strategy design is where Hypothesis pays off or doesn't. | ||
| # The choices below are deliberately broad so the tool can discover input | ||
| # combinations the authors wouldn't reach for by hand. | ||
|
|
||
| # Every status type. UnknownStatus takes no message, so it is the one fixed value. | ||
| _status_strategy = st.one_of( | ||
| st.builds(ActiveStatus, message=st.text()), | ||
| st.builds(BlockedStatus, message=st.text()), | ||
| st.builds(MaintenanceStatus, message=st.text()), | ||
| st.builds(WaitingStatus, message=st.text()), | ||
| st.builds(ErrorStatus, message=st.text()), | ||
| st.just(UnknownStatus()), | ||
| ) | ||
|
|
||
| # All three concrete Port types; TCPPort/UDPPort accept validated port numbers. | ||
| _port_strategy = st.one_of( | ||
| st.integers(min_value=1, max_value=65535).map(TCPPort), | ||
| st.integers(min_value=1, max_value=65535).map(UDPPort), | ||
| st.just(ICMPPort()), | ||
| ) | ||
|
|
||
|
|
||
| # ── tests ──────────────────────────────────────────────────────────────────── | ||
|
|
||
|
|
||
| @given(status=_status_strategy) | ||
| def test_entity_status_ops_roundtrip(status: _EntityStatus) -> None: | ||
| """_EntityStatus → ops.StatusBase → _EntityStatus preserves name and message. | ||
|
|
||
| This bridges the scenario and ops status representations; any mismatch in | ||
| the name/message mapping would silently corrupt the visible status after a | ||
| charm event. | ||
| """ | ||
| round_tripped = _EntityStatus.from_ops(status._to_ops()) | ||
| assert round_tripped == status | ||
| assert round_tripped.name == status.name | ||
| assert round_tripped.message == status.message | ||
|
|
||
|
|
||
| @given(port=_port_strategy) | ||
| def test_port_to_ops_preserves_attributes(port: TCPPort | UDPPort | ICMPPort) -> None: | ||
| """Port._to_ops() faithfully copies port number and protocol to ops.Port. | ||
|
|
||
| If the mapping were to swap protocol strings or clamp the port number, ops | ||
| would open the wrong port at runtime. | ||
| """ | ||
| ops_port = port._to_ops() | ||
| assert ops_port.port == port.port | ||
| assert ops_port.protocol == port.protocol | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.