Skip to content

Commit

Permalink
Add script to locally run tests suites on CI docker containers
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne authored and trasher committed Jun 21, 2021
1 parent f2945b4 commit 3d0ce14
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Expand Up @@ -10,7 +10,8 @@
"/public/lib/*",
"/public/build/*",
"/tests/config/*",
"/vendor/*"
"/vendor/*",
"**/*.min.js"
],
"env": {
"browser": true,
Expand Down
7 changes: 1 addition & 6 deletions .github/actions/init_containers-start.sh
@@ -1,10 +1,5 @@
#!/bin/bash -e

# Import variables from .env file if it exists
if [[ -f .env ]]; then
. .env
fi

echo "Init app container home"
mkdir -p $APP_CONTAINER_HOME

Expand All @@ -15,7 +10,7 @@ docker-compose start

if [[ "$UPDATE_FILES_ACL" = true ]]; then
echo "Change files rights to give write access to app container user"
sudo apt-get install acl
sudo apt-get install --assume-yes --no-install-recommends --quiet acl
setfacl --recursive --modify u:1000:rwx $APPLICATION_ROOT
setfacl --recursive --modify u:1000:rwx $APP_CONTAINER_HOME
fi
Expand Down
10 changes: 10 additions & 0 deletions .github/actions/init_initialize-imap-fixtures.sh
@@ -0,0 +1,10 @@
#!/bin/bash -e

ROOT_DIR=$(readlink -f "$(dirname $0)/../..")

echo "Initialize email fixtures"
docker-compose exec -T --user root dovecot doveadm expunge -u glpi mailbox 'INBOX' all
docker-compose exec -T --user root dovecot doveadm purge -u glpi
for f in `ls $ROOT_DIR/tests/emails-tests/*.eml`; do
cat $f | docker-compose exec -T --user glpi dovecot getmail_maildir /home/glpi/Maildir/
done
Expand Up @@ -12,10 +12,3 @@ done
for f in `ls $ROOT_DIR/tests/LDAP/ldif/*.ldif`; do
cat $f | docker-compose exec -T openldap ldapadd -x -H ldap://127.0.0.1:3890/ -D "cn=Manager,dc=glpi,dc=org" -w insecure
done

echo "Initialize email fixtures"
docker-compose exec -T --user root dovecot doveadm expunge -u glpi mailbox 'INBOX' all
docker-compose exec -T --user root dovecot doveadm purge -u glpi
for f in `ls $ROOT_DIR/tests/emails-tests/*.eml`; do
cat $f | docker-compose exec -T --user glpi dovecot getmail_maildir /home/glpi/Maildir/
done
1 change: 0 additions & 1 deletion .github/actions/lint_php-lint.sh
@@ -1,7 +1,6 @@
#!/bin/bash -e

ROOT_DIR=$(readlink -f "$(dirname $0)/../..")
echo $ROOT_DIR

echo "Check for syntax errors"
vendor/bin/parallel-lint \
Expand Down
11 changes: 7 additions & 4 deletions .github/actions/test_install.sh
@@ -1,13 +1,16 @@
#!/bin/bash -e

LOG_FILE="./tests/files/_log/install.log"
mkdir -p $(dirname "$LOG_FILE")

# Execute install
bin/console glpi:database:install \
--config-dir=./tests --ansi --no-interaction \
--reconfigure --db-name=glpi --db-host=db --db-user=root
--config-dir=./tests/config --ansi --no-interaction \
--reconfigure --db-name=glpi --db-host=db --db-user=root --force

# Execute update
## Should do nothing.
bin/console glpi:database:update --config-dir=./tests --ansi --no-interaction | tee migration.log
if [[ -z $(grep "No migration needed." migration.log) ]];
bin/console glpi:database:update --config-dir=./tests/config --ansi --no-interaction | tee $LOG_FILE
if [[ -z $(grep "No migration needed." $LOG_FILE) ]];
then echo "glpi:database:update command FAILED" && exit 1;
fi
32 changes: 18 additions & 14 deletions .github/actions/test_update-from-older-version.sh
@@ -1,48 +1,52 @@
#!/bin/bash -e

LOG_FILE="./tests/files/_log/migration.log"
mkdir -p $(dirname "$LOG_FILE")

# Reconfigure DB
bin/console glpi:database:configure \
--config-dir=./tests --ansi --no-interaction \
--config-dir=./tests/config --ansi --no-interaction \
--reconfigure --db-name=glpitest0723 --db-host=db --db-user=root

# Execute update
## First run should do the migration.
bin/console glpi:database:update --config-dir=./tests --ansi --no-interaction --allow-unstable | tee ~/migration.log
if [[ -n $(grep "No migration needed." ~/migration.log) ]];
bin/console glpi:database:update --config-dir=./tests/config --ansi --no-interaction --allow-unstable | tee $LOG_FILE
if [[ -n $(grep "No migration needed." $LOG_FILE) ]];
then echo "bin/console glpi:database:update command FAILED" && exit 1;
fi
## Second run should do nothing.
bin/console glpi:database:update --config-dir=./tests --ansi --no-interaction --allow-unstable | tee ~/migration.log
if [[ -z $(grep "No migration needed." ~/migration.log) ]];
bin/console glpi:database:update --config-dir=./tests/config --ansi --no-interaction --allow-unstable | tee $LOG_FILE
if [[ -z $(grep "No migration needed." $LOG_FILE) ]];
then echo "bin/console glpi:database:update command FAILED" && exit 1;
fi

# Execute myisam_to_innodb migration
## First run should do the migration.
bin/console glpi:migration:myisam_to_innodb --config-dir=./tests --ansi --no-interaction | tee ~/migration.log
if [[ -n $(grep "No migration needed." ~/migration.log) ]];
bin/console glpi:migration:myisam_to_innodb --config-dir=./tests/config --ansi --no-interaction | tee $LOG_FILE
if [[ -n $(grep "No migration needed." $LOG_FILE) ]];
then echo "bin/console glpi:migration:myisam_to_innodb command FAILED" && exit 1;
fi
## Second run should do nothing.
bin/console glpi:migration:myisam_to_innodb --config-dir=./tests --ansi --no-interaction | tee ~/migration.log
if [[ -z $(grep "No migration needed." ~/migration.log) ]];
bin/console glpi:migration:myisam_to_innodb --config-dir=./tests/config --ansi --no-interaction | tee $LOG_FILE
if [[ -z $(grep "No migration needed." $LOG_FILE) ]];
then echo "bin/console glpi:migration:myisam_to_innodb command FAILED" && exit 1;
fi

# Execute timestamps migration
## First run should do the migration.
bin/console glpi:migration:timestamps --config-dir=./tests --ansi --no-interaction | tee ~/migration.log
if [[ -n $(grep "No migration needed." ~/migration.log) ]];
bin/console glpi:migration:timestamps --config-dir=./tests/config --ansi --no-interaction | tee $LOG_FILE
if [[ -n $(grep "No migration needed." $LOG_FILE) ]];
then echo "bin/console glpi:migration:timestamps command FAILED" && exit 1;
fi
## Second run should do nothing.
bin/console glpi:migration:timestamps --config-dir=./tests --ansi --no-interaction | tee ~/migration.log
if [[ -z $(grep "No migration needed." ~/migration.log) ]];
bin/console glpi:migration:timestamps --config-dir=./tests/config --ansi --no-interaction | tee $LOG_FILE
if [[ -z $(grep "No migration needed." $LOG_FILE) ]];
then echo "bin/console glpi:migration:timestamps command FAILED" && exit 1;
fi

# Test that updated DB has same schema as newly installed DB
bin/console glpi:database:configure \
--config-dir=./tests --no-interaction \
--config-dir=./tests/config --no-interaction \
--reconfigure --db-name=glpi --db-host=db --db-user=root
vendor/bin/atoum \
-p 'php -d memory_limit=512M' \
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/ci.yml
Expand Up @@ -157,11 +157,6 @@ jobs:
if: env.skip != 'true'
run: |
.github/actions/init_show-versions.sh
- name: "Initialize fixtures"
if: env.skip != 'true'
run: |
.github/actions/init_initialize-old-dbs.sh
.github/actions/init_initialize-services-fixtures.sh
- name: "Install dependencies"
if: env.skip != 'true'
run: |
Expand All @@ -173,6 +168,7 @@ jobs:
- name: "Update DB tests"
if: env.skip != 'true'
run: |
.github/actions/init_initialize-old-dbs.sh
docker-compose exec -T app .github/actions/test_update-from-older-version.sh
- name: "Unit tests"
if: env.skip != 'true'
Expand All @@ -185,10 +181,12 @@ jobs:
- name: "LDAP tests"
if: env.skip != 'true'
run: |
.github/actions/init_initialize-ldap-fixtures.sh
docker-compose exec -T app .github/actions/test_tests-ldap.sh
- name: "IMAP tests"
if: env.skip != 'true'
run: |
.github/actions/init_initialize-imap-fixtures.sh
docker-compose exec -T app .github/actions/test_tests-imap.sh
- name: "WEB tests"
if: env.skip != 'true'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -20,6 +20,7 @@ phpunit.xml
/tests/code-coverage/
/tests/coverage-*/
/tests/files/
/tests/.env
/tests/glpicrypt.key
/config/based_config.php
/config/config.php
Expand Down
28 changes: 19 additions & 9 deletions tests/README.md
Expand Up @@ -22,17 +22,17 @@ Creating a dedicated database
-----------------------------

Use the **glpi:database:install** CLI command to create a new database,
only used for the test suite, using the `--config-dir=./tests` option:
only used for the test suite, using the `--config-dir=./tests/config` option:

```bash
$ bin/console glpi:database:install --config-dir=./tests --db-name=glpitests --db-user=root --db-password=xxxx
$ bin/console glpi:database:install --config-dir=./tests/config --db-name=glpitests --db-user=root --db-password=xxxx
Creating the database...
Saving configuration file...
Loading default schema...
Installation done.
```

The configuration file is saved as `tests/config_db.php`.
The configuration file is saved as `tests/config/config_db.php`.

The database is created using the default schema for current version.

Expand All @@ -43,14 +43,17 @@ If you need to recreate the database (e.g. for a new schema), you need to run
Changing database configuration
-------------------------------

Using the same database than the web application is not recommended. Use the `tests/config_db.php` file to adjust connection settings.
Using the same database than the web application is not recommended. Use the `tests/config/config_db.php` file to adjust connection settings.

Running the test suite
----------------------
Running the test suite on developpement env
-------------------------------------------

There are two directories for tests:
- `tests/units` for main core tests;
- `tests/api` for API tests.
There are multiple directories for tests:
- `tests/units` for unit tests;
- `tests/functionnal` for functionnal tests;
- `tests/imap` for Mail collector tests;
- `tests/LDAP` for LDAP connection tests;
- `tests/web` for API tests.

You can choose to run tests on a whole directory, on any file, or on any \<class::method>. You have to specify a bootstrap file each time:

Expand Down Expand Up @@ -85,3 +88,10 @@ Note that if you do not use the `-ncc` switch; coverage will be generated in the
On first run, additional data are loaded into the test database. On following run, this step is skipped. Note that if the test dataset version changes; you'll have to reset your database using the **CliInstall** script again.

Note: you may see a skipped tests regarding missing extension `event`; this is expected ;)

Running the test suite on containerized env
-------------------------------------------

If you want to execute tests in an environment similar to what is done by CI, you can use the `tests/run_tests.sh`.
This scripts requires both "docker" and "docker-compose" utilities to be installed.
Run `tests/run_tests.sh --help` for more information about its usage.
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Expand Up @@ -34,7 +34,7 @@
error_reporting(E_ALL);

define('GLPI_ROOT', __DIR__ . '/../');
define('GLPI_CONFIG_DIR', __DIR__);
define('GLPI_CONFIG_DIR', __DIR__ . '/config');
define('GLPI_VAR_DIR', __DIR__ . '/files');
define('GLPI_URI', (getenv('GLPI_URI') ?: 'http://localhost:8088'));

Expand All @@ -54,7 +54,7 @@
include (GLPI_ROOT . "/inc/based_config.php");

if (!file_exists(GLPI_CONFIG_DIR . '/config_db.php')) {
die("\nConfiguration file for tests not found\n\nrun: bin/console glpi:database:install --config-dir=./tests ...\n\n");
die("\nConfiguration file for tests not found\n\nrun: bin/console glpi:database:install --config-dir=./tests/config ...\n\n");
}

// Create subdirectories of GLPI_VAR_DIR based on defined constants
Expand Down
2 changes: 2 additions & 0 deletions tests/config/.gitignore
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 1 addition & 1 deletion tests/router.php
Expand Up @@ -30,7 +30,7 @@
* ---------------------------------------------------------------------
*/

define('GLPI_CONFIG_DIR', __DIR__);
define('GLPI_CONFIG_DIR', __DIR__ . '/config');
define('GLPI_PICTURE_DIR', __DIR__ . '/files/_pictures');

define(
Expand Down

0 comments on commit 3d0ce14

Please sign in to comment.