Revert "Refactor datastores to classes (#9179)" (#11273)

This reverts commit e5dad7a64e.
This commit is contained in:
Tony Murray
2020-03-11 07:52:52 -05:00
committed by GitHub
parent e5dad7a64e
commit 7fe895bd08
51 changed files with 1007 additions and 2523 deletions

View File

@@ -1,81 +0,0 @@
<?php
/**
* DatastoreTest.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\Unit\Data;
use LibreNMS\Config;
use LibreNMS\Tests\TestCase;
/**
* @group datastores
*/
class DatastoreTest extends TestCase
{
public function setUp() : void
{
parent::setUp();
Config::forget([
'graphite',
'influxdb',
'opentsdb',
'prometheus',
'rrd',
]);
}
public function testDefaultInitialization()
{
$ds = $this->app->make('Datastore');
$stores = $ds->getStores();
$this->assertCount(1, $stores, 'Incorrect number of default stores enabled');
$this->assertEquals('LibreNMS\Data\Store\Rrd', get_class($stores[0]), 'The default enabled store should be Rrd');
}
public function testInitialization()
{
Config::set('rrd.enable', false);
Config::set('graphite.enable', true);
Config::set('influxdb.enable', true);
Config::set('opentsdb.enable', true);
Config::set('prometheus.enable', true);
$ds = $this->app->make('Datastore');
$stores = $ds->getStores();
$this->assertCount(4, $stores, 'Incorrect number of default stores enabled');
$enabled = array_map('get_class', $stores);
$expected_enabled = [
'LibreNMS\Data\Store\Graphite',
'LibreNMS\Data\Store\InfluxDB',
'LibreNMS\Data\Store\OpenTSDB',
'LibreNMS\Data\Store\Prometheus',
];
$this->assertEquals($expected_enabled, $enabled, 'Expected all non-default stores to be initialized');
}
}

View File

@@ -1,108 +0,0 @@
<?php
/**
* GraphiteStoreTest.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\Unit\Data;
use Carbon\Carbon;
use LibreNMS\Data\Store\Graphite;
use LibreNMS\Tests\TestCase;
/**
* @group datastores
*/
class GraphiteStoreTest extends TestCase
{
protected $timestamp = 997464400;
public function setUp() : void
{
parent::setUp();
// fix the date
Carbon::setTestNow(Carbon::createFromTimestamp($this->timestamp));
}
public function tearDown() : void
{
// restore Carbon:now() to normal
Carbon::setTestNow();
parent::tearDown();
}
public function testSocketConnectError()
{
$mockFactory = \Mockery::mock(\Socket\Raw\Factory::class);
$mockFactory->shouldReceive('createClient')
->andThrow('Socket\Raw\Exception', 'Failed to handle connect exception');
new Graphite($mockFactory);
}
public function testSocketWriteError()
{
$mockSocket = \Mockery::mock(\Socket\Raw\Socket::class);
$graphite = $this->mockGraphite($mockSocket);
$mockSocket->shouldReceive('write')
->andThrow('Socket\Raw\Exception', 'Did not handle socket exception');
$graphite->put(['hostname' => 'test'], 'fake', ['rrd_name' => 'name'], ['one' => 1]);
}
public function testSimpleWrite()
{
$mockSocket = \Mockery::mock(\Socket\Raw\Socket::class);
$graphite = $this->mockGraphite($mockSocket);
$device = ['hostname' => 'testhost'];
$measurement = 'testmeasure';
$tags = ['rrd_name' => 'rrd_name', 'ifName' => 'testifname', 'type' => 'testtype'];
$fields = ['ifIn' => 234234, 'ifOut' => 53453];
$mockSocket->shouldReceive('write')
->with("testhost.testmeasure.rrd_name.ifIn 234234 $this->timestamp\n");
$mockSocket->shouldReceive('write')
->with("testhost.testmeasure.rrd_name.ifOut 53453 $this->timestamp\n");
$graphite->put($device, $measurement, $tags, $fields);
}
/**
* @param $mockSocket
* @return Graphite
*/
private function mockGraphite($mockSocket)
{
$mockFactory = \Mockery::mock(\Socket\Raw\Factory::class);
$mockFactory->shouldReceive('createClient')
->andReturn($mockSocket);
$graphite = new Graphite($mockFactory);
return $graphite;
}
}

View File

@@ -1,66 +0,0 @@
<?php
/**
* InfluxStoreTest.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\Unit\Data;
use InfluxDB\Point;
use LibreNMS\Config;
use LibreNMS\Data\Store\InfluxDB;
use LibreNMS\Tests\TestCase;
/**
* @group datastores
*/
class InfluxDBStoreTest extends TestCase
{
public function testBadSettings()
{
Config::set('influxdb.host', '');
Config::set('influxdb.port', 'abc');
$influx = new InfluxDB(InfluxDB::createFromConfig());
\Log::shouldReceive('debug');
\Log::shouldReceive('error')->once()->with('InfluxDB exception: Unable to parse URI: http://:0'); // the important one
$influx->put(['hostname' => 'test'], 'fake', [], ['one' => 1]);
}
public function testSimpleWrite()
{
// Create a mock of the Random Interface
$mock = \Mockery::mock(\InfluxDB\Database::class);
$mock->shouldReceive('exists')->once()->andReturn(true);
$influx = new InfluxDB($mock);
$device = ['hostname' => 'testhost'];
$measurement = 'testmeasure';
$tags = ['ifName' => 'testifname', 'type' => 'testtype'];
$fields = ['ifIn' => 234234, 'ifOut' => 53453];
$expected = [new Point($measurement, null, ['hostname' => $device['hostname']] + $tags, $fields)];
$mock->shouldReceive('writePoints')->withArgs([$expected])->once();
$influx->put($device, $measurement, $tags, $fields);
}
}

View File

@@ -1,124 +0,0 @@
<?php
/**
* OpenTSDBStoreTest.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\Unit\Data;
use Carbon\Carbon;
use GuzzleHttp\Client;
use LibreNMS\Data\Store\OpenTSDB;
use LibreNMS\Tests\TestCase;
/**
* @group datastores
*/
class OpenTSDBStoreTest extends TestCase
{
protected $timestamp = 990464400;
public function setUp() : void
{
parent::setUp();
// fix the date
Carbon::setTestNow(Carbon::createFromTimestamp($this->timestamp));
}
public function tearDown() : void
{
// restore Carbon:now() to normal
Carbon::setTestNow();
parent::tearDown();
}
public function testSocketConnectError()
{
$mockFactory = \Mockery::mock(\Socket\Raw\Factory::class);
$mockFactory->shouldReceive('createClient')
->andThrow('Socket\Raw\Exception', 'Failed to handle connect exception');
new OpenTSDB($mockFactory);
}
public function testSocketWriteError()
{
$mockSocket = \Mockery::mock(\Socket\Raw\Socket::class);
$opentsdb = $this->mockOpenTSDB($mockSocket);
$mockSocket->shouldReceive('write')
->andThrow('Socket\Raw\Exception', 'Did not handle socket exception');
$opentsdb->put(['hostname' => 'test'], 'fake', [], ['one' => 1]);
}
public function testSimpleWrite()
{
$mockSocket = \Mockery::mock(\Socket\Raw\Socket::class);
$opentsdb = $this->mockOpenTSDB($mockSocket);
$device = ['hostname' => 'testhost'];
$measurement = 'testmeasure';
$tags = ['ifName' => 'testifname', 'type' => 'testtype'];
$fields = ['ifIn' => 234234, 'ifOut' => 53453];
$mockSocket->shouldReceive('write')
->with("put net.testmeasure $this->timestamp 0.000000 234234\n");
$mockSocket->shouldReceive('write')
->with("put net.testmeasure $this->timestamp 0.000000 53453\n");
$opentsdb->put($device, $measurement, $tags, $fields);
}
public function testPortWrite()
{
$mockSocket = \Mockery::mock(\Socket\Raw\Socket::class);
$opentsdb = $this->mockOpenTSDB($mockSocket);
$device = ['hostname' => 'testhost'];
$measurement = 'port';
$tags = ['ifName' => 'testifname', 'type' => 'testtype'];
$fields = ['ifIn' => 897238, 'ifOut' => 2342];
$mockSocket->shouldReceive('write')
->with("put net.port.ifin $this->timestamp 0.000000 897238\n");
$mockSocket->shouldReceive('write')
->with("put net.port.ifout $this->timestamp 0.000000 2342\n");
$opentsdb->put($device, $measurement, $tags, $fields);
}
/**
* @param $mockSocket
* @return OpenTSDB
*/
private function mockOpenTSDB($mockSocket)
{
$mockFactory = \Mockery::mock(\Socket\Raw\Factory::class);
$mockFactory->shouldReceive('createClient')
->andReturn($mockSocket);
return new OpenTSDB($mockFactory);
}
}

View File

@@ -1,105 +0,0 @@
<?php
/**
* PrometheusStoreTest.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\Unit\Data;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use LibreNMS\Config;
use LibreNMS\Data\Store\Prometheus;
use LibreNMS\Tests\TestCase;
/**
* @group datastores
*/
class PrometheusStoreTest extends TestCase
{
public function setUp() : void
{
parent::setUp();
Config::set('prometheus.enable', true);
Config::set('prometheus.url', 'http://fake:9999');
}
public function testFailWrite()
{
$stack = HandlerStack::create(new MockHandler([
new Response(422, [], 'Bad response'),
new RequestException("Exception thrown", new Request('POST', 'test'))
]));
$client = new Client(['handler' => $stack]);
$prometheus = new Prometheus($client);
\Log::shouldReceive('debug');
\Log::shouldReceive('error')->once()->with("Prometheus Exception: Client error: `POST http://fake:9999/metrics/job/librenms/instance/test/measurement/none` resulted in a `422 Unprocessable Entity` response:\nBad response\n");
\Log::shouldReceive('error')->once()->with('Prometheus Exception: Exception thrown');
$prometheus->put(['hostname' => 'test'], 'none', [], ['one' => 1]);
$prometheus->put(['hostname' => 'test'], 'none', [], ['one' => 1]);
}
public function testSimpleWrite()
{
$stack = HandlerStack::create(new MockHandler([
new Response(200),
]));
$container = [];
$history = Middleware::history($container);
$stack->push($history);
$client = new Client(['handler' => $stack]);
$prometheus = new Prometheus($client);
$device = ['hostname' => 'testhost'];
$measurement = 'testmeasure';
$tags = ['ifName' => 'testifname', 'type' => 'testtype'];
$fields = ['ifIn' => 234234, 'ifOut' => 53453];
\Log::shouldReceive('debug');
\Log::shouldReceive('error')->times(0);
$prometheus->put($device, $measurement, $tags, $fields);
$this->assertCount(1, $container, 'Did not receive the expected number of requests');
/** @var Request $request */
$request = $container[0]['request'];
$this->assertEquals('POST', $request->getMethod());
$this->assertEquals("/metrics/job/librenms/instance/testhost/measurement/testmeasure/ifName/testifname/type/testtype", $request->getUri()->getPath());
$this->assertEquals('fake', $request->getUri()->getHost());
$this->assertEquals(9999, $request->getUri()->getPort());
$this->assertEquals("ifIn 234234\nifOut 53453\n", (string)$request->getBody());
}
}