-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathtest-java-examples.sh
More file actions
executable file
·196 lines (165 loc) · 5.89 KB
/
test-java-examples.sh
File metadata and controls
executable file
·196 lines (165 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# This script verifies that the Java client examples can run correctly
# against a local Kudu cluster.
#
# Usage:
# test-java-examples.sh [example-name]
#
# If no argument is provided, tests all Java examples.
#
# To add a new example:
# 1. Add the example directory name to the VALID_EXAMPLES array below
# 2. Add a case in the test_java_example() function with the command to run it
# List of valid example names (single source of truth)
VALID_EXAMPLES=(
"java-example"
"insert-loadgen"
"collectl"
)
JAVA_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)
# KUDU_HOME should point to the root of the Kudu repository
if [[ -z "$KUDU_HOME" ]]; then
# Try to infer it if not set
KUDU_HOME=$(cd "$JAVA_DIR/.."; pwd)
echo "KUDU_HOME not set, inferring from script location: $KUDU_HOME"
fi
if [[ ! -d "$KUDU_HOME" ]]; then
exit_error "KUDU_HOME directory does not exist: $KUDU_HOME"
fi
EXAMPLES_DIR="$KUDU_HOME/examples/java"
# Source the common cluster management functions
source "$KUDU_HOME/build-support/test-cluster-common.sh"
BUILD_DIR="$KUDU_HOME/build/latest"
BIN_DIR="$BUILD_DIR/bin"
# Clean up after the test. Must be idempotent.
cleanup() {
cleanup_cluster
}
trap cleanup EXIT
set -e
set -o pipefail
set -x
if ! command -v mvn &> /dev/null; then
exit_error "Maven (mvn) not found. Please install Maven to run Java examples tests."
fi
JAVA_VERSION_STR=$(java -version 2>&1 | awk -F '"' '/version/ {print $2; exit}')
JAVA_MAJOR_VERSION=$(echo "$JAVA_VERSION_STR" | awk -F. '{ if ($1 == "1") print $2; else print $1 }')
SUREFIRE_ARG_LINE=""
# Maven's Guice/cglib path can require this module opening on modern JDKs.
# Add it automatically for Java 9+ to keep example builds JDK17-compatible.
if [[ -n "$JAVA_MAJOR_VERSION" && "$JAVA_MAJOR_VERSION" -ge 9 ]]; then
if [[ "$MAVEN_OPTS" != *"--add-opens=java.base/java.lang=ALL-UNNAMED"* ]]; then
export MAVEN_OPTS="${MAVEN_OPTS:+$MAVEN_OPTS }--add-opens=java.base/java.lang=ALL-UNNAMED"
fi
# Surefire runs tests in a forked JVM and may ignore MAVEN_OPTS.
# Pass module opens via argLine so test JVMs get the same flags.
SUREFIRE_ARG_LINE="--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED"
fi
KUDU_VERSION=$(cat "$KUDU_HOME/version.txt")
echo "Using Kudu version: $KUDU_VERSION"
echo "Publishing Kudu Java artifacts to local Maven repository..."
pushd "$JAVA_DIR"
./gradlew publishToMavenLocal
popd
test_java_example() {
local example_name=$1
local example_dir="$EXAMPLES_DIR/$example_name"
echo "Testing Java example: $example_name"
if [[ ! -d "$example_dir" ]]; then
echo "WARNING: Example directory not found: $example_dir, skipping"
return 0
fi
pushd "$example_dir"
echo "Building $example_name with Maven..."
# Build the example using locally built Kudu
local -a mvn_args=(
clean package
"-Dkudu-version=$KUDU_VERSION"
"-DkuduBinDir=$KUDU_HOME/build/latest/bin"
"-DuseLocalKuduBin=true"
)
if [[ -n "$SUREFIRE_ARG_LINE" ]]; then
mvn_args+=("-DargLine=$SUREFIRE_ARG_LINE")
fi
if ! mvn "${mvn_args[@]}" ; then
exit_error "Failed to build $example_name"
fi
echo "Running $example_name..."
case "$example_name" in
java-example)
if ! java -DkuduMasters=$LOCALHOST_IP:$MASTER_RPC_PORT \
-jar target/kudu-java-example-*.jar ; then
exit_error "$example_name failed"
fi
;;
insert-loadgen)
# Run insert-loadgen for a limited time
local test_table="loadgen_test_table"
# Run in background and kill after 5 seconds
java -DkuduMasters=$LOCALHOST_IP:$MASTER_RPC_PORT \
-jar target/kudu-insert-loadgen-*.jar \
$LOCALHOST_IP:$MASTER_RPC_PORT "$test_table" &
local loadgen_pid=$!
sleep 5
kill $loadgen_pid 2>/dev/null || true
wait $loadgen_pid 2>/dev/null || true
local row_count=$("$BIN_DIR/kudu" table scan $LOCALHOST_IP:$MASTER_RPC_PORT "$test_table" 2>/dev/null | wc -l)
if [[ $row_count -lt 1 ]]; then
exit_error "insert-loadgen did not insert enough data (row_count=$row_count)"
fi
echo "insert-loadgen inserted data successfully (rows: $row_count)"
;;
collectl)
# Collectl requires external socket input, so just verify it builds
echo "collectl requires external input, verifying build only"
;;
*)
echo "Unknown example: $example_name"
;;
esac
echo "$example_name completed successfully!"
popd
}
# Parse command-line arguments
EXAMPLE_NAME="$1"
# Validate example name (optional)
if [[ -n "$EXAMPLE_NAME" ]]; then
valid=false
for example in "${VALID_EXAMPLES[@]}"; do
if [[ "$EXAMPLE_NAME" == "$example" ]]; then
valid=true
break
fi
done
if [[ "$valid" != "true" ]]; then
exit_error "Invalid example name: $EXAMPLE_NAME. Must be one of: ${VALID_EXAMPLES[*]}"
fi
fi
start_test_cluster "$BIN_DIR" "java_examples-test"
if [[ -z "$EXAMPLE_NAME" ]]; then
echo "No example specified, testing all Java examples"
for example in "${VALID_EXAMPLES[@]}"; do
test_java_example "$example"
done
else
test_java_example "$EXAMPLE_NAME"
fi
echo "All Java examples tests completed successfully!"