Skip to content

Commit 75e99eb

Browse files
authored
Redo backport of bats refactoring in PR 3706 from main to branch_10x. (#4585)
1 parent a897839 commit 75e99eb

3 files changed

Lines changed: 42 additions & 44 deletions

File tree

solr/packaging/test/bats_helper.bash

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -97,41 +97,21 @@ collection_exists() {
9797
return 1
9898
}
9999

100-
# Wait for a collection to be queryable
101-
wait_for_collection() {
102-
local collection="$1"
103-
local timeout=${2:-180}
104-
local start_ts
105-
start_ts=$(date +%s)
106-
while true; do
107-
if curl -s -S -f "http://localhost:${SOLR_PORT}/solr/${collection}/select?q=*:*" | grep -q '"responseHeader"'; then
100+
# Utility function to retry a command until it succeeds or times out
101+
wait_for() {
102+
local timeout="${1:-30}" # Default 30 seconds timeout
103+
local interval="${2:-1}" # Default 1 second between retries
104+
shift 2 # Remove timeout and interval from args
105+
local command=("$@") # Remaining args are the command to execute
106+
107+
local end_time=$(($(date +%s) + timeout))
108+
109+
while [ $(date +%s) -lt $end_time ]; do
110+
if "${command[@]}"; then
108111
return 0
109112
fi
110-
local now
111-
now=$(date +%s)
112-
if [ $(( now - start_ts )) -ge ${timeout} ]; then
113-
echo "Timed out waiting for collection '${collection}' to become queryable" >&2
114-
return 1
115-
fi
116-
sleep 3
113+
sleep "$interval"
117114
done
118-
}
119-
120-
# Apply the ExtractingRequestHandler via Config API and print error body on failure
121-
apply_extract_handler() {
122-
local collection="$1"
123-
local json="{\"add-requesthandler\":{\"name\":\"/update/extract\",\"class\":\"org.apache.solr.handler.extraction.ExtractingRequestHandler\",\"tikaserver.url\":\"http://localhost:${TIKA_PORT}\",\"defaults\":{\"lowernames\":\"true\",\"captureAttr\":\"true\"}}}"
124-
local url="http://localhost:${SOLR_PORT}/solr/${collection}/config"
125-
# Capture body and status code
126-
local resp code body
127-
sleep 5
128-
resp=$(curl -s -S -w "\n%{http_code}" -X POST -H 'Content-type:application/json' -d "$json" "$url")
129-
code="${resp##*$'\n'}"
130-
body="${resp%$'\n'*}"
131-
if [ "$code" = "200" ]; then
132-
return 0
133-
else
134-
echo "Config API error applying ExtractingRequestHandler to ${collection} (HTTP ${code}): ${body}" >&3
135-
return 1
136-
fi
115+
116+
return 1 # Timeout reached
137117
}

solr/packaging/test/test_extraction.bats

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@
1818

1919
load bats_helper
2020

21+
# Apply the ExtractingRequestHandler via Config API and print error body on failure
22+
apply_extract_handler() {
23+
local collection="$1"
24+
local json="{\"add-requesthandler\":{\"name\":\"/update/extract\",\"class\":\"org.apache.solr.handler.extraction.ExtractingRequestHandler\",\"tikaserver.url\":\"http://localhost:${TIKA_PORT}\",\"defaults\":{\"lowernames\":\"true\",\"captureAttr\":\"true\"}}}"
25+
local url="http://localhost:${SOLR_PORT}/solr/${collection}/config"
26+
# Capture body and status code
27+
local resp code body
28+
sleep 5
29+
resp=$(curl -s -S -w "\n%{http_code}" -X POST -H 'Content-type:application/json' -d "$json" "$url")
30+
code="${resp##*$'\n'}"
31+
body="${resp%$'\n'*}"
32+
if [ "$code" = "200" ]; then
33+
return 0
34+
else
35+
echo "Config API error applying ExtractingRequestHandler to ${collection} (HTTP ${code}): ${body}" >&3
36+
return 1
37+
fi
38+
}
39+
2140
setup_file() {
2241
if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then
2342
export TIKA_PORT=$((SOLR_PORT+5))
@@ -31,7 +50,7 @@ setup_file() {
3150
fi
3251
else
3352
export DOCKER_UNAVAILABLE=1
34-
echo "WARNING: Docker not available (CLI missing or daemon not running); Tika-dependent tests will be bypassed and marked as passed." >&3
53+
echo "WARNING: Docker not available (CLI missing or daemon not running); Tika-dependent tests will be bypassed." >&3
3554
fi
3655
}
3756

@@ -57,8 +76,7 @@ teardown() {
5776
@test "using curl to extract a single pdf file" {
5877

5978
if [ -n "${DOCKER_UNAVAILABLE:-}" ]; then
60-
echo "WARNING: Docker not available; bypassing test." >&3
61-
return 0
79+
skip "Docker is not available"
6280
fi
6381

6482
# Disable security manager to allow extraction
@@ -67,7 +85,7 @@ teardown() {
6785
solr start -Dsolr.modules=extraction
6886

6987
solr create -c gettingstarted -d _default
70-
wait_for_collection gettingstarted 30
88+
wait_for 30 3 curl -s -S -f "http://localhost:${SOLR_PORT}/solr/gettingstarted/select?q=*:*" -o /dev/null
7189
apply_extract_handler gettingstarted
7290

7391
curl "http://localhost:${SOLR_PORT}/solr/gettingstarted/update/extract?literal.id=doc1&commit=true" -F "myfile=@${SOLR_TIP}/example/exampledocs/solr-word.pdf"
@@ -79,8 +97,7 @@ teardown() {
7997
@test "using the bin/solr post tool to extract content from pdf" {
8098

8199
if [ -n "${DOCKER_UNAVAILABLE:-}" ]; then
82-
echo "WARNING: Docker not available; bypassing test." >&3
83-
return 0
100+
skip "Docker is not available"
84101
fi
85102

86103
# Disable security manager to allow extraction
@@ -89,7 +106,7 @@ teardown() {
89106
solr start -Dsolr.modules=extraction
90107

91108
solr create -c content_extraction -d _default
92-
wait_for_collection content_extraction 30
109+
wait_for 30 3 curl -s -S -f "http://localhost:${SOLR_PORT}/solr/content_extraction/select?q=*:*" -o /dev/null
93110
apply_extract_handler content_extraction
94111

95112
# We filter to pdf to invoke the Extract handler.
@@ -105,8 +122,7 @@ teardown() {
105122
@test "using the bin/solr post tool to crawl web site" {
106123

107124
if [ -n "${DOCKER_UNAVAILABLE:-}" ]; then
108-
echo "WARNING: Docker not available; bypassing test." >&3
109-
return 0
125+
skip "Docker is not available"
110126
fi
111127

112128
# Disable security manager to allow extraction
@@ -115,7 +131,7 @@ teardown() {
115131
solr start -Dsolr.modules=extraction
116132

117133
solr create -c website_extraction -d _default
118-
wait_for_collection website_extraction 30
134+
wait_for 30 3 curl -s -S -f "http://localhost:${SOLR_PORT}/solr/website_extraction/select?q=*:*" -o /dev/null
119135
apply_extract_handler website_extraction
120136

121137
# Change to --recursive 1 to crawl multiple pages, but may be too slow.

solr/packaging/test/test_start_solr.bats

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ teardown() {
6060
# for start/stop/restart we parse the args separate from picking the command
6161
# which means you don't get an error message for passing a start arg, like --jvm-opts to a stop commmand.
6262

63+
# Pre-check
64+
timeout || skip "timeout utility is not available"
6365
# Set a timeout duration (in seconds)
6466
TIMEOUT_DURATION=2
6567

0 commit comments

Comments
 (0)