#!/bin/bash # Library routines and initial setup for Mononoke-related tests. function get_free_socket { # From https://unix.stackexchange.com/questions/55913/whats-the-easiest-way-to-find-an-unused-local-port cat > "$TESTTMP/get_free_socket.py" <> "$TESTTMP/mononoke.out" 2>&1 & export MONONOKE_PID=$! echo "$MONONOKE_PID" >> "$DAEMON_PIDS" } function mononoke_hg_sync { $MONONOKE_HG_SYNC \ --do-not-init-cachelib \ --mononoke-config-path mononoke-config \ ssh://user@dummy/"$1" sync-once --start-id "$2" } # Wait until a Mononoke server is available for this repo. function wait_for_mononoke { # MONONOKE_START_TIMEOUT is set in seconds # Number of attempts is timeout multiplied by 10, since we # sleep every 0.1 seconds. local attempts=$(expr ${MONONOKE_START_TIMEOUT:-15} \* 10) SSLCURL="sslcurl --noproxy localhost \ https://localhost:$MONONOKE_SOCKET" for _ in $(seq 1 $attempts); do $SSLCURL 2>&1 | grep -q 'Empty reply' && break sleep 0.1 done if ! $SSLCURL 2>&1 | grep -q 'Empty reply'; then echo "Mononoke did not start" >&2 cat "$TESTTMP/mononoke.out" exit 1 fi } # Wait until cache warmup finishes function wait_for_mononoke_cache_warmup { local attempts=150 for _ in $(seq 1 $attempts); do grep -q "finished initial warmup" "$TESTTMP/mononoke.out" && break sleep 0.1 done if ! grep -q "finished initial warmup" "$TESTTMP/mononoke.out"; then echo "Mononoke warmup did not finished" >&2 cat "$TESTTMP/mononoke.out" exit 1 fi } function setup_common_hg_configs { cat >> "$HGRCPATH" <> "$TESTTMP"/pushrebaserecording.sql < repos/repo/server.toml <> repos/repo/server.toml <> repos/repo/server.toml <> repos/repo/server.toml <> repos/repo/server.toml <> repos/repo/server.toml <> repos/repo/server.toml <> repos/repo/server.toml < repos/disabled_repo/server.toml <> repos/repo/server.toml } function blobimport { input="$1" output="$2" shift 2 mkdir -p "$output" $MONONOKE_BLOBIMPORT --repo_id 0 \ --mononoke-config-path "$TESTTMP/mononoke-config" \ "$input" --do-not-init-cachelib "$@" >> "$TESTTMP/blobimport.out" 2>&1 BLOBIMPORT_RC="$?" if [[ $BLOBIMPORT_RC -ne 0 ]]; then cat "$TESTTMP/blobimport.out" # set exit code, otherwise previous cat sets it to 0 return "$BLOBIMPORT_RC" fi } function bonsai_verify { GLOG_minloglevel=2 $MONONOKE_BONSAI_VERIFY --repo_id 0 \ --mononoke-config-path "$TESTTMP/mononoke-config" "$@" } function setup_no_ssl_apiserver { APISERVER_PORT=$(get_free_socket) no_ssl_apiserver --http-host "127.0.0.1" --http-port "$APISERVER_PORT" wait_for_apiserver --no-ssl } function apiserver { $MONONOKE_APISERVER "$@" --mononoke-config-path "$TESTTMP/mononoke-config" \ --ssl-ca "$TESTDIR/testcert.crt" \ --ssl-private-key "$TESTDIR/testcert.key" \ --ssl-certificate "$TESTDIR/testcert.crt" \ --ssl-ticket-seeds "$TESTDIR/server.pem.seeds" \ --do-not-init-cachelib >> "$TESTTMP/apiserver.out" 2>&1 & export APISERVER_PID=$! echo "$APISERVER_PID" >> "$DAEMON_PIDS" } function no_ssl_apiserver { $MONONOKE_APISERVER "$@" \ --mononoke-config-path "$TESTTMP/mononoke-config" >> "$TESTTMP/apiserver.out" 2>&1 & echo $! >> "$DAEMON_PIDS" } function wait_for_apiserver { for _ in $(seq 1 200); do if [[ -a "$TESTTMP/apiserver.out" ]]; then PORT=$(grep "Listening to" < "$TESTTMP/apiserver.out" | grep -Pzo "(\\d+)\$") && break fi sleep 0.1 done if [[ -z "$PORT" ]]; then echo "error: Mononoke API Server is not started" cat "$TESTTMP/apiserver.out" exit 1 fi export APIHOST="localhost:$PORT" export APISERVER APISERVER="https://localhost:$PORT" if [[ ($# -eq 1 && "$1" == "--no-ssl") ]]; then APISERVER="http://localhost:$PORT" fi } function extract_json_error { input=$(cat) echo "$input" | head -1 | jq -r '.message' echo "$input" | tail -n +2 } # Run an hg binary configured with the settings required to talk to Mononoke. function hgmn { hg --config ui.ssh="$DUMMYSSH" --config paths.default=ssh://user@dummy/repo --config ui.remotecmd="$MONONOKE_HGCLI" "$@" } function hgmn_show { echo "LOG $*" hgmn log --template 'node:\t{node}\np1node:\t{p1node}\np2node:\t{p2node}\nauthor:\t{author}\ndate:\t{date}\ndesc:\t{desc}\n\n{diff()}' -r "$@" hgmn update "$@" echo echo "CONTENT $*" find . -type f -not -path "./.hg/*" -print -exec cat {} \; } function hginit_treemanifest() { hg init "$@" cat >> "$1"/.hg/hgrc <> "$2"/.hg/hgrc <> .hg/hgrc <> .hg/hgrc <> .hg/hgrc <> repos/repo/server.toml <> "$HGRCPATH" <> .hg/hgrc <> .hg/hgrc < "$1" hg add "$1" hg ci -m "$1" } function pushrebase_replay() { DB_INDICES=$1 REPLAY_CA_PEM="$TESTDIR/testcert.crt" \ THRIFT_TLS_CL_CERT_PATH="$TESTDIR/testcert.crt" \ THRIFT_TLS_CL_KEY_PATH="$TESTDIR/testcert.key" $PUSHREBASE_REPLAY \ --mononoke-config-path "$TESTTMP/mononoke-config" \ --reponame repo \ --hgcli "$MONONOKE_HGCLI" \ --mononoke-admin "$MONONOKE_ADMIN" \ --mononoke-address "[::1]:$MONONOKE_SOCKET" \ --mononoke-server-common-name localhost \ --db-indices "$DB_INDICES" \ --repoid 0 \ --bundle-provider filesystem \ --filesystem-bundles-storage-path "$TESTTMP" \ --sqlite3-path "$TESTTMP/pushrebaserecording" }