<commit_before>#!/bin/bash
# Runs the acceptance tests in a VM created using Docker
# The results of the acceptance tests will be in JUnit XML format in local `ontrack-acceptance.xml` file

# Help function
function show_help {
	echo "Ontrack acceptance tests using Docker."
	echo ""
	echo "Available options are:"
	echo "General:"
	echo "    -h, --help                    Displays this help"
	echo "Input:"
	echo "    -j, --jar                     (* required) Path to the ontrack JAR"
	echo "    -a, --acceptance              (* required) Path to the acceptance test JAR"
	echo "    -h, --host                    Address of the Docker container (defaults to 'localhost' or IP in 'DOCKER_HOST')"
	echo "    --dry-run                     Does not run the acceptance tests"
	echo "Control:"
	echo "    -k, --keep                    If set, the container is not destroyed after"
	echo "    -d, --delay                   Number of seconds to wait for Ontrack to start (defaults to 120)"
	echo "    -u, --docker-user=<user>      Docker user UID or name to use (defaults to none, can be computed using 'id -u <user>')"
}

# Check function

function check {
	if [ "$1" == "" ]
	then
		echo $2
		exit 1
	fi
}

# Defaults

if [ "${DOCKER_HOST}" == "" ]
then
    ONTRACK_HOST=localhost
else
    ONTRACK_HOST=`echo ${DOCKER_HOST} | sed -E 's/.*\/([0-9\.]+):.*/\1/'`
fi

# TODO Supports https: through nginx
ONTRACK_PROTOCOL=http
ONTRACK_JAR=
ONTRACK_ACCEPTANCE_JAR=

CONTROL_KEEP=no
CONTROL_DELAY=120
CONTROL_USER=
CONTROL_DRY_RUN=no

# Command central

for i in "$@"
do
	case $i in
		-h|--help)
			show_help
			exit 0
			;;
		-h=*|--host=*)
            ONTRACK_HOST=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
			;;
		-j=*|--jar=*)
            ONTRACK_JAR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
			;;
		-a=*|--acceptance=*)
            ONTRACK_ACCEPTANCE_JAR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
			;;
		-d=*|--delay=*)
            CONTROL_DELAY=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
			;;
		-k|--keep)
            CONTROL_KEEP=yes
			;;
		--dry-run)
            CONTROL_DRY_RUN=yes
			;;
		-u=*|--docker-user=*)
            CONTROL_USER=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
			;;
		*)
			echo "Unknown option: $i"
			show_help
			exit 1
		;;
	esac
done

# Checks

check "$ONTRACK_JAR" "Ontrack JAR (--jar) is required."
check "$ONTRACK_ACCEPTANCE_JAR" "Ontrack Acceptance JAR (--acceptance) is required."

# Logging
echo "Docker host:            ${ONTRACK_HOST}"
echo "Docker user:            ${CONTROL_USER}"
echo "Ontrack protocol:       ${ONTRACK_PROTOCOL}"
echo "Ontrack JAR:            ${ONTRACK_JAR}"
echo "Startup delay:          ${CONTROL_DELAY} s"
echo "Keeping containers:     ${CONTROL_KEEP}"
echo "Dry run:                ${CONTROL_DRY_RUN}"

# Mount point

MOUNT=`pwd`/acceptance
rm -f ${MOUNT}
mkdir -p ${MOUNT}
echo "Ontrack data at:        ${MOUNT}"

# Docker Ontrack VM up
# TODO Docker nginx VM up

DOCKER_OPTIONS=
if [ "${CONTROL_USER}" != "" ]
then
    DOCKER_OPTIONS="${DOCKER_OPTIONS} --docker-user=${CONTROL_USER}"
fi
echo "Docker options:         ${DOCKER_OPTIONS}"

# Ontrack container

echo "Starting the Ontrack container"

./docker-setup.sh \
    --docker-image=ontrack \
    --mount=${MOUNT} \
    ${DOCKER_OPTIONS} \
    --run \
    --jar=${ONTRACK_JAR}

ONTRACK_CID=`cat ontrack.cid`

echo "[ACCEPTANCE] Ontrack container created: ${ONTRACK_CID}"

# nginx container

echo "[ACCEPTANCE] Preparation of the nginx image"

# Generation of the nginx configuration and generation of self-signed certificates
docker-nginx/nginx.sh \
	--target=`pwd`/docker-nginx/build \
	--host=ontrack \
	--port=8080 \
	--name=ontrack \
	--cert-subject="/C=BE/L=Brussel/CN=ontrack"


# Generation of the Nginx image
echo "[ACCEPTANCE] Building the nginx image..."
docker build -t="ontrack-nginx" docker-nginx/

# Creating the Nginx container
echo "[ACCEPTANCE] Starting the nginx container..."
docker run -d -P --cidfile=nginx.cid ontrack-nginx
NGINX_CID=`cat nginx.cid`
echo "[ACCEPTANCE] Nginx container created: ${NGINX_CID}"

# Getting the public facing port
<commit_msg>#141 Getting the Ontrack container unique name<commit_after>#!/bin/bash
# Runs the acceptance tests in a VM created using Docker
# The results of the acceptance tests will be in JUnit XML format in local `ontrack-acceptance.xml` file

# Help function
function show_help {
	echo "Ontrack acceptance tests using Docker."
	echo ""
	echo "Available options are:"
	echo "General:"
	echo "    -h, --help                    Displays this help"
	echo "Input:"
	echo "    -j, --jar                     (* required) Path to the ontrack JAR"
	echo "    -a, --acceptance              (* required) Path to the acceptance test JAR"
	echo "    -h, --host                    Address of the Docker container (defaults to 'localhost' or IP in 'DOCKER_HOST')"
	echo "    --dry-run                     Does not run the acceptance tests"
	echo "Control:"
	echo "    -k, --keep                    If set, the container is not destroyed after"
	echo "    -d, --delay                   Number of seconds to wait for Ontrack to start (defaults to 120)"
	echo "    -u, --docker-user=<user>      Docker user UID or name to use (defaults to none, can be computed using 'id -u <user>')"
}

# Check function

function check {
	if [ "$1" == "" ]
	then
		echo $2
		exit 1
	fi
}

# Defaults

if [ "${DOCKER_HOST}" == "" ]
then
    ONTRACK_HOST=localhost
else
    ONTRACK_HOST=`echo ${DOCKER_HOST} | sed -E 's/.*\/([0-9\.]+):.*/\1/'`
fi

# TODO Supports https: through nginx
ONTRACK_PROTOCOL=http
ONTRACK_JAR=
ONTRACK_ACCEPTANCE_JAR=

CONTROL_KEEP=no
CONTROL_DELAY=120
CONTROL_USER=
CONTROL_DRY_RUN=no

# Command central

for i in "$@"
do
	case $i in
		-h|--help)
			show_help
			exit 0
			;;
		-h=*|--host=*)
            ONTRACK_HOST=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
			;;
		-j=*|--jar=*)
            ONTRACK_JAR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
			;;
		-a=*|--acceptance=*)
            ONTRACK_ACCEPTANCE_JAR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
			;;
		-d=*|--delay=*)
            CONTROL_DELAY=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
			;;
		-k|--keep)
            CONTROL_KEEP=yes
			;;
		--dry-run)
            CONTROL_DRY_RUN=yes
			;;
		-u=*|--docker-user=*)
            CONTROL_USER=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
			;;
		*)
			echo "Unknown option: $i"
			show_help
			exit 1
		;;
	esac
done

# Checks

check "$ONTRACK_JAR" "Ontrack JAR (--jar) is required."
check "$ONTRACK_ACCEPTANCE_JAR" "Ontrack Acceptance JAR (--acceptance) is required."

# Logging
echo "Docker host:            ${ONTRACK_HOST}"
echo "Docker user:            ${CONTROL_USER}"
echo "Ontrack protocol:       ${ONTRACK_PROTOCOL}"
echo "Ontrack JAR:            ${ONTRACK_JAR}"
echo "Startup delay:          ${CONTROL_DELAY} s"
echo "Keeping containers:     ${CONTROL_KEEP}"
echo "Dry run:                ${CONTROL_DRY_RUN}"

# Mount point

MOUNT=`pwd`/acceptance
rm -f ${MOUNT}
mkdir -p ${MOUNT}
echo "Ontrack data at:        ${MOUNT}"

# Docker Ontrack VM up
# TODO Docker nginx VM up

DOCKER_OPTIONS=
if [ "${CONTROL_USER}" != "" ]
then
    DOCKER_OPTIONS="${DOCKER_OPTIONS} --docker-user=${CONTROL_USER}"
fi
echo "Docker options:         ${DOCKER_OPTIONS}"

# Ontrack container

echo "Starting the Ontrack container"
# TODO Do not expose Ontrack port
./docker-setup.sh \
    --docker-image=ontrack \
    --mount=${MOUNT} \
    ${DOCKER_OPTIONS} \
    --run \
    --jar=${ONTRACK_JAR}

ONTRACK_CID=`cat ontrack.cid`
ONTRACK_NAME=`docker inspect -f "{{ .Name }}" ${ONTRACK_CID}`
ONTRACK_NAME="${ONTRACK_NAME:1}"

echo "[ACCEPTANCE] Ontrack container created: ${ONTRACK_CID} (${ONTRACK_NAME})"

# nginx container

echo "[ACCEPTANCE] Preparation of the nginx image"

# Generation of the nginx configuration and generation of self-signed certificates
docker-nginx/nginx.sh \
	--target=`pwd`/docker-nginx/build \
	--host=ontrack \
	--port=8080 \
	--name=ontrack \
	--cert-subject="/C=BE/L=Brussel/CN=ontrack"


# Generation of the Nginx image
echo "[ACCEPTANCE] Building the nginx image..."
docker build -t="ontrack-nginx" docker-nginx/

# Creating the Nginx container
echo "[ACCEPTANCE] Starting the nginx container..."
docker run -d -P  --cidfile=nginx.cid ontrack-nginx
NGINX_CID=`cat nginx.cid`
echo "[ACCEPTANCE] Nginx container created: ${NGINX_CID}"

# Getting the public facing port

{'fraction_non_alphanumeric': 0.1480500722195474, 'fraction_numerical': 0.007221954742416947, 'mean_word_length': 2.674922600619195, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '914990', 'n_tokens_mistral': 3225, 'n_tokens_neox': 2997, 'n_words': 817}