mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Our test suite mixing integration and few unit tests, is difficult to understand and expand with new features. This commit rewrite completely the test suite in order to separate both: integration and unit tests in different suites in a more maintainable and useful way.
173 lines
5.1 KiB
PHP
173 lines
5.1 KiB
PHP
<?php
|
|
namespace InfluxDB\Adapter;
|
|
|
|
use DateTime;
|
|
use DateTimeZone;
|
|
use InfluxDB\Options;
|
|
use GuzzleHttp\Client as GuzzleHttpClient;
|
|
use InfluxDB\Adapter\GuzzleAdapter as InfluxHttpAdapter;
|
|
use InfluxDB\Client;
|
|
use Prophecy\Argument;
|
|
|
|
class UdpAdapterTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
/**
|
|
* @dataProvider getMessages
|
|
*/
|
|
public function testRewriteMessages($input, $response)
|
|
{
|
|
$object = new UdpAdapter(new Options());
|
|
$reflection = new \ReflectionClass(get_class($object));
|
|
$method = $reflection->getMethod("serialize");
|
|
$method->setAccessible(true);
|
|
|
|
$message = $method->invokeArgs($object, [$input]);
|
|
|
|
$this->assertEquals($response, $message);
|
|
}
|
|
|
|
public function getMessages()
|
|
{
|
|
return [
|
|
[
|
|
[
|
|
"time" => "2009-11-10T23:00:00Z",
|
|
"points" => [
|
|
[
|
|
"measurement" => "cpu",
|
|
"fields" => [
|
|
"value" => 1,
|
|
],
|
|
],
|
|
],
|
|
],
|
|
"cpu value=1 1257894000000000000"
|
|
],
|
|
[
|
|
[
|
|
"tags" => [
|
|
"region" => "us-west",
|
|
"host" => "serverA",
|
|
"env" => "prod",
|
|
"target" => "servers",
|
|
"zone" => "1c",
|
|
],
|
|
"time" => "2009-11-10T23:00:00Z",
|
|
"points" => [
|
|
[
|
|
"measurement" => "cpu",
|
|
"fields" => [
|
|
"cpu" => 18.12,
|
|
"free" => 712432,
|
|
],
|
|
],
|
|
],
|
|
],
|
|
"cpu,region=us-west,host=serverA,env=prod,target=servers,zone=1c cpu=18.12,free=712432 1257894000000000000"
|
|
],
|
|
[
|
|
[
|
|
"tags" => [
|
|
"region" => "us-west",
|
|
"host" => "serverA",
|
|
"env" => "prod",
|
|
"target" => "servers",
|
|
"zone" => "1c",
|
|
],
|
|
"time" => "2009-11-10T23:00:00Z",
|
|
"points" => [
|
|
[
|
|
"measurement" => "cpu",
|
|
"fields" => [
|
|
"cpu" => 18.12,
|
|
],
|
|
],
|
|
[
|
|
"measurement" => "mem",
|
|
"fields" => [
|
|
"free" => 712432,
|
|
],
|
|
],
|
|
],
|
|
],
|
|
<<<EOF
|
|
cpu,region=us-west,host=serverA,env=prod,target=servers,zone=1c cpu=18.12 1257894000000000000
|
|
mem,region=us-west,host=serverA,env=prod,target=servers,zone=1c free=712432 1257894000000000000
|
|
EOF
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @group udp
|
|
*/
|
|
public function testUdpIpWriteDataWillBeConvertedAsLineProtocol()
|
|
{
|
|
$options = (new Options())->setDatabase("test");
|
|
$adapter = $this->getMockBuilder("InfluxDB\\Adapter\\UdpAdapter")
|
|
->setConstructorArgs([$options])
|
|
->setMethods(["write", "generateTimeInNanoSeconds"])
|
|
->getMock();
|
|
|
|
$adapter->expects($this->any())
|
|
->method("generateTimeInNanoSeconds")
|
|
->will($this->returnValue(1245));
|
|
|
|
$adapter->expects($this->once())
|
|
->method("write")
|
|
->with("udp.test mark=\"element\" 1245");
|
|
|
|
$adapter->send([
|
|
"points" => [
|
|
[
|
|
"measurement" => "udp.test",
|
|
"fields" => [
|
|
"mark" => "element"
|
|
]
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @group udp
|
|
*/
|
|
public function testSendMultipleMeasurementWithUdpIp()
|
|
{
|
|
$options = (new Options())->setDatabase("test");
|
|
$adapter = $this->getMockBuilder("InfluxDB\\Adapter\\UdpAdapter")
|
|
->setConstructorArgs([$options])
|
|
->setMethods(["write", "generateTimeInNanoSeconds"])
|
|
->getMock();
|
|
|
|
$adapter->expects($this->any())
|
|
->method("generateTimeInNanoSeconds")
|
|
->will($this->onConsecutiveCalls(1245, 1246));
|
|
|
|
$adapter->expects($this->once())
|
|
->method("write")
|
|
->with(<<<EOF
|
|
mem free=712423 1245
|
|
cpu cpu=18.12 1245
|
|
EOF
|
|
);
|
|
|
|
$adapter->send([
|
|
"points" => [
|
|
[
|
|
"measurement" => "mem",
|
|
"fields" => [
|
|
"free" => 712423,
|
|
],
|
|
],
|
|
[
|
|
"measurement" => "cpu",
|
|
"fields" => [
|
|
"cpu" => 18.12,
|
|
],
|
|
],
|
|
]
|
|
]);
|
|
}
|
|
}
|