Skip to content

Commit a356305

Browse files
committed
Move unit tests to GHA
Not all uses are now implemented, the deprications is still missing for example. Renamed the current job which checked if our codecov was out of date to fix tab completion.
1 parent 301dcec commit a356305

File tree

5 files changed

+299
-6
lines changed

5 files changed

+299
-6
lines changed

.github/jobs/base.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/bash
2+
3+
set -x
4+
5+
. .github/jobs/data/gha_ci_bashrc
6+
7+
lsb_release -a
8+
9+
cat > ~/.my.cnf <<EOF
10+
[client]
11+
host=sqlserver
12+
user=root
13+
password=domjudge
14+
EOF
15+
cat ~/.my.cnf
16+
17+
# FIXME: This chicken-egg problem is annoying but let us bootstrap for now.
18+
echo "CREATE DATABASE IF NOT EXISTS \`domjudge\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" | mysql
19+
echo "CREATE USER 'domjudge'@'%' IDENTIFIED BY 'domjudge';" | mysql
20+
echo "GRANT SELECT, INSERT, UPDATE, DELETE ON \`domjudge\`.* TO 'domjudge'@'%';" | mysql
21+
22+
# Increase max_allowed_packet for following connections.
23+
echo "SET GLOBAL max_allowed_packet = 100*1024*1024;" | mysql
24+
25+
# Test that SQL upgrade scripts also work with this setting
26+
if [ -n "${MYSQL_REQUIRE_PRIMARY_KEY:-}" ]; then
27+
echo 'SET GLOBAL sql_require_primary_key = 1;' | mysql
28+
fi
29+
30+
# Generate a dbpasswords file
31+
echo "unused:sqlserver:domjudge:domjudge:domjudge:3306" > etc/dbpasswords.secret
32+
33+
# Generate APP_SECRET for symfony
34+
# shellcheck disable=SC2164
35+
( cd etc ; ./gensymfonysecret > symfony_app.secret )
36+
37+
cat > webapp/config/static.yaml <<EOF
38+
parameters:
39+
domjudge.version: unconfigured
40+
domjudge.bindir: /bin
41+
domjudge.etcdir: /etc
42+
domjudge.wwwdir: /www
43+
domjudge.webappdir: /webapp
44+
domjudge.libdir: /lib
45+
domjudge.sqldir: /sql
46+
domjudge.libvendordir: /lib/vendor
47+
domjudge.logdir: /output/log
48+
domjudge.rundir: /output/run
49+
domjudge.tmpdir: /output/tmp
50+
domjudge.baseurl: http://localhost/domjudge
51+
EOF
52+
53+
# install all php dependencies
54+
export APP_ENV="prod"
55+
composer install --no-scripts
56+
echo -e "\033[0m"
57+
58+
# configure, make and install (but skip documentation)
59+
make configure
60+
./configure --with-baseurl='http://localhost/domjudge/' --with-domjudge-user=domjudge --with-judgehost_chrootdir=${DIR}/chroot/domjudge |& tee "$ARTIFACTS/configure.log"
61+
make build-scripts domserver judgehost docs |& tee "$ARTIFACTS/make.log"
62+
sudo make install-domserver install-judgehost install-docs |& tee -a "$ARTIFACTS/make.log"
63+
64+
# setup database and add special user
65+
# shellcheck disable=SC2164
66+
cd /opt/domjudge/domserver
67+
setfacl -m u:www-data:r etc/restapi.secret etc/initial_admin_password.secret \
68+
etc/dbpasswords.secret etc/symfony_app.secret
69+
70+
# configure and restart nginx
71+
sudo rm -f /etc/nginx/sites-enabled/*
72+
sudo cp /opt/domjudge/domserver/etc/nginx-conf /etc/nginx/sites-enabled/domjudge
73+
sudo /usr/sbin/nginx
74+
75+
# configure and restart php-fpm
76+
# shellcheck disable=SC2154
77+
php_version="${version:-}"
78+
sudo cp /opt/domjudge/domserver/etc/domjudge-fpm.conf "/etc/php/$php_version/fpm/pool.d/domjudge-fpm.conf"
79+
echo "php_admin_value[date.timezone] = Europe/Amsterdam" | sudo tee -a "/etc/php/$php_version/fpm/pool.d/domjudge-fpm.conf"
80+
sudo /usr/sbin/php-fpm${php_version}
81+
82+
83+
passwd=$(cat etc/initial_admin_password.secret)
84+
echo "machine localhost login admin password $passwd" >> ~www-data/.netrc
85+
sudo -u www-data bin/dj_setup_database -uroot -pdomjudge bare-install
86+
87+
# shellcheck disable=SC2154
88+
if [ -n "${integration:-}" ]; then
89+
# Make sure admin has a team associated to insert submissions as well.
90+
echo "UPDATE user SET teamid=1 WHERE userid=1;" | mysql domjudge
91+
fi
92+
93+
sudo -u www-data bin/dj_setup_database -uroot -pdomjudge install-examples

.github/jobs/data/gha_ci_bashrc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# Expand aliases for non-interactive shell
4+
shopt -s expand_aliases
5+
6+
# Fail pipeline when variable is not set or individual command has an non-zero exitcode.
7+
set -euo pipefail
8+
9+
# Show which commands are being run
10+
set -x
11+
12+
# Chown the checked out files
13+
sudo chown -R domjudge:domjudge .
14+
15+
# Shared constants between jobs
16+
export DIR=$(pwd)
17+
export GITSHA=$(git rev-parse HEAD || true)
18+
export PS4='(${BASH_SOURCE}:${LINENO}): - [$?] $ '
19+
export LOGFILE="/opt/domjudge/domserver/webapp/var/log/prod.log"
20+
21+
# Functions to annotate the Github actions logs
22+
alias trace_on='set -x'
23+
alias trace_off='{ set +x; } 2>/dev/null'
24+
25+
section_start_internal () {
26+
echo "::group::$1"
27+
trace_on
28+
}
29+
30+
section_end_internal () {
31+
echo "::endgroup::"
32+
trace_on
33+
}
34+
35+
alias section_start='trace_off ; section_start_internal '
36+
alias section_end='trace_off ; section_end_internal '
37+
38+
# Shared storage for all artifacts
39+
export ARTIFACTS="$DIR/artifacts"
40+
mkdir -p "$ARTIFACTS"
41+
42+
function show_phpinfo() {
43+
phpversion=$1
44+
section_start phpinfo
45+
update-alternatives --set php /usr/bin/php"${phpversion}"
46+
php -v
47+
php -m
48+
section_end phpinfo
49+
}
50+
51+
function log_on_err() {
52+
echo -e "\\n\\n=======================================================\\n"
53+
echo "Symfony log:"
54+
if sudo test -f "$LOGFILE" ; then
55+
sudo cat "$LOGFILE"
56+
fi
57+
}
58+
59+
set -eux

.github/jobs/unit-tests.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
3+
. .github/jobs/data/gha_ci_bashrc
4+
5+
section_start chown_checkout
6+
git config --global --add safe.directory /__w/domjudge/domjudge
7+
section_end chown_checkout
8+
9+
section_start current_database_domjudge "Currently installed databases (domjudge)"
10+
set +eu
11+
echo "show databases" | mysql -hsqlserveranother -udomjudge -pdomjudge
12+
echo "show databases" | mysql -hsqlserver -udomjudge -pdomjudge
13+
set -eu
14+
section_end current_database_domjudge
15+
16+
section_start current_database_root "Currently installed databases (root)"
17+
set +eu
18+
echo "show databases" | mysql -hsqlserver -uroot -pdomjudge
19+
set -eu
20+
section_end current_database_root
21+
22+
export version=$1
23+
unittest=$2
24+
[ "$version" = "8.1" ] && CODECOVERAGE=1 || CODECOVERAGE=0
25+
26+
show_phpinfo $version
27+
28+
.github/jobs/base.sh
29+
30+
# Add team to admin user
31+
echo "INSERT INTO userrole (userid, roleid) VALUES (1, 3);" | mysql domjudge
32+
echo "UPDATE user SET teamid = 1 WHERE userid = 1;" | mysql domjudge
33+
34+
# Copy the .env.test file, as this is normally not done during
35+
# installation and we need it.
36+
cp webapp/.env.test /opt/domjudge/domserver/webapp/
37+
38+
# We also need the composer.json for PHPunit to detect the correct directory.
39+
cp composer.json /opt/domjudge/domserver/
40+
41+
cd /opt/domjudge/domserver
42+
43+
export APP_ENV="test"
44+
45+
# Run phpunit tests.
46+
pcov=""
47+
phpcov=""
48+
if [ "$CODECOVERAGE" -eq 1 ]; then
49+
phpcov="-dpcov.enabled=1 -dpcov.directory=webapp/src"
50+
pcov="--coverage-html=${PWD}/coverage-html --coverage-clover coverage.xml"
51+
fi
52+
set +e
53+
php $phpcov lib/vendor/bin/phpunit -c webapp/phpunit.xml.dist webapp/tests/$unittest --log-junit ${DIR}/unit-tests.xml --colors=never $pcov > "$ARTIFACTS"/phpunit.out
54+
UNITSUCCESS=$?
55+
set -e
56+
CNT=0
57+
if [ $CODECOVERAGE -eq 1 ]; then
58+
CNT=$(sed -n '/Generating code coverage report/,$p' "$ARTIFACTS"/phpunit.out | grep -v DoctrineTestBundle | grep -cv ^$)
59+
FILE=deprecation.txt
60+
sed -n '/Generating code coverage report/,$p' "$ARTIFACTS"/phpunit.out > "$DIR/$FILE"
61+
if [ $CNT -le 12 ]; then
62+
STATE=success
63+
else
64+
STATE=failure
65+
fi
66+
ORIGINAL="gitlab.com/DOMjudge"
67+
REPLACETO="domjudge.gitlab.io/-"
68+
# Copied from CCS
69+
#curl https://api.github.com/repos/domjudge/domjudge/statuses/$CI_COMMIT_SHA \
70+
# -X POST \
71+
# -H "Authorization: token $GH_BOT_TOKEN_OBSCURED" \
72+
# -H "Accept: application/vnd.github.v3+json" \
73+
# -d "{\"state\": \"$STATE\", \"target_url\": \"${CI_JOB_URL/$ORIGINAL/$REPLACETO}/artifacts/$FILE\", \"description\":\"Symfony deprecations\", \"context\": \"Symfony deprecation\"}"
74+
fi
75+
if [ $UNITSUCCESS -ne 0 ]; then
76+
exit $UNITSUCCESS
77+
fi
78+
79+
if [ $CODECOVERAGE -eq 1 ]; then
80+
section_start uploadcoverage "Upload code coverage"
81+
# Only upload when we got working unit-tests.
82+
set +u # Uses some variables which are not set
83+
# shellcheck disable=SC1090
84+
. $DIR/.github/jobs/uploadcodecov.sh &>/dev/zero
85+
set -u # Undo set dance
86+
section_end uploadcoverage
87+
fi
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Unit tests (Codecov script)
2+
on:
3+
push:
4+
branches:
5+
- main
6+
- '[0-9]+.[0-9]+'
7+
pull_request:
8+
branches:
9+
- main
10+
- '[0-9]+.[0-9]+'
11+
12+
jobs:
13+
check-static-codecov:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Download latest codecov upload script
18+
run: wget https://codecov.io/bash -O newcodecov
19+
- name: Detect changes to manually verify
20+
run: diff newcodecov .github/jobs/uploadcodecov.sh
21+

.github/workflows/unit-tests.yml

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Unit tests
1+
name: Unit tests CI
22
on:
33
push:
44
branches:
@@ -10,12 +10,45 @@ on:
1010
- '[0-9]+.[0-9]+'
1111

1212
jobs:
13-
check-static-codecov:
13+
run-unit-tests:
1414
runs-on: ubuntu-latest
15+
container: domjudge/gitlabci:2.1
16+
defaults:
17+
run:
18+
shell: bash
19+
strategy:
20+
matrix:
21+
sqlserver: ['mariadb']
22+
phpversion: ['7.4','8.0','8.1','8.2']
23+
testtype: ["Unit","E2E"]
24+
include:
25+
- phpversion: "8.1"
26+
sqlserver: "mysql"
27+
testtype: "Unit"
28+
- phpversion: "8.1"
29+
sqlserver: "mysql"
30+
testtype: "E2E"
31+
services:
32+
sqlserver:
33+
image: "${{ matrix.sqlserver }}"
34+
env:
35+
MARIADB_ROOT_PASSWORD: domjudge
1536
steps:
1637
- uses: actions/checkout@v2
17-
- name: Download latest codecov upload script
18-
run: wget https://codecov.io/bash -O newcodecov
19-
- name: Detect changes to manually verify
20-
run: diff newcodecov .github/jobs/uploadcodecov.sh
38+
- name: Show current directory
39+
run: pwd
40+
- name: Run gitlab script in altered form
41+
run: .github/jobs/unit-tests.sh "${{ matrix.phpversion }}" "${{ matrix.testtype }}"
42+
- name: Publish Test Results
43+
uses: EnricoMi/publish-unit-test-result-action@v2
44+
if: always()
45+
with:
46+
comment_mode: off
47+
files: |
48+
unit-tests.xml
49+
- uses: actions/upload-artifact@v3
50+
with:
51+
name: ci-run-logs-configs
52+
path: |
53+
"artifacts"
2154

0 commit comments

Comments
 (0)