Skip to content

Commit

Permalink
Log failure where attempting to use Cylc 8 on installed Cylc 7 workflow.
Browse files Browse the repository at this point in the history
Tests for cylc.flow.network

response to review
  • Loading branch information
wxtim committed Jul 21, 2021
1 parent 7346279 commit 1008c28
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cylc/flow/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,18 @@ def __str__(self):
for key, value in data.items():
ret += f'\n {key}: {value}'
return ret


class CylcVersionError(CylcError):
"""Contact file is for a Cylc Version not supported by this script."""
def __init__(self, version=None):
self.version = version

def __str__(self):
if self.version is not None:
return (
f'Installed Cylc {self.version} workflow is not '
'compatible with Cylc 8.'
)
else:
return "Installed workflow is not compatible with Cylc 8."
9 changes: 8 additions & 1 deletion cylc/flow/network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from cylc.flow.exceptions import (
ClientError,
CylcError,
CylcVersionError,
ServiceFileError,
WorkflowStopped
)
Expand Down Expand Up @@ -71,6 +72,7 @@ def get_location(workflow: str):
Tuple[str, int, int]: tuple with the host name and port numbers.
Raises:
ClientError: if the workflow is not running.
CylcVersionError: if target is a Cylc 7 (or earlier) workflow.
"""
try:
contact = load_contact_file(workflow)
Expand All @@ -80,7 +82,12 @@ def get_location(workflow: str):
host = contact[ContactFileFields.HOST]
host = get_fqdn_by_host(host)
port = int(contact[ContactFileFields.PORT])
pub_port = int(contact[ContactFileFields.PUBLISH_PORT])
if ContactFileFields.PUBLISH_PORT in contact:
pub_port = int(contact[ContactFileFields.PUBLISH_PORT])
else:
version = (
contact['CYLC_VERSION'] if 'CYLC_VERSION' in contact else None)
raise CylcVersionError(version=version)
return host, port, pub_port


Expand Down
61 changes: 61 additions & 0 deletions tests/unit/network/test__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 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/>.
"""Test __init__.py for network interfaces to Cylc scheduler objects."""

import pytest

import cylc
from cylc.flow.exceptions import CylcVersionError
from cylc.flow.network import get_location
from cylc.flow.workflow_files import load_contact_file, ContactFileFields


BASE_CONTACT_DATA = {
ContactFileFields.HOST: 'foo',
ContactFileFields.PORT: '42',
}


@pytest.fixture()
def mpatch_get_fqdn_by_host(monkeypatch):
"""Monkeypatch function used the same by all tests."""
monkeypatch.setattr(
cylc.flow.network, 'get_fqdn_by_host', lambda _ : 'myhost.x.y.z'
)


def test_get_location_ok(monkeypatch, mpatch_get_fqdn_by_host):
"""It passes when information is available."""
contact_data = BASE_CONTACT_DATA.copy()
contact_data[ContactFileFields.PUBLISH_PORT] = '8042'
monkeypatch.setattr(
cylc.flow.network, 'load_contact_file', lambda _ : contact_data
)
assert get_location('_') == (
'myhost.x.y.z', 42, 8042
)


def test_get_location_old_contact_file(monkeypatch, mpatch_get_fqdn_by_host):
"""It Fails because it's not a Cylc 8 workflow."""
contact_data = BASE_CONTACT_DATA.copy()
contact_data['CYLC_SUITE_PUBLISH_PORT'] = '8042'
contact_data['CYLC_VERSION'] = '5.1.2'
monkeypatch.setattr(
cylc.flow.network, 'load_contact_file', lambda _ : contact_data
)
with pytest.raises(CylcVersionError, match=r'.*5.1.2.*') as exc:
get_location('_')

0 comments on commit 1008c28

Please sign in to comment.