| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
"""Test cylc.flow.client.WorkflowRuntimeClient.""" |
| 18 |
import json |
| 19 |
from unittest.mock import Mock |
| 20 |
import pytest |
| 21 |
|
| 22 |
from cylc.flow.exceptions import ClientError |
| 23 |
from cylc.flow.network.client import WorkflowRuntimeClient |
| 24 |
from cylc.flow.network.server import PB_METHOD_MAP |
| 25 |
|
| 26 |
|
| 27 |
@pytest.fixture(scope='module') |
| 28 |
async def harness(mod_flow, mod_scheduler, mod_run, mod_one_conf): |
| 29 |
id_ = mod_flow(mod_one_conf) |
| 30 |
schd = mod_scheduler(id_) |
| 31 |
async with mod_run(schd): |
| 32 |
client = WorkflowRuntimeClient(id_) |
| 33 |
yield schd, client |
| 34 |
|
| 35 |
|
| 36 |
async def test_graphql(harness): |
| 37 |
"""It should return True if running.""" |
| 38 |
schd, client = harness |
| 39 |
ret = await client.async_request( |
| 40 |
'graphql', |
| 41 |
{'request_string': 'query { workflows { id } }'} |
| 42 |
) |
| 43 |
workflows = ret['workflows'] |
| 44 |
assert len(workflows) == 1 |
| 45 |
workflow = workflows[0] |
| 46 |
assert schd.workflow in workflow['id'] |
| 47 |
|
| 48 |
|
| 49 |
async def test_protobuf(harness): |
| 50 |
"""It should return True if running.""" |
| 51 |
schd, client = harness |
| 52 |
ret = await client.async_request('pb_entire_workflow') |
| 53 |
pb_data = PB_METHOD_MAP['pb_entire_workflow']() |
| 54 |
pb_data.ParseFromString(ret) |
| 55 |
assert schd.workflow in pb_data.workflow.id |
| 56 |
|
| 57 |
|
| 58 |
async def test_command_validation_failure(harness): |
| 59 |
"""It should send the correct response if a command fails validation. |
| 60 |
|
| 61 |
Command arguments are validated before the command is queued. Any issues at |
| 62 |
this stage will be communicated back via the mutation "result". |
| 63 |
|
| 64 |
See https://github.com/cylc/cylc-flow/pull/6112 |
| 65 |
""" |
| 66 |
schd, client = harness |
| 67 |
|
| 68 |
# run a mutation that will fail validation |
| 69 |
response = await client.async_request( |
| 70 |
'graphql', |
| 71 |
{ |
| 72 |
'request_string': ''' |
| 73 |
mutation { |
| 74 |
set( |
| 75 |
workflows: ["*"], |
| 76 |
tasks: ["*"], |
| 77 |
# this list of prerequisites fails validation: |
| 78 |
prerequisites: ["1/a", "all"] |
| 79 |
) { |
| 80 |
result |
| 81 |
} |
| 82 |
} |
| 83 |
''' |
| 84 |
}, |
| 85 |
) |
| 86 |
|
| 87 |
# the validation error should be returned to the client |
| 88 |
assert response['set']['result'] == [ |
| 89 |
{ |
| 90 |
'id': schd.id, |
| 91 |
'response': [False, '--pre=all must be used alone'], |
| 92 |
} |
| 93 |
] |
| 94 |
|
| 95 |
|
| 96 |
@pytest.mark.parametrize( |
| 97 |
'sock_response, expected', |
| 98 |
[ |
| 99 |
pytest.param({'error': 'message'}, r"^message$", id="basic"), |
| 100 |
pytest.param( |
| 101 |
{'foo': 1}, |
| 102 |
r"^Received invalid response for" |
| 103 |
r" Cylc 8\.[\w.]+: \{'foo': 1[^}]*\}$", |
| 104 |
id="no-err-field", |
| 105 |
), |
| 106 |
pytest.param( |
| 107 |
{'cylc_version': '8.x.y'}, |
| 108 |
r"^Received invalid.+\n\(Workflow is running in Cylc 8.x.y\)$", |
| 109 |
id="no-err-field-with-version", |
| 110 |
), |
| 111 |
], |
| 112 |
) |
| 113 |
async def test_async_request_err( |
| 114 |
one, start, monkeypatch: pytest.MonkeyPatch, sock_response, expected |
| 115 |
): |
| 116 |
async def mock_recv(): |
| 117 |
return json.dumps(sock_response).encode() |
| 118 |
|
| 119 |
async with start(one): |
| 120 |
client = WorkflowRuntimeClient(one.workflow) |
| 121 |
with monkeypatch.context() as mp: |
| 122 |
mp.setattr(client, 'socket', Mock(recv=mock_recv)) |
| 123 |
mp.setattr(client, 'poller', Mock()) |
| 124 |
|
| 125 |
with pytest.raises(ClientError, match=expected): |
| 126 |
await client.async_request('graphql') |