Skip to content

Commit

Permalink
Merge pull request cylc#89 from wxtim/fix.platform_optional_output
Browse files Browse the repository at this point in the history
changed the output typing of platform utility functions;
  • Loading branch information
MetRonnie authored Oct 29, 2021
2 parents 8577a4b + 3fd6304 commit d7f5f06
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
16 changes: 11 additions & 5 deletions cylc/rose/platform_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
"""
from optparse import Values
from pathlib import Path
from typing import Optional, Dict, Any
from typing import Dict, Any

from cylc.flow.config import WorkflowConfig
from cylc.flow.exceptions import PlatformLookupError
from cylc.flow.rundb import CylcWorkflowDAO
from cylc.flow.workflow_files import parse_reg
from cylc.flow.platforms import get_platform


def get_platform_from_task_def(
flow: str, task: str
) -> Optional[Dict[str, Any]]:
) -> Dict[str, Any]:
"""Return the platform dictionary for a particular task.
Uses the flow definition - designed to be used with tasks
Expand All @@ -42,17 +43,22 @@ def get_platform_from_task_def(
Returns:
Platform Dictionary.
"""
flow_name, flow_file = parse_reg(flow, src=True)
_, flow_file = parse_reg(flow, src=True)
config = WorkflowConfig(flow, flow_file, Values())
# Get entire task spec to allow Cylc 7 platform from host guessing.
task_spec = config.pcfg.get(['runtime', task])
platform = get_platform(task_spec)
if platform is None:
raise PlatformLookupError(
'Platform lookup failed; platform is a subshell to be evaluated: '
f' Task: {task}, platform: {task_spec["platform"]}.'
)
return platform


def get_platforms_from_task_jobs(
flow: str, cyclepoint: str
) -> Optional[Dict[str, Any]]:
) -> Dict[str, Any]:
"""Access flow database. Return platform for task at fixed cycle point
Uses the workflow database - designed to be used with tasks where jobs
Expand All @@ -66,7 +72,7 @@ def get_platforms_from_task_jobs(
Returns:
Platform Dictionary.
"""
flow_name, flow_file = parse_reg(flow, src=True)
_, flow_file = parse_reg(flow, src=True)
dbfilepath = Path(flow_file).parent / '.service/db'
dao = CylcWorkflowDAO(dbfilepath)
task_platform_map: Dict = {}
Expand Down
17 changes: 17 additions & 0 deletions tests/test_platform_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from shutil import rmtree
from subprocess import run
from uuid import uuid4
from cylc.flow.exceptions import PlatformLookupError

from cylc.rose.platform_utils import (
get_platform_from_task_def,
Expand Down Expand Up @@ -125,6 +126,8 @@ def fake_flow():
[[qux]]
[[[remote]]]
host = cheese
[[kanga]]
platform = $(echo "myplatform")
[[BAR]]
platform = milk
[[child_of_bar]]
Expand Down Expand Up @@ -180,6 +183,20 @@ def test_get_platform_from_task_def(
assert platform['name'] == expected_platform_n


def test_get_platform_from_task_def_raises(
mock_glbl_cfg, fake_flow
):
"""Test getting platform from task definition.
This is approaching an integration test, because
although it's only testing one unit of Cylc Rose, that unit
is calling lots of Cylc Parts, which aren't mocked.
"""
mock_glbl_cfg(*MOCK_GLBL_CFG)
with pytest.raises(PlatformLookupError, match='Platform lookup failed.*'):
get_platform_from_task_def(fake_flow[0], 'kanga')


@pytest.mark.parametrize(
'task, cycle, expect',
[
Expand Down

0 comments on commit d7f5f06

Please sign in to comment.