From cb005210d24070d4ee6659caa0f87104344c56dd Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Tue, 12 Mar 2019 23:59:03 -0500 Subject: [PATCH] Resubmit #9608 (#9941) * Reorganize trap tests * Testing db DRIVER to prevent .env from interfering * New code to detect if Laravel is booted. Hopefully more reliable. * WIP external test process * revert module test helper * Use .env in Eloquent::boot() * Fix test database settings loading * fix undefined classes (didn't find the one I needed) * Fix incorrect Config usages And RrdDefinition return type * fix .env loading * use the right DB * slightly more accurate isConnected * Move db_name to DBSetupTest specifically * restore $_SERVER in AuthSSOTest * missed item * WIP * tear down in the correct order. * some testing cleanups * remove check for duplicate event listener, it's not working right * Don't need this change anymore * Implement Log::event to replace legacy function log_event() * fix port tests * fix up tests * remove pointless TrapTestCase class * fix style * Fix db config not being merged... * skip env check for tests * defer database operations until after Laravel is booted. * don't include dbFaciale... * redundant use --- LibreNMS/Authentication/SSOAuthorizer.php | 2 +- LibreNMS/Config.php | 21 +- LibreNMS/DB/Eloquent.php | 15 +- LibreNMS/RRD/RrdDefinition.php | 2 +- .../Handlers/AuthenticationFailure.php | 6 +- .../Handlers/BgpBackwardTransition.php | 2 +- LibreNMS/Snmptrap/Handlers/BgpEstablished.php | 2 +- .../Snmptrap/Handlers/EquipStatusTrap.php | 4 +- LibreNMS/Snmptrap/Handlers/LinkDown.php | 8 +- LibreNMS/Snmptrap/Handlers/LinkUp.php | 8 +- LibreNMS/Snmptrap/Handlers/LogTrap.php | 3 +- .../Snmptrap/Handlers/UpsmgUtilityFailure.php | 3 +- .../Handlers/UpsmgUtilityRestored.php | 3 +- LibreNMS/Util/Laravel.php | 14 +- app/Facades/LogManager.php | 53 ++++++ app/Http/Controllers/Ajax/NetCommand.php | 2 +- app/Listeners/AuthEventListener.php | 3 - app/Models/DeviceGroup.php | 20 +- app/Providers/AppServiceProvider.php | 21 +- config/database.php | 17 +- database/factories/ModelFactory.php | 1 + includes/common.php | 3 +- includes/dbFacile.php | 7 +- includes/functions.php | 2 +- includes/helpers.php | 4 +- includes/polling/ipmi.inc.php | 1 + phpunit.xml | 1 + resources/views/locations.blade.php | 2 +- resources/views/widgets/graylog.blade.php | 4 +- tests/AuthSSOTest.php | 3 +- tests/ConfigTest.php | 2 +- tests/DBSetupTest.php | 8 + tests/DBTestCase.php | 6 +- tests/Feature/SnmpTraps/BgpTrapTest.php | 84 ++++++++ tests/Feature/SnmpTraps/CommonTrapTest.php | 85 +++++++++ tests/Feature/SnmpTraps/PortsTrapTest.php | 102 ++++++++++ tests/LaravelTestCase.php | 18 ++ tests/OSDiscoveryTest.php | 3 +- tests/OSModulesTest.php | 2 +- tests/SnmpTrapTest.php | 179 ------------------ tests/TestCase.php | 6 + tests/YamlTest.php | 4 +- tests/snmpsim/fabos_1.snmprec | 2 +- 43 files changed, 460 insertions(+), 278 deletions(-) create mode 100644 app/Facades/LogManager.php create mode 100644 tests/Feature/SnmpTraps/BgpTrapTest.php create mode 100644 tests/Feature/SnmpTraps/CommonTrapTest.php create mode 100644 tests/Feature/SnmpTraps/PortsTrapTest.php delete mode 100644 tests/SnmpTrapTest.php diff --git a/LibreNMS/Authentication/SSOAuthorizer.php b/LibreNMS/Authentication/SSOAuthorizer.php index ed074f982f..6cb27bdd76 100644 --- a/LibreNMS/Authentication/SSOAuthorizer.php +++ b/LibreNMS/Authentication/SSOAuthorizer.php @@ -26,9 +26,9 @@ namespace LibreNMS\Authentication; use LibreNMS\Config; -use LibreNMS\Util\IP; use LibreNMS\Exceptions\AuthenticationException; use LibreNMS\Exceptions\InvalidIpException; +use LibreNMS\Util\IP; /** * Some functionality in this mechanism is inspired by confluence_http_authenticator (@chauth) and graylog-plugin-auth-sso (@Graylog) diff --git a/LibreNMS/Config.php b/LibreNMS/Config.php index 0de4b9b7d8..7df867c0bd 100644 --- a/LibreNMS/Config.php +++ b/LibreNMS/Config.php @@ -533,19 +533,14 @@ class Config } // Check for testing database - if (getenv('DBTEST')) { - if (isset($config['test_db_name'])) { - putenv('DB_DATABASE=' . $config['test_db_name']); - $config['db_name'] = $config['test_db_name']; - } - if (isset($config['test_db_user'])) { - putenv('DB_USERNAME=' . $config['test_db_user']); - $config['db_user'] = $config['test_db_user']; - } - if (isset($config['test_db_pass'])) { - putenv('DB_PASSWORD=' . $config['test_db_pass']); - $config['db_pass'] = $config['test_db_pass']; - } + if (isset($config['test_db_name'])) { + putenv('DB_TEST_DATABASE=' . $config['test_db_name']); + } + if (isset($config['test_db_user'])) { + putenv('DB_TEST_USERNAME=' . $config['test_db_user']); + } + if (isset($config['test_db_pass'])) { + putenv('DB_TEST_PASSWORD=' . $config['test_db_pass']); } return array_intersect_key($config, $keys); // return only the db settings diff --git a/LibreNMS/DB/Eloquent.php b/LibreNMS/DB/Eloquent.php index 837c6ca2df..6513176cb8 100644 --- a/LibreNMS/DB/Eloquent.php +++ b/LibreNMS/DB/Eloquent.php @@ -25,22 +25,25 @@ namespace LibreNMS\DB; +use Dotenv\Dotenv; use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Events\StatementPrepared; use Illuminate\Events\Dispatcher; +use LibreNMS\Util\Laravel; class Eloquent { /** @var Capsule static reference to capsule */ private static $capsule; - private static $legacy_listener_installed = false; public static function boot($options = []) { // boot Eloquent outside of Laravel - if (!defined('LARAVEL_START') && class_exists(Capsule::class) && is_null(self::$capsule)) { + if (!Laravel::isBooted() && is_null(self::$capsule)) { $install_dir = realpath(__DIR__ . '/../../'); + (new Dotenv($install_dir))->load(); + $db_config = include($install_dir . '/config/database.php'); $settings = $db_config['connections'][$db_config['default']]; @@ -75,7 +78,7 @@ class Eloquent public static function initLegacyListeners() { - if (self::isConnected() && !self::$legacy_listener_installed) { + if (self::isConnected()) { // set FETCH_ASSOC for queries that required by setting the global variable $PDO_FETCH_ASSOC (for dbFacile) self::DB()->getEventDispatcher()->listen(StatementPrepared::class, function ($event) { global $PDO_FETCH_ASSOC; @@ -83,7 +86,6 @@ class Eloquent $event->statement->setFetchMode(\PDO::FETCH_ASSOC); } }); - self::$legacy_listener_installed = true; } } @@ -107,8 +109,7 @@ class Eloquent try { $conn = self::DB(); if ($conn) { - $conn->getPdo(); - return true; + return !is_null($conn->getPdo()); } } catch (\PDOException $e) { return false; @@ -125,7 +126,7 @@ class Eloquent public static function DB() { // check if Laravel is booted - if (defined('LARAVEL_START') && class_exists('DB')) { + if (Laravel::isBooted()) { return \DB::connection(); } diff --git a/LibreNMS/RRD/RrdDefinition.php b/LibreNMS/RRD/RrdDefinition.php index 8411f864bf..d132f73170 100644 --- a/LibreNMS/RRD/RrdDefinition.php +++ b/LibreNMS/RRD/RrdDefinition.php @@ -49,7 +49,7 @@ class RrdDefinition * @param int $min Minimum allowed value. null means undefined. * @param int $max Maximum allowed value. null means undefined. * @param int $heartbeat Heartbeat for this dataset. Uses the global setting if null. - * @return $this + * @return RrdDefinition */ public function addDataset($name, $type, $min = null, $max = null, $heartbeat = null) { diff --git a/LibreNMS/Snmptrap/Handlers/AuthenticationFailure.php b/LibreNMS/Snmptrap/Handlers/AuthenticationFailure.php index d342061fac..473a5f545c 100644 --- a/LibreNMS/Snmptrap/Handlers/AuthenticationFailure.php +++ b/LibreNMS/Snmptrap/Handlers/AuthenticationFailure.php @@ -28,10 +28,10 @@ namespace LibreNMS\Snmptrap\Handlers; use App\Models\Device; use LibreNMS\Interfaces\SnmptrapHandler; use LibreNMS\Snmptrap\Trap; +use Log; class AuthenticationFailure implements SnmptrapHandler { - /** * Handle snmptrap. * Data is pre-parsed and delivered as a Trap. @@ -42,8 +42,6 @@ class AuthenticationFailure implements SnmptrapHandler */ public function handle(Device $device, Trap $trap) { - //FIXME added device hostname format helper in some branch, use that when merged - $device_array = $device->toArray(); - log_event('SNMP Trap: Authentication Failure: ' . format_hostname($device_array), $device_array, 'auth', 3, $device->hostname); + Log::event('SNMP Trap: Authentication Failure: ' . $device->displayName(), $device->device_id, 'auth', 3); } } diff --git a/LibreNMS/Snmptrap/Handlers/BgpBackwardTransition.php b/LibreNMS/Snmptrap/Handlers/BgpBackwardTransition.php index 7db2cebe27..a1a74c5472 100644 --- a/LibreNMS/Snmptrap/Handlers/BgpBackwardTransition.php +++ b/LibreNMS/Snmptrap/Handlers/BgpBackwardTransition.php @@ -55,7 +55,7 @@ class BgpBackwardTransition implements SnmptrapHandler $bgpPeer->bgpPeerState = $trap->getOidData($state_oid); if ($bgpPeer->isDirty('bgpPeerState')) { - log_event('SNMP Trap: BGP Down ' . $bgpPeer->bgpPeerIdentifier . ' ' . get_astext($bgpPeer->bgpPeerRemoteAs) . ' is now ' . $bgpPeer->bgpPeerState, $device->toArray(), 'bgpPeer', 5, $bgpPeerIp); + \Log::event('SNMP Trap: BGP Down ' . $bgpPeer->bgpPeerIdentifier . ' ' . get_astext($bgpPeer->bgpPeerRemoteAs) . ' is now ' . $bgpPeer->bgpPeerState, $device->device_id, 'bgpPeer', 5, $bgpPeerIp); } $bgpPeer->save(); diff --git a/LibreNMS/Snmptrap/Handlers/BgpEstablished.php b/LibreNMS/Snmptrap/Handlers/BgpEstablished.php index b092cebb13..e73dba3916 100644 --- a/LibreNMS/Snmptrap/Handlers/BgpEstablished.php +++ b/LibreNMS/Snmptrap/Handlers/BgpEstablished.php @@ -55,7 +55,7 @@ class BgpEstablished implements SnmptrapHandler $bgpPeer->bgpPeerState = $trap->getOidData($state_oid); if ($bgpPeer->isDirty('bgpPeerState')) { - log_event('SNMP Trap: BGP Up ' . $bgpPeer->bgpPeerIdentifier . ' ' . get_astext($bgpPeer->bgpPeerRemoteAs) . ' is now ' . $bgpPeer->bgpPeerState, $device->toArray(), 'bgpPeer', 1, $bgpPeerIp); + Log::event('SNMP Trap: BGP Up ' . $bgpPeer->bgpPeerIdentifier . ' ' . get_astext($bgpPeer->bgpPeerRemoteAs) . ' is now ' . $bgpPeer->bgpPeerState, $device->device_id, 'bgpPeer', 1, $bgpPeerIp); } $bgpPeer->save(); diff --git a/LibreNMS/Snmptrap/Handlers/EquipStatusTrap.php b/LibreNMS/Snmptrap/Handlers/EquipStatusTrap.php index 7cc9d6bb4c..067c96c6a9 100644 --- a/LibreNMS/Snmptrap/Handlers/EquipStatusTrap.php +++ b/LibreNMS/Snmptrap/Handlers/EquipStatusTrap.php @@ -28,10 +28,10 @@ namespace LibreNMS\Snmptrap\Handlers; use App\Models\Device; use LibreNMS\Interfaces\SnmptrapHandler; use LibreNMS\Snmptrap\Trap; +use Log; class EquipStatusTrap implements SnmptrapHandler { - /** * Handle snmptrap. * Data is pre-parsed and delivered as a Trap. @@ -45,7 +45,7 @@ class EquipStatusTrap implements SnmptrapHandler $state = $trap->getOidData('EQUIPMENT-MIB::equipStatus.0'); $severity = $this->getSeverity($state); - log_event('SNMP Trap: Equipment Status ' . $state, $device->toArray(), 'state', $severity); + Log::event('SNMP Trap: Equipment Status ' . $state, $device->device_id, 'state', $severity); } private function getSeverity($state) diff --git a/LibreNMS/Snmptrap/Handlers/LinkDown.php b/LibreNMS/Snmptrap/Handlers/LinkDown.php index ef04277c0b..da1b680be5 100644 --- a/LibreNMS/Snmptrap/Handlers/LinkDown.php +++ b/LibreNMS/Snmptrap/Handlers/LinkDown.php @@ -54,16 +54,14 @@ class LinkDown implements SnmptrapHandler $port->ifOperStatus = $trap->getOidData("IF-MIB::ifOperStatus.$ifIndex"); $port->ifAdminStatus = $trap->getOidData("IF-MIB::ifAdminStatus.$ifIndex"); - $device_array = $device->toArray(); - - log_event("SNMP Trap: linkDown $port->ifAdminStatus/$port->ifOperStatus " . $port->ifDescr, $device_array, 'interface', 5, $port->port_id); + Log::event("SNMP Trap: linkDown $port->ifAdminStatus/$port->ifOperStatus " . $port->ifDescr, $device->device_id, 'interface', 5, $port->port_id); if ($port->isDirty('ifAdminStatus')) { - log_event("Interface Disabled : " . $port['ifDescr'] . " (TRAP)", $device_array, "interface", 3, $port['port_id']); + Log::event("Interface Disabled : $port->ifDescr (TRAP)", $device->device_id, "interface", 3, $port->port_id); } if ($port->isDirty('ifOperStatus')) { - log_event("Interface went Down : " . $port['ifDescr'] . " (TRAP)", $device_array, "interface", 5, $port['port_id']); + Log::event("Interface went Down : $port->ifDescr (TRAP)", $device->device_id, "interface", 5, $port->port_id); } $port->save(); diff --git a/LibreNMS/Snmptrap/Handlers/LinkUp.php b/LibreNMS/Snmptrap/Handlers/LinkUp.php index 10d3dcdabc..afae582553 100644 --- a/LibreNMS/Snmptrap/Handlers/LinkUp.php +++ b/LibreNMS/Snmptrap/Handlers/LinkUp.php @@ -55,16 +55,14 @@ class LinkUp implements SnmptrapHandler $port->ifOperStatus = $trap->getOidData("IF-MIB::ifAdminStatus.$ifIndex"); $port->ifAdminStatus = $trap->getOidData("IF-MIB::ifOperStatus.$ifIndex"); - $device_array = $device->toArray(); - - log_event("SNMP Trap: linkUp $port->ifAdminStatus/$port->ifOperStatus " . $port->ifDescr, $device_array, "interface", 1, $port->port_id); + Log::event("SNMP Trap: linkUp $port->ifAdminStatus/$port->ifOperStatus " . $port->ifDescr, $device->device_id, "interface", 1, $port->port_id); if ($port->isDirty('ifAdminStatus')) { - log_event("Interface Enabled : $port->ifDescr (TRAP)", $device_array, "interface", 3, $port->port_id); + Log::event("Interface Enabled : $port->ifDescr (TRAP)", $device->device_id, "interface", 3, $port->port_id); } if ($port->isDirty('ifOperStatus')) { - log_event("Interface went Up : $port->ifDescr (TRAP)", $device_array, "interface", 1, $port->port_id); + Log::event("Interface went Up : $port->ifDescr (TRAP)", $device->device_id, "interface", 1, $port->port_id); } $port->save(); diff --git a/LibreNMS/Snmptrap/Handlers/LogTrap.php b/LibreNMS/Snmptrap/Handlers/LogTrap.php index 6f26d8efee..b4642290b5 100644 --- a/LibreNMS/Snmptrap/Handlers/LogTrap.php +++ b/LibreNMS/Snmptrap/Handlers/LogTrap.php @@ -28,6 +28,7 @@ namespace LibreNMS\Snmptrap\Handlers; use App\Models\Device; use LibreNMS\Interfaces\SnmptrapHandler; use LibreNMS\Snmptrap\Trap; +use Log; class LogTrap implements SnmptrapHandler { @@ -52,7 +53,7 @@ class LogTrap implements SnmptrapHandler $state = $trap->getOidData('LOG-MIB::logEquipStatusV2.'.$index); $severity = $this->getSeverity($state); - log_event('SNMP Trap: Log '.$logName.' '.$logEvent.' '.$logPC.' '.$logAI.' '.$state, $device->toArray(), 'log', $severity); + Log::event('SNMP Trap: Log '.$logName.' '.$logEvent.' '.$logPC.' '.$logAI.' '.$state, $device->device_id, 'log', $severity); } private function getSeverity($state) diff --git a/LibreNMS/Snmptrap/Handlers/UpsmgUtilityFailure.php b/LibreNMS/Snmptrap/Handlers/UpsmgUtilityFailure.php index 9258074a0b..f63cf99250 100644 --- a/LibreNMS/Snmptrap/Handlers/UpsmgUtilityFailure.php +++ b/LibreNMS/Snmptrap/Handlers/UpsmgUtilityFailure.php @@ -49,7 +49,6 @@ class UpsmgUtilityFailure implements SnmptrapHandler } $sensor->sensor_current = 1; $sensor->save(); - $device_array = $device->toArray(); - log_event("UPS power failed, state sensor " . $sensor->sensor_descr . " has changed to ".$sensor->sensor_current . ".", $device_array, "Power", 5); + Log::event("UPS power failed, state sensor " . $sensor->sensor_descr . " has changed to ".$sensor->sensor_current . ".", $device->device_id, "Power", 5); } } diff --git a/LibreNMS/Snmptrap/Handlers/UpsmgUtilityRestored.php b/LibreNMS/Snmptrap/Handlers/UpsmgUtilityRestored.php index 0cbb4d2f26..538dfa3705 100644 --- a/LibreNMS/Snmptrap/Handlers/UpsmgUtilityRestored.php +++ b/LibreNMS/Snmptrap/Handlers/UpsmgUtilityRestored.php @@ -47,7 +47,6 @@ class UpsmgUtilityRestored implements SnmptrapHandler } $sensor->sensor_current = 2; $sensor->save(); - $device_array = $device->toArray(); - log_event("UPS power restored, state sensor " . $sensor->sensor_descr . " has changed to ".$sensor->sensor_current . ".", $device_array, "Power", 1); + Log::event("UPS power restored, state sensor " . $sensor->sensor_descr . " has changed to ".$sensor->sensor_current . ".", $device->device_id, "Power", 1); } } diff --git a/LibreNMS/Util/Laravel.php b/LibreNMS/Util/Laravel.php index 97b4582d2c..fe7f7aa9d3 100644 --- a/LibreNMS/Util/Laravel.php +++ b/LibreNMS/Util/Laravel.php @@ -27,7 +27,6 @@ namespace LibreNMS\Util; use App; use Illuminate\Database\Events\QueryExecuted; -use LibreNMS\Config; use LibreNMS\DB\Eloquent; use Log; @@ -36,7 +35,7 @@ class Laravel public static function bootCli() { // make sure Laravel isn't already booted - if (class_exists('App') && App::isBooted()) { + if (self::isBooted()) { return; } @@ -47,6 +46,11 @@ class Laravel $kernel->bootstrap(); } + public static function isBooted() + { + return !empty(app()->isAlias('Illuminate\Foundation\Application')) && app()->isBooted(); + } + public static function enableQueryDebug() { $db = Eloquent::DB(); @@ -62,7 +66,7 @@ class Laravel return $item; })->toJson(); - if (class_exists('Log')) { + if (self::isBooted()) { Log::debug("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n", ['color' => true]); } else { c_echo("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n"); @@ -83,14 +87,14 @@ class Laravel public static function enableCliDebugOutput() { - if (class_exists('\Log') && App::runningInConsole()) { + if (self::isBooted() && App::runningInConsole()) { Log::setDefaultDriver('console'); } } public static function disableCliDebugOutput() { - if (class_exists('Log')) { + if (self::isBooted()) { Log::setDefaultDriver('logfile'); } } diff --git a/app/Facades/LogManager.php b/app/Facades/LogManager.php new file mode 100644 index 0000000000..7e6bd92054 --- /dev/null +++ b/app/Facades/LogManager.php @@ -0,0 +1,53 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2019 Tony Murray + * @author Tony Murray + */ + +namespace App\Facades; + +use Auth; + +class LogManager extends \Illuminate\Log\LogManager +{ + /** + * Log events to the event table + * + * @param string $text message describing the event + * @param \App\Models\Device|int $device device array or device_id + * @param string $type brief category for this event. Examples: sensor, state, stp, system, temperature, interface + * @param int $severity 1: ok, 2: info, 3: notice, 4: warning, 5: critical, 0: unknown + * @param int $reference the id of the referenced entity. Supported types: interface + */ + public function event($text, $device = null, $type = null, $severity = 2, $reference = null) + { + (new \App\Models\Eventlog([ + 'device_id' => $device instanceof \App\Models\Device ? $device->device_id : $device, + 'reference' => $reference, + 'type' => $type, + 'datetime' => \Carbon\Carbon::now(), + 'severity' => $severity, + 'message' => $text, + 'username' => Auth::user() ? Auth::user()->username : '', + ]))->save(); + } +} diff --git a/app/Http/Controllers/Ajax/NetCommand.php b/app/Http/Controllers/Ajax/NetCommand.php index 5e1b73fc33..e90297bee2 100644 --- a/app/Http/Controllers/Ajax/NetCommand.php +++ b/app/Http/Controllers/Ajax/NetCommand.php @@ -26,7 +26,7 @@ namespace App\Http\Controllers\Ajax; use App\Http\Controllers\Controller; -use Config; +use \LibreNMS\Config; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\Process\Process; diff --git a/app/Listeners/AuthEventListener.php b/app/Listeners/AuthEventListener.php index 83a51fea96..55ae5b0f8d 100644 --- a/app/Listeners/AuthEventListener.php +++ b/app/Listeners/AuthEventListener.php @@ -2,15 +2,12 @@ namespace App\Listeners; -use App\Checks; -use App\Events\Event; use App\Models\User; use DB; use Illuminate\Auth\Events\Login; use Illuminate\Auth\Events\Logout; use LibreNMS\Authentication\LegacyAuth; use Request; -use Session; use Toastr; class AuthEventListener diff --git a/app/Models/DeviceGroup.php b/app/Models/DeviceGroup.php index 3561d6f803..c4bb56f8a7 100644 --- a/app/Models/DeviceGroup.php +++ b/app/Models/DeviceGroup.php @@ -25,9 +25,7 @@ namespace App\Models; -use App\Util; use DB; -use Settings; class DeviceGroup extends BaseModel { @@ -116,7 +114,7 @@ class DeviceGroup extends BaseModel return $pattern; } - foreach (Settings::get('alert.macros.group', []) as $macro => $value) { + foreach (\LibreNMS\Config::get('alert.macros.group', []) as $macro => $value) { $value = str_replace(['%', '&&', '||'], ['', 'AND', 'OR'], $value); // this might need something more complex if (!str_contains($macro, ' ')) { $pattern = str_replace('macros.'.$macro, '('.$value.')', $pattern); @@ -258,14 +256,14 @@ class DeviceGroup extends BaseModel * * @param array|string $params */ - public function setParamsAttribute($params) - { - if (!Util::isJson($params)) { - $params = json_encode($params); - } - - $this->attributes['params'] = $params; - } +// public function setParamsAttribute($params) +// { +// if (!Util::isJson($params)) { +// $params = json_encode($params); +// } +// +// $this->attributes['params'] = $params; +// } /** * Check if the stored pattern is v1 diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index f47b145dd8..361296ba44 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -6,16 +6,11 @@ use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Log; use Illuminate\Support\ServiceProvider; -use Illuminate\Validation\Rule; use LibreNMS\Config; -use LibreNMS\Exceptions\DatabaseConnectException; use LibreNMS\Util\IP; use LibreNMS\Util\Validate; -use Request; use Validator; -include_once __DIR__ . '/../../includes/dbFacile.php'; - class AppServiceProvider extends ServiceProvider { /** @@ -25,11 +20,8 @@ class AppServiceProvider extends ServiceProvider */ public function boot() { - // Install legacy dbFacile fetch mode listener - \LibreNMS\DB\Eloquent::initLegacyListeners(); - - // load config - Config::load(); + $this->app->booted('\LibreNMS\DB\Eloquent::initLegacyListeners'); + $this->app->booted('\LibreNMS\Config::load'); $this->bootCustomBladeDirectives(); $this->bootCustomValidators(); @@ -43,6 +35,7 @@ class AppServiceProvider extends ServiceProvider */ public function register() { + $this->registerFacades(); $this->registerGeocoder(); } @@ -69,6 +62,14 @@ class AppServiceProvider extends ServiceProvider ]); } + private function registerFacades() + { + // replace log manager so we can add the event function + $this->app->bind('log', function ($app) { + return new \App\Facades\LogManager($app); + }); + } + private function registerGeocoder() { $this->app->alias(\LibreNMS\Interfaces\Geocoder::class, 'geocoder'); diff --git a/config/database.php b/config/database.php index 6dd714d32f..fdad62b26f 100644 --- a/config/database.php +++ b/config/database.php @@ -25,7 +25,7 @@ return [ | */ - 'default' => env('DB_CONNECTION', 'mysql'), + 'default' => env('DB_CONNECTION', env('DBTEST') ? 'testing' : 'mysql'), /* |-------------------------------------------------------------------------- @@ -68,6 +68,21 @@ return [ 'engine' => null, ], + 'testing' => [ + 'driver' => env('DB_TEST_DRIVER', 'mysql'), + 'host' => env('DB_TEST_HOST', 'localhost'), + 'port' => env('DB_TEST_PORT', ''), + 'database' => env('DB_TEST_DATABASE', 'librenms_phpunit_78hunjuybybh'), + 'username' => env('DB_TEST_USERNAME', 'root'), + 'password' => env('DB_TEST_PASSWORD', ''), + 'unix_socket' => env('DB_TEST_SOCKET', ''), + 'charset' => 'utf8', + 'collation' => 'utf8_unicode_ci', + 'prefix' => '', + 'strict' => true, + 'engine' => null, + ], + 'pgsql' => [ 'driver' => 'pgsql', 'host' => env('DB_HOST', '127.0.0.1'), diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 5349bd3f38..0b1ca80e0f 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -53,6 +53,7 @@ $factory->define(\App\Models\Port::class, function (Faker\Generator $faker) { return [ 'ifIndex' => $faker->unique()->numberBetween(), 'ifName' => $faker->text(20), + 'ifDescr' => $faker->text(255), 'ifLastChange' => $faker->unixTime(), ]; }); diff --git a/includes/common.php b/includes/common.php index 4747f54856..d61084069c 100644 --- a/includes/common.php +++ b/includes/common.php @@ -22,6 +22,7 @@ use LibreNMS\Exceptions\InvalidIpException; use LibreNMS\Util\Git; use LibreNMS\Util\Html; use LibreNMS\Util\IP; +use LibreNMS\Util\Laravel; function generate_priority_label($priority) { @@ -663,7 +664,7 @@ if (!function_exists('d_echo')) { { global $debug; - if (class_exists('\Log')) { + if (Laravel::isBooted()) { \Log::debug(is_string($text) ? rtrim($text) : $text); } elseif ($debug) { print_r($text); diff --git a/includes/dbFacile.php b/includes/dbFacile.php index e7917f6e58..89ab5bbc48 100644 --- a/includes/dbFacile.php +++ b/includes/dbFacile.php @@ -21,6 +21,7 @@ use Illuminate\Database\QueryException; use LibreNMS\Config; use LibreNMS\Exceptions\DatabaseConnectException; use LibreNMS\DB\Eloquent; +use LibreNMS\Util\Laravel; function dbIsConnected() { @@ -463,11 +464,9 @@ function dbHandleException(QueryException $exception) } } - foreach ($exception->getTrace() as $trace) { - $message .= "\n " . $trace['file'] . ':' . $trace['line']; - } + $message .= $exception->getTraceAsString(); - if (class_exists('Log')) { + if (Laravel::isBooted()) { Log::error($message); } else { c_echo("%rSQL Error!%n "); diff --git a/includes/functions.php b/includes/functions.php index 72e832f56c..c50f19be06 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -998,7 +998,7 @@ function send_mail($emails, $subject, $message, $html = false) } $mail->send(); return true; - } catch (phpmailerException $e) { + } catch (\PHPMailer\PHPMailer\Exception $e) { return $e->errorMessage(); } catch (Exception $e) { return $e->getMessage(); diff --git a/includes/helpers.php b/includes/helpers.php index 39d6689b2e..d854c5bae0 100644 --- a/includes/helpers.php +++ b/includes/helpers.php @@ -23,6 +23,8 @@ * @author Tony Murray */ +use LibreNMS\Util\Laravel; + if (!function_exists('d_echo')) { /** * Legacy convenience function - please use this instead of 'if ($debug) { echo ...; }' @@ -35,7 +37,7 @@ if (!function_exists('d_echo')) { { global $debug; - if (class_exists('\Log')) { + if (Laravel::isBooted()) { \Log::debug(is_string($text) ? rtrim($text) : $text); } elseif ($debug) { print_r($text); diff --git a/includes/polling/ipmi.inc.php b/includes/polling/ipmi.inc.php index 9773956eea..ef2ead1008 100644 --- a/includes/polling/ipmi.inc.php +++ b/includes/polling/ipmi.inc.php @@ -1,5 +1,6 @@ + diff --git a/resources/views/locations.blade.php b/resources/views/locations.blade.php index 0dd87c437d..5f367f9f4c 100644 --- a/resources/views/locations.blade.php +++ b/resources/views/locations.blade.php @@ -148,7 +148,7 @@ locationId = $btn.data('id'); if (locationMap === null) { - config = {"tile_url": "{{ Config::get('leaflet.tile_url', '{s}.tile.openstreetmap.org') }}"}; + config = {"tile_url": "{{ \LibreNMS\Config::get('leaflet.tile_url', '{s}.tile.openstreetmap.org') }}"}; locationMap = init_map('location-edit-map', '{{ $maps_engine }}', '{{ $maps_api }}', config); locationMarker = init_map_marker(locationMap, location); } diff --git a/resources/views/widgets/graylog.blade.php b/resources/views/widgets/graylog.blade.php index f131f1e7ec..b58132fcb9 100644 --- a/resources/views/widgets/graylog.blade.php +++ b/resources/views/widgets/graylog.blade.php @@ -19,11 +19,11 @@ rowCount: ['{{ $limit }}', 25,50,100,250,-1], formatters: { "browserTime": function(column, row) { - @if(Config::get('graylog.timezone')) + @config('graylog.timezone') return row.timestamp; @else return moment.parseZone(row.timestamp).local().format("YYYY-MM-DD HH:MM:SS"); - @endif + @endconfig } }, post: function () diff --git a/tests/AuthSSOTest.php b/tests/AuthSSOTest.php index 98686ffca5..e4bec0b83b 100644 --- a/tests/AuthSSOTest.php +++ b/tests/AuthSSOTest.php @@ -472,11 +472,10 @@ class AuthSSOTest extends DBTestCase public function tearDown() { - parent::tearDown(); Config::set('auth_mechanism', $this->original_auth_mech); Config::forget('sso'); $this->breakUser(); - $_SERVER = $this->server; + parent::tearDown(); } } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 99b1171db5..1e3e1073ea 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -28,7 +28,7 @@ namespace LibreNMS\Tests; use LibreNMS\Config; use LibreNMS\DB\Eloquent; -class ConfigTest extends TestCase +class ConfigTest extends LaravelTestCase { public function testGetBasic() { diff --git a/tests/DBSetupTest.php b/tests/DBSetupTest.php index 34d82ddcba..c150722aae 100644 --- a/tests/DBSetupTest.php +++ b/tests/DBSetupTest.php @@ -29,6 +29,14 @@ use \PHPUnit\Framework\ExpectationFailedException as PHPUnitException; class DBSetupTest extends DBTestCase { + protected $db_name; + + public function setUp() + { + parent::setUp(); + $this->db_name = dbFetchCell('SELECT DATABASE()'); + } + public function testSetupDB() { global $schema; diff --git a/tests/DBTestCase.php b/tests/DBTestCase.php index dddd6805e1..26d25bac3d 100644 --- a/tests/DBTestCase.php +++ b/tests/DBTestCase.php @@ -25,15 +25,13 @@ namespace LibreNMS\Tests; -abstract class DBTestCase extends TestCase +abstract class DBTestCase extends LaravelTestCase { - protected $db_name; - public function setUp() { parent::setUp(); $this->dbSetUp(); - $this->db_name = dbFetchCell('SELECT DATABASE()'); + set_debug(false); } public function tearDown() diff --git a/tests/Feature/SnmpTraps/BgpTrapTest.php b/tests/Feature/SnmpTraps/BgpTrapTest.php new file mode 100644 index 0000000000..b756409f63 --- /dev/null +++ b/tests/Feature/SnmpTraps/BgpTrapTest.php @@ -0,0 +1,84 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2019 Tony Murray + * @author Tony Murray + */ + +namespace LibreNMS\Tests\Feature\SnmpTraps; + +use App\Models\BgpPeer; +use App\Models\Device; +use Illuminate\Foundation\Testing\DatabaseTransactions; +use LibreNMS\Snmptrap\Dispatcher; +use LibreNMS\Snmptrap\Trap; +use LibreNMS\Tests\LaravelTestCase; + +class BgpTrapTest extends LaravelTestCase +{ + use DatabaseTransactions; + + public function testBgpUp() + { + $device = factory(Device::class)->create(); + $bgppeer = factory(BgpPeer::class)->make(['bgpPeerState' => 'idle']); + $device->bgppeers()->save($bgppeer); + + $trapText = "$device->hostname +UDP: [$device->ip]:57602->[192.168.5.5]:162 +DISMAN-EVENT-MIB::sysUpTimeInstance 302:12:56:24.81 +SNMPv2-MIB::snmpTrapOID.0 BGP4-MIB::bgpEstablished +BGP4-MIB::bgpPeerLastError.$bgppeer->bgpPeerIdentifier \"04 00 \" +BGP4-MIB::bgpPeerState.$bgppeer->bgpPeerIdentifier established\n"; + + $message = "SNMP Trap: BGP Up $bgppeer->bgpPeerIdentifier " . get_astext($bgppeer->bgpPeerRemoteAs) . " is now established"; + \Log::shouldReceive('event')->once()->with($message, $device->device_id, 'bgpPeer', 1, $bgppeer->bgpPeerIdentifier); + + $trap = new Trap($trapText); + $this->assertTrue(Dispatcher::handle($trap), 'Could not handle bgpEstablished'); + + $bgppeer = $bgppeer->fresh(); // refresh from database + $this->assertEquals($bgppeer->bgpPeerState, 'established'); + } + + public function testBgpDown() + { + $device = factory(Device::class)->create(); + $bgppeer = factory(BgpPeer::class)->make(['bgpPeerState' => 'established']); + $device->bgppeers()->save($bgppeer); + + $trapText = "$device->hostname +UDP: [$device->ip]:57602->[185.29.68.52]:162 +DISMAN-EVENT-MIB::sysUpTimeInstance 302:12:55:33.47 +SNMPv2-MIB::snmpTrapOID.0 BGP4-MIB::bgpBackwardTransition +BGP4-MIB::bgpPeerLastError.$bgppeer->bgpPeerIdentifier \"04 00 \" +BGP4-MIB::bgpPeerState.$bgppeer->bgpPeerIdentifier idle\n"; + + $message = "SNMP Trap: BGP Down $bgppeer->bgpPeerIdentifier " . get_astext($bgppeer->bgpPeerRemoteAs) . " is now idle"; + \Log::shouldReceive('event')->once()->with($message, $device->device_id, 'bgpPeer', 5, $bgppeer->bgpPeerIdentifier); + + $trap = new Trap($trapText); + $this->assertTrue(Dispatcher::handle($trap), 'Could not handle bgpBackwardTransition'); + + $bgppeer = $bgppeer->fresh(); // refresh from database + $this->assertEquals($bgppeer->bgpPeerState, 'idle'); + } +} diff --git a/tests/Feature/SnmpTraps/CommonTrapTest.php b/tests/Feature/SnmpTraps/CommonTrapTest.php new file mode 100644 index 0000000000..5577ac1e00 --- /dev/null +++ b/tests/Feature/SnmpTraps/CommonTrapTest.php @@ -0,0 +1,85 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2019 Tony Murray + * @author Tony Murray + */ + +namespace LibreNMS\Tests; + +use App\Models\Device; +use App\Models\Ipv4Address; +use App\Models\Port; +use Illuminate\Foundation\Testing\DatabaseTransactions; +use LibreNMS\Snmptrap\Dispatcher; +use LibreNMS\Snmptrap\Trap; +use LibreNMS\Tests\Feature\SnmpTraps\TrapTestCase; +use Log; + +class CommonTrapTest extends LaravelTestCase +{ + use DatabaseTransactions; + + public function testGarbage() + { + $trapText = "Garbage\n"; + + $trap = new Trap($trapText); + $this->assertFalse(Dispatcher::handle($trap), 'Found handler for trap with no snmpTrapOID'); + } + + public function testFindByIp() + { + $device = factory(Device::class)->create(); + $port = factory(Port::class)->make(); + $device->ports()->save($port); + $ipv4 = factory(Ipv4Address::class)->make(); // test ipv4 lookup of device + $port->ipv4()->save($ipv4); + + $trapText = "something +UDP: [$ipv4->ipv4_address]:64610->[192.168.5.5]:162 +DISMAN-EVENT-MIB::sysUpTimeInstance 198:2:10:48.91\n"; + + $trap = new Trap($trapText); + $this->assertFalse(Dispatcher::handle($trap), 'Found handler for trap with no snmpTrapOID'); + + // check that the device was found + $this->assertEquals($device->hostname, $trap->getDevice()->hostname); + } + + public function testAuthorization() + { + $device = factory(Device::class)->create(); + + $trapText = "$device->hostname +UDP: [$device->ip]:64610->[192.168.5.5]:162 +DISMAN-EVENT-MIB::sysUpTimeInstance 198:2:10:48.91 +SNMPv2-MIB::snmpTrapOID.0 SNMPv2-MIB::authenticationFailure\n"; + + Log::shouldReceive('event')->once()->with('SNMP Trap: Authentication Failure: ' . $device->displayName(), $device->device_id, 'auth', 3); + + $trap = new Trap($trapText); + $this->assertTrue(Dispatcher::handle($trap)); + + // check that the device was found + $this->assertEquals($device->hostname, $trap->getDevice()->hostname); + } +} diff --git a/tests/Feature/SnmpTraps/PortsTrapTest.php b/tests/Feature/SnmpTraps/PortsTrapTest.php new file mode 100644 index 0000000000..6273ddf632 --- /dev/null +++ b/tests/Feature/SnmpTraps/PortsTrapTest.php @@ -0,0 +1,102 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2019 Tony Murray + * @author Tony Murray + */ + +namespace LibreNMS\Tests\Feature\SnmpTraps; + +use App\Models\Device; +use App\Models\Port; +use Illuminate\Foundation\Testing\DatabaseTransactions; +use LibreNMS\Snmptrap\Dispatcher; +use LibreNMS\Snmptrap\Trap; +use LibreNMS\Tests\DBTestCase; +use LibreNMS\Tests\LaravelTestCase; +use Log; +use Mockery\Mock; + +class PortsTrapTest extends LaravelTestCase +{ + use DatabaseTransactions; + + public function testLinkDown() + { + // make a device and associate a port with it + $device = factory(Device::class)->create(); + $port = factory(Port::class)->make(['ifAdminStatus' => 'up', 'ifOperStatus' => 'up']); + $device->ports()->save($port); + + $trapText = " +UDP: [$device->ip]:57123->[192.168.4.4]:162 +DISMAN-EVENT-MIB::sysUpTimeInstance 2:15:07:12.87 +SNMPv2-MIB::snmpTrapOID.0 IF-MIB::linkDown +IF-MIB::ifIndex.$port->ifIndex $port->ifIndex +IF-MIB::ifAdminStatus.$port->ifIndex down +IF-MIB::ifOperStatus.$port->ifIndex down +IF-MIB::ifDescr.$port->ifIndex GigabitEthernet0/5 +IF-MIB::ifType.$port->ifIndex ethernetCsmacd +OLD-CISCO-INTERFACES-MIB::locIfReason.$port->ifIndex \"down\"\n"; + + Log::shouldReceive('event')->once()->with("SNMP Trap: linkDown down/down " . $port->ifDescr, $device->device_id, 'interface', 5, $port->port_id); + Log::shouldReceive('event')->once()->with("Interface Disabled : $port->ifDescr (TRAP)", $device->device_id, 'interface', 3, $port->port_id); + Log::shouldReceive('event')->once()->with("Interface went Down : $port->ifDescr (TRAP)", $device->device_id, 'interface', 5, $port->port_id); + + $trap = new Trap($trapText); + $this->assertTrue(Dispatcher::handle($trap), 'Could not handle linkDown'); + + + $port = $port->fresh(); // refresh from database + $this->assertEquals($port->ifAdminStatus, 'down'); + $this->assertEquals($port->ifOperStatus, 'down'); + } + + public function testLinkUp() + { + // make a device and associate a port with it + $device = factory(Device::class)->create(); + $port = factory(Port::class)->make(['ifAdminStatus' => 'down', 'ifOperStatus' => 'down']); + $device->ports()->save($port); + + $trapText = " +UDP: [$device->ip]:57123->[185.29.68.52]:162 +DISMAN-EVENT-MIB::sysUpTimeInstance 2:15:07:18.21 +SNMPv2-MIB::snmpTrapOID.0 IF-MIB::linkUp +IF-MIB::ifIndex.$port->ifIndex $port->ifIndex +IF-MIB::ifAdminStatus.$port->ifIndex up +IF-MIB::ifOperStatus.$port->ifIndex up +IF-MIB::ifDescr.$port->ifIndex GigabitEthernet0/5 +IF-MIB::ifType.$port->ifIndex ethernetCsmacd +OLD-CISCO-INTERFACES-MIB::locIfReason.$port->ifIndex \"up\"\n"; + + Log::shouldReceive('event')->once()->with("SNMP Trap: linkUp up/up " . $port->ifDescr, $device->device_id, 'interface', 1, $port->port_id); + Log::shouldReceive('event')->once()->with("Interface Enabled : $port->ifDescr (TRAP)", $device->device_id, 'interface', 3, $port->port_id); + Log::shouldReceive('event')->once()->with("Interface went Up : $port->ifDescr (TRAP)", $device->device_id, 'interface', 1, $port->port_id); + + $trap = new Trap($trapText); + $this->assertTrue(Dispatcher::handle($trap), 'Could not handle linkUp'); + + $port = $port->fresh(); // refresh from database + $this->assertEquals($port->ifAdminStatus, 'up'); + $this->assertEquals($port->ifOperStatus, 'up'); + } +} diff --git a/tests/LaravelTestCase.php b/tests/LaravelTestCase.php index d4ac6ebfec..53c29bfe5b 100644 --- a/tests/LaravelTestCase.php +++ b/tests/LaravelTestCase.php @@ -8,4 +8,22 @@ abstract class LaravelTestCase extends BaseTestCase { use CreatesApplication; use SnmpsimHelpers; + + public function dbSetUp() + { + if (getenv('DBTEST')) { + \LibreNMS\DB\Eloquent::boot(); + \LibreNMS\DB\Eloquent::setStrictMode(); + \LibreNMS\DB\Eloquent::DB()->beginTransaction(); + } else { + $this->markTestSkipped('Database tests not enabled. Set DBTEST=1 to enable.'); + } + } + + public function dbTearDown() + { + if (getenv('DBTEST')) { + \LibreNMS\DB\Eloquent::DB()->rollBack(); + } + } } diff --git a/tests/OSDiscoveryTest.php b/tests/OSDiscoveryTest.php index da879df39c..b2001edcea 100644 --- a/tests/OSDiscoveryTest.php +++ b/tests/OSDiscoveryTest.php @@ -26,7 +26,6 @@ namespace LibreNMS\Tests; use LibreNMS\Config; -use PHPUnit_Framework_ExpectationFailedException as PHPUnitException; class OSDiscoveryTest extends TestCase { @@ -69,7 +68,7 @@ class OSDiscoveryTest extends TestCase }); if (empty($files)) { - throw new PHPUnitException("No snmprec files found for $os_name!"); + $this->fail("No snmprec files found for $os_name!"); } foreach ($files as $file) { diff --git a/tests/OSModulesTest.php b/tests/OSModulesTest.php index f2a70ae890..a7cf37d5b1 100644 --- a/tests/OSModulesTest.php +++ b/tests/OSModulesTest.php @@ -60,6 +60,7 @@ class OSModulesTest extends DBTestCase { $this->requireSnmpsim(); // require snmpsim for tests global $snmpsim; + load_all_os(); // wiped out by application refresh try { $helper = new ModuleTestHelper($modules, $os, $variant); @@ -112,7 +113,6 @@ class OSModulesTest extends DBTestCase } } - public function dumpedDataProvider() { $modules = array(); diff --git a/tests/SnmpTrapTest.php b/tests/SnmpTrapTest.php deleted file mode 100644 index 853c1872d7..0000000000 --- a/tests/SnmpTrapTest.php +++ /dev/null @@ -1,179 +0,0 @@ -. - * - * @package LibreNMS - * @link http://librenms.org - * @copyright 2018 Tony Murray - * @author Tony Murray - */ - -namespace LibreNMS\Tests; - -use App\Models\BgpPeer; -use App\Models\Device; -use App\Models\Ipv4Address; -use App\Models\Port; -use Illuminate\Foundation\Testing\DatabaseTransactions; -use LibreNMS\Snmptrap\Dispatcher; -use LibreNMS\Snmptrap\Trap; - -class SnmpTrapTest extends LaravelTestCase -{ - use DatabaseTransactions; - - public function testGarbage() - { - $trapText = "Garbage\n"; - - $trap = new Trap($trapText); - $this->assertFalse(Dispatcher::handle($trap), 'Found handler for trap with no snmpTrapOID'); - } - - public function testFindByIp() - { - $device = factory(Device::class)->create(); - $port = factory(Port::class)->make(); - $device->ports()->save($port); - $ipv4 = factory(Ipv4Address::class)->make(); // test ipv4 lookup of device - $port->ipv4()->save($ipv4); - - $trapText = "something -UDP: [$ipv4->ipv4_address]:64610->[192.168.5.5]:162 -DISMAN-EVENT-MIB::sysUpTimeInstance 198:2:10:48.91\n"; - - $trap = new Trap($trapText); - $this->assertFalse(Dispatcher::handle($trap), 'Found handler for trap with no snmpTrapOID'); - - // check that the device was found - $this->assertEquals($device->hostname, $trap->getDevice()->hostname); - } - - public function testAuthorization() - { - $device = factory(Device::class)->create(); - - $trapText = "$device->hostname -UDP: [$device->ip]:64610->[192.168.5.5]:162 -DISMAN-EVENT-MIB::sysUpTimeInstance 198:2:10:48.91 -SNMPv2-MIB::snmpTrapOID.0 SNMPv2-MIB::authenticationFailure\n"; - - $trap = new Trap($trapText); - $this->assertTrue(Dispatcher::handle($trap)); - - // check that the device was found - $this->assertEquals($device->hostname, $trap->getDevice()->hostname); - -// $event = \App\Models\LogEvent::orderBy('datetime', 'desc')->first(); - -// dd($event); - } - - public function testBgpUp() - { - $device = factory(Device::class)->create(); - $bgppeer = factory(BgpPeer::class)->make(['bgpPeerState' => 'idle']); - $device->bgppeers()->save($bgppeer); - - $trapText = "$device->hostname -UDP: [$device->ip]:57602->[192.168.5.5]:162 -DISMAN-EVENT-MIB::sysUpTimeInstance 302:12:56:24.81 -SNMPv2-MIB::snmpTrapOID.0 BGP4-MIB::bgpEstablished -BGP4-MIB::bgpPeerLastError.$bgppeer->bgpPeerIdentifier \"04 00 \" -BGP4-MIB::bgpPeerState.$bgppeer->bgpPeerIdentifier established\n"; - - $trap = new Trap($trapText); - $this->assertTrue(Dispatcher::handle($trap), 'Could not handle bgpEstablished'); - - $bgppeer = $bgppeer->fresh(); // refresh from database - $this->assertEquals($bgppeer->bgpPeerState, 'established'); - } - - public function testBgpDown() - { - $device = factory(Device::class)->create(); - $bgppeer = factory(BgpPeer::class)->make(['bgpPeerState' => 'established']); - $device->bgppeers()->save($bgppeer); - - $trapText = "$device->hostname -UDP: [$device->ip]:57602->[185.29.68.52]:162 -DISMAN-EVENT-MIB::sysUpTimeInstance 302:12:55:33.47 -SNMPv2-MIB::snmpTrapOID.0 BGP4-MIB::bgpBackwardTransition -BGP4-MIB::bgpPeerLastError.$bgppeer->bgpPeerIdentifier \"04 00 \" -BGP4-MIB::bgpPeerState.$bgppeer->bgpPeerIdentifier idle\n"; - - $trap = new Trap($trapText); - $this->assertTrue(Dispatcher::handle($trap), 'Could not handle bgpBackwardTransition'); - - $bgppeer = $bgppeer->fresh(); // refresh from database - $this->assertEquals($bgppeer->bgpPeerState, 'idle'); - } - - public function testLinkDown() - { - // make a device and associate a port with it - $device = factory(Device::class)->create(); - $port = factory(Port::class)->make(['ifAdminStatus' => 'up', 'ifOperStatus' => 'up']); - $device->ports()->save($port); - - $trapText = " -UDP: [$device->ip]:57123->[192.168.4.4]:162 -DISMAN-EVENT-MIB::sysUpTimeInstance 2:15:07:12.87 -SNMPv2-MIB::snmpTrapOID.0 IF-MIB::linkDown -IF-MIB::ifIndex.$port->ifIndex $port->ifIndex -IF-MIB::ifAdminStatus.$port->ifIndex down -IF-MIB::ifOperStatus.$port->ifIndex down -IF-MIB::ifDescr.$port->ifIndex GigabitEthernet0/5 -IF-MIB::ifType.$port->ifIndex ethernetCsmacd -OLD-CISCO-INTERFACES-MIB::locIfReason.$port->ifIndex \"down\"\n"; - - $trap = new Trap($trapText); - $this->assertTrue(Dispatcher::handle($trap), 'Could not handle linkDown'); - - - $port = $port->fresh(); // refresh from database - $this->assertEquals($port->ifAdminStatus, 'down'); - $this->assertEquals($port->ifOperStatus, 'down'); - } - - public function testLinkUp() - { - // make a device and associate a port with it - $device = factory(Device::class)->create(); - $port = factory(Port::class)->make(['ifAdminStatus' => 'down', 'ifOperStatus' => 'down']); - $device->ports()->save($port); - - $trapText = " -UDP: [$device->ip]:57123->[185.29.68.52]:162 -DISMAN-EVENT-MIB::sysUpTimeInstance 2:15:07:18.21 -SNMPv2-MIB::snmpTrapOID.0 IF-MIB::linkUp -IF-MIB::ifIndex.$port->ifIndex $port->ifIndex -IF-MIB::ifAdminStatus.$port->ifIndex up -IF-MIB::ifOperStatus.$port->ifIndex up -IF-MIB::ifDescr.$port->ifIndex GigabitEthernet0/5 -IF-MIB::ifType.$port->ifIndex ethernetCsmacd -OLD-CISCO-INTERFACES-MIB::locIfReason.$port->ifIndex \"up\"\n"; - - $trap = new Trap($trapText); - $this->assertTrue(Dispatcher::handle($trap), 'Could not handle linkUp'); - - $port = $port->fresh(); // refresh from database - $this->assertEquals($port->ifAdminStatus, 'up'); - $this->assertEquals($port->ifOperStatus, 'up'); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index 218d5c7b52..b6138d84ee 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -42,6 +42,12 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase $this->snmpsim = $snmpsim; } + public function setUp() + { + parent::setUp(); + set_debug(false); // prevent warnings from stopping execution for legacy code + } + public function dbSetUp() { if (getenv('DBTEST')) { diff --git a/tests/YamlTest.php b/tests/YamlTest.php index b47d842887..2fd1874b22 100644 --- a/tests/YamlTest.php +++ b/tests/YamlTest.php @@ -28,7 +28,7 @@ namespace LibreNMS\Tests; use JsonSchema\Constraints\Constraint; use JsonSchema\Exception\JsonDecodingException; use LibreNMS\Config; -use PHPUnit_Framework_ExpectationFailedException as PHPUnitException; +use PHPUnit\Framework\ExpectationFailedException; use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Yaml; @@ -60,7 +60,7 @@ class YamlTest extends TestCase try { $data = Yaml::parse(file_get_contents($path)); } catch (ParseException $e) { - throw new PHPUnitException("$path Could not be parsed", null, $e); + throw new ExpectationFailedException("$path Could not be parsed", null, $e); } try { diff --git a/tests/snmpsim/fabos_1.snmprec b/tests/snmpsim/fabos_1.snmprec index fd5c7ec429..b9feba5777 100644 --- a/tests/snmpsim/fabos_1.snmprec +++ b/tests/snmpsim/fabos_1.snmprec @@ -1,2 +1,2 @@ -1.3.6.1.2.1.1.2.0|4|FC5022 16Gb SAN Scalable Switch:IBM Flex System FC5022 24-port 16Gb ESB SAN Scalable Switch +1.3.6.1.2.1.1.1.0|4|FC5022 16Gb SAN Scalable Switch:IBM Flex System FC5022 24-port 16Gb ESB SAN Scalable Switch 1.3.6.1.2.1.1.2.0|6|1.3.6.1.4.1.1588.2.1.1.117