Files
librenms-librenms/tests/ClientTest.php
T

400 lines
12 KiB
PHP
Raw Normal View History

2014-09-11 14:50:31 +02:00
<?php
namespace InfluxDB;
2015-06-10 15:40:10 +02:00
use DateTime;
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()
{
2015-06-10 14:00:20 +02:00
$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($adapter);
2014-09-12 20:20:23 +02:00
$this->object = $influx;
2014-09-13 08:19:51 +02:00
$databases = $this->object->getDatabases();
2015-06-04 13:14:38 +02:00
if (array_key_exists("values", $databases["results"][0]["series"][0])) {
foreach ($databases["results"][0]["series"][0]["values"] as $database) {
$this->object->deleteDatabase($database[0]);
}
2014-09-13 08:19:51 +02:00
}
$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()
{
2015-06-08 18:58:52 +02:00
$this->object->mark("tcp.test", ["mark" => "element"]);
2014-09-11 14:50:31 +02:00
2015-06-04 13:14:38 +02:00
sleep(1);
$body = $this->object->query("select * from \"tcp.test\"");
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("mark", $body["results"][0]["series"][0]["columns"][1]);
$this->assertEquals("element", $body["results"][0]["series"][0]["values"][0][1]);
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"]);
2015-06-07 12:31:11 +02:00
sleep(1);
2014-09-12 20:20:23 +02:00
2015-06-07 12:31:11 +02:00
$body = $this->object->query("select * from \"tcp.test\"");
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("mark", $body["results"][0]["series"][0]["columns"][1]);
$this->assertEquals("element", $body["results"][0]["series"][0]["values"][0][1]);
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 testGuzzleHttpQueryApiWithMultipleData()
{
$this->object->mark("tcp.test", ["mark" => "element"]);
$this->object->mark("tcp.test", ["mark" => "element2"]);
$this->object->mark("tcp.test", ["mark" => "element3"]);
2015-06-07 12:31:11 +02:00
sleep(1);
2014-09-12 20:20:23 +02:00
2015-06-07 12:31:11 +02:00
$body = $this->object->query("select mark from \"tcp.test\"", "s");
$this->assertCount(3, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("mark", $body["results"][0]["series"][0]["columns"][1]);
$this->assertEquals("element", $body["results"][0]["series"][0]["values"][0][1]);
2014-09-12 20:20:23 +02:00
}
/**
* @group tcp
*/
public function testWriteDirectMessages()
{
$this->object->mark([
"tags" => [
"dc" => "eu-west-1",
],
"points" => [
[
2015-06-10 11:40:43 +02:00
"measurement" => "vm-serie",
"fields" => [
"cpu" => 18.12,
"free" => 712423,
],
],
]
]);
sleep(1);
$body = $this->object->query("select * from \"vm-serie\"");
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("cpu", $body["results"][0]["series"][0]["columns"][1]);
$this->assertEquals(18.12, $body["results"][0]["series"][0]["values"][0][1]);
}
2014-09-13 00:10:45 +02:00
/**
2015-06-10 11:40:43 +02:00
* @group tcp
2014-09-13 00:10:45 +02:00
*/
2015-06-10 11:40:43 +02:00
public function testOverrideDatabaseNameViaMessage()
2014-09-13 00:10:45 +02:00
{
2015-06-10 11:40:43 +02:00
$this->options->setDatabase("a-wrong-database");
2014-09-13 00:10:45 +02:00
2015-06-10 11:40:43 +02:00
$this->object->mark([
"database" => "tcp.test",
"points" => [
[
"measurement" => "vm-serie",
"fields" => [
"cpu" => 18.12,
"free" => 712423,
],
],
]
]);
2014-09-13 00:10:45 +02:00
2015-06-07 12:31:11 +02:00
sleep(1);
2014-09-13 00:10:45 +02:00
2015-06-10 11:40:43 +02:00
$this->options->setDatabase("tcp.test");
$body = $this->object->query("select * from \"vm-serie\"");
2014-09-13 00:10:45 +02:00
2015-06-10 11:40:43 +02:00
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("cpu", $body["results"][0]["series"][0]["columns"][1]);
$this->assertEquals(18.12, $body["results"][0]["series"][0]["values"][0][1]);
$this->assertEquals(712423, $body["results"][0]["series"][0]["values"][0][2]);
2014-09-13 00:10:45 +02:00
}
2014-09-13 08:19:51 +02:00
2015-06-08 18:58:52 +02:00
/**
* @group udp
*/
2015-06-10 11:40:43 +02:00
public function testUdpIpWriteData()
2015-06-08 18:58:52 +02:00
{
2015-06-10 15:19:41 +02:00
$object = $this->createClientWithUdpAdapter();
2015-06-08 18:58:52 +02:00
2015-06-10 11:40:43 +02:00
$object->mark("udp.test", ["mark" => "element"]);
sleep(1);
$object->mark("udp.test", ["mark" => "element1"]);
sleep(1);
$object->mark("udp.test", ["mark" => "element2"]);
2015-06-08 18:58:52 +02:00
sleep(1);
2015-06-10 11:40:43 +02:00
$object->mark("udp.test", ["mark" => "element3"]);
2015-06-08 18:58:52 +02:00
2015-06-10 11:40:43 +02:00
// Wait UDP/IP message arrives
sleep(2);
2015-06-08 18:58:52 +02:00
2015-06-10 11:40:43 +02:00
$this->options->setDatabase("udp.test");
$body = $this->object->query("select * from \"udp.test\"");
$this->assertCount(4, $body["results"][0]["series"][0]["values"]);
2015-06-08 18:58:52 +02:00
}
2015-06-10 14:53:31 +02:00
/**
* @group udp
*/
public function testSendMultipleMeasurementWithUdpIp()
{
2015-06-10 15:19:41 +02:00
$object = $this->createClientWithUdpAdapter();
2015-06-10 14:53:31 +02:00
$object->mark([
"points" => [
[
"measurement" => "mem",
"fields" => [
"free" => 712423,
],
],
[
"measurement" => "cpu",
"fields" => [
"cpu" => 18.12,
],
],
]
]);
sleep(2);
$this->options->setDatabase("udp.test");
$body = $this->object->query("select * from \"cpu\"");
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("cpu", $body["results"][0]["series"][0]["columns"][1]);
$this->assertEquals(18.12, $body["results"][0]["series"][0]["values"][0][1]);
$body = $this->object->query("select * from \"mem\"");
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("free", $body["results"][0]["series"][0]["columns"][1]);
$this->assertEquals(712423, $body["results"][0]["series"][0]["values"][0][1]);
}
2015-06-07 13:08:33 +02:00
/**
* @group udp
*/
public function testWriteDirectMessageWithUdpIp()
{
2015-06-10 15:19:41 +02:00
$object = $this->createClientWithUdpAdapter();
2015-06-07 13:08:33 +02:00
2015-06-07 23:15:40 +02:00
$object->mark([
"points" => [
[
2015-06-10 11:40:43 +02:00
"measurement" => "vm-serie",
2015-06-07 23:15:40 +02:00
"fields" => [
"cpu" => 18.12,
"free" => 712423,
],
],
]
]);
2015-06-10 11:40:43 +02:00
sleep(2);
2015-06-07 23:15:40 +02:00
$this->options->setDatabase("udp.test");
$body = $this->object->query("select * from \"vm-serie\"");
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("cpu", $body["results"][0]["series"][0]["columns"][1]);
$this->assertEquals(18.12, $body["results"][0]["series"][0]["values"][0][1]);
$this->assertEquals(712423, $body["results"][0]["series"][0]["values"][0][2]);
}
2015-06-10 15:40:10 +02:00
/**
* @group udp
* @group date
*/
public function testWriteDirectMessageWillPreserveActualTime()
{
$object = $this->createClientWithUdpAdapter();
$object->mark([
"points" => [
[
"measurement" => "vm-serie",
"fields" => [
"cpu" => 18.12,
"free" => 712423,
],
],
]
]);
sleep(2);
$this->options->setDatabase("udp.test");
$body = $this->object->query("select * from \"vm-serie\"");
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("time", $body["results"][0]["series"][0]["columns"][0]);
$saved = new DateTime($body["results"][0]["series"][0]["values"][0][0]);
$this->assertEquals(date("Y-m-d"), $saved->format("Y-m-d"));
}
/**
* @group udp
* @group date
*/
public function testWriteDirectMessageWillPreserveDatetime()
{
$object = $this->createClientWithUdpAdapter();
$object->mark([
"time" => "2009-11-10T23:00:00Z",
"points" => [
[
"measurement" => "vm-serie",
"fields" => [
"cpu" => 18.12,
"free" => 712423,
],
],
]
]);
sleep(2);
$this->options->setDatabase("udp.test");
$body = $this->object->query("select * from \"vm-serie\"");
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("time", $body["results"][0]["series"][0]["columns"][0]);
$this->assertEquals("2009-11-10T23:00:00Z", $body["results"][0]["series"][0]["values"][0][0]);
}
2015-06-07 23:15:40 +02:00
/**
* @group udp
* @group tags
*/
public function testTagsAreWrittenCorrectly()
{
2015-06-10 15:19:41 +02:00
$object = $this->createClientWithUdpAdapter();
2015-06-07 23:15:40 +02:00
$object->mark([
2015-06-07 13:08:33 +02:00
"tags" => [
2015-06-07 23:15:40 +02:00
"region" => "eu",
2015-06-07 13:08:33 +02:00
],
"points" => [
[
2015-06-10 11:40:43 +02:00
"measurement" => "vm-serie",
2015-06-07 23:15:40 +02:00
"tags" => [
"dc" => "eu-west-1",
"one" => "two",
],
2015-06-07 13:08:33 +02:00
"fields" => [
"cpu" => 18.12,
"free" => 712423,
],
],
2015-06-07 23:15:40 +02:00
[
2015-06-10 11:40:43 +02:00
"measurement" => "vm-serie",
2015-06-07 23:15:40 +02:00
"tags" => [
"dc" => "us-east-1",
],
"fields" => [
"cpu" => 28.12,
"free" => 412923,
],
],
2015-06-07 13:08:33 +02:00
]
]);
2015-06-10 11:40:43 +02:00
sleep(2);
2015-06-07 13:08:33 +02:00
$this->options->setDatabase("udp.test");
2015-06-07 23:15:40 +02:00
$body = $this->object->query("select * from \"vm-serie\" where dc='eu-west-1'");
2015-06-07 13:08:33 +02:00
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
$this->assertEquals("cpu", $body["results"][0]["series"][0]["columns"][1]);
$this->assertEquals(18.12, $body["results"][0]["series"][0]["values"][0][1]);
$this->assertEquals(712423, $body["results"][0]["series"][0]["values"][0][2]);
}
2014-09-13 08:19:51 +02:00
public function testListActiveDatabses()
{
$databases = $this->object->getDatabases();
2015-06-07 12:31:11 +02:00
$this->assertCount(2, $databases["results"][0]["series"][0]["values"]);
2014-09-13 08:19:51 +02:00
}
public function testCreateANewDatabase()
{
$this->object->createDatabase("walter");
2015-06-07 12:31:11 +02:00
sleep(1);
2014-09-13 08:19:51 +02:00
$databases = $this->object->getDatabases();
2015-06-07 12:31:11 +02:00
$this->assertCount(3, $databases["results"][0]["series"][0]["values"]);
2014-09-13 08:19:51 +02:00
$this->object->deleteDatabase("walter");
}
2015-06-10 15:19:41 +02:00
private function createClientWithUdpAdapter()
{
$rawOptions = $this->rawOptions;
$options = new Options();
$options->setHost($rawOptions["udp"]["host"]);
$options->setUsername($rawOptions["udp"]["username"]);
$options->setPassword($rawOptions["udp"]["password"]);
$options->setPort($rawOptions["udp"]["port"]);
$options->setDatabase($rawOptions["udp"]["database"]);
$adapter = new UdpAdapter($options);
$object = new Client($adapter);
return $object;
}
2014-09-11 14:50:31 +02:00
}