Skip to content

Commit

Permalink
tests: refactor bootstrapping, initial Gitlab-CI..
Browse files Browse the repository at this point in the history
...support. Currently expects three test-runners offering Debian Jessie,
Ubuntu 16.04 LTS (Xenial) and CentOS 7. Removed all dependencies on the
Icinga Web 2 test module, installing phpunit should suffice to run tests
in a normal environment.

This is a first attempt to make things easier, more to come.
  • Loading branch information
Thomas-Gelf committed Nov 3, 2016
1 parent 6c23a02 commit b996a68
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 16 deletions.
102 changes: 102 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
stages:
- Unit-Tests with DB

CentOS 7/MySQL:
stage: Unit-Tests with DB
tags:
- centos7
- director
variables:
DIRECTOR_TESTDB: "director_test_${CI_BUILD_ID}_${CI_RUNNER_ID}"
DIRECTOR_TESTDB_RES: "Director MySQL TestDB"
before_script:
- mysql -u root -e "CREATE DATABASE $DIRECTOR_TESTDB"
after_script:
- mysql -u root -e "DROP DATABASE $DIRECTOR_TESTDB"
script:
- phpunit

CentOS 7/PostgreSQL:
stage: Unit-Tests with DB
tags:
- centos7
- director
variables:
DIRECTOR_TESTDB: "director_test_${CI_BUILD_ID}_${CI_RUNNER_ID}"
DIRECTOR_TESTDB_RES: "Director PostgreSQL TestDB"
DIRECTOR_TESTDB_USER: "director_${CI_BUILD_ID}_${CI_RUNNER_ID}"
before_script:
- psql postgres -q -c "CREATE DATABASE $DIRECTOR_TESTDB WITH ENCODING 'UTF8';"
- psql $DIRECTOR_TESTDB -q -c "CREATE USER $DIRECTOR_TESTDB_USER WITH PASSWORD 'testing'; GRANT ALL PRIVILEGES ON DATABASE $DIRECTOR_TESTDB TO $DIRECTOR_TESTDB_USER; CREATE EXTENSION pgcrypto;"
after_script:
- psql postgres -c "DROP DATABASE $DIRECTOR_TESTDB"
- psql postgres -c "DROP USER $DIRECTOR_TESTDB_USER"
script:
- phpunit

Jessie/MySQL:
stage: Unit-Tests with DB
tags:
- jessie
- director
variables:
DIRECTOR_TESTDB: "director_test_${CI_BUILD_ID}_${CI_RUNNER_ID}"
DIRECTOR_TESTDB_RES: "Director MySQL TestDB"
before_script:
- mysql -u root -e "CREATE DATABASE $DIRECTOR_TESTDB"
after_script:
- mysql -u root -e "DROP DATABASE $DIRECTOR_TESTDB"
script:
- phpunit

Jessie/PostgreSQL:
stage: Unit-Tests with DB
tags:
- jessie
- director
variables:
DIRECTOR_TESTDB: "director_test_${CI_BUILD_ID}_${CI_RUNNER_ID}"
DIRECTOR_TESTDB_RES: "Director PostgreSQL TestDB"
DIRECTOR_TESTDB_USER: "director_${CI_BUILD_ID}_${CI_RUNNER_ID}"
before_script:
- psql postgres -q -c "CREATE DATABASE $DIRECTOR_TESTDB WITH ENCODING 'UTF8';"
- psql $DIRECTOR_TESTDB -q -c "CREATE USER $DIRECTOR_TESTDB_USER WITH PASSWORD 'testing'; GRANT ALL PRIVILEGES ON DATABASE $DIRECTOR_TESTDB TO $DIRECTOR_TESTDB_USER; CREATE EXTENSION pgcrypto;"
after_script:
- psql postgres -c "DROP DATABASE $DIRECTOR_TESTDB"
- psql postgres -c "DROP USER $DIRECTOR_TESTDB_USER"
script:
- phpunit

Xenial/MySQL:
stage: Unit-Tests with DB
tags:
- xenial
- director
variables:
DIRECTOR_TESTDB: "director_test_${CI_BUILD_ID}_${CI_RUNNER_ID}"
DIRECTOR_TESTDB_RES: "Director MySQL TestDB"
before_script:
- mysql -u root -e "CREATE DATABASE $DIRECTOR_TESTDB"
after_script:
- mysql -u root -e "DROP DATABASE $DIRECTOR_TESTDB"
script:
- phpunit

Xenial/PostgreSQL:
stage: Unit-Tests with DB
tags:
- ubuntu
- director
variables:
DIRECTOR_TESTDB: "director_test_${CI_BUILD_ID}_${CI_RUNNER_ID}"
DIRECTOR_TESTDB_RES: "Director PostgreSQL TestDB"
DIRECTOR_TESTDB_USER: "director_${CI_BUILD_ID}_${CI_RUNNER_ID}"
before_script:
- psql postgres -q -c "CREATE DATABASE $DIRECTOR_TESTDB WITH ENCODING 'UTF8';"
- psql $DIRECTOR_TESTDB -q -c "CREATE USER $DIRECTOR_TESTDB_USER WITH PASSWORD 'testing'; GRANT ALL PRIVILEGES ON DATABASE $DIRECTOR_TESTDB TO $DIRECTOR_TESTDB_USER; CREATE EXTENSION pgcrypto;"
after_script:
- psql postgres -c "DROP DATABASE $DIRECTOR_TESTDB"
- psql postgres -c "DROP USER $DIRECTOR_TESTDB_USER"
script:
- phpunit

26 changes: 15 additions & 11 deletions library/Director/Test/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Icinga\Module\Director\Test;

use Icinga\Application\Icinga;
use Icinga\Application\Cli; // <- remove
use Icinga\Application\Config;
use Icinga\Data\ResourceFactory;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Migrations;
Expand Down Expand Up @@ -40,7 +40,11 @@ protected function hasDb()

protected function getDbResourceName()
{
return Config::module('director')->get('testing', 'db_resource');
if (array_key_exists('DIRECTOR_TESTDB_RES', $_SERVER)) {
return $_SERVER['DIRECTOR_TESTDB_RES'];
} else {
return Config::module('director')->get('testing', 'db_resource');
}
}

protected function getDb()
Expand All @@ -52,7 +56,14 @@ protected function getDb()
'Could not run DB-based tests, please configure a testing db resource'
);
}
$this->db = Db::fromResourceName($resourceName);
$dbConfig = ResourceFactory::getResourceConfig($resourceName);
if (array_key_exists('DIRECTOR_TESTDB', $_SERVER)) {
$dbConfig->dbname = $_SERVER['DIRECTOR_TESTDB'];
}
if (array_key_exists('DIRECTOR_TESTDB_USER', $_SERVER)) {
$dbConfig->username = $_SERVER['DIRECTOR_TESTDB_USER'];
}
$this->db = new Db($dbConfig);
$migrations = new Migrations($this->db);
$migrations->applyPendingMigrations();
}
Expand All @@ -73,14 +84,7 @@ protected function newObject($type, $name, $properties = array())
protected function app()
{
if (self::$app === null) {
// TODO: Replace this..
$testModuleDir = $_SERVER['PWD'];
$libDir = dirname(dirname($testModuleDir)) . '/library';
require_once $libDir . '/Icinga/Application/Cli.php';
self::$app = Cli::start();

// With this:
// self::$app = Icinga::app();
self::$app = Icinga::app();
}

return self::$app;
Expand Down
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="test/bootstrap.php"
>
<testsuites>
<testsuite name="Director PHP Unit tests">
<directory suffix=".php">test/php</directory>
</testsuite>
</testsuites>
</phpunit>
16 changes: 11 additions & 5 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

use Icinga\Application\Cli;

// TODO: fix paths
require_once '/usr/local/icingaweb2/library/Icinga/Application/Cli.php';
require_once dirname(__DIR__) . '/library/Director/Test/BaseTestCase.php';
Cli::start('/usr/local/icingaweb2')->getModuleManager()->loadModule('director');

call_user_func(function() {
error_reporting(E_ALL | E_STRICT);
$testbase = __DIR__;
$base = dirname($testbase);
require_once 'Icinga/Application/Cli.php';
require_once $base . '/library/Director/Test/BaseTestCase.php';
symlink($base, $testbase . '/modules/director');
Cli::start($testbase, $testbase . '/config')
->getModuleManager()
->loadModule('director');
});
Empty file added test/config/authentication.ini
Empty file.
Empty file added test/config/config.ini
Empty file.
13 changes: 13 additions & 0 deletions test/config/resources.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Director MySQL TestDB]
type = "db"
db = "mysql"
host = "localhost"
username = "root"
charset = "utf8"

[Director PostgreSQL TestDB]
type = "db"
db = "pgsql"
host = "localhost"
password = "testing"
charset = "utf8"
Empty file added test/modules/.gitkeep
Empty file.

0 comments on commit b996a68

Please sign in to comment.