Files
librenms-librenms/tests/bootstrap.php
Paul Heinrichs ebe2ecf524 Laravel migrations (#8868)
* Add migrations and seeds

* Fix spacing validation issues

* Fix linting

* Update tabs to spaces

* Update daily and install process

* Make build-base.php use the new migrations if empty or at dbschema 1000.
Seed 1000 into the database.
Temp fix for the route table index length (this table basically isn't used...)

* Fix typo in seed.
Hard code legacy schema checks to version 1000 (999 would have worked as is)

* Port association table no longer exists

* Make database validate again

* DB schema, remove as many DB::statement as possible

* update migrations
add librenms cli entry point (artisan)
update validate to check laravel migrations

* remove statements from users migration

* Fix up daily.sh and the 1000 migration

* Update migrations to current state
Take advantage of environment variables to set DB credentials.

* Fix style issues

* Update schema

* fix test db collation

* Fix migration table definition

* update db migrations

* Update migrations

* Update stats callback.  Just count the total migrations applied.

* Update 1000.sql.

* update migrations

* remove the graph type seeder, it is no longer needed

* update docs

* fix whitespace

* remove extra schema

* update tests

* fix sort

* add message

* dbSchema should actually be 1000

* add character set to db create

* Fix some artisan issues

* Update schema
2019-01-14 06:44:23 -06:00

116 lines
3.6 KiB
PHP

<?php
/**
* bootstrap.php
*
* Initialize the Autoloader and includes for phpunit to be able to run tests
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2016 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
use LibreNMS\Config;
use LibreNMS\DB\Eloquent;
use LibreNMS\Exceptions\DatabaseConnectException;
use LibreNMS\Util\Snmpsim;
global $config;
$install_dir = realpath(__DIR__ . '/..');
$init_modules = array('web', 'discovery', 'polling', 'nodb');
if (!getenv('SNMPSIM')) {
$init_modules[] = 'mocksnmp';
}
if (getenv('DBTEST')) {
if (!is_file($install_dir . '/config.php')) {
exec("cp $install_dir/tests/config/config.test.php $install_dir/config.php");
}
}
require $install_dir . '/includes/init.php';
chdir($install_dir);
ini_set('display_errors', 1);
//error_reporting(E_ALL & ~E_WARNING);
update_os_cache(true); // Force update of OS Cache
$snmpsim = new Snmpsim('127.1.6.2', 1162, null);
if (getenv('SNMPSIM')) {
$snmpsim->fork();
// make PHP hold on a reference to $snmpsim so it doesn't get destructed
register_shutdown_function(function (Snmpsim $ss) {
$ss->stop();
}, $snmpsim);
}
if (getenv('DBTEST')) {
global $schema, $sql_mode;
// create testing table if needed
$db_config = Config::getDatabaseSettings();
$db_name = $db_config['db_name'];
$connection = new PDO("mysql:host={$db_config['db_host']}", $db_config['db_user'], $db_config['db_pass']);
$connection->query("CREATE DATABASE IF NOT EXISTS $db_name CHARACTER SET utf8 COLLATE utf8_unicode_ci");
unset($connection); // close connection
\LibreNMS\DB\Eloquent::boot();
\LibreNMS\DB\Eloquent::setStrictMode();
$empty_db = (dbFetchCell("SELECT count(*) FROM `information_schema`.`tables` WHERE `table_type` = 'BASE TABLE' AND `table_schema` = ?", [$db_name]) == 0);
$cmd = $config['install_dir'] . '/build-base.php';
exec($cmd, $schema);
Config::load(); // reload the config including database config
load_all_os();
register_shutdown_function(function () use ($empty_db, $sql_mode) {
global $config;
\LibreNMS\DB\Eloquent::boot();
echo "Cleaning database...\n";
$db_name = dbFetchCell('SELECT DATABASE()');
if ($empty_db) {
dbQuery("DROP DATABASE $db_name");
} elseif (isset($config['test_db_name']) && $config['test_db_name'] == $db_name) {
// truncate tables
$tables = dbFetchColumn('SHOW TABLES');
$excluded = array(
'alert_templates',
'config', // not sure about this one
'dbSchema',
'widgets',
);
$truncate = array_diff($tables, $excluded);
dbQuery("SET FOREIGN_KEY_CHECKS = 0");
foreach ($truncate as $table) {
dbQuery("TRUNCATE TABLE $table");
}
dbQuery("SET FOREIGN_KEY_CHECKS = 1");
}
});
}