mirror of
				https://github.com/librenms/librenms.git
				synced 2024-10-07 16:52:45 +00:00 
			
		
		
		
	feature: Added phpunit db setup tests (#5594)
This commit is contained in:
		| @@ -45,6 +45,6 @@ after_failure: | ||||
| script: | ||||
|   - php scripts/pre-commit.php -l | ||||
|   - php scripts/pre-commit.php -s | ||||
|   - SNMPSIM=1 phpunit --stop-on-failure | ||||
|   - SNMPSIM=1 DBTEST=1 phpunit --stop-on-failure | ||||
|   - bash -n daily.sh | ||||
|   - pylint -E poller-wrapper.py discovery-wrapper.py poller-service.py | ||||
|   | ||||
| @@ -3,6 +3,7 @@ | ||||
|  | ||||
| // MYSQL Check - FIXME | ||||
| // 1 UNKNOWN | ||||
| $config['db_port'] = null; | ||||
| include 'config.php'; | ||||
|  | ||||
| if (!isset($sql_file)) { | ||||
|   | ||||
							
								
								
									
										51
									
								
								includes/build-base.inc.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								includes/build-base.inc.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,51 @@ | ||||
| <?php | ||||
|  | ||||
| if (!isset($sql_file)) { | ||||
|     $sql_file = 'build.sql'; | ||||
| } | ||||
|  | ||||
| $sql_fh = fopen($sql_file, 'r'); | ||||
| if ($sql_fh === false) { | ||||
|     echo 'ERROR: Cannot open SQL build script '.$sql_file."\n"; | ||||
|     exit(1); | ||||
| } | ||||
|  | ||||
| $database_link = mysqli_connect('p:'.$config['db_host'], $config['db_user'], $config['db_pass']); | ||||
| if ($database_link === false) { | ||||
|     echo 'ERROR: Cannot connect to database: '.mysqli_error($database_link)."\n"; | ||||
|     exit(1); | ||||
| } | ||||
|  | ||||
| $select = mysqli_select_db($database_link, $config['db_name']); | ||||
| if ($select === false) { | ||||
|     echo 'ERROR: Cannot select database: '.mysqli_error($database_link)."\n"; | ||||
|     exit(1); | ||||
| } | ||||
|  | ||||
| $limit = 0; | ||||
| while (!feof($sql_fh)) { | ||||
|     $line = fgetss($sql_fh); | ||||
|     if (isset($_SESSION['stage'])) { | ||||
|         $limit++; | ||||
|         if (isset($_SESSION['offset']) && $limit < $_REQUEST['offset']) { | ||||
|             continue; | ||||
|         } elseif (time()-$_SESSION['last'] > 45) { | ||||
|             $_SESSION['offset'] = $limit; | ||||
|             $GLOBALS['refresh'] = '<b>Installing, please wait..</b><sub>'.date('r').'</sub><script>window.location.href = "install.php?offset='.$limit.'";</script>'; | ||||
|             return; | ||||
|         } else { | ||||
|             echo 'Step #'.$limit.' ...'.PHP_EOL; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     if (!empty($line)) { | ||||
|         $creation = mysqli_query($database_link, $line); | ||||
|         if (!$creation) { | ||||
|             echo 'WARNING: Cannot execute query ('.$line.'): '.mysqli_error($database_link)."\n"; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| fclose($sql_fh); | ||||
|  | ||||
| require 'includes/sql-schema/update.php'; | ||||
| @@ -31,9 +31,10 @@ $install_dir = realpath(__DIR__ . '/..'); | ||||
| $config['install_dir'] = $install_dir; | ||||
| chdir($install_dir); | ||||
|  | ||||
| // Libraries | ||||
| require('Net/IPv4.php'); | ||||
| require('Net/IPv6.php'); | ||||
| if (!getenv('TRAVIS')) { | ||||
|     require('Net/IPv4.php'); | ||||
|     require('Net/IPv6.php'); | ||||
| } | ||||
|  | ||||
| # composer autoload | ||||
| require $install_dir . '/vendor/autoload.php'; | ||||
| @@ -49,7 +50,11 @@ require $install_dir . '/includes/influxdb.inc.php'; | ||||
| require $install_dir . '/includes/datastore.inc.php'; | ||||
| require $install_dir . '/includes/billing.php'; | ||||
| require $install_dir . '/includes/syslog.php'; | ||||
| require $install_dir . '/includes/snmp.inc.php'; | ||||
| if (module_selected('mocksnmp', $init_modules)) { | ||||
|     require $install_dir . '/tests/mocks/mock.snmp.inc.php'; | ||||
| } else { | ||||
|     require $install_dir . '/includes/snmp.inc.php'; | ||||
| } | ||||
| require $install_dir . '/includes/services.inc.php'; | ||||
| require $install_dir . '/includes/mergecnf.inc.php'; | ||||
| require $install_dir . '/includes/functions.php'; | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| CREATE TABLE IF NOT EXISTS `device_graphs` (  `device_id` int(11) NOT NULL,  `graph` varchar(32) COLLATE utf8_unicode_ci NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; | ||||
| DROP TABLE IF EXISTS `graph_types`; | ||||
| CREATE TABLE IF NOT EXISTS `graph_types` (  `graph_type` varchar(32) COLLATE utf8_unicode_ci NOT NULL,  `graph_subtype` varchar(32) COLLATE utf8_unicode_ci NOT NULL,  `graph_section` varchar(32) COLLATE utf8_unicode_ci NOT NULL,  `graph_descr` varchar(64) COLLATE utf8_unicode_ci NOT NULL,  `graph_order` int(11) NOT NULL,  KEY `graph_type` (`graph_type`),  KEY `graph_subtype` (`graph_subtype`),  KEY `graph_section` (`graph_section`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; | ||||
| INSERT INTO `graph_types` (`graph_type`, `graph_subtype`, `graph_section`, `graph_descr`, `graph_order`) VALUES('device', 'bits', 'netstats', 'Total Traffic', 0),('device', 'hr_users', 'system', 'Users Logged In', 0),('device', 'ucd_load', 'system', 'Load Averages', 0),('device', 'ucd_cpu', 'system', 'Detailed Processor Usage', 0),('device', 'ucd_memory', 'system', 'Detailed Memory Usage', 0),('device', 'netstat_tcp', 'netstats', 'TCP Statistics', 0),('device', 'netstat_icmp_info', 'netstats', 'ICMP Informational Statistics', 0),('device', 'netstat_icmp_stat', 'netstats', 'ICMP Statistics', 0),('device', 'netstat_ip', 'netstats', 'IP Statistics', 0),('device', 'netstat_ip_frag', 'netstats', 'IP Fragmentation Statistics', 0),('device', 'netstat_udp', 'netstats', 'UDP Statistics', 0),('device', 'netstat_snmp', 'netstats', 'SNMP Statistics', 0),('device', 'temperatures', 'system', 'Temperatures', 0),('device', 'mempools', 'system', 'Memory Pool Usage', 0),('device', 'processors', 'system', 'Processor Usage', 0),('device', 'storage', 'system', 'Filesystem Usage', 0),('device', 'hr_processes', 'system', 'Running Processes', 0),('device', 'uptime', 'system', 'System Uptime', ''),('device', 'ipsystemstats_ipv4', 'netstats', 'IPv4 Packet Statistics', 0),('device', 'ipsystemstats_ipv6_frag', 'netstats', 'IPv6 Fragmentation Statistics', 0),('device', 'ipsystemstats_ipv6', 'netstats', 'IPv6 Packet Statistics', 0),('device', 'ipsystemstats_ipv4_frag', 'netstats', 'IPv4 Fragmentation Statistics', 0),('device',  'fortigate_sessions',  'firewall',  'Active Sessions',  ''), ('device',  'screenos_sessions',  'firewall',  'Active Sessions',  ''), ('device',  'fdb_count',  'system',  'MAC Addresses Learnt',  '0'),('device', 'cras_sessions', 'firewall', 'Remote Access Sessions', 0); | ||||
| DROP TABLE `frequency`; | ||||
| ALTER TABLE  `mempools` CHANGE  `mempool_index`  `mempool_index` VARCHAR( 16 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL; | ||||
| ALTER TABLE `vrfs` CHANGE `mplsVpnVrfRouteDistinguisher` `mplsVpnVrfRouteDistinguisher` varchar(26) NOT NULL; | ||||
|   | ||||
							
								
								
									
										74
									
								
								tests/DBSetupTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								tests/DBSetupTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,74 @@ | ||||
| <?php | ||||
| /** | ||||
|  * DBSetup.php | ||||
|  * | ||||
|  * -Description- | ||||
|  * | ||||
|  * 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  2017 Neil Lathwood | ||||
|  * @author     Neil Lathwood <librenms+n@laf.io> | ||||
|  */ | ||||
|  | ||||
| namespace LibreNMS\Tests; | ||||
|  | ||||
| use PHPUnit_Framework_ExpectationFailedException as PHPUnitException; | ||||
|  | ||||
| class DBSetupTest extends \PHPUnit_Framework_TestCase | ||||
| { | ||||
|  | ||||
|     private static $schema; | ||||
|     private static $sql_mode; | ||||
|     private static $db_created; | ||||
|     protected $backupGlobals = false; | ||||
|  | ||||
|     public static function setUpBeforeClass() | ||||
|     { | ||||
|         if (getenv('DBTEST')) { | ||||
|             global $config; | ||||
|  | ||||
|             self::$sql_mode = dbFetchCell("SELECT @@global.sql_mode as sql_mode"); | ||||
|             self::$db_created = dbQuery("CREATE DATABASE " . $config['db_name']); | ||||
|             dbQuery("SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'"); | ||||
|             $build_base = $config['install_dir'] . '/build-base.php'; | ||||
|             exec($build_base, $schema); | ||||
|             self::$schema = $schema; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static function tearDownAfterClass() | ||||
|     { | ||||
|         if (getenv('DBTEST')) { | ||||
|             global $config; | ||||
|  | ||||
|             dbQuery("SET GLOBAL sql_mode='" . self::$sql_mode . "'"); | ||||
|             if (self::$db_created) { | ||||
|                 dbQuery("DROP DATABASE " . $config['db_name']); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public function testSetupDB() | ||||
|     { | ||||
|         if (getenv('DBTEST')) { | ||||
|             foreach (self::$schema as $output) { | ||||
|                 if (preg_match('/([1-9]+) errors/', $output) || preg_match('/Cannot execute query/', $output)) { | ||||
|                     throw new PHPUnitException("Errors loading DB Schema: " . $output); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -23,35 +23,26 @@ | ||||
|  * @author     Tony Murray <murraytony@gmail.com> | ||||
|  */ | ||||
|  | ||||
| // get the current LibreNMS install directory | ||||
| $install_dir = realpath(__DIR__ . '/..'); | ||||
|  | ||||
| require $install_dir . '/includes/defaults.inc.php'; | ||||
| $init_modules = array('web'); | ||||
|  | ||||
| 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); | ||||
|  | ||||
| // definitions, don't want to initialize mysql... | ||||
| $config['install_dir'] = $install_dir; | ||||
| $config['mib_dir'] = $install_dir . '/mibs'; | ||||
| $config['snmpget'] = 'snmpget'; | ||||
|  | ||||
| // initialize the class loader | ||||
| require $install_dir . '/vendor/autoload.php'; | ||||
|  | ||||
| require $install_dir . '/includes/common.php'; | ||||
| require $install_dir . '/html/includes/functions.inc.php'; | ||||
| require $install_dir . '/includes/definitions.inc.php'; | ||||
| require $install_dir . '/includes/rrdtool.inc.php'; | ||||
| require $install_dir . '/includes/syslog.php'; | ||||
| require $install_dir . '/includes/dbFacile.php'; | ||||
| require $install_dir . '/includes/functions.php'; | ||||
|  | ||||
| if (getenv('SNMPSIM')) { | ||||
|     require $install_dir . '/includes/snmp.inc.php'; | ||||
| } else { | ||||
|     require $install_dir . '/tests/mocks/mock.snmp.inc.php'; | ||||
| } | ||||
|  | ||||
| ini_set('display_errors', 1); | ||||
| error_reporting(E_ALL & ~E_WARNING); | ||||
| //error_reporting(E_ALL); | ||||
| //$debug=true; | ||||
| //$vdebug=true; | ||||
|   | ||||
							
								
								
									
										49
									
								
								tests/config/config.test.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										49
									
								
								tests/config/config.test.php
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,49 @@ | ||||
| <?php | ||||
|  | ||||
| ## Have a look in includes/defaults.inc.php for examples of settings you can set here. DO NOT EDIT defaults.inc.php! | ||||
|  | ||||
| ### Database config | ||||
| $config['db_host'] = 'localhost'; | ||||
| $config['db_user'] = 'root'; | ||||
| $config['db_pass'] = ''; | ||||
| $config['db_name'] = 'librenms_phpunit_78hunjuybybh'; | ||||
| $config['db']['extension'] = 'mysqli';// mysql or mysqli | ||||
|  | ||||
| // This is the user LibreNMS will run as | ||||
| //Please ensure this user is created and has the correct permissions to your install | ||||
| $config['user'] = 'librenms'; | ||||
|  | ||||
| ### This should *only* be set if you want to *force* a particular hostname/port | ||||
| ### It will prevent the web interface being usable form any other hostname | ||||
| $config['base_url']        = "/"; | ||||
|  | ||||
| ### Enable this to use rrdcached. Be sure rrd_dir is within the rrdcached dir | ||||
| ### and that your web server has permission to talk to rrdcached. | ||||
| #$config['rrdcached']    = "unix:/var/run/rrdcached.sock"; | ||||
|  | ||||
| ### Default community | ||||
| $config['snmp']['community'] = array("public"); | ||||
|  | ||||
| ### Authentication Model | ||||
| $config['auth_mechanism'] = "mysql"; # default, other options: ldap, http-auth | ||||
| #$config['http_auth_guest'] = "guest"; # remember to configure this user if you use http-auth | ||||
|  | ||||
| ### List of RFC1918 networks to allow scanning-based discovery | ||||
| #$config['nets'][] = "10.0.0.0/8"; | ||||
| #$config['nets'][] = "172.16.0.0/12"; | ||||
| #$config['nets'][] = "192.168.0.0/16"; | ||||
|  | ||||
| # Uncomment the next line to disable daily updates | ||||
| #$config['update'] = 0; | ||||
|  | ||||
| # Number in days of how long to keep old rrd files. 0 disables this feature | ||||
| $config['rrd_purge'] = 0; | ||||
|  | ||||
| # Uncomment to submit callback stats via proxy | ||||
| #$config['callback_proxy'] = "hostname:port"; | ||||
|  | ||||
| # Enable the in-built billing extension | ||||
| $config['enable_billing'] = 1; | ||||
|  | ||||
| # Enable the in-built services support (Nagios plugins) | ||||
| $config['show_services'] = 1; | ||||
		Reference in New Issue
	
	Block a user