-
Couldn't load subscription status.
- Fork 293
Post-Python3-fixup for xcp-rrdd/scripts/rrdd.py: Fix crash on socket.error on connect() to xcp-rrdd
#5418
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
Merged
psafont
merged 2 commits into
master
from
fix-print-statement-in-rrdd.API.wait_until_next_reading
Feb 7, 2024
Merged
Post-Python3-fixup for xcp-rrdd/scripts/rrdd.py: Fix crash on socket.error on connect() to xcp-rrdd
#5418
Changes from all commits
Commits
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
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
79 changes: 79 additions & 0 deletions
79
ocaml/xcp-rrdd/scripts/rrdd/test_api_wait_until_next_reading.py
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,79 @@ | ||
| # Test: pytest -v -s ocaml/xcp-rrdd/scripts/rrdd/test_api_wait_until_next_reading.py | ||
| """Parametrized test exercising all conditions in rrdd.API.wait_until_next_reading()""" | ||
| import socket | ||
| from warnings import catch_warnings as import_without_warnings, simplefilter | ||
|
|
||
| # Dependencies: | ||
| # pip install pytest-mock | ||
| import pytest | ||
|
|
||
| # Handle DeprecationWarning from importing imp (it was removed with Python 3.12) | ||
| with import_without_warnings(): | ||
| simplefilter(action="ignore", category=DeprecationWarning) | ||
| import rrdd | ||
|
|
||
|
|
||
| # pylint:disable=no-member,redefined-outer-name # pytest fixture, see below | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def api(mocker): | ||
| """Pytest fixture for creating a rrdd.API() instance""" | ||
| instance = rrdd.API("plugin_id") | ||
| instance.deregister = mocker.Mock() | ||
| return instance | ||
|
|
||
|
|
||
| # pylint:disable=too-many-arguments # pytest parametrized test, see below | ||
| @pytest.mark.parametrize( | ||
| "neg_shift, interval, reading, sleep", | ||
| [ | ||
| # Happy path tests with various realistic test values | ||
| (None, 5, (6,), 5), # Test the default value of neg_shift | ||
| (1, 5, (6,), 5), # to call in the same sleep as neg_shift=1 | ||
| (2.25, 5, (6,), 3.75), # Test neg_shift as float to get sleep as float | ||
| (0.5, 30, (30.5,), 30), # Also as a fraction of a second | ||
| (2, 120, (122,), 120), # Test large interval and reading | ||
| # Edge cases | ||
| (11, 5, (1,), 0), # large neg_shift results in no sleep | ||
| (1, 10, (1,), 0), # neg_shift equals reading from xcp-rrdd | ||
| (1, 9, (10,), 9), # wait_time is exactly one cycle | ||
| (1, 10, (9,), 8), # wait_time is negative, should wrap around | ||
| # Error case | ||
| (1, 7, (socket.error, 6), 5), # first register raises socket.error | ||
| ], | ||
| ) | ||
| def test_params(api, mocker, neg_shift, interval, reading, sleep, capsys): | ||
| """Test that wait_until_reading_from_xcp_rrd() works with various test values""" | ||
| # Arrange | ||
| api.frequency_in_seconds = interval | ||
| api.lazy_complete_init = mocker.Mock() | ||
| api.register = mocker.Mock(side_effect=reading) | ||
| api.deregister = mocker.Mock() | ||
|
|
||
| # Act | ||
| mock_sleep = mocker.patch("time.sleep") | ||
| if neg_shift is None: | ||
| rrdd.API.wait_until_next_reading(api) | ||
| else: | ||
| rrdd.API.wait_until_next_reading(api, neg_shift) | ||
|
|
||
| # Assert | ||
| mock_sleep.assert_called_with(sleep) | ||
|
|
||
| with capsys.disabled(): | ||
| stderr = capsys.readouterr().err | ||
| stdout = capsys.readouterr().out | ||
| if reading[0] is socket.error: | ||
| assert stderr == "Failed to contact xcp-rrdd. Sleeping for 5 seconds ..\n" | ||
| else: | ||
| assert stderr == "" | ||
| assert stdout == "" | ||
|
|
||
|
|
||
| def test_api_getter_functions(api): | ||
| """Test that the API getter functions work (and cover the code)""" | ||
| api.header = "header" | ||
| api.path = "path" | ||
| assert api.get_header() == "header" | ||
| assert api.get_path() == "path" |
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.