cassandra/test/run
2017-07-19 17:22:19 +02:00

207 lines
6.1 KiB
Bash
Executable file

#!/bin/bash
#
# Test the Cassandra image.
#
# IMAGE_NAME specifies the name of the candidate image used for testing.
# The image has to be available before this script is executed.
#
set -exo nounset
shopt -s nullglob
IMAGE_NAME=${IMAGE_NAME-mycass}
source test/common
TEST_LIST="\
run_container_creation_tests
run_configuration_tests
run_general_tests
run_mount_config_test"
# the change password tests does not work or make sense in cassandra
#run_change_password_test
# the doc test is not working yet
#run_doc_test CASSANDRA_ADMIN_PASSWORD volume 9042"
test $# -eq 1 -a "${1-}" == --list && echo "$TEST_LIST" && exit 0
test -n "${IMAGE_NAME-}" || false 'make sure $IMAGE_NAME is defined'
CID_FILE_DIR=$(mktemp --suffix=cassandra_test_cidfiles -d)
# used in cleanup function
EXPECTED_EXIT_CODE=143
function cqlsh_cmd() {
docker run --rm "$IMAGE_NAME" cqlsh "$@"
}
function remove_container() {
local name="$1" ; shift
CONTAINER=$(get_cid ${name})
: "Stopping and removing container $CONTAINER..."
docker exec $CONTAINER nodetool stopdaemon || [ "$?" == "137" ]
while [ "$(docker inspect -f '{{.State.Running}}' $CONTAINER)" == "true" ] ; do
sleep 2
done
exit_status=$(docker inspect -f '{{.State.ExitCode}}' $CONTAINER)
if [ "$exit_status" != "0" ]; then
: "Dumping logs for $CONTAINER"
docker logs $CONTAINER | tail
fi
docker rm $CONTAINER
rm $CID_FILE_DIR/$name
: "Removed."
}
function test_config_option() {
local setting=$1 ; shift
local value=$1 ; shift
local name="configuration_${setting}"
CONTAINER_ARGS="
-e CASSANDRA_${setting^^}=${value}
"
create_container $name
test_connection $name
# If nothing is found, grep returns 1 and test fails.
docker exec $(get_cid ${name}) bash -c "cat /etc/cassandra/cassandra.yaml | grep -q ${setting}:\ ${value}"
remove_container $name
}
function test_connection() {
local name="$1" ; shift
local ip=$(get_container_ip $name)
: " Testing cqlsh connection to $ip..."
local max_attempts=20
local sleep_time=2
local i
for i in $(seq $max_attempts); do
: " Trying to connect..."
if cqlsh_cmd "$ip" "$@" <<< 'exit'; then
: " Success!"
return 0
fi
sleep $sleep_time
done
echo "ERROR: Giving up, failed to connect."
return 1
}
function test_general() {
: " Testing general usage ('$1')"
local name=$1 ; shift
create_container $name
test_connection $name
test_cqlsh $name
remove_container $name
}
function test_cqlsh() {
local name="$1" ; shift
local ip=$(get_container_ip $name)
: " Testing basic cqlsh commands"
cqlsh_cmd "$ip" <<< 'CREATE KEYSPACE cycling WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };'
cqlsh_cmd "$ip" <<< 'CREATE TABLE cycling.cyclist_name ( id UUID, fname text, lname text, PRIMARY KEY (id));'
cqlsh_cmd "$ip" <<< 'INSERT INTO cycling.cyclist_name (id, fname, lname) VALUES (7562c0f3-2f6c-41da-b276-88abac471eaf, 'john', 'smith');'
cqlsh_cmd "$ip" <<< 'INSERT INTO cycling.cyclist_name (id, fname, lname) VALUES (e69be414-f7eb-4e5a-b635-446ee5849810, 'john', 'doe');'
cqlsh_cmd "$ip" <<< 'INSERT INTO cycling.cyclist_name (id, fname, lname) VALUES (d4aa012a-8d71-4189-bcc8-24a858b713b6, 'john', 'smith');'
cqlsh_cmd "$ip" <<< 'SELECT * FROM cycling.cyclist_name WHERE lname = 'smith' ALLOW FILTERING;'
cqlsh_cmd "$ip" <<< 'DROP TABLE cycling.cyclist_name;'
cqlsh_cmd "$ip" <<< 'DROP KEYSPACE cycling;'
: " Success!"
}
function run_container_creation_tests() {
# there are no invalid combinations of variables in cassandra yet
# : " Testing invalid combinations of variables"
# assert_container_creation_fails
# : " Success!"
: " Testing invalid values of variables"
assert_container_creation_fails -e CASSANDRA_CLUSTER_NAME=cool cluster
: " Success!"
}
function run_configuration_tests() {
: " Testing image configuration settings"
test_config_option num_tokens 256
# not allowing to have a space character in the cluster name
test_config_option cluster_name cool_cluster
: " Success!"
}
function run_general_tests() {
CONTAINER_ARGS= test_general no_admin
# Test with arbitrary uid for the container
# the permissions are not set for arbitrary user to run the container
#CONTAINER_ARGS="-u 12345" run_tests no_admin_altuid
}
function run_mount_config_test() {
local name="mount_config"
: " Testing config file mount"
local tmpdir=$(mktemp -d)
chmod a+rwx $tmpdir
config_file=${tmpdir}/cassandra.yaml
echo 'cluster_name: cool_cluster
commitlog_sync: periodic
commitlog_sync_period_in_ms: 10000
partitioner: org.apache.cassandra.dht.Murmur3Partitioner
endpoint_snitch: SimpleSnitch
start_native_transport: true
seed_provider:
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
- seeds: "172.17.0.2"' > $config_file
chmod a+r ${config_file}
CONTAINER_ARGS="
-v ${config_file}:/etc/cassandra/cassandra.yaml:Z
"
create_container $name
# need this to wait for the container to start up
test_connection $name
: " Testing if mounted config file works"
docker exec $(get_cid ${name}) nodetool describecluster | grep -q Name:\ cool_cluster
rm -r $tmpdir
: " Success!"
}
function run_change_password_test() {
local name="change_password"
local admin_password='adminPassword'
local volume_dir
volume_dir=`mktemp -d --tmpdir cassandra-testdata.XXXXX`
chmod a+rwx ${volume_dir}
CONTAINER_ARGS="
-e CASSANDRA_ADMIN_PASSWORD=${admin_password}
-v ${volume_dir}:/var/lib/cassandra/data:Z
"
create_container $name
# need this to wait for the container to start up
test_connection $name -u admin -p ${admin_password}
echo " Changing passwords"
docker stop $(get_cid ${name})
CONTAINER_ARGS="
-e CASSANDRA_ADMIN_PASSWORD=NEW_${admin_password}
-v ${volume_dir}:/var/lib/cassandra/data:Z
"
create_container "${name}_NEW"
# need this to wait for the container to start up
test_connection "${name}_NEW" -u admin -p NEW_${admin_password}
# need to remove volume_dir with sudo because of permissions of files written
# by the Docker container
sudo rm -rf ${volume_dir}
echo " Success!"
}
# Run the chosen tests
TEST_LIST=${@:-$TEST_LIST} run_all_tests