Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last gitlab ci #2825

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions .github/jobs/baseinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

export version="$1"
db=${2:-install}
${MYSQL_ROOT_PASSWORD}=${MYSQL_ROOT_PASSWORD:-root}

# If this script is called from unit-tests.sh, we use the test environment
export APP_ENV="${3:-prod}"

# In the test environment, we need to use a different database
[ "$APP_ENV" = "prod" ] && DATABASE_NAME=domjudge || DATABASE_NAME=domjudge_test

set -eux

Expand Down Expand Up @@ -42,40 +49,39 @@ else
make domserver
make install-domserver
fi

section_end

section_start "SQL settings"
cat > ~/.my.cnf <<EOF
[client]
host=sqlserver
user=root
password=root
password=${MYSQL_ROOT_PASSWORD}
EOF
cat ~/.my.cnf

mysql_root "CREATE DATABASE IF NOT EXISTS \`domjudge\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql_root "CREATE USER IF NOT EXISTS \`domjudge\`@'%' IDENTIFIED BY 'domjudge';"
mysql_root "GRANT SELECT, INSERT, UPDATE, DELETE ON \`domjudge\`.* TO 'domjudge'@'%';"
mysql_root "CREATE DATABASE IF NOT EXISTS \`$DATABASE_NAME\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql_root "CREATE USER IF NOT EXISTS \`$DATABASE_NAME\`@'%' IDENTIFIED BY 'domjudge';"
mysql_root "GRANT SELECT, INSERT, UPDATE, DELETE ON \`$DATABASE_NAME\`.* TO 'domjudge'@'%';"
mysql_root "FLUSH PRIVILEGES;"

# Show some MySQL debugging
mysql_root "show databases"
mysql_root "SELECT CURRENT_USER();"
mysql_root "SELECT USER();"
mysql_root "SELECT user,host FROM mysql.user"
echo "unused:sqlserver:domjudge:domjudge:domjudge:3306" > /opt/domjudge/domserver/etc/dbpasswords.secret
echo "unused:sqlserver:domjudge:domjudge:$DATABASE_NAME:3306" > /opt/domjudge/domserver/etc/dbpasswords.secret
mysql_user "SELECT CURRENT_USER();"
mysql_user "SELECT USER();"
section_end

if [ "${db}" = "install" ]; then
section_start "Install DOMjudge database"
/opt/domjudge/domserver/bin/dj_setup_database -uroot -proot bare-install
/opt/domjudge/domserver/bin/dj_setup_database -uroot -p${MYSQL_ROOT_PASSWORD} bare-install
section_end
elif [ "${db}" = "upgrade" ]; then
section_start "Upgrade DOMjudge database"
/opt/domjudge/domserver/bin/dj_setup_database -uroot -proot upgrade
/opt/domjudge/domserver/bin/dj_setup_database -uroot -p${MYSQL_ROOT_PASSWORD} upgrade
section_end
fi

Expand Down Expand Up @@ -113,30 +119,33 @@ section_end

if [ "${db}" = "install" ]; then
section_start "Install the example data"
/opt/domjudge/domserver/bin/dj_setup_database -uroot -proot install-examples | tee -a "$ARTIFACTS/mysql.txt"
if [ "$version" = "unit" ]; then
# Make sure admin has no team associated so we will not insert submissions during unit tests.
mysql_root "UPDATE user SET teamid=null WHERE userid=1;" $DATABASE_NAME
fi
/opt/domjudge/domserver/bin/dj_setup_database -uroot -p${MYSQL_ROOT_PASSWORD} install-examples | tee -a "$ARTIFACTS/mysql.txt"
section_end
fi

section_start "Setup user"
# We're using the admin user in all possible roles
mysql_root "DELETE FROM userrole WHERE userid=1;" domjudge
mysql_root "DELETE FROM userrole WHERE userid=1;" $DATABASE_NAME
if [ "$version" = "team" ]; then
# Add team to admin user
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 3);" domjudge
mysql_root "UPDATE user SET teamid = 1 WHERE userid = 1;" domjudge
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 3);" $DATABASE_NAME
mysql_root "UPDATE user SET teamid = 1 WHERE userid = 1;" $DATABASE_NAME
elif [ "$version" = "jury" ]; then
# Add jury to admin user
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 2);" domjudge
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 2);" $DATABASE_NAME
elif [ "$version" = "balloon" ]; then
# Add balloon to admin user
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 4);" domjudge
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 4);" $DATABASE_NAME
elif [ "$version" = "admin" ]; then
# Add admin to admin user
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 1);" domjudge
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 1);" $DATABASE_NAME
elif [ "$version" = "all" ]; then
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 1);" domjudge
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 3);" domjudge
mysql_root "UPDATE user SET teamid = 1 WHERE userid = 1;" domjudge
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 1);" $DATABASE_NAME
mysql_root "INSERT INTO userrole (userid, roleid) VALUES (1, 3);" $DATABASE_NAME
mysql_root "UPDATE user SET teamid = 1 WHERE userid = 1;" $DATABASE_NAME
fi
section_end

61 changes: 61 additions & 0 deletions .github/jobs/unit-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

. .github/jobs/ci_settings.sh

DIR="$PWD"

export version=$1
unittest=$2
[ "$version" = "8.1" ] && CODECOVERAGE=1 || CODECOVERAGE=0

show_phpinfo $version

# Set up
export unit=1

"$( dirname "${BASH_SOURCE[0]}" )"/baseinstall.sh unit install test

# Add team to admin user
echo "UPDATE user SET teamid = 1 WHERE userid = 1;" | mysql domjudge_test

# Copy the .env.test file, as this is normally not done during
# installation and we need it.
cp webapp/.env.test /opt/domjudge/domserver/webapp/

# We also need the composer.json for PHPunit to detect the correct directory.
cp webapp/composer.json /opt/domjudge/domserver/webapp/

cd /opt/domjudge/domserver

# Run phpunit tests.
pcov=""
phpcov=""
if [ "$CODECOVERAGE" -eq 1 ]; then
phpcov="-dpcov.enabled=1 -dpcov.directory=webapp/src"
pcov="--coverage-html=${DIR}/coverage-html --coverage-clover coverage.xml"
fi
set +e
php $phpcov webapp/bin/phpunit -c webapp/phpunit.xml.dist webapp/tests/$unittest --log-junit ${DIR}/unit-tests.xml --colors=never $pcov > "$ARTIFACTS"/phpunit.out
UNITSUCCESS=$?
set -e

if [ $CODECOVERAGE -eq 1 ]; then
CNT=$(sed -n '/Generating code coverage report/,$p' "$ARTIFACTS"/phpunit.out | grep -v DoctrineTestBundle | grep -cv ^$)
if [ $CNT -gt 32 ]; then
UNITSUCCESS=1
fi
fi
cp webapp/var/log/test.log "$ARTIFACTS"/test.log

if [ $UNITSUCCESS -ne 0 ]; then
exit 1
fi

if [ $CODECOVERAGE -eq 1 ]; then
section_start "Upload code coverage"
# Only upload when we got working unit-tests.
set +u # Uses some variables which are not set
# shellcheck disable=SC1090
. $DIR/.github/jobs/uploadcodecov.sh 1>/dev/zero 2>/dev/zero
section_end
fi
43 changes: 41 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
name: Unit tests
# We can speedup with: https://github.com/actions/cache
on:
push:
merge_group:
pull_request:
branches:
- main
- '[0-9]+.[0-9]+'

jobs:
check-static-codecov:
unit-tests:
services:
sqlserver:
image: ${{ matrix.SQL }}
ports:
- 3306:3306
env:
MYSQL_ROOT_PASSWORD: root
options: ${{ matrix.SQL_OPTIONS }}
container:
image: domjudge/gitlabci:24.04
strategy:
matrix:
PHPVERSION: [8.1]
TEST: [UNIT,E2E]
CRAWL_SHADOW_MODE: [0]
SQL_OPTIONS: --health-cmd="healthcheck.sh --connect --innodb_initialized" --health-interval=10s --health-timeout=5s --health-retries=3
include:
- PHPVERSION: 8.4
TEST: UNIT
SQL: mysql
SQL_OPTIONS: --mysql-native-password --authentication_policy=mysql_native_password
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Download latest codecov upload script
run: wget https://codecov.io/bash -O newcodecov
- name: Detect changes to manually verify
run: diff newcodecov .github/jobs/uploadcodecov.sh

- name: Run the unit tests
run: .github/jobs/unit-tests.sh ${{ matrix.PHPVERSION }} ${{ matrix.TEST }}
#- name: Download latest codecov upload script
# run: wget https://codecov.io/bash -O newcodecov
#- name: Detect changes to manually verify
# run: diff newcodecov .github/jobs/uploadcodecov.sh
#- name: Publish Test Results
# uses: EnricoMi/publish-unit-test-result-action@v2
# if: always()
# with:
# files: |
# test-results/**/*.xml
- name: Upload artifact for debugging
uses: actions/upload-artifact@v3
with:
name: unit-test-output
path: /tmp/artifacts
1 change: 0 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
include:
- '/gitlab/ci/unit.yml'
- '/gitlab/ci/template.yml'

stages:
Expand Down
19 changes: 0 additions & 19 deletions gitlab/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ export APP_ENV="${1:-prod}"

lsb_release -a

cat > ~/.my.cnf <<EOF
[client]
host=sqlserver
user=root
password=${MYSQL_ROOT_PASSWORD}
EOF
cat ~/.my.cnf

# FIXME: This chicken-egg problem is annoying but let us bootstrap for now.
echo "CREATE DATABASE IF NOT EXISTS \`${DATABASE_NAME}\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" | mysql
echo "CREATE USER 'domjudge'@'%' IDENTIFIED BY 'domjudge';" | mysql
Expand Down Expand Up @@ -62,17 +54,6 @@ set +e
composer install --no-scripts || rm -rf vendor
set -e

# install all php dependencies
composer install --no-scripts
echo -e "\033[0m"
cd $DIR

# configure, make and install (but skip documentation)
make configure
./configure --with-baseurl='http://localhost/domjudge/' --with-domjudge-user=domjudge --with-judgehost_chrootdir=${DIR}/chroot/domjudge |& tee "$GITLABARTIFACTS/configure.log"
make build-scripts domserver judgehost docs |& tee "$GITLABARTIFACTS/make.log"
sudo make install-domserver install-judgehost install-docs |& tee -a "$GITLABARTIFACTS/make.log"

# setup database and add special user
# shellcheck disable=SC2164
cd /opt/domjudge/domserver
Expand Down
51 changes: 0 additions & 51 deletions gitlab/ci/unit.yml

This file was deleted.

Loading
Loading