Skip to content

Commit

Permalink
CRDB Set up integration tests
Browse files Browse the repository at this point in the history
This sets up integration tests in GitHub actions.

Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX committed Nov 11, 2022
1 parent 6a8a6e9 commit d95c343
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
34 changes: 33 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,36 @@ jobs:
cache: true

- name: Run tests
run: go test -v ./storage/crdb/...
run: go test -v ./storage/crdb/...

integration-crdb:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: '1.19'
check-latest: true
cache: true

- name: Run CockroachDB
run: docker run --rm -d --name=roach -p 8080:8080 -p 26257:26257 -v "${PWD}/cockroach-data:/cockroach/cockroach-data" cockroachdb/cockroach:latest start-single-node --insecure

- name: Wait for CockroachDB
uses: nick-fields/retry@v2
with:
timeout_seconds: 15
max_attempts: 3
retry_on: error
command: docker exec roach ./cockroach sql --insecure -e "SELECT 1"

- name: Get crdb logs
run: docker logs roach

- name: Run tests
run: ./integration/integration_test.sh
env:
TEST_COCKROACHDB_URI: postgresql://root@localhost:26257/defaultdb?sslmode=disable
CRDB_IN_CONTAINER: true
CRDB_CONTAINER_NAME: roach
9 changes: 8 additions & 1 deletion integration/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ log_prep_test() {
local log_signer_count=${2:-1}

# Wipe the test database
yes | bash "${TRILLIAN_PATH}/scripts/resetdb.sh"
if [[ "${TEST_MYSQL_URI}" != "" ]]; then
yes | bash "${TRILLIAN_PATH}/scripts/resetdb.sh"
elif [[ "${TEST_COCKROACHDB_URI}" != "" ]]; then
yes | bash "${TRILLIAN_PATH}/scripts/resetcrdb.sh"
fi

local logserver_opts=''
local logsigner_opts=''
Expand All @@ -131,6 +135,9 @@ log_prep_test() {
if [[ "${TEST_MYSQL_URI}" != "" ]]; then
logserver_opts+=" --mysql_uri=${TEST_MYSQL_URI}"
logsigner_opts+=" --mysql_uri=${TEST_MYSQL_URI}"
elif [[ "${TEST_COCKROACHDB_URI}" != "" ]]; then
logserver_opts+="--storage_system=crdb --crdb_uri=${TEST_COCKROACHDB_URI}"
logsigner_opts+="--storage_system=crdb --crdb_uri=${TEST_COCKROACHDB_URI}"
fi

# Start a local etcd instance (if configured).
Expand Down
24 changes: 18 additions & 6 deletions scripts/resetcrdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Accepts environment variables:
(default: zaphod).
- CRDB_USER_HOST: The host that the Trillian user will connect from; use '%' as
a wildcard (default: localhost).
- CRDB_IN_CONTAINER: If set, the script will assume it is running in a Docker
container and will exec into the container to operate.
- CRDB_CONTAINER_NAME: The name of the Docker container to exec into (default:
roach).
EOF
}

Expand All @@ -37,6 +41,8 @@ collect_vars() {
[ -z ${CRDB_PASSWORD+x} ] && CRDB_PASSWORD="zaphod"
[ -z ${CRDB_USER_HOST+x} ] && CRDB_USER_HOST="localhost"
[ -z ${CRDB_INSECURE+x} ] && CRDB_INSECURE="true"
[ -z ${CRDB_IN_CONTAINER+x} ] && CRDB_IN_CONTAINER="false"
[ -z ${CRDB_CONTAINER_NAME+x} ] && CRDB_CONTAINER_NAME="roach"
FLAGS=()

# handle flags
Expand Down Expand Up @@ -68,6 +74,12 @@ collect_vars() {

# append password if supplied
[ -z ${CRDB_ROOT_PASSWORD+x} ] || FLAGS+=(-p"${CRDB_ROOT_PASSWORD}")

if [[ ${CRDB_IN_CONTAINER} = 'true' ]]; then
CMD="docker exec ${CRDB_CONTAINER_NAME} cockroach"
else
CMD="cockroach"
fi
}

main() {
Expand All @@ -84,20 +96,20 @@ main() {
then
echo "Resetting DB..."
set -eux
cockroach sql "${FLAGS[@]}" -e "DROP DATABASE IF EXISTS ${CRDB_DATABASE};" || \
$CMD sql "${FLAGS[@]}" -e "DROP DATABASE IF EXISTS ${CRDB_DATABASE};" || \
die "Error: Failed to drop database '${CRDB_DATABASE}'."
cockroach sql "${FLAGS[@]}" -e "CREATE DATABASE ${CRDB_DATABASE};" || \
$CMD sql "${FLAGS[@]}" -e "CREATE DATABASE ${CRDB_DATABASE};" || \
die "Error: Failed to create database '${CRDB_DATABASE}'."
if [[ ${CRDB_INSECURE} = 'true' ]]; then
cockroach sql "${FLAGS[@]}" -e "CREATE USER IF NOT EXISTS ${CRDB_USER};" || \
$CMD sql "${FLAGS[@]}" -e "CREATE USER IF NOT EXISTS ${CRDB_USER};" || \
die "Error: Failed to create user '${CRDB_USER}'."
else
cockroach sql "${FLAGS[@]}" -e "CREATE USER IF NOT EXISTS ${CRDB_USER} WITH PASSWORD '${CRDB_PASSWORD}';" || \
$CMD sql "${FLAGS[@]}" -e "CREATE USER IF NOT EXISTS ${CRDB_USER} WITH PASSWORD '${CRDB_PASSWORD}';" || \
die "Error: Failed to create user '${CRDB_USER}'."
fi
cockroach sql "${FLAGS[@]}" -e "GRANT ALL PRIVILEGES ON DATABASE ${CRDB_DATABASE} TO ${CRDB_USER} WITH GRANT OPTION" || \
$CMD sql "${FLAGS[@]}" -e "GRANT ALL PRIVILEGES ON DATABASE ${CRDB_DATABASE} TO ${CRDB_USER} WITH GRANT OPTION" || \
die "Error: Failed to grant '${CRDB_USER}' user all privileges on '${CRDB_DATABASE}'."
cockroach sql "${FLAGS[@]}" -d ${CRDB_DATABASE} < ${TRILLIAN_PATH}/storage/crdb/schema/storage.sql || \
$CMD sql "${FLAGS[@]}" -d ${CRDB_DATABASE} < ${TRILLIAN_PATH}/storage/crdb/schema/storage.sql || \
die "Error: Failed to create tables in '${CRDB_DATABASE}' database."
echo "Reset Complete"
fi
Expand Down

0 comments on commit d95c343

Please sign in to comment.