2016-08-22 10:32:05 -05:00
< ? 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 />.
*
* @ link http :// librenms . org
* @ copyright 2016 Tony Murray
* @ author Tony Murray < murraytony @ gmail . com >
*/
2018-04-11 10:15:13 -05:00
use LibreNMS\Config ;
2017-12-20 08:36:49 -06:00
use LibreNMS\Util\Snmpsim ;
2016-08-22 10:32:05 -05:00
$install_dir = realpath ( __DIR__ . '/..' );
2020-09-21 14:54:51 +02:00
$init_modules = [ 'web' , 'discovery' , 'polling' , 'nodb' ];
2016-09-19 21:12:26 -05:00
2020-09-21 14:54:51 +02:00
if ( ! getenv ( 'SNMPSIM' )) {
2017-02-07 15:08:52 +00:00
$init_modules [] = 'mocksnmp' ;
}
2016-08-22 10:32:05 -05:00
2017-02-07 15:08:52 +00:00
require $install_dir . '/includes/init.php' ;
chdir ( $install_dir );
2016-11-21 14:12:59 -06:00
2016-09-23 15:44:17 -05:00
ini_set ( 'display_errors' , 1 );
2017-12-20 08:36:49 -06:00
//error_reporting(E_ALL & ~E_WARNING);
2017-04-07 09:07:44 -05:00
2018-05-09 06:53:45 -05:00
$snmpsim = new Snmpsim ( '127.1.6.2' , 1162 , null );
2017-12-20 08:36:49 -06:00
if ( getenv ( 'SNMPSIM' )) {
2020-06-08 06:32:15 +02:00
$snmpsim -> fork ( 6 );
2017-12-20 08:36:49 -06:00
// make PHP hold on a reference to $snmpsim so it doesn't get destructed
register_shutdown_function ( function ( Snmpsim $ss ) {
$ss -> stop ();
}, $snmpsim );
}
2017-04-07 09:07:44 -05:00
if ( getenv ( 'DBTEST' )) {
2019-10-13 13:40:38 +00:00
global $migrate_result , $migrate_output ;
2017-04-07 09:07:44 -05:00
2018-08-17 15:29:20 -05:00
// create testing table if needed
2020-09-21 15:59:34 +02:00
$db_config = \config ( 'database.connections.testing' );
2021-01-03 07:54:01 +01:00
$connection = new PDO ( " mysql:host= { $db_config [ 'host' ] } ;port= { $db_config [ 'port' ] } " , $db_config [ 'username' ], $db_config [ 'password' ]);
2019-05-22 13:31:24 -05:00
$result = $connection -> query ( " CREATE DATABASE IF NOT EXISTS { $db_config [ 'database' ] } CHARACTER SET utf8 COLLATE utf8_unicode_ci " );
if ( $connection -> errorCode () == '42000' ) {
echo implode ( ' ' , $connection -> errorInfo ()) . PHP_EOL ;
echo " Either create database { $db_config [ 'database' ] } or populate DB_TEST_USERNAME and DB_TEST_PASSWORD in your .env with credentials that can " . PHP_EOL ;
exit ( 1 );
}
2018-08-17 15:29:20 -05:00
unset ( $connection ); // close connection
2020-05-22 16:49:21 -05:00
// sqlite db file
// $dbFile = fopen(storage_path('testing.sqlite'), 'a+');
// ftruncate($dbFile, 0);
// fclose($dbFile);
2019-10-13 13:40:38 +00:00
// try to avoid erasing people's primary databases
if ( $db_config [ 'database' ] !== \config ( 'database.connections.mysql.database' , 'librenms' )) {
2020-09-21 14:54:51 +02:00
if ( ! getenv ( 'SKIP_DB_REFRESH' )) {
2020-09-21 15:59:34 +02:00
echo 'Refreshing database...' ;
2020-05-23 14:43:41 -05:00
$migrate_result = Artisan :: call ( 'migrate:fresh' , [ '--seed' => true , '--env' => 'testing' , '--database' => 'testing' ]);
$migrate_output = Artisan :: output ();
echo " done \n " ;
}
2019-10-13 13:40:38 +00:00
} else {
echo " Info: Refusing to reset main database: { $db_config [ 'database' ] } . Running migrations. \n " ;
$migrate_result = Artisan :: call ( 'migrate' , [ '--seed' => true , '--env' => 'testing' , '--database' => 'testing' ]);
$migrate_output = Artisan :: output ();
}
unset ( $db_config );
2017-04-07 09:07:44 -05:00
}
2019-10-13 13:40:38 +00:00
2020-05-19 14:35:32 -05:00
Config :: reload (); // reload the config including database config
\LibreNMS\Util\OS :: updateCache ( true ); // Force update of OS Cache