Refactor tests (#10625)

* Refactor tests
Boot Laravel for all tests.
Config use private static property for storage instead of global

* Backup/restore modules

* disable snmpsim log

* Fixing DBTestCase

* Fix macros loading to the wrong place

* trap and other tests should check if db is available

* don't include snmp.inc.php if mock.snmp.inc.php is already included...

* fix migration

* if we don't reset the db, run migrations at least.

* set vars for migrate too

* Fix style

* ignore issues with undefined indexes in legacy code
This commit is contained in:
Tony Murray
2019-10-13 13:40:38 +00:00
committed by GitHub
parent 4cf584b425
commit 63442e8b4a
48 changed files with 298 additions and 324 deletions

View File

@@ -25,6 +25,7 @@
namespace LibreNMS\Authentication;
use Illuminate\Support\Arr;
use LibreNMS\Config;
use LibreNMS\Exceptions\AuthenticationException;
use LibreNMS\Exceptions\InvalidIpException;
@@ -120,14 +121,21 @@ class SSOAuthorizer extends MysqlAuthorizer
return false;
}
foreach (Config::get('sso.trusted_proxies') as $value) {
$proxies = Config::get('sso.trusted_proxies');
if (is_array($proxies)) {
foreach ($proxies as $value) {
$proxy = IP::parse($value);
if ($proxies == '8.8.8.0/25') {
dd($source->innetwork((string) $proxy));
}
if ($source->innetwork((string) $proxy)) {
// Proxy matches trusted subnet
return true;
}
}
}
// No match, proxy is untrusted
return false;
} catch (InvalidIpException $e) {
@@ -182,7 +190,7 @@ class SSOAuthorizer extends MysqlAuthorizer
public function authSSOParseGroups()
{
// Parse a delimited group list
$groups = explode(Config::get('sso.group_delimiter'), $this->authSSOGetAttr(Config::get('sso.group_attr')));
$groups = explode(Config::get('sso.group_delimiter', ';'), $this->authSSOGetAttr(Config::get('sso.group_attr')));
$valid_groups = array();

View File

@@ -32,14 +32,18 @@ use LibreNMS\DB\Eloquent;
class Config
{
private static $config;
/**
* Load the config, if the database connected, pull in database settings.
*
* return &array
*/
public static function &load()
public static function load()
{
global $config;
if (!is_null(self::$config)) {
return self::$config;
}
self::loadFiles();
@@ -58,7 +62,21 @@ class Config
self::processConfig(false);
}
return $config;
// set to global for legacy/external things
global $config;
$config = self::$config;
return self::$config;
}
/**
* Reload the config from files/db
* @return mixed
*/
public static function reload()
{
self::$config = null;
return self::load();
}
/**
@@ -69,8 +87,6 @@ class Config
*/
private static function &loadFiles()
{
global $config;
$config = []; // start fresh
$install_dir = realpath(__DIR__ . '/../');
@@ -82,12 +98,15 @@ class Config
// import standard settings
$macros = json_decode(file_get_contents($install_dir . '/misc/macros.json'), true);
self::set('alert.macros.rule', $macros);
$config['alert']['macros']['rule'] = $macros;
// Load user config
@include $install_dir . '/config.php';
return $config;
// set it
self::$config = $config;
return self::$config;
}
@@ -100,10 +119,8 @@ class Config
*/
public static function get($key, $default = null)
{
global $config;
if (isset($config[$key])) {
return $config[$key];
if (isset(self::$config[$key])) {
return self::$config[$key];
}
if (!str_contains($key, '.')) {
@@ -112,7 +129,7 @@ class Config
$keys = explode('.', $key);
$curr = &$config;
$curr = &self::$config;
foreach ($keys as $k) {
// do not add keys that don't exist
if (!isset($curr[$k])) {
@@ -136,8 +153,7 @@ class Config
*/
public static function forget($key)
{
global $config;
Arr::forget($config, $key);
Arr::forget(self::$config, $key);
}
/**
@@ -175,11 +191,9 @@ class Config
*/
public static function getOsSetting($os, $key, $default = null)
{
global $config;
if ($os) {
if (isset($config['os'][$os][$key])) {
return $config['os'][$os][$key];
if (isset(self::$config['os'][$os][$key])) {
return self::$config['os'][$os][$key];
}
if (!str_contains($key, '.')) {
@@ -207,13 +221,11 @@ class Config
*/
public static function getCombined($os, $key, $default = array())
{
global $config;
if (!self::has($key)) {
return self::get("os.$os.$key", $default);
}
if (!isset($config['os'][$os][$key])) {
if (!isset(self::$config['os'][$os][$key])) {
if (!str_contains($key, '.')) {
return self::get($key, $default);
}
@@ -241,8 +253,6 @@ class Config
*/
public static function set($key, $value, $persist = false, $default = null, $descr = null, $group = null, $sub_group = null)
{
global $config;
if ($persist) {
try {
\App\Models\Config::updateOrCreate(['config_name' => $key], collect([
@@ -267,7 +277,7 @@ class Config
$keys = explode('.', $key);
$curr = &$config;
$curr = &self::$config;
foreach ($keys as $k) {
$curr = &$curr[$k];
}
@@ -283,9 +293,7 @@ class Config
*/
public static function has($key)
{
global $config;
if (isset($config[$key])) {
if (isset(self::$config[$key])) {
return true;
}
@@ -296,7 +304,7 @@ class Config
$keys = explode('.', $key);
$last = array_pop($keys);
$curr = &$config;
$curr = &self::$config;
foreach ($keys as $k) {
// do not add keys that don't exist
if (!isset($curr[$k])) {
@@ -315,9 +323,7 @@ class Config
*/
public static function json_encode()
{
global $config;
return json_encode($config);
return json_encode(self::$config);
}
/**
@@ -326,8 +332,7 @@ class Config
*/
public static function getAll()
{
global $config;
return $config;
return self::$config;
}
/**
@@ -336,8 +341,6 @@ class Config
*/
private static function mergeDb()
{
global $config;
$db_config = [];
try {
@@ -349,13 +352,11 @@ class Config
// possibly table config doesn't exist yet
}
$config = array_replace_recursive($db_config, $config);
self::$config = array_replace_recursive($db_config, self::$config);
}
private static function loadGraphsFromDb()
{
global $config;
try {
$graph_types = GraphType::all()->toArray();
} catch (QueryException $e) {
@@ -376,7 +377,7 @@ class Config
$g[$key] = $v;
}
$config['graph_types'][$g['type']][$g['subtype']] = $g;
self::$config['graph_types'][$g['type']][$g['subtype']] = $g;
}
}

View File

@@ -622,7 +622,7 @@ class IRCBot
private function _reload()
{
if ($this->user['level'] == 10) {
$new_config = Config::load();
$new_config = Config::reload();
$this->respond('Reloading configuration & defaults');
if ($new_config != $this->config) {
return $this->__construct();

View File

@@ -10,4 +10,4 @@ over:
discovery:
-
sysObjectID: .1.3.6.1.4.1.4515.100.1.1000
sysDescr_regex_except: '^[Pp][Ll]'
sysDescr_regex_except: '/^[Pp][Ll]/'

View File

@@ -66,7 +66,7 @@ require_once $install_dir . '/includes/billing.php';
require_once $install_dir . '/includes/syslog.php';
if (module_selected('mocksnmp', $init_modules)) {
require_once $install_dir . '/tests/mocks/mock.snmp.inc.php';
} else {
} elseif (!in_array($install_dir . '/tests/mocks/mock.snmp.inc.php', get_included_files())) {
require_once $install_dir . '/includes/snmp.inc.php';
}
require_once $install_dir . '/includes/services.inc.php';

View File

@@ -32,11 +32,28 @@ use LibreNMS\Exceptions\AuthenticationException;
// Note that as this test set depends on mres(), it is a DBTestCase even though the database is unused
class AuthHTTPTest extends DBTestCase
{
private $original_auth_mech;
private $server;
public function setUp(): void
{
parent::setUp();
$this->original_auth_mech = Config::get('auth_mechanism');
Config::set('auth_mechanism', 'http-auth');
$this->server = $_SERVER;
}
public function tearDown(): void
{
Config::set('auth_mechanism', $this->original_auth_mech);
$_SERVER = $this->server;
parent::tearDown();
}
// Document the modules current behaviour, so that changes trigger test failures
public function testCapabilityFunctions()
{
Config::set('auth_mechanism', 'http-auth');
$a = LegacyAuth::reset();
$this->assertTrue($a->canUpdatePasswords() === 0);
@@ -51,7 +68,6 @@ class AuthHTTPTest extends DBTestCase
$old_username = null;
$new_username = null;
Config::set('auth_mechanism', 'http-auth');
$users = array('steve', ' steve', 'steve ', ' steve ', ' steve ', '', 'CAT');
$vars = array('REMOTE_USER', 'PHP_AUTH_USER');
@@ -81,7 +97,5 @@ class AuthHTTPTest extends DBTestCase
unset($_SERVER[$v]);
}
Config::forget('auth_mechanism');
}
}

View File

@@ -27,9 +27,19 @@ namespace LibreNMS\Tests;
use LibreNMS\Config;
use LibreNMS\DB\Eloquent;
use ReflectionClass;
class ConfigTest extends LaravelTestCase
class ConfigTest extends TestCase
{
private $config;
public function setUp(): void
{
parent::setUp();
$this->config = new \ReflectionProperty(Config::class, 'config');
$this->config->setAccessible(true);
}
public function testGetBasic()
{
$dir = realpath(__DIR__ . '/..');
@@ -38,26 +48,27 @@ class ConfigTest extends LaravelTestCase
public function testSetBasic()
{
global $config;
Config::set('basics', 'first');
$this->assertEquals('first', $config['basics']);
$this->assertEquals('first', $this->config->getValue()['basics']);
}
public function testGet()
{
global $config;
$this->setConfig(function (&$config) {
$config['one']['two']['three'] = 'easy';
});
$this->assertEquals('easy', Config::get('one.two.three'));
}
public function testGetDeviceSetting()
{
global $config;
$device = array('set' => true, 'null' => null);
$this->setConfig(function (&$config) {
$config['null'] = 'notnull!';
$config['noprefix'] = true;
$config['prefix']['global'] = true;
});
$this->assertNull(Config::getDeviceSetting($device, 'unset'), 'Non-existing settings should return null');
$this->assertTrue(Config::getDeviceSetting($device, 'set'), 'Could not get setting from device array');
@@ -80,9 +91,10 @@ class ConfigTest extends LaravelTestCase
public function testGetOsSetting()
{
global $config;
$this->setConfig(function (&$config) {
$config['os']['nullos']['fancy'] = true;
$config['fallback'] = true;
});
$this->assertNull(Config::getOsSetting(null, 'unset'), '$os is null, should return null');
$this->assertNull(Config::getOsSetting('nullos', 'unset'), 'Non-existing settings should return null');
@@ -93,13 +105,14 @@ class ConfigTest extends LaravelTestCase
public function testGetCombined()
{
global $config;
$this->setConfig(function (&$config) {
$config['num'] = array('one', 'two');
$config['os']['nullos']['num'] = array('two', 'three');
$config['assoc'] = array('a' => 'same', 'b' => 'same');
$config['os']['nullos']['assoc'] = array('b' => 'different', 'c' => 'still same');
$config['os']['nullos']['osset'] = true;
$config['gset'] = true;
});
$this->assertTrue(Config::getCombined('nullos', 'non-existent', true), 'Did not return default value on non-existent key');
$this->assertTrue(Config::getCombined('nullos', 'osset', false), 'Did not return OS value when global value is not set');
@@ -114,10 +127,9 @@ class ConfigTest extends LaravelTestCase
public function testSet()
{
global $config;
Config::set('you.and.me', "I'll be there");
$this->assertEquals("I'll be there", $config['you']['and']['me']);
$this->assertEquals("I'll be there", $this->config->getValue()['you']['and']['me']);
}
public function testSetPersist()
@@ -164,7 +176,6 @@ class ConfigTest extends LaravelTestCase
}
public function testGetSubtree()
{
Config::set('words.top', 'August');
@@ -178,4 +189,15 @@ class ConfigTest extends LaravelTestCase
$this->assertEquals($expected, Config::get('words'));
}
/**
* Pass an anonymous function which will be passed the config variable to modify before it is set
* @param callable $function
*/
private function setConfig($function)
{
$config = $this->config->getValue();
$function($config);
$this->config->setValue($config);
}
}

View File

@@ -39,14 +39,9 @@ class DBSetupTest extends DBTestCase
public function testSetupDB()
{
global $schema;
foreach ($schema as $output) {
if (preg_match('/([1-9]+) errors/', $output) || preg_match('/Cannot execute query/', $output)) {
throw new PHPUnitException("Errors loading DB Schema: " . $output);
}
}
global $migrate_output, $migrate_result;
$this->expectNotToPerformAssertions();
$this->assertSame(0, $migrate_result, "Errors loading DB Schema: " . $migrate_output);
}
public function testSchemaFiles()
@@ -126,7 +121,7 @@ class DBSetupTest extends DBTestCase
public function testSqlMode()
{
$this->assertEquals(
'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION',
'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION',
dbFetchCell("SELECT @@sql_mode")
);
}

View File

@@ -25,18 +25,14 @@
namespace LibreNMS\Tests;
abstract class DBTestCase extends LaravelTestCase
abstract class DBTestCase extends TestCase
{
public function setUp(): void
public static function setUpBeforeClass()
{
parent::setUp();
$this->dbSetUp();
set_debug(false);
if (!getenv('DBTEST')) {
static::markTestSkipped('Database tests not enabled. Set DBTEST=1 to enable.');
}
public function tearDown(): void
{
$this->dbTearDown();
parent::tearDown();
parent::setUpBeforeClass();
}
}

View File

@@ -26,15 +26,11 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class AdvaAccThresholdCrossingAlertTest extends LaravelTestCase
class AdvaAccThresholdCrossingAlertTest extends SnmpTrapTestCase
{
use DatabaseTransactions;
public function testAccThresholdTrap()
{
$device = factory(Device::class)->create();

View File

@@ -26,15 +26,11 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class AdvaAttributeChangeTest extends LaravelTestCase
class AdvaAttributeChangeTest extends SnmpTrapTestCase
{
use DatabaseTransactions;
public function testSyslogIPVersionModified()
{
$device = factory(Device::class)->create();

View File

@@ -26,15 +26,11 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class AdvaSnmpDyingGaspTest extends LaravelTestCase
class AdvaSnmpDyingGaspTest extends SnmpTrapTestCase
{
use DatabaseTransactions;
public function testDyingGasp()
{
$device = factory(Device::class)->create();

View File

@@ -26,15 +26,11 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class AdvaNetThresholdCrossingAlertTest extends LaravelTestCase
class AdvaNetThresholdCrossingAlertTest extends SnmpTrapTestCase
{
use DatabaseTransactions;
public function testNetThresholdTrap()
{
$device = factory(Device::class)->create();

View File

@@ -26,15 +26,11 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class AdvaNetworkElementAlmTest extends LaravelTestCase
class AdvaNetworkElementAlmTest extends SnmpTrapTestCase
{
use DatabaseTransactions;
public function testElementAlarmCleared()
{
$device = factory(Device::class)->create();

View File

@@ -26,15 +26,11 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class AdvaObjectCreationTest extends LaravelTestCase
class AdvaObjectCreationTest extends SnmpTrapTestCase
{
use DatabaseTransactions;
public function testUserCreation()
{
$device = factory(Device::class)->create();

View File

@@ -26,15 +26,11 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class AdvaObjectDeletionTest extends LaravelTestCase
class AdvaObjectDeletionTest extends SnmpTrapTestCase
{
use DatabaseTransactions;
public function testUserDeletion()
{
$device = factory(Device::class)->create();

View File

@@ -26,15 +26,11 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Tests\LaravelTestCase;
use LibreNMS\Snmptrap\Trap;
class AdvaStateChangeTrapTest extends LaravelTestCase
class AdvaStateChangeTrapTest extends SnmpTrapTestCase
{
use DatabaseTransactions;
public function testAccessPortChg()
{
$device = factory(Device::class)->create();

View File

@@ -26,15 +26,11 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class AdvaSysAlmTrapTest extends LaravelTestCase
class AdvaSysAlmTrapTest extends SnmpTrapTestCase
{
use DatabaseTransactions;
public function testCriticalAlarm()
{
$device = factory(Device::class)->create();

View File

@@ -25,12 +25,15 @@ namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use App\Models\Ipv4Address;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
use LibreNMS\Tests\DBTestCase;
class ApcPduOutletTest extends LaravelTestCase
class ApcPduOutletTest extends DBTestCase
{
use DatabaseTransactions;
public function testOutletOff()
{
$device = factory(Device::class)->create();

View File

@@ -30,9 +30,9 @@ use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
use LibreNMS\Tests\DBTestCase;
class BgpTrapTest extends LaravelTestCase
class BgpTrapTest extends DBTestCase
{
use DatabaseTransactions;

View File

@@ -34,7 +34,7 @@ use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use Log;
class CommonTrapTest extends LaravelTestCase
class CommonTrapTest extends DBTestCase
{
use DatabaseTransactions;

View File

@@ -26,12 +26,15 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
use LibreNMS\Tests\DBTestCase;
class FgTrapAvOversizeTest extends LaravelTestCase
class FgTrapAvOversizeTest extends DBTestCase
{
use DatabaseTransactions;
public function testAvOversize()
{
$device = factory(Device::class)->create();

View File

@@ -27,12 +27,15 @@ namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use App\Models\Ipv4Address;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
use LibreNMS\Tests\DBTestCase;
class FgTrapIpsTest extends LaravelTestCase
class FgTrapIpsTest extends DBTestCase
{
use DatabaseTransactions;
public function testIpsAnomaly()
{
$device = factory(Device::class)->create();

View File

@@ -27,12 +27,15 @@ namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use App\Models\Ipv4Address;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
use LibreNMS\Tests\DBTestCase;
class FgTrapVpnTunTest extends LaravelTestCase
class FgTrapVpnTunTest extends DBTestCase
{
use DatabaseTransactions;
public function testVpnTunDown()
{
$device = factory(Device::class)->create();

View File

@@ -26,12 +26,14 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
use LibreNMS\Tests\DBTestCase;
class FgTrapLogRateThresholdTest extends LaravelTestCase
class FgTrapLogRateThresholdTest extends DBTestCase
{
use DatabaseTransactions;
public function testAvOversize()
{

View File

@@ -31,11 +31,10 @@ use App\Models\BgpPeer;
use App\Models\Device;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use Log;
use LibreNMS\Tests\Feature\SnmpTraps\SnmpTrapTestCase;
class JnxBgpM2Test extends LaravelTestCase
class JnxBgpM2Test extends SnmpTrapTestCase
{
public function testBgpPeerUnknown()
{

View File

@@ -30,11 +30,10 @@ namespace LibreNMS\Tests;
use App\Models\Device;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use Log;
use LibreNMS\Tests\Feature\SnmpTraps\SnmpTrapTestCase;
class JnxCmCfgChangeTest extends LaravelTestCase
class JnxCmCfgChangeTest extends SnmpTrapTestCase
{
public function testConfigChangeTrap()
{

View File

@@ -31,11 +31,10 @@ use App\Models\Device;
use App\Models\Port;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use Log;
use LibreNMS\Tests\Feature\SnmpTraps\SnmpTrapTestCase;
class JnxDomAlarmTest extends LaravelTestCase
class JnxDomAlarmTest extends SnmpTrapTestCase
{
public function testJnxDomAlarmSetTrap()
{
$device = factory(Device::class)->create();

View File

@@ -31,11 +31,10 @@ use App\Models\Device;
use App\Models\Port;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use Log;
use LibreNMS\Tests\Feature\SnmpTraps\SnmpTrapTestCase;
class JnxDomLaneAlarmTest extends LaravelTestCase
class JnxDomLaneAlarmTest extends SnmpTrapTestCase
{
public function testJnxDomLaneAlarmSetTrap()
{
$device = factory(Device::class)->create();

View File

@@ -31,11 +31,10 @@ use App\Models\Device;
use App\Models\Ipv4Address;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use Log;
use LibreNMS\Tests\Feature\SnmpTraps\SnmpTrapTestCase;
class JnxLdpLspTest extends LaravelTestCase
class JnxLdpLspTest extends SnmpTrapTestCase
{
public function testLdpLspDownTrap()
{
$device = factory(Device::class)->create();

View File

@@ -31,11 +31,10 @@ use App\Models\Device;
use App\Models\Port;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use Log;
use LibreNMS\Tests\Feature\SnmpTraps\SnmpTrapTestCase;
class JnxLdpSesTest extends LaravelTestCase
class JnxLdpSesTest extends SnmpTrapTestCase
{
public function testJnxLdpSesDownTrap()
{
$device = factory(Device::class)->create();

View File

@@ -32,11 +32,10 @@ use App\Models\Device;
use App\Models\Port;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use Log;
use LibreNMS\Tests\Feature\SnmpTraps\SnmpTrapTestCase;
class JnxVpnIfTest extends LaravelTestCase
class JnxVpnIfTest extends SnmpTrapTestCase
{
public function testVpnIfDown()
{
$device = factory(Device::class)->create();

View File

@@ -32,11 +32,10 @@ use App\Models\Device;
use App\Models\Port;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use Log;
use LibreNMS\Tests\Feature\SnmpTraps\SnmpTrapTestCase;
class JnxVpnPwTest extends LaravelTestCase
class JnxVpnPwTest extends SnmpTrapTestCase
{
public function testVpnPwDown()
{
$device = factory(Device::class)->create();

View File

@@ -24,12 +24,10 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use App\Models\Ipv4Address;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class NetgearFailedUserLoginTest extends LaravelTestCase
class NetgearFailedUserLoginTest extends SnmpTrapTestCase
{
public function testManagedSeries()
{

View File

@@ -27,18 +27,12 @@ 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
class PortsTrapTest extends SnmpTrapTestCase
{
use DatabaseTransactions;
public function testLinkDown()
{
// make a device and associate a port with it

View File

@@ -30,9 +30,8 @@ namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class RuckusEventTrap extends LaravelTestCase
class RuckusEventTrap extends SnmpTrapTestCase
{
public function testRuckusAssocTrap()
{

View File

@@ -30,9 +30,8 @@ namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class RuckusSzClusterStateTest extends LaravelTestCase
class RuckusSzClusterStateTest extends SnmpTrapTestCase
{
public function testClusterInMaintenance()
{

View File

@@ -30,9 +30,8 @@ namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\LaravelTestCase;
class RuckusSzEventTest extends LaravelTestCase
class RuckusSzEventTest extends SnmpTrapTestCase
{
public function testSzApConf()
{

View File

@@ -0,0 +1,34 @@
<?php
/**
* SnmpTrapTestCase.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 Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Tests\DBTestCase;
class SnmpTrapTestCase extends DBTestCase
{
use DatabaseTransactions; // people skip this a lot so extract it to here
}

View File

@@ -1,29 +0,0 @@
<?php
namespace LibreNMS\Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
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

@@ -121,9 +121,9 @@ class OSDiscoveryTest extends TestCase
{
return [
'device_id' => 1,
'hostname' => $this->snmpsim->getIP(),
'hostname' => $this->getSnmpsim()->getIP(),
'snmpver' => 'v2c',
'port' => $this->snmpsim->getPort(),
'port' => $this->getSnmpsim()->getPort(),
'timeout' => 3,
'retries' => 0,
'snmp_max_repeaters' => 10,

View File

@@ -25,12 +25,34 @@
namespace LibreNMS\Tests;
use LibreNMS\Config;
use LibreNMS\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper;
class OSModulesTest extends DBTestCase
{
private $discoveryModules;
private $pollerModules;
public function setUp(): void
{
parent::setUp();
// backup modules
$this->discoveryModules = Config::get('discovery_modules');
$this->pollerModules = Config::get('poller_modules');
}
public function tearDown(): void
{
// restore modules
Config::set('discovery_modules', $this->discoveryModules);
Config::set('poller_modules', $this->pollerModules);
parent::tearDown();
}
/**
* Test all modules for a particular OS
*
@@ -59,16 +81,15 @@ class OSModulesTest extends DBTestCase
public function testOS($os, $variant, $modules)
{
$this->requireSnmpsim(); // require snmpsim for tests
global $snmpsim;
load_all_os(); // wiped out by application refresh
try {
set_debug(false); // avoid all undefined index errors in the legacy code
$helper = new ModuleTestHelper($modules, $os, $variant);
$helper->setQuiet();
$filename = $helper->getJsonFilepath(true);
$expected_data = $helper->getTestData();
$results = $helper->generateTestData($snmpsim, true);
$results = $helper->generateTestData($this->getSnmpsim(), true);
} catch (FileNotFoundException $e) {
$this->fail($e->getMessage());
return;

View File

@@ -29,7 +29,7 @@ use LibreNMS\Alerting\QueryBuilderFluentParser;
use LibreNMS\Alerting\QueryBuilderParser;
use LibreNMS\Config;
class QueryBuilderTest extends LaravelTestCase
class QueryBuilderTest extends TestCase
{
private $data_file = 'tests/data/misc/querybuilder.json';

View File

@@ -25,12 +25,26 @@
namespace LibreNMS\Tests;
use LibreNMS\Util\Snmpsim;
trait SnmpsimHelpers
{
/** @var Snmpsim snmpsim instance */
protected $snmpsim = null;
public function requireSnmpsim()
{
if (!getenv('SNMPSIM')) {
$this->markTestSkipped('Snmpsim required for this test. Set SNMPSIM=1 to enable.');
}
}
public function getSnmpsim()
{
if (!$this->snmpsim) {
global $snmpsim;
$this->snmpsim = $snmpsim;
}
return $this->snmpsim;
}
}

View File

@@ -1,68 +1,37 @@
<?php
/**
* TestCase.php
*
* Base Test Case for all tests
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests;
use LibreNMS\Util\Snmpsim;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends \PHPUnit\Framework\TestCase
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use SnmpsimHelpers;
/** @var Snmpsim snmpsim instance */
protected $snmpsim = null;
public function __construct($name = null, $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
// grab global $snmpsim from bootstrap and make it accessible
global $snmpsim;
$this->snmpsim = $snmpsim;
}
public function setUp(): void
{
parent::setUp();
set_debug(false); // prevent warnings from stopping execution for legacy code
$this->getSnmpsim();
}
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')) {
try {
\LibreNMS\DB\Eloquent::DB()->rollBack();
} catch (\Exception $e) {
$this->fail("Exception when rolling back transaction.\n" . $e->getTraceAsString());
}
}
}
}

View File

@@ -29,9 +29,10 @@ use App\Models\Device;
use App\Models\Ipv4Address;
use App\Models\Port;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Tests\LaravelTestCase;
use LibreNMS\Tests\DBTestCase;
use LibreNMS\Tests\TestCase;
class DeviceTest extends LaravelTestCase
class DeviceTest extends DBTestCase
{
use DatabaseTransactions;

View File

@@ -29,10 +29,10 @@ use App\Models\Bill;
use App\Models\Device;
use App\Models\Port;
use App\Models\User;
use LibreNMS\Tests\LaravelTestCase;
use LibreNMS\Tests\TestCase;
use Mockery\Mock;
class PermissionsTest extends LaravelTestCase
class PermissionsTest extends TestCase
{
public function testUserCanAccessDevice()
{

View File

@@ -36,12 +36,6 @@ 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);
@@ -61,53 +55,28 @@ if (getenv('SNMPSIM')) {
}
if (getenv('DBTEST')) {
global $schema, $sql_mode;
global $migrate_result, $migrate_output;
// create testing table if needed
$db_config = Config::getDatabaseSettings();
$db_name = $db_config['db_name'];
$connection = new PDO("mysql:host={$db_config['db_host']}", $db_config['db_user'], $db_config['db_pass']);
$connection->query("CREATE DATABASE IF NOT EXISTS $db_name CHARACTER SET utf8 COLLATE utf8_unicode_ci");
$db_config = \config("database.connections.testing");
$connection = new PDO("mysql:host={$db_config['host']}", $db_config['username'], $db_config['password']);
$connection->query("CREATE DATABASE IF NOT EXISTS {$db_config['database']} CHARACTER SET utf8 COLLATE utf8_unicode_ci");
unset($connection); // close connection
Eloquent::boot();
Eloquent::setStrictMode();
$empty_db = (dbFetchCell("SELECT count(*) FROM `information_schema`.`tables` WHERE `table_type` = 'BASE TABLE' AND `table_schema` = ?", [$db_name]) == 0);
$cmd = Config::get('install_dir') . '/build-base.php';
exec($cmd, $schema);
Config::load(); // reload the config including database config
load_all_os();
register_shutdown_function(function () use ($empty_db, $sql_mode) {
Eloquent::boot();
echo "Cleaning database...\n";
$db_name = dbFetchCell('SELECT DATABASE()');
if ($empty_db) {
dbQuery("DROP DATABASE $db_name");
} elseif (Config::get('test_db_name') == $db_name) {
// truncate tables
$tables = dbFetchColumn('SHOW TABLES');
$excluded = array(
'alert_templates',
'config', // not sure about this one
'dbSchema',
'migrations',
'widgets',
);
$truncate = array_diff($tables, $excluded);
dbQuery("SET FOREIGN_KEY_CHECKS = 0");
foreach ($truncate as $table) {
dbQuery("TRUNCATE TABLE $table");
// try to avoid erasing people's primary databases
if ($db_config['database'] !== \config('database.connections.mysql.database', 'librenms')) {
echo "Refreshing database...";
$migrate_result = Artisan::call('migrate:fresh', ['--seed' => true, '--env' => 'testing', '--database' => 'testing']);
$migrate_output = Artisan::output();
echo "done\n";
} else {
echo "Info: Refusing to reset main database: {$db_config['database']}. Running migrations.\n";
$migrate_result = Artisan::call('migrate', ['--seed' => true, '--env' => 'testing', '--database' => 'testing']);
$migrate_output = Artisan::output();
}
dbQuery("SET FOREIGN_KEY_CHECKS = 1");
}
});
unset($db_config);
}
// reload the config including database config
Config::reload();
load_all_os();