Files
librenms-librenms/tests/InfluxDB/ClientTest.php
T

166 lines
4.8 KiB
PHP
Raw Normal View History

2014-09-11 14:50:31 +02:00
<?php
namespace InfluxDB;
use InfluxDB\Adapter\GuzzleAdapter as InfluxHttpAdapter;
2014-09-11 14:50:31 +02:00
use InfluxDB\Options;
2014-09-13 00:10:45 +02:00
use InfluxDB\Adapter\UdpAdapter;
2014-09-11 14:50:31 +02:00
use GuzzleHttp\Client as GuzzleHttpClient;
class ClientTest extends \PHPUnit_Framework_TestCase
{
2014-09-13 00:10:45 +02:00
private $rawOptions;
2014-09-11 14:50:31 +02:00
private $object;
private $options;
private $anotherClient;
public function setUp()
{
$options = include __DIR__ . '/../bootstrap.php';
2014-09-13 00:10:45 +02:00
$this->rawOptions = $options;
2014-09-11 14:50:31 +02:00
2014-09-12 20:20:23 +02:00
$tcpOptions = $options["tcp"];
2014-09-11 14:50:31 +02:00
$options = new Options();
$options->setHost($tcpOptions["host"]);
$options->setPort($tcpOptions["port"]);
$options->setUsername($tcpOptions["username"]);
$options->setPassword($tcpOptions["password"]);
2014-09-12 08:24:21 +02:00
$options->setDatabase($tcpOptions["database"]);
2014-09-11 14:50:31 +02:00
2014-09-12 20:20:23 +02:00
$this->options = $options;
2014-09-11 14:50:31 +02:00
$guzzleHttp = new GuzzleHttpClient();
$adapter = new InfluxHttpAdapter($guzzleHttp, $options);
2014-09-11 14:50:31 +02:00
$influx = new Client();
$influx->setAdapter($adapter);
2014-09-12 20:20:23 +02:00
$this->object = $influx;
2014-09-13 08:19:51 +02:00
$databases = $this->object->getDatabases();
foreach ($databases as $database) {
$this->object->deleteDatabase($database["name"]);
}
$this->object->createDatabase($this->rawOptions["udp"]["database"]);
$this->object->createDatabase($this->rawOptions["tcp"]["database"]);
2014-09-12 20:20:23 +02:00
}
2014-09-11 14:50:31 +02:00
2014-09-13 00:10:45 +02:00
/**
* @group tcp
*/
2014-09-12 20:20:23 +02:00
public function testGuzzleHttpApiWorksCorrectly()
{
$this->object->mark("tcp.test", ["mark" => "element"]);
2014-09-11 14:50:31 +02:00
2014-09-12 23:05:29 +02:00
$body = $this->object->query("select * from tcp.test");
$this->assertCount(1, $body[0]["points"]);
$this->assertEquals("element", $body[0]["points"][0][2]);
2014-09-11 14:50:31 +02:00
}
2014-09-12 20:20:23 +02:00
2014-09-13 00:10:45 +02:00
/**
* @group tcp
*/
2014-09-12 20:20:23 +02:00
public function testGuzzleHttpQueryApiWorksCorrectly()
{
$this->object->mark("tcp.test", ["mark" => "element"]);
$body = $this->object->query("select * from tcp.test");
$this->assertCount(1, $body);
$this->assertEquals("tcp.test", $body[0]["name"]);
$this->assertEquals("element", $body[0]["points"][0][2]);
}
2014-09-13 00:10:45 +02:00
/**
* @group tcp
*/
2014-09-12 20:20:23 +02:00
public function testGuzzleHttpQueryApiWithMultipleData()
{
$this->object->mark("tcp.test", ["mark" => "element"]);
$this->object->mark("tcp.test", ["mark" => "element2"]);
$this->object->mark("tcp.test", ["mark" => "element3"]);
$body = $this->object->query("select mark from tcp.test", "s");
$this->assertCount(3, $body[0]["points"]);
$this->assertEquals("tcp.test", $body[0]["name"]);
}
2014-09-13 00:10:45 +02:00
/**
* @group tcp
*/
2014-09-12 20:20:23 +02:00
public function testGuzzleHttpQueryApiWithTimePrecision()
{
$this->object->mark("tcp.test", ["mark" => "element"]);
$body = $this->object->query("select mark from tcp.test", "s");
$this->assertCount(1, $body[0]["points"]);
$this->assertEquals("tcp.test", $body[0]["name"]);
}
2014-09-13 00:10:45 +02:00
2014-09-13 09:17:34 +02:00
/**
* @group tcp
*/
public function testGuzzleHttpWriteApiWithTimePrecision()
{
$this->object->mark("tcp.test", ["time" => 1410591552, "mark" => "element"], "s");
$body = $this->object->query("select mark from tcp.test", "ms");
$this->assertCount(1, $body[0]["points"]);
$this->assertEquals("tcp.test", $body[0]["name"]);
$this->assertEquals("1410591552000", $body[0]["points"][0][0]);
}
2014-09-13 00:10:45 +02:00
/**
* @group udp
*/
public function testUdpIpWriteData()
{
$rawOptions = $this->rawOptions;
$options = new Options();
2014-09-13 08:19:51 +02:00
$options->setHost($rawOptions["udp"]["host"]);
2014-09-13 00:10:45 +02:00
$options->setUsername($rawOptions["udp"]["username"]);
$options->setPassword($rawOptions["udp"]["password"]);
$options->setPort($rawOptions["udp"]["port"]);
$adapter = new UdpAdapter($options);
$object = new Client();
$object->setAdapter($adapter);
$object->mark("udp.test", ["mark" => "element"]);
$object->mark("udp.test", ["mark" => "element1"]);
$object->mark("udp.test", ["mark" => "element2"]);
$object->mark("udp.test", ["mark" => "element3"]);
// Wait UDP/IP message arrives
usleep(200e3);
2014-09-13 08:19:51 +02:00
$this->options->setDatabase("udp.test");
2014-09-13 00:10:45 +02:00
$body = $this->object->query("select * from udp.test");
$this->assertCount(4, $body[0]["points"]);
$this->assertEquals("udp.test", $body[0]["name"]);
}
2014-09-13 08:19:51 +02:00
public function testListActiveDatabses()
{
$databases = $this->object->getDatabases();
$this->assertCount(2, $databases);
}
public function testCreateANewDatabase()
{
$this->object->createDatabase("walter");
$databases = $this->object->getDatabases();
$this->assertCount(3, $databases);
$this->object->deleteDatabase("walter");
}
2014-09-11 14:50:31 +02:00
}