librenms-librenms/tests/bootstrap.php
Tony Murray fff66d3c00 Feature: Generic discovery and poller tests (#7873)
* Processor Tests!

* Capture data from live devices easily.

* fix up some stuff, remove powerconnect things as they seem to be just broken.

* formatting, fix missing assignment
add netonix processor data

* fix multi-line, always add sysDescr and sysObjectID
ios cpm test file

* revert composer change and fix whitespace issues

* add help text

* missed help text

* tighter debug output

* handle empty strings properly and mibs with numbers

* use keys for sorting as intended

* fix type with empty data

* oops :)

* whitespace fix

* try installing fping

* Fix TestCase collision + cleanup

* mark TestCase as abstract
don't run two instances of snmpsim

* better database dumps, improved capture

* style fixes

* fix quotes add a few more tables

* add --prefer-new, properly merge data

* Support separate discovery and poller data. But don't waste space if they aren't needed.

* refactor to use class
collects all code in one place for reusability

* reorganize

* Print out when not saving.

* Support for running multiple (or all) modules at once.

* tidy

* Change unit test to a generic name and test all modules we have data for.

* Add documentation and a few more tidies

* whitespace fixes

* Fix tests, add a couple more modules, more docs

* More docs updates

* Fix scrutinizer issues

* add bgp-peers
2017-12-20 08:36:49 -06:00

108 lines
3.3 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\Proc;
use LibreNMS\Util\Snmpsim;
global $config;
$install_dir = realpath(__DIR__ . '/..');
$init_modules = array('web', 'discovery', 'polling');
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");
}
} else {
$init_modules[] = 'nodb';
}
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
if (getenv('SNMPSIM')) {
$snmpsim = new Snmpsim('127.1.6.2');
$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;
$sql_mode = dbFetchCell("SELECT @@global.sql_mode");
$empty_db = (dbFetchCell("SELECT count(*) FROM `information_schema`.`tables` WHERE `table_type` = 'BASE TABLE' AND `table_schema` = ?", array($config['db_name'])) == 0);
dbQuery("SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'");
$cmd = $config['install_dir'] . '/build-base.php';
exec($cmd, $schema);
register_shutdown_function(function () use ($empty_db, $sql_mode) {
global $config;
dbConnect();
echo "Cleaning database...\n";
// restore sql_mode
dbQuery("SET GLOBAL sql_mode='$sql_mode'");
if ($empty_db) {
dbQuery("DROP DATABASE " . $config['db_name']);
} elseif (isset($config['test_db_name']) && $config['db_name'] == $config['test_db_name']) {
// truncate tables
$tables = dbFetchColumn('SHOW TABLES');
$excluded = array(
'alert_templates',
'config', // not sure about this one
'dbSchema',
'graph_types',
'port_association_mode',
'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");
}
});
}