Skip to content

Commit

Permalink
HMMM
Browse files Browse the repository at this point in the history
  • Loading branch information
MetRonnie committed Jan 28, 2025
1 parent ee2cccc commit a258aa2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 20 deletions.
36 changes: 36 additions & 0 deletions tests/integration/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Test cylc.flow.client.WorkflowRuntimeClient."""
from contextlib import suppress
import json
from unittest.mock import Mock
import pytest

from cylc.flow.exceptions import ClientError
from cylc.flow.network.client import WorkflowRuntimeClient
from cylc.flow.network.server import PB_METHOD_MAP

Expand Down Expand Up @@ -88,3 +92,35 @@ async def test_command_validation_failure(harness):
'response': [False, '--pre=all must be used alone'],
}
]


@pytest.mark.parametrize(
'sock_response, expected',
[
pytest.param({'error': 'message'}, r"^message$", id="basic"),
pytest.param(
{'foo': 1},
r"^Received invalid response for Cylc 8\.[\w.]+: \{'foo': 1[^}]*\}$",
id="no-err-field",
),
pytest.param(
{'cylc_version': '8.x.y'},
r"^Received invalid.+\n\(Workflow is running in Cylc 8.x.y\)$",
id="no-err-field-with-version",
),
],
)
async def test_async_request_err(
harness, monkeypatch: pytest.MonkeyPatch, sock_response, expected
):
async def mock_recv():
return json.dumps(sock_response).encode()

client: WorkflowRuntimeClient
schd, client = harness
with monkeypatch.context() as mp:
mp.setattr(client, 'socket', Mock(recv=mock_recv))
mp.setattr(client.poller, 'poll', Mock())

with suppress(ClientError):
await client.async_request('graphql')
57 changes: 37 additions & 20 deletions tests/integration/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import logging
from typing import Callable
from async_timeout import timeout
from getpass import getuser

import pytest
from cylc.flow import __version__ as CYLC_VERSION

from cylc.flow.network.server import PB_METHOD_MAP
from cylc.flow.scheduler import Scheduler
Expand Down Expand Up @@ -89,35 +91,50 @@ async def test_stop(one: Scheduler, start):
assert one.server.stopped


async def test_receiver(one: Scheduler, start):
async def test_receiver_basic(one: Scheduler, start, log_filter):
"""Test the receiver with different message objects."""
async with timeout(5):
async with start(one):
# start with a message that works
msg = {'command': 'api', 'user': '', 'args': {}}
assert 'error' not in one.server.receiver(msg)
assert 'data' in one.server.receiver(msg)

# remove the user field - should error
msg2 = dict(msg)
msg2.pop('user')
assert 'error' in one.server.receiver(msg2)

# remove the command field - should error
msg3 = dict(msg)
msg3.pop('command')
assert 'error' in one.server.receiver(msg3)

# provide an invalid command - should error
msg4 = {**msg, 'command': 'foobar'}
assert 'error' in one.server.receiver(msg4)
msg = {'command': 'api', 'user': 'bono', 'args': {}}
res = one.server.receiver(msg)
x = res.get('error')
x = res['data']
x =res['cylc_version'] == CYLC_VERSION

# simulate a command failure with the original message
# (the one which worked earlier) - should error
def _api(*args, **kwargs):
raise Exception('foo')
raise Exception('oopsie')
one.server.api = _api
assert 'error' in one.server.receiver(msg)
res = one.server.receiver(msg)


@pytest.mark.parametrize(
'msg, expected',
[
pytest.param(
{'command': 'api', 'args': {}},
f"Request missing field 'user' required for Cylc {CYLC_VERSION}",
id='missing-user',
),
pytest.param(
{'user': 'bono', 'args': {}},
f"Request missing field 'command' required for Cylc {CYLC_VERSION}",
id='missing-command',
),
pytest.param(
{'command': 'foobar', 'user': 'bono', 'args': {}},
f"No method by the name 'foobar' at Cylc {CYLC_VERSION}",
id='bad-command',
),
],
)
async def test_receiver_bad_requests(one: Scheduler, start, msg, expected):
"""Test the receiver with different bad requests."""
async with timeout(5):
async with start(one):
res = one.server.receiver(msg)


async def test_publish_before_shutdown(
Expand Down

0 comments on commit a258aa2

Please sign in to comment.