Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Add integration test for WorkflowRuntimeClient.async_request() response format
Add unit tests for WorkflowRuntimeServer.receiver()
  • Loading branch information
MetRonnie committed Aug 10, 2022
1 parent d7f5a0a commit 8270f3b
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cylc/flow/network/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def receiver(
err=ResponseErrTuple(str(exc), traceback.format_exc())
)

return ResponseTuple(content=response)
return ResponseTuple(content=response, user=user)

def register_endpoints(self):
"""Register all exposed methods."""
Expand Down
93 changes: 93 additions & 0 deletions tests/integration/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Test cylc.flow.client.WorkflowRuntimeClient."""

import json
import pytest
from typing import Optional, Union
from unittest.mock import Mock

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


def async_return(value):
"""Return an awaitable that returns ``value``."""
async def _async_return():
return value
return _async_return


@pytest.fixture(scope='module')
async def harness(mod_flow, mod_scheduler, mod_run, mod_one_conf):
reg = mod_flow(mod_one_conf)
Expand Down Expand Up @@ -50,3 +62,84 @@ async def test_protobuf(harness):
pb_data = PB_METHOD_MAP['pb_entire_workflow']()
pb_data.ParseFromString(ret)
assert schd.workflow in pb_data.workflow.id


@pytest.mark.asyncio
@pytest.mark.parametrize(
'cmd, timeout, mock_response, expected',
[
pytest.param(
'graphql',
False,
b'[{"ringbearer": "frodo"}, null, null]',
{"ringbearer": "frodo"},
id="Normal graphql"
),
pytest.param(
'pb_entire_workflow',
False,
b'mock protobuf response',
b'mock protobuf response',
id="Normal PB"
),
pytest.param(
'graphql',
True,
None,
ClientTimeout("blah"),
id="Timeout"
),
pytest.param(
'graphql',
False,
b'[null, ["mock error msg", "mock traceback"], null]',
ClientError("mock error msg", "mock traceback"),
id="Client error"
),
pytest.param(
'graphql',
False,
b'[null, null, null]',
ClientError("No response from server. Check the workflow log."),
id="Empty response"
),
]
)
async def test_async_request(
cmd: str,
timeout: bool,
mock_response: Optional[bytes],
expected: Union[bytes, object, Exception],
harness,
monkeypatch: pytest.MonkeyPatch
):
"""Test structure of date sent/received by
WorkflowRuntimeClient.async_request()
Params:
cmd: Network command to be tested.
timeout: Whether to simulate a client timeout.
mock_response: Simulated response from the server.
expected: Expected return value, or an Exception expected to be raised.
"""
client: WorkflowRuntimeClient
_, client = harness
mock_socket = Mock()
mock_socket.recv.side_effect = async_return(mock_response)
monkeypatch.setattr(client, 'socket', mock_socket)
mock_poller = Mock()
mock_poller.poll.return_value = not timeout
monkeypatch.setattr(client, 'poller', mock_poller)

args = {} # type: ignore[var-annotated]
expected_msg = {'command': cmd, 'args': args, **client.header}

call = client.async_request(cmd, args)
if isinstance(expected, Exception):
with pytest.raises(type(expected)) as ei:
await call
if isinstance(expected, ClientError):
assert ei.value.args == expected.args
else:
assert await call == expected
client.socket.send_string.assert_called_with(json.dumps(expected_msg))
3 changes: 1 addition & 2 deletions tests/integration/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import pytest

from cylc.flow.network.server import PB_METHOD_MAP

from cylc.flow.scheduler import Scheduler


Expand Down Expand Up @@ -114,7 +113,7 @@ async def test_receiver(one: Scheduler, start):
async with timeout(5):
async with start(one):
# start with a message that works
msg = {'command': 'api', 'args': {}}
msg: dict = {'command': 'api', 'args': {}}
response = one.server.receiver(msg, user)
assert response.err is None
assert response.content is not None
Expand Down
98 changes: 98 additions & 0 deletions tests/unit/network/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from typing import Any, NoReturn
from unittest.mock import Mock

import pytest

from cylc.flow.network.server import WorkflowRuntimeServer


class CustomError(Exception):
...


def _raise_exc(*a, **k) -> NoReturn:
raise CustomError("Mock error")


@pytest.mark.parametrize(
'message, expected_method_args, expected_content, expected_err_msg',
[
pytest.param(
{'command': 'some_method', 'args': {'foo': 1}, 'meta': 42},
{'foo': 1, 'user': 'darmok', 'meta': 42},
"Ok",
None,
id="Normal"
),
pytest.param(
{'command': 'non_exist', 'args': {}},
None,
None,
"No method by the name",
id="Method doesn't exist"
),
pytest.param(
{'args': {}},
None,
None,
"Request missing required field",
id="Missing command field"
),
pytest.param(
{'command': 'some_method'},
None,
None,
"Request missing required field",
id="Missing args field"
),
pytest.param(
{'command': 'raise_exc', 'args': {}},
None,
None,
"Mock error",
id="Exception in command method"
),
]
)
def test_receiver(
message: dict,
expected_method_args: dict,
expected_content: Any,
expected_err_msg: str
):
"""Test receiver."""
user = 'darmok'
_some_method = Mock(return_value="Ok")
server = WorkflowRuntimeServer(Mock())
server.some_method = _some_method # type: ignore[attr-defined]
server.raise_exc = _raise_exc # type: ignore[attr-defined]

result = server.receiver(message, user)
if expected_method_args is not None:
_some_method.assert_called_with(**expected_method_args)
# Zeroth element of response tuple should be content
assert result[0] == expected_content
# Next should be error
if expected_err_msg:
assert isinstance(result[1], tuple)
assert expected_err_msg in result[1][0]
else:
assert result[1] is None
# Last is user
assert result[2] == user

0 comments on commit 8270f3b

Please sign in to comment.