|
| 1 | += CATS: Solr's shell tests |
| 2 | +:toc: left |
| 3 | +:toclevels: 2 |
| 4 | + |
| 5 | +CATS (Cross-platform Automated Testing System) is how Solr tests `bin/solr` and `bin\solr.cmd`. A test is a Java class that runs real `solr` commands against an unpacked distribution, through `bash` on Unix and `cmd.exe` on Windows. The same test covers both platforms, so script, quoting and packaging bugs show up on the OS where they actually happen. |
| 6 | + |
| 7 | +* Suites: `solr/packaging/src/test/org/apache/solr/shell/` |
| 8 | +* Base class: `SolrCatsTestCase` (in `solr/test-framework`, package `org.apache.solr.shell`) |
| 9 | +* Harness internals: package `org.apache.solr.cats`, same module |
| 10 | +
|
| 11 | +CATS replaces the BATS suites in `solr/packaging/test/`. Don't add new `.bats` files. |
| 12 | + |
| 13 | +== Running |
| 14 | + |
| 15 | +[source,bash] |
| 16 | +---- |
| 17 | +./gradlew -p solr/packaging catsTests # all suites (builds the distribution first) |
| 18 | +./gradlew -p solr/packaging catsTests --tests "*ZkTest" # one suite |
| 19 | +./gradlew -p solr/test-framework test --tests "org.apache.solr.cats.*" # the harness' own unit tests |
| 20 | +---- |
| 21 | + |
| 22 | +`catsTestsPicocli` and `catsTestsCommonsCli` run the suites against a specific CLI implementation. To run a suite from the IDE, build a distribution with `./gradlew -p solr/packaging dev` and point the test at it with `SOLR_TIP` or `-Dsolr.cli.solr.tip=<dir>`. |
| 23 | + |
| 24 | +CATS is not part of `check`. CI runs it on Linux and Windows for PRs that touch `solr/bin`, `solr/packaging` or the CLI (`.github/workflows/cats-test.yml`). |
| 25 | + |
| 26 | +== A first test |
| 27 | + |
| 28 | +[source,java] |
| 29 | +---- |
| 30 | +package org.apache.solr.shell; |
| 31 | +
|
| 32 | +import org.junit.BeforeClass; |
| 33 | +import org.junit.Test; |
| 34 | +
|
| 35 | +public class MyFeatureTest extends SolrCatsTestCase { |
| 36 | +
|
| 37 | + @BeforeClass |
| 38 | + public static void startSolr() { |
| 39 | + // Lives for the whole class and is stopped automatically. |
| 40 | + classCli.run("solr start"); |
| 41 | + } |
| 42 | +
|
| 43 | + @Test |
| 44 | + public void createsCollection() { |
| 45 | + run("solr create -c films") |
| 46 | + .assertContains("Created collection 'films'") |
| 47 | + .assertContains("assuming solr url is http://localhost:${SOLR_PORT}"); |
| 48 | + assertTrue(collectionExists("films")); |
| 49 | + } |
| 50 | +
|
| 51 | + @Test |
| 52 | + public void rejectsUnknownOption() { |
| 53 | + runFail("solr create -c films --no-such-option").assertContains("no-such-option"); |
| 54 | + } |
| 55 | +
|
| 56 | + @Test |
| 57 | + public void uploadsAConfigFile() { |
| 58 | + write("myfile.txt", "hello"); |
| 59 | + run("solr zk cp myfile.txt zk:/myfile.txt -z localhost:${ZK_PORT}"); |
| 60 | + run("solr zk ls / -z localhost:${ZK_PORT}").assertContains("myfile.txt"); |
| 61 | + } |
| 62 | +} |
| 63 | +---- |
| 64 | + |
| 65 | +Things to know: |
| 66 | + |
| 67 | +* `solr` resolves to `bin/solr` or `bin\solr.cmd`, because the distribution's `bin` is on `PATH`. |
| 68 | +* Each test class gets its own `SOLR_HOME`, its own block of ports and its own work directory, so classes run in parallel. Paths in the DSL are relative to the work directory. |
| 69 | +* `classCli` is the session for the whole class; use it in `@BeforeClass`.Inside a test, the bare `run(...)` and friends use that test's own session.Nodes a test starts are stopped when it ends, and nodes the class starts are stopped when the class ends.A leftover process fails the class. |
| 70 | +* `+${SOLR_PORT}+`, `+${ZK_PORT}+` etc. are resolved by the harness itself, in both commands and expected text. A typo in a variable name fails immediately instead of expanding to an empty string. |
| 71 | + |
| 72 | +== The DSL |
| 73 | + |
| 74 | +`SolrCatsTestCase` gives you a small vocabulary. Use it instead of shell tools, so the test works on both shells. |
| 75 | + |
| 76 | +=== Running commands |
| 77 | + |
| 78 | +[cols="1,2", options="header"] |
| 79 | +|=== |
| 80 | +| method | does |
| 81 | + |
| 82 | +| `run(line)` | runs and asserts exit code 0 |
| 83 | +| `runFail(line)` | asserts a non-zero exit code |
| 84 | +| `tryRun(line)` | asserts nothing; check the result yourself |
| 85 | +| `run(List.of(...))` | takes ready-split arguments and quotes each one for the current shell |
| 86 | +| `runWithStdin(input, line)` | feeds `input` on stdin |
| 87 | +| `runInBackground(line)` | for long-running commands |
| 88 | +| `run(Duration, line)` | overrides the timeout for one command |
| 89 | +|=== |
| 90 | + |
| 91 | +Each returns a `CommandResult`, whose assertions can be chained: `assertContains`, `assertNotContains`, `assertMatches`, `assertExact`, `assertLine`, `assertLineCount`, `assertExitCode`. For custom checks use `output()`, `lines()`, `linesMatching(regex)`, `firstMatch(regex)` or `fields(lineIndex)`. |
| 92 | + |
| 93 | +=== Environment, files and HTTP |
| 94 | + |
| 95 | +* Environment: `env(name, value)`, `unset(name)`, `env(name)`, `addPath(dir)`, `cd(dir)` |
| 96 | +* Files: `write`, `append`, `read`, `touch`, `mkdirs`, `copy`, `move`, `rm`, `exists`, `fileContains`, `list(dir, glob)`, `copyResource`, `workDir()` |
| 97 | +* HTTP: `http(pathAndQuery)` (GETs the first node), `http(port, path)`, `httpPost`, `httpPut`, `https(...)`, with user/password overloads for auth tests |
| 98 | +* Waiting: `awaitOrFail(what, timeout, interval, condition)`. Never sleep. |
| 99 | + |
| 100 | +=== Solr-specific helpers |
| 101 | + |
| 102 | +* Ports, enough for up to three nodes per class: `ports().solr()`, `ports().solr2()`, `ports().solr3()` and `ports().zk()` in Java, exported to commands as `+${SOLR_PORT}+`, `+${SOLR2_PORT}+`, `+${SOLR3_PORT}+` and `+${ZK_PORT}+`. Stop ports are `ports().stop()`, `ports().stop2()` and `ports().stop3()`. A second node joins the first with `+-z localhost:${ZK_PORT}+`. |
| 103 | +* `collectionExists`, `configExists`, `deleteAllCollections`, `shutdownAll`, `dumpSolrLog` |
| 104 | +* `forCli(commonsCliText, picocliText)` picks the expected wording when the two CLI implementations differ only in text. `@EnabledOnCli("picocli")` skips a test or class that only applies to one of them. |
| 105 | + |
| 106 | +=== What the harness refuses |
| 107 | + |
| 108 | +A command line that only works on one shell is rejected, and the error message names the replacement: |
| 109 | + |
| 110 | +* Unix tools such as `grep`, `sed`, `awk`, `cat`, `ls`, `curl`, `jq`, `sleep`, `kill`, `rm`: use the assertions, file helpers, `http(...)` or `awaitOrFail` above. |
| 111 | +* `|`, `&&`, `||`, `;`: make two `run()` calls, or pass data along with `runWithStdin`. |
| 112 | +* `$(...)` and backticks: compute the value in Java. |
| 113 | +* Single quotes: use double quotes or `run(List.of(...))`. |
| 114 | +* `/tmp/`, `/dev/null`: use a path under `workDir()`. |
| 115 | +* `FOO=bar solr ...` and `%FOO%`: use `env("FOO", "bar")` and `+${FOO}+`. |
| 116 | + |
| 117 | +When a test really does only make sense on one shell, annotate it `@EnabledOnShell(BASH)` (or `CMD`). It is then skipped on the other shell, and shell-specific syntax is allowed. |
| 118 | + |
| 119 | +== When a test fails |
| 120 | + |
| 121 | +The failure message gives the path to a snapshot of the test's `SOLR_HOME` under `solr/packaging/build/`. Start with `transcript.log` in there: it is a shell session you can paste, with every command, its output, exit status and timing, plus HTTP calls written as the equivalent `curl`. `dumpSolrLog()` prints the node log to the test output. |
| 122 | + |
| 123 | +Ports are randomized per run; set `CATS_PORT_BASE` (or `-Dcats.port.base=<n>`) to pin them, e.g. when replaying a failure. Other settings: `cats.timeout` (default 300 seconds per command) and `cats.shell` (`bash` or `cmd`). |
0 commit comments