From 10d3411845c141283f77f661d2adb45743efbdc0 Mon Sep 17 00:00:00 2001
From: Juan Antonio Osorio <juan.osoriorobles@eu.equinix.com>
Date: Thu, 10 Nov 2022 17:30:37 +0200
Subject: [PATCH] crdb: Set up integration tests

This sets up integration tests in GitHub actions.

Signed-off-by: Juan Antonio Osorio <juan.osoriorobles@eu.equinix.com>
---
 .github/workflows/test.yaml     | 37 ++++++++++++++++++++++++++++++++-
 cmd/trillian_log_server/main.go |  1 +
 integration/functions.sh        | 10 ++++++++-
 scripts/resetcrdb.sh            | 26 ++++++++++++++++-------
 4 files changed, 65 insertions(+), 9 deletions(-)

diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml
index 1de66c5a62..19fb961c2f 100644
--- a/.github/workflows/test.yaml
+++ b/.github/workflows/test.yaml
@@ -33,4 +33,39 @@ jobs:
         cache: true
 
     - name: Run tests
-      run: go test -v ./storage/crdb/... ./quota/crdbqm/...
\ No newline at end of file
+      run: go test -v ./storage/crdb/... ./quota/crdbqm/...
+
+  integration:
+    runs-on: ubuntu-22.04
+    steps:
+    - uses: actions/checkout@v3
+
+    - uses: actions/setup-go@v3
+      with:
+        go-version: '1.19'
+        check-latest: true
+        cache: true
+
+    - name: Build before tests
+      run: go mod download && go build ./...
+    
+    - 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
diff --git a/cmd/trillian_log_server/main.go b/cmd/trillian_log_server/main.go
index a2aa7fffee..745881c9c2 100644
--- a/cmd/trillian_log_server/main.go
+++ b/cmd/trillian_log_server/main.go
@@ -94,6 +94,7 @@ func main() {
 			klog.Exitf("Failed to load flags from config file %q: %s", *configFile, err)
 		}
 	}
+	klog.Info("**** Log Server Starting ****")
 
 	ctx, cancel := context.WithCancel(context.Background())
 	defer cancel()
diff --git a/integration/functions.sh b/integration/functions.sh
index bc66730081..b359992cbe 100755
--- a/integration/functions.sh
+++ b/integration/functions.sh
@@ -117,12 +117,17 @@ kill_pid() {
 #  - SOFTHSM_CONF    : location of the SoftHSM configuration file
 #
 log_prep_test() {
+  set -x
   # Default to one of each.
   local rpc_server_count=${1:-1}
   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=''
@@ -131,6 +136,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+="--quota_system=crdb --storage_system=crdb --crdb_uri=${TEST_COCKROACHDB_URI}"
+    logsigner_opts+="--quota_system=crdb --storage_system=crdb --crdb_uri=${TEST_COCKROACHDB_URI}"
   fi
 
   # Start a local etcd instance (if configured).
diff --git a/scripts/resetcrdb.sh b/scripts/resetcrdb.sh
index b7f0e752ab..4ffd669548 100755
--- a/scripts/resetcrdb.sh
+++ b/scripts/resetcrdb.sh
@@ -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
 }
 
@@ -36,7 +40,9 @@ collect_vars() {
   [ -z ${CRDB_USER+x} ] && CRDB_USER="test"
   [ -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_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
@@ -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 -i ${CRDB_CONTAINER_NAME} cockroach"
+  else
+    CMD="cockroach"
+  fi
 }
 
 main() {
@@ -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