-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathtest-python-examples.sh
More file actions
executable file
·177 lines (150 loc) · 5.13 KB
/
test-python-examples.sh
File metadata and controls
executable file
·177 lines (150 loc) · 5.13 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
#!/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 Python client examples can run correctly
# against a local Kudu cluster. It is similar to client_examples-test.sh
# but for Python examples.
#
# Usage:
# test-python-examples.sh <python2|python3> [example-script]
#
# If no example script is provided, tests all basic Python examples.
#
# To add a new example:
# 1. Add the script name to the VALID_EXAMPLES array below
# 2. Add a case in the test_example() function with the command to run it
# List of valid example scripts
VALID_EXAMPLES=(
"basic_example.py"
"non_unique_primary_key.py"
"array_example.py"
)
PYTHON_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)
if [[ -z "$KUDU_HOME" ]]; then
# Try to infer it if not set
KUDU_HOME=$(cd "$PYTHON_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/python"
# 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"
# Track virtualenv directories for cleanup
VENV_DIRS=()
# Clean up after the test. Must be idempotent.
cleanup() {
cleanup_cluster
for venv_dir in "${VENV_DIRS[@]}"; do
if [[ -n "$venv_dir" ]] && [[ -d "$venv_dir" ]]; then
rm -rf "$venv_dir"
fi
done
}
trap cleanup EXIT
set -e
set -o pipefail
set -x
test_example() {
local example_script=$1
local python_version=$2
case "$example_script" in
basic_example.py)
echo "Testing basic_example.py..."
if ! "$python_version" "$EXAMPLES_DIR/basic-python-example/basic_example.py" \
--masters $LOCALHOST_IP \
--ports $MASTER_RPC_PORT ; then
exit_error "basic_example.py failed with $python_version"
fi
;;
non_unique_primary_key.py)
echo "Testing non_unique_primary_key.py..."
if ! "$python_version" "$EXAMPLES_DIR/basic-python-example/non_unique_primary_key.py" \
--masters $LOCALHOST_IP \
--ports $MASTER_RPC_PORT ; then
exit_error "non_unique_primary_key.py failed with $python_version"
fi
;;
array_example.py)
echo "Testing array_example.py..."
if ! "$python_version" "$EXAMPLES_DIR/basic-python-example/array_example.py" \
7798
--masters $LOCALHOST_IP \
--ports $MASTER_RPC_PORT ; then
exit_error "array_example.py failed with $python_version"
fi
;;
*)
exit_error "Unknown example script: $example_script"
;;
esac
}
test_with_python_version() {
local python_version=$1
local example_name=$2
if ! command -v "$python_version" &> /dev/null; then
echo "WARNING: $python_version not found, skipping $python_version tests"
return 0
fi
# Create a temporary virtualenv for this Python version
local venv_dir=$(mktemp -d -t kudu-python-examples-test-${python_version}.XXXXXXXXXXXXX)
VENV_DIRS+=("$venv_dir")
echo "Creating virtualenv for $python_version in $venv_dir"
virtualenv -p "$python_version" "$venv_dir"
source "$venv_dir/bin/activate"
pip install -r "$PYTHON_DIR/requirements.txt"
make clean
make install
echo "Running Python examples with $python_version..."
if [[ -z "$example_name" ]]; then
for example in "${VALID_EXAMPLES[@]}"; do
test_example "$example" "$python_version"
done
else
test_example "$example_name" "$python_version"
fi
echo "All $python_version examples completed successfully!"
deactivate
}
# Parse command-line arguments
PYTHON_VERSION="$1"
EXAMPLE_NAME="$2"
# Validate Python version (required)
if [[ "$PYTHON_VERSION" != "python2" ]] && [[ "$PYTHON_VERSION" != "python3" ]]; then
exit_error "Usage: test-python-examples.sh <python2|python3> [example-script]
Valid example scripts: ${VALID_EXAMPLES[*]}"
fi
# Validate example script 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 script: $EXAMPLE_NAME. Must be one of: ${VALID_EXAMPLES[*]}"
fi
fi
start_test_cluster "$BIN_DIR" "python_examples-test"
test_with_python_version "$PYTHON_VERSION" "$EXAMPLE_NAME"
echo "All Python examples tests completed successfully!"