Skip to content

Commit 5c9100c

Browse files
janhoydsmiley
authored andcommitted
SOLR-18642 Replace BATS with CATS (#5323)
(cherry picked from commit 784fd5e)
1 parent c4329cf commit 5c9100c

104 files changed

Lines changed: 14649 additions & 135 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/labeler.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ examples:
3030
bats-tests:
3131
- changed-files:
3232
- any-glob-to-any-file:
33-
- solr/packaging/tests/**
33+
- solr/packaging/test/**
3434

3535
# Add 'docker' label for changes to docker
3636
docker:
@@ -161,6 +161,9 @@ tests:
161161
- any-glob-to-any-file:
162162
- '**/src/test/**'
163163
- '**/src/test-files/**'
164+
# The CATS harness library is test infrastructure, but ships in src/java.
165+
- solr/test-framework/src/java/org/apache/solr/cats/**
166+
- solr/test-framework/src/java/org/apache/solr/shell/**
164167

165168
# Add category labels loosely based on java packages
166169
# Add 'api' label

‎.github/workflows/bin-solr-test.yml‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
name: Solr Script Tests
1+
# DEPRECATED, and manual-only: the BATS suites it runs are deprecated as of Solr 10.2, and every
2+
# .bats file has a ported counterpart under solr/packaging/src/test/org/apache/solr/shell run by
3+
# cats-test.yml, which also covers bin/solr.cmd. This workflow goes away with the .bats files.
4+
name: Solr Script Tests (BATS, deprecated)
25

36
on:
4-
pull_request:
5-
branches:
6-
- '*'
7-
paths:
8-
- '.github/workflows/bin-solr-test.yml'
9-
- 'solr/bin/**'
10-
- 'solr/packaging/**'
11-
- 'solr/core/src/java/org/apache/solr/cli/**'
7+
workflow_dispatch:
128

139
concurrency:
14-
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
10+
group: ${{ github.workflow }}-${{ github.ref }}
1511
cancel-in-progress: true
1612

1713
jobs:

‎.github/workflows/cats-test.yml‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# CATS — Cross-platform Automated Testing System: the JUnit-based harness that drives bin/solr and
2+
# bin\solr.cmd. Named after BATS, which it replaces.
3+
4+
name: Solr CATS Tests
5+
6+
on:
7+
pull_request:
8+
branches:
9+
- '*'
10+
paths:
11+
- '.github/workflows/cats-test.yml'
12+
- 'solr/bin/solr*'
13+
- 'solr/packaging/src/**'
14+
- 'solr/packaging/build.gradle'
15+
- 'solr/core/src/java/org/apache/solr/cli/**'
16+
- 'solr/test-framework/src/java/org/apache/solr/cats/**'
17+
- 'solr/test-framework/src/test/org/apache/solr/cats/**'
18+
- 'solr/test-framework/src/test-files/cats/**'
19+
- 'solr/test-framework/src/java/org/apache/solr/shell/**'
20+
- 'solr/test-framework/src/test/org/apache/solr/shell/**'
21+
22+
concurrency:
23+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
24+
cancel-in-progress: true
25+
26+
jobs:
27+
test:
28+
name: CATS (${{ matrix.name }})
29+
30+
strategy:
31+
# Both platforms always report. Without this, a failure on one cancels the other, and "did it
32+
# break on Windows or everywhere?" is the first question a reviewer asks.
33+
fail-fast: false
34+
matrix:
35+
include:
36+
- name: bash
37+
os: ubuntu-latest
38+
shell: bash
39+
gradlew: ./gradlew
40+
timeout: 40
41+
- name: cmd.exe
42+
os: windows-latest
43+
shell: cmd
44+
gradlew: .\gradlew.bat
45+
# Windows runners are slower, and the suites start real Solr JVMs rather than mocking
46+
# them, so the same work takes appreciably longer there.
47+
timeout: 70
48+
49+
runs-on: ${{ matrix.os }}
50+
timeout-minutes: ${{ matrix.timeout }}
51+
52+
defaults:
53+
run:
54+
# Each leg launches Gradle with its own shell and wrapper script. The bash gradlew cannot
55+
# start Gradle under the Windows bash: it hands the already Windows-style wrapper jar path
56+
# to cygpath a second time, which splits it at the drive letter's colon into two entries,
57+
# neither of them the jar. The launching shell does not reach the tests - the harness picks
58+
# cmd.exe from os.name - so the Windows job still drives bin\solr.cmd under cmd.exe.
59+
shell: ${{ matrix.shell }}
60+
61+
steps:
62+
- name: Checkout code
63+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
64+
65+
- uses: ./.github/actions/prepare-for-build
66+
67+
# The harness' own unit tests. No distribution needed, so this fails fast if the harness itself
68+
# is broken. Run on both platforms: many of these cases exercise the cmd.exe half of it.
69+
- name: Test the CATS harness
70+
run: ${{ matrix.gradlew }} -p solr/test-framework test --tests "org.apache.solr.cats.*"
71+
72+
- name: Test bin/solr with CATS
73+
run: ${{ matrix.gradlew }} -p solr/packaging catsTests
74+
75+
# Linux only: the two CLI implementations differ in Java-side wording, which is the same on both
76+
# platforms. Reuses the distribution built above, so it costs only the suite run.
77+
# The step above already covers picocli, which SOLR-18561 made the default, so this one pins the
78+
# deprecated implementation. Delete it along with commons-cli itself.
79+
- name: Test bin/solr with CATS using the deprecated commons-cli
80+
if: ${{ matrix.os == 'ubuntu-latest' }}
81+
run: ${{ matrix.gradlew }} -p solr/packaging catsTestsCommonsCli
82+
83+
- name: Archive logs
84+
if: ${{ failure() }}
85+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
86+
with:
87+
# Per platform: upload-artifact refuses two uploads under one name, so without the suffix
88+
# the second job to fail would error on the upload instead of reporting its tests.
89+
name: logs-${{ matrix.name }}
90+
path: |
91+
solr/packaging/build/cats-test-output
92+
solr/packaging/build/cats-test-output-picocli
93+
solr/packaging/build/cats-test-output-commons-cli
94+
solr/test-framework/build/test-results

‎AGENTS.md‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,21 @@ While README.md and CONTRIBUTING.md are mainly written for humans, this file is
2727

2828
- See `dev-docs/gradle-help/tests.txt` for hints on running tests
2929
- To run a specific test: `gradlew :solr:core:test --tests "org.apache.solr.search.TestCaffeineCache"`
30-
- To run a specific BATS test: `gradlew iTest --tests test_adminconsole_urls.bats`
30+
- To run CATS (Cross-platform Automated Testing System), the cross-platform CLI suites: `gradlew -p solr/packaging catsTests --tests "*ZkTest"`
31+
- To run a specific BATS test (deprecated, replaced by CATS): `gradlew iTest --tests test_adminconsole_urls.bats`
3132
- The randomization seed is important. To repeat a failing tests, pass the same seed given in the failure by adding to Gradle: `-Ptests.seed=HEXADECIMALHERE`.
3233
- Test output goes to `solr/<module>/build/test-results/test/outputs/OUTPUT-<fully.qualified.TestName>.txt` (stdout/stderr log) and `solr/<module>/build/test-results/test/TEST-<fully.qualified.TestName>.xml` (JUnit XML with pass/fail/error details)
3334
- To scan test output for a specific issue across already-run tests: `grep -rl "pattern" solr/*/build/test-results/test/outputs/`
3435

3536
## Writing Tests
3637

37-
- When adding a test to an existing suite/file, keep the same style / design choices
38-
- When adding a *new* Java test suite/file:
39-
- Subclass SolrTestCase, or if SolrCloud is needed then SolrCloudTestCase
40-
- If SolrTestCase and need to embed Solr, use either EmbeddedSolrServerTestRule (doesn't use HTTP) or SolrJettyTestRule if HTTP/Jetty is relevant to what is being tested.
41-
- Use SolrTestCase instead of SolrTestCaseJ4 for new tests
42-
- System properties set in a test are restored after each test by the base classes (`SystemPropertiesRestoreRule`); do not add `try/finally` + `System.clearProperty` cleanup
38+
**Read `dev-docs/writing-tests.adoc` before writing any test.** It is the authority on which harness to use — `SolrTestCase` by default, `SolrCloudTestCase` only when a cluster is needed, `SolrCatsTestCase` for `bin/solr` and `bin\solr.cmd`, `AdminUiTestBase` for the browser, a Testcontainers `@ClassRule` for a real external service, and never `SolrTestCaseJ4`, a new `.bats` file, or `docker run` from a test — and on the lifecycle, randomization and waiting conventions, with a worked example per kind.
39+
40+
When adding a test to a suite that already exists, match the design choices of the file you are editing.
41+
4342
- For BATS shell integration tests in `solr/packaging/test/`:
4443
- Always use `run <command>` followed by `assert_output --partial "..."` or `refute_output --partial "..."` instead of capturing output into local variables and using `[[ ]]` comparisons
4544
- Avoid patterns like `local var=$(cmd | grep ...); [[ "$var" == *"..."* ]]` — use `run cmd` + `assert_output`/`refute_output` instead
46-
- Avoid `Thread.sleep`; fixed sleeps waste build time on every run and can flake under load.
47-
- Have the other thread signal when it reaches a point, e.g. complete a `CompletableFuture` that the test thread `get`s with a timeout.
48-
- If you must poll, use `org.apache.solr.util.TimeOut#waitFor` (solr-core) rather than a hand-rolled sleep loop.
49-
- Derive timeouts from what they bound and define them as constants, e.g. `ABORT_DEADLINE_MS = SlowServlet.DELAY_MS / 2`, not a bare `2, SECONDS`.
5045

5146
## Documentation
5247

@@ -73,6 +68,7 @@ Internals:
7368
Process & tooling:
7469

7570
- `dev-docs/solr-source-code.adoc`, `git.adoc`, `IDEs.adoc`, `jvms.adoc` — build and dev environment
71+
- `dev-docs/writing-tests.adoc` — the test harnesses, how to choose one, lifecycle and randomization conventions
7672
- `dev-docs/ref-guide/` — ref-guide authoring (AsciiDoc syntax, Antora templates)
7773
- `dev-docs/dependency-upgrades.adoc`, `lucene-upgrade.md`, `working-between-major-versions.adoc` — upgrades and branch management
7874
- `dev-docs/releasing.adoc`, `changelog.adoc`, `asf-jenkins.adoc` — release and CI process
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# See https://github.com/apache/solr/blob/main/dev-docs/changelog.adoc
2+
3+
title: >
4+
CATS (Cross-platform Automated Testing System), a new bespoke test harness for the Solr CLI in
5+
`org.apache.solr.shell`, exercises `bin/solr` under bash and `bin\solr.cmd` under cmd.exe from one
6+
JUnit test source, covering the start scripts on Windows for the first time. The BATS suites in
7+
`solr/packaging/test` are ported to CATS and deprecated.
8+
type: other
9+
authors:
10+
- name: Jan Høydahl
11+
url: https://home.apache.org/phonebook.html?uid=janhoy
12+
links:
13+
- name: SOLR-18642
14+
url: https://issues.apache.org/jira/browse/SOLR-18642

‎dev-docs/README.adoc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ details, see the information in `/solr/licenses/README.committers.txt`.
1111
3. The Solr Reference Guide, which provides primarily end user oriented documentation.
1212
However, the Ref Guide hosts its own documentation on how to write Ref Guide content.
1313
14+
4. `writing-tests.adoc`, which covers the kinds of test Solr has, how to pick one, and the lifecycle conventions they share.
15+
1416
There are a number of tools related to doing development on Solr available in the `/dev-tools`
1517
directory.

‎dev-docs/cats.adoc‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)