Fix device missing from traps/new Log::event() (#9963)

* Fix device missing from traps/new Log::event()
This commit is contained in:
Tony Murray
2019-03-16 03:24:59 -05:00
committed by TheGreatDoc
parent 907579dbef
commit 3825133602
3 changed files with 32 additions and 3 deletions

View File

@@ -26,12 +26,12 @@
namespace LibreNMS\Tests;
use App\Models\Device;
use App\Models\Eventlog;
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
@@ -58,6 +58,13 @@ class CommonTrapTest extends LaravelTestCase
UDP: [$ipv4->ipv4_address]:64610->[192.168.5.5]:162
DISMAN-EVENT-MIB::sysUpTimeInstance 198:2:10:48.91\n";
Log::shouldReceive('info')->once()->with('Unhandled trap snmptrap', ['device' => $device->hostname, 'oid' => null]);
Log::shouldReceive('event')->once()->withArgs(function ($e_message, $e_device, $e_type) use ($device) {
return $e_message == 'SNMP trap received: ' &&
$device->is($e_device) &&
$e_type == 'trap';
});
$trap = new Trap($trapText);
$this->assertFalse(Dispatcher::handle($trap), 'Found handler for trap with no snmpTrapOID');
@@ -65,6 +72,28 @@ DISMAN-EVENT-MIB::sysUpTimeInstance 198:2:10:48.91\n";
$this->assertEquals($device->hostname, $trap->getDevice()->hostname);
}
public function testGenericTrap()
{
$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::someOid\n";
$trap = new Trap($trapText);
$this->assertFalse(Dispatcher::handle($trap));
$this->assertEquals([
'device_id' => $device->device_id,
'message' => 'SNMP trap received: SNMPv2-MIB::someOid',
'type' => 'trap',
'reference' => null,
'username' => '',
'severity' => 2,
], Eventlog::orderBy('event_id', 'asc')->select(['device_id', 'message', 'type', 'reference', 'username', 'severity'])->first()->toArray());
}
public function testAuthorization()
{
$device = factory(Device::class)->create();