Skip to content

Commit

Permalink
Add unit test for a workflow using an expression for the resources
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Aug 18, 2022
1 parent 0cf7198 commit d89fd84
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/test_reqs_hints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Test for Requirements and Hints in cwltool."""
import json
from io import StringIO

from cwltool.main import main
from .util import get_data


def test_workflow_reqs_are_evaluated_eagerly_default_args() -> None:
"""Test that a Workflow process will evaluate the requirements eagerly.
Uses the default input values.
This means that workflow steps, such as Expression and Command Line Tools
can both use resources without re-evaluating expressions. This is useful
when you have an expression that, for instance, dynamically decides
how many threads/cpus to use.
Issue: https://github.com/common-workflow-language/cwltool/issues/1330
"""
stream = StringIO()

assert (
main(
[get_data("tests/wf/1330.cwl")],
stdout=stream,
)
== 0
)

out = json.loads(stream.getvalue())
assert out["out"] == "4\n"


def test_workflow_reqs_are_evaluated_eagerly_provided_inputs() -> None:
"""Test that a Workflow process will evaluate the requirements eagerly.
Passes inputs via a job file.
This means that workflow steps, such as Expression and Command Line Tools
can both use resources without re-evaluating expressions. This is useful
when you have an expression that, for instance, dynamically decides
how many threads/cpus to use.
Issue: https://github.com/common-workflow-language/cwltool/issues/1330
"""
stream = StringIO()

assert (
main(
[get_data("tests/wf/1330.cwl"), get_data("tests/wf/1330.json")],
stdout=stream,
)
== 0
)

out = json.loads(stream.getvalue())
assert out["out"] == "3\n"
39 changes: 39 additions & 0 deletions tests/wf/1330.cwl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Original file: tests/eager-eval-reqs-hints/wf-reqs.cwl
# From: https://github.com/common-workflow-language/cwl-v1.2/pull/195
cwlVersion: v1.2
class: Workflow

requirements:
ResourceRequirement:
coresMax: $(inputs.threads_max)

inputs:
threads_max:
type: int
default: 4

steps:
one:
in: []
run:
class: CommandLineTool
inputs:
other_input:
type: int
default: 8
baseCommand: echo
arguments: [ $(runtime.cores) ]
stdout: out.txt
outputs:
out:
type: string
outputBinding:
glob: out.txt
loadContents: true
outputEval: $(self[0].contents)
out: [out]

outputs:
out:
type: string
outputSource: one/out
3 changes: 3 additions & 0 deletions tests/wf/1330.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"threads_max": 3
}

0 comments on commit d89fd84

Please sign in to comment.