[icinga-checkins] icinga.org: icingaweb2-module-director/feature/test-runner-12905: tests: refactor bootstrapping, initial Gitlab-CI..

git at icinga.org git at icinga.org
Thu Nov 3 02:52:49 CET 2016


Module: icingaweb2-module-director
Branch: feature/test-runner-12905
Commit: b996a684bea9111af4300d4250b8a1f74133f733
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=b996a684bea9111af4300d4250b8a1f74133f733

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Nov  3 02:50:39 2016 +0100

tests: refactor bootstrapping, initial Gitlab-CI..

...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.

---

 .gitlab-ci.yml                         |  102 ++++++++++++++++++++++++++++++++
 library/Director/Test/BaseTestCase.php |   26 ++++----
 phpunit.xml                            |   18 ++++++
 test/bootstrap.php                     |   16 +++--
 test/config/resources.ini              |   13 ++++
 5 files changed, 159 insertions(+), 16 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..ca11439
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -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
+
diff --git a/library/Director/Test/BaseTestCase.php b/library/Director/Test/BaseTestCase.php
index 866598f..f0c7ab1 100644
--- a/library/Director/Test/BaseTestCase.php
+++ b/library/Director/Test/BaseTestCase.php
@@ -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;
@@ -40,7 +40,11 @@ class BaseTestCase extends PHPUnit_Framework_TestCase
 
     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()
@@ -52,7 +56,14 @@ class BaseTestCase extends PHPUnit_Framework_TestCase
                     '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();
         }
@@ -73,14 +84,7 @@ class BaseTestCase extends PHPUnit_Framework_TestCase
     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;
diff --git a/phpunit.xml b/phpunit.xml
new file mode 100644
index 0000000..4cfc6b8
--- /dev/null
+++ b/phpunit.xml
@@ -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>
diff --git a/test/bootstrap.php b/test/bootstrap.php
index be1d364..b1b9a26 100644
--- a/test/bootstrap.php
+++ b/test/bootstrap.php
@@ -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');
+});
diff --git a/test/config/authentication.ini b/test/config/authentication.ini
new file mode 100644
index 0000000..e69de29
diff --git a/test/config/config.ini b/test/config/config.ini
new file mode 100644
index 0000000..e69de29
diff --git a/test/config/resources.ini b/test/config/resources.ini
new file mode 100644
index 0000000..1f64e52
--- /dev/null
+++ b/test/config/resources.ini
@@ -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"
diff --git a/test/modules/.gitkeep b/test/modules/.gitkeep
new file mode 100644
index 0000000..e69de29



More information about the icinga-checkins mailing list