* 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
This commit is contained in:
Tony Murray 2019-03-12 23:59:03 -05:00 committed by GitHub
parent 17b5d7f0a3
commit cb005210d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
43 changed files with 460 additions and 278 deletions

View File

@ -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)

View File

@ -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

View File

@ -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();
}

View File

@ -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)
{

View File

@ -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);
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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)

View File

@ -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();

View File

@ -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();

View File

@ -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)

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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');
}
}

View File

@ -0,0 +1,53 @@
<?php
/**
* Log.php
*
* Extending the built in logging to add an event logger function
*
* 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 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
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();
}
}

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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');

View File

@ -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'),

View File

@ -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(),
];
});

View File

@ -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);

View File

@ -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 ");

View File

@ -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();

View File

@ -23,6 +23,8 @@
* @author Tony Murray <murraytony@gmail.com>
*/
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);

View File

@ -1,5 +1,6 @@
<?php
use LibreNMS\Config;
use LibreNMS\RRD\RrdDefinition;
$ipmi_rows = dbFetchRows("SELECT * FROM sensors WHERE device_id = ? AND poller_type='ipmi'", array($device['device_id']));

View File

@ -39,5 +39,6 @@
<env name="MAIL_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="testing"/>
</php>
</phpunit>

View File

@ -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);
}

View File

@ -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 ()

View File

@ -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();
}
}

View File

@ -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()
{

View File

@ -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;

View File

@ -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()

View File

@ -0,0 +1,84 @@
<?php
/**
* BgpTrapTest.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 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
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');
}
}

View File

@ -0,0 +1,85 @@
<?php
/**
* CommonTrapTest.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 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
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);
}
}

View File

@ -0,0 +1,102 @@
<?php
/**
* PortsTrapTest.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 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
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 = "<UNKNOWN>
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 = "<UNKNOWN>
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');
}
}

View File

@ -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();
}
}
}

View File

@ -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) {

View File

@ -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();

View File

@ -1,179 +0,0 @@
<?php
/**
* SnmpTrapTest.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 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
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 = "<UNKNOWN>
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 = "<UNKNOWN>
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');
}
}

View File

@ -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')) {

View File

@ -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 {

View File

@ -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