| 1 |
# Functional Tests |
| 2 |
|
| 3 |
This directory contains Cylc functional tests. |
| 4 |
|
| 5 |
|
| 6 |
## How To Run These Tests |
| 7 |
|
| 8 |
```console |
| 9 |
# run all tests in this directory |
| 10 |
$ etc/bin/run-functional-tests tests/f |
| 11 |
|
| 12 |
# run a specified test |
| 13 |
$ etc/bin/run-functional-tests tests/f/cylc-cat-log/00-local.t |
| 14 |
|
| 15 |
# run a specified test in debug mode |
| 16 |
$ etc/bin/run-functional-tests -v tests/f/cylc-cat-log/00-local.t |
| 17 |
|
| 18 |
# run tests with 5x parallelism |
| 19 |
$ etc/bin/run-functional-tests -j 5 tests/f |
| 20 |
|
| 21 |
# split the tests into 4 "chunks" and run the first chunk |
| 22 |
$ CHUNK='1/4' etc/bin/run-functional-tests tests/f |
| 23 |
|
| 24 |
# measure code coverage |
| 25 |
# (coverage files are not automatically transferred from remote platforms) |
| 26 |
$ export CYLC_COVERAGE=1 |
| 27 |
$ etc/bin/run-functional-tests tests/f |
| 28 |
$ coverage combine |
| 29 |
``` |
| 30 |
|
| 31 |
|
| 32 |
## What Are Functional Tests? |
| 33 |
|
| 34 |
These tests ensure end-to-end functionality is as expected. |
| 35 |
|
| 36 |
With Cylc this typically involves building workflow configurations which |
| 37 |
cause many parts of the system (and other systems) to be activated. |
| 38 |
|
| 39 |
This includes interaction with other systems (e.g. batch schedulers/job runners), |
| 40 |
command line interfaces / outputs, etc. |
| 41 |
|
| 42 |
|
| 43 |
## How Are Tests Implemented |
| 44 |
|
| 45 |
Tests are written in files with the `.t` extension. These are Bash scripts |
| 46 |
(despite the file extension). The tests are run with a tool called `prove` |
| 47 |
which is invoked via the `etc/bin/run-functional-tests` command. |
| 48 |
|
| 49 |
Each test file starts by sourcing the file `lib/bash/test_header`, this |
| 50 |
contains various Bash functions which provide assertion functions, e.g. |
| 51 |
`run_ok` (which asserts that the provided command succeeds). |
| 52 |
|
| 53 |
Assertions run a command, then write either `ok` or `not ok` to stdout, prove |
| 54 |
then scrapes this output to determine test outcome. |
| 55 |
|
| 56 |
Each assertion (or subtest) needs a name, this is usually the first argument |
| 57 |
to the function. You should use the prefix `$TEST_NAME_BASE`, e.g, this |
| 58 |
assertion tests that the command `sleep 10` succeeds |
| 59 |
`run_ok "${TEST_NAME_BASE}-sleep" sleep 10`. Note, some assertions infer the |
| 60 |
test name for you from the arguments. |
| 61 |
|
| 62 |
For the list of available assertions, and usage info, see the comments at the |
| 63 |
top of `lib/bash/test_header`. |
| 64 |
|
| 65 |
Each test file needs to declare the number of subtests within it, this is |
| 66 |
done with the function `set_test_number` which must be run at the start of the |
| 67 |
test. |
| 68 |
|
| 69 |
Many tests install workflows for testing. These are installed into cylc-run as |
| 70 |
usual under the prefix `cylctb`. |
| 71 |
|
| 72 |
|
| 73 |
## recording tests that fail |
| 74 |
|
| 75 |
When running many tests you can record the tests that fail using |
| 76 |
`--state=save`. You can then re-run the only the failed tests using |
| 77 |
`--state=failed`. |
| 78 |
|
| 79 |
For more details see `man prove`. |
| 80 |
|
| 81 |
|
| 82 |
## Debugging |
| 83 |
|
| 84 |
When a test fails, it may print some directories to the screen which contain |
| 85 |
the stderr of the failed tests, or the workflow logs. |
| 86 |
|
| 87 |
Tips: |
| 88 |
|
| 89 |
* Run the test using the `-v` argument, this will reveal which subtest(s) |
| 90 |
failed. |
| 91 |
* Some tests reveal extra debug info when you set `CYLC_TEST_DEBUG=true`. |
| 92 |
* If the test fails, any workflows installed into the `~/cylc-run/cylctb???` |
| 93 |
directory will not be deleted, you can inspect the workflow logs there. |
| 94 |
* In GitHub actions, the `~/cylc-run` directory is uploaded as an "artifact" |
| 95 |
where it can be downloaded for debugging. |
| 96 |
* Any text that gets written to stdout will be swallowed by the test framework, |
| 97 |
redirect to stderr in order to use `echo` statements for debugging, e.g, |
| 98 |
`echo "DEBUG: platform=$CYLC_PLATFORM_NAME" >&2`. |
| 99 |
|
| 100 |
|
| 101 |
### Python Debuggers |
| 102 |
|
| 103 |
"Normal" Python debuggers (e.g, `pdb`) will not work from within the functional |
| 104 |
test framework because the Python code maybe run non-interactively. |
| 105 |
|
| 106 |
However, you can use remote debuggers |
| 107 |
(e.g. [`remote_pdb`](https://pypi.org/project/remote-pdb/)). |
| 108 |
|
| 109 |
Remote PDB is probably the simplest remote debugger, here's a simple example |
| 110 |
to get started: |
| 111 |
|
| 112 |
Open two terminal tabs and run this code in both of them: |
| 113 |
|
| 114 |
```shell |
| 115 |
PYTHONBREAKPOINT=remote_pdb.set_trace |
| 116 |
REMOTE_PDB_HOST=0.0.0.0 |
| 117 |
REMOTE_PDB_PORT=${-4444} |
| 118 |
export PYTHONBREAKPOINT REMOTE_PDB_HOST REMOTE_PDB_PORT |
| 119 |
``` |
| 120 |
|
| 121 |
Run the test in the first tab, then run this command in the second: |
| 122 |
|
| 123 |
```shell |
| 124 |
nc -C "$REMOTE_PDB_HOST" "$REMOTE_PDB_PORT" |
| 125 |
``` |
| 126 |
|
| 127 |
|
| 128 |
### Traps for the unwary |
| 129 |
|
| 130 |
#### `grep_ok` vs `comp_ok` |
| 131 |
|
| 132 |
Tests that use `comp_ok` generally compare `${TEST_NAME}.stdout` or |
| 133 |
`${TEST_NAME}.stderr` against either a reference or against `/dev/null`. |
| 134 |
They expect the entire output to be **exactly** the same as the |
| 135 |
reference and are therefore very unforgiving. |
| 136 |
|
| 137 |
`grep_ok` is much less sensitive only requiring the reference output to |
| 138 |
be present **somewhere** in the test output. |
| 139 |
|
| 140 |
|
| 141 |
### Further Reading |
| 142 |
|
| 143 |
#### Heredocs |
| 144 |
|
| 145 |
If you see code that looks like this: |
| 146 |
|
| 147 |
```bash |
| 148 |
cat >'hello.py' <<'__HELLO_PY__' |
| 149 |
print("Hello World") |
| 150 |
__HELLO_PY__ |
| 151 |
``` |
| 152 |
|
| 153 |
You are looking at an "heredoc" and you may wish to read about heredocs: |
| 154 |
[A modern looking bloggy guide](https://linuxize.com/post/bash-heredoc/) |
| 155 |
[A web 1.0 manual](http://tldp.org/LDP/abs/html/here-docs.html) |
| 156 |
|
| 157 |
|
| 158 |
## How To Run "Non-Generic" Tests? |
| 159 |
|
| 160 |
Some tests require job runners (e.g. at, slurm, pbs), remote platforms or |
| 161 |
specific configurations. |
| 162 |
|
| 163 |
To run these tests you must configure remote platforms, your options are: |
| 164 |
|
| 165 |
1. Use a swarm of docker containers |
| 166 |
|
| 167 |
(currently does not cover all platform types) |
| 168 |
|
| 169 |
$ etc/bin/swarm configure |
| 170 |
$ etc/bin/swarm build |
| 171 |
$ etc/bin/swarm run |
| 172 |
|
| 173 |
2. Configure your own remote platforms in your Cylc config. |
| 174 |
|
| 175 |
See the next section for the platform matrix and naming conventions. |
| 176 |
|
| 177 |
Once you have defined your remote platforms provide them with the `-p` arg: |
| 178 |
|
| 179 |
```console |
| 180 |
# run ONLY tests compatible with the _remote_background_indep_tcp platform |
| 181 |
$ etc/bin/run-functional-tests -p _remote_background_indep_tcp tests/f |
| 182 |
|
| 183 |
# run tests on the first compatible platform with the "at" job runner |
| 184 |
$ etc/bin/run-functional-tests -p '_*at*' tests/f |
| 185 |
|
| 186 |
# run tests on the first compatible platform configured |
| 187 |
$ etc/bin/run-functional-tests -p '*' tests/f |
| 188 |
``` |
| 189 |
|
| 190 |
|
| 191 |
## Test Platform Names |
| 192 |
|
| 193 |
Each platform is named using this convention: |
| 194 |
|
| 195 |
_<submission_locality>_<job_runner>_<filesystem>_<comms_method> |
| 196 |
|
| 197 |
`loc` "submission locality" - `{local, remote}`: |
| 198 |
Where do jobs get submitted? |
| 199 |
|
| 200 |
* Locally (on the scheduler host). |
| 201 |
* Remotely (on a remote host). |
| 202 |
|
| 203 |
`runner` "job runner" - `{background, at, slurm, ...}`: |
| 204 |
The name of the job runner the container is configured to use. |
| 205 |
|
| 206 |
`fs` "filesystem" - `{indep, shared}`: |
| 207 |
What is the relationship between the filesystem in the container to |
| 208 |
the filesystem on the host? |
| 209 |
|
| 210 |
* Independent (indep) - it is a completely different filesystem. |
| 211 |
* Shared (shared) - the cylc-run directory is shared. |
| 212 |
|
| 213 |
warning: For shared filesystems cylc-run is shared, however, the |
| 214 |
absolute path to cylc-run on the host system may be different |
| 215 |
to that on the container, use ~/cylc-run for safety. |
| 216 |
|
| 217 |
`comms` "task communication method" - `{tcp, ssh, poll}` |
| 218 |
The task communication method to use. |
| 219 |
|
| 220 |
Define any test platforms in your global config e.g: |
| 221 |
|
| 222 |
``` |
| 223 |
# ~/.cylc/global.cylc |
| 224 |
[platforms] |
| 225 |
[[_remote_background_indep_tcp]] |
| 226 |
hosts = my_remote_host |
| 227 |
``` |
| 228 |
|
| 229 |
|
| 230 |
## Test Global Config |
| 231 |
|
| 232 |
Cylc supports a `global-tests.cylc` file which can be used to define some |
| 233 |
top-level configurations to run tests with. |
| 234 |
|
| 235 |
Do not use this file to define test platforms, put them in your regular global |
| 236 |
config where they can also be used for interactive work. |
| 237 |
|
| 238 |
|
| 239 |
## How To Configure "Non-Generic" Tests? |
| 240 |
|
| 241 |
By default tests require the platform `_local_background_indep_tcp`. |
| 242 |
|
| 243 |
If you want your test to run on any other platform export an environment |
| 244 |
variable called `REQUIRE_PLATFORM` *before* the `test_header` is sourced. |
| 245 |
|
| 246 |
This variable should define a test's requirements of a platform e.g: |
| 247 |
|
| 248 |
```bash |
| 249 |
# require a remote platform with a shared filesystem and tcp task comms |
| 250 |
export REQUIRE_PLATFORM='loc:remote fs:shared comms:tcp' |
| 251 |
``` |
| 252 |
|
| 253 |
Bash extglob pattern matching can be provided |
| 254 |
(https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html) |
| 255 |
e.g: |
| 256 |
|
| 257 |
```bash |
| 258 |
# run for all job runners with tcp comms on remote platforms |
| 259 |
export REQUIRE_PLATFORM='loc:remote runner:* comms:tcp' |
| 260 |
# run for tcp and ssh comms on remote platform |
| 261 |
export REQUIRE_PLATFORM='loc:remote comms:?(tcp|ssh)' |
| 262 |
``` |
| 263 |
|
| 264 |
If a field is not provided it defaults to `*` with the exception of |
| 265 |
`loc` which defaults to `local` i.e: |
| 266 |
|
| 267 |
```bash |
| 268 |
# run for all job runners on local platforms |
| 269 |
export REQUIRE_PLATFORM='runner:*' |
| 270 |
# run for all job runners on remote platforms |
| 271 |
export REQUIRE_PLATFORM='loc:remote runner:*' |
| 272 |
``` |
| 273 |
|
| 274 |
|
| 275 |
## Guidelines |
| 276 |
|
| 277 |
Don't write functional tests when you can write integration tests: |
| 278 |
|
| 279 |
* If a test requires a workflow to be put in an "exotic" state, consider if |
| 280 |
this can be achieved artificially. |
| 281 |
* If a test can be broken down into smaller more targeted sub-tests then |
| 282 |
integration testing might be more appropriate. |
| 283 |
|