Files
librenms-librenms/tests/ClientTest.php

440 lines
13 KiB
PHP
Raw Normal View History

2014-09-11 14:50:31 +02:00
<?php
namespace InfluxDB;
use DateTime;
use DateTimeZone;
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($adapter);
2014-09-12 20:20:23 +02:00
$this->object = $influx;
$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]);
}
}
$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
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]);
}
/**
* @group tcp
* @group proxy
*/
public function testGuzzleHttpApiWorksCorrectlyWithProxies()
{
$this->options->setHost("localhost");
$this->options->setPort(9000);
$this->options->setPrefix("/influxdb");
$this->object->mark("tcp.test", ["mark" => "element"]);
sleep(2);
2015-06-04 13:14:38 +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-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" => [
[
"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
/**
* @group tcp
2014-09-13 00:10:45 +02:00
*/
public function testOverrideDatabaseNameViaMessage()
2014-09-13 00:10:45 +02:00
{
$this->options->setDatabase("a-wrong-database");
2014-09-13 00:10:45 +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
$this->options->setDatabase("tcp.test");
$body = $this->object->query("select * from \"vm-serie\"");
2014-09-13 00:10:45 +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
}
/**
* @group udp
*/
public function testUdpIpWriteData()
{
$object = $this->createClientWithUdpAdapter();
$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
sleep(2);
$this->options->setDatabase("udp.test");
$body = $this->object->query("select * from \"udp.test\"");
$this->assertCount(4, $body["results"][0]["series"][0]["values"]);
}
/**
* @group udp
*/
public function testSendMultipleMeasurementWithUdpIp()
{
$object = $this->createClientWithUdpAdapter();
$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]);
}
/**
* @group udp
*/
public function testWriteDirectMessageWithUdpIp()
{
$object = $this->createClientWithUdpAdapter();
2015-06-07 23:15:40 +02:00
$object->mark([
"points" => [
[
"measurement" => "vm-serie",
2015-06-07 23:15:40 +02:00
"fields" => [
"cpu" => 18.12,
"free" => 712423,
],
],
]
]);
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]);
}
/**
* @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 = $body["results"][0]["series"][0]["values"][0][0];
$this->assertRegExp("/".date("Y-m-d")."/i", $saved);
}
/**
* @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()
{
$object = $this->createClientWithUdpAdapter();
2015-06-07 23:15:40 +02:00
$object->mark([
"tags" => [
2015-06-07 23:15:40 +02:00
"region" => "eu",
],
"points" => [
[
"measurement" => "vm-serie",
2015-06-07 23:15:40 +02:00
"tags" => [
"dc" => "eu-west-1",
"one" => "two",
],
"fields" => [
"cpu" => 18.12,
"free" => 712423,
],
],
2015-06-07 23:15:40 +02:00
[
"measurement" => "vm-serie",
2015-06-07 23:15:40 +02:00
"tags" => [
"dc" => "us-east-1",
],
"fields" => [
"cpu" => 28.12,
"free" => 412923,
],
],
]
]);
sleep(2);
$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'");
$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]);
}
/**
* Test that we handle socket problems correctly in the UDP
* adapter, and that they don't inturrupt the user's application.
*
* @group udp
*/
public function testReplicateIssue27()
{
$options = new \InfluxDB\Options();
// Configure options
$options->setHost('172.16.1.182');
$options->setPort(4444);
$options->setDatabase('...');
$options->setUsername('root');
$options->setPassword('root');
$httpAdapter = new \InfluxDB\Adapter\UdpAdapter($options);
$client = new \InfluxDB\Client($httpAdapter);
$client->mark("udp.test", ["mark" => "element"]);
}
public function testListActiveDatabses()
{
$databases = $this->object->getDatabases();
2015-06-07 12:31:11 +02:00
$this->assertCount(2, $databases["results"][0]["series"][0]["values"]);
}
public function testCreateANewDatabase()
{
$this->object->createDatabase("walter");
2015-06-07 12:31:11 +02:00
sleep(1);
$databases = $this->object->getDatabases();
2015-06-07 12:31:11 +02:00
$this->assertCount(3, $databases["results"][0]["series"][0]["values"]);
$this->object->deleteDatabase("walter");
}
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
}