Skip to content

Commit

Permalink
Merge pull request #702 from Icinga/add-perfdata-unittests
Browse files Browse the repository at this point in the history
Add perfdata unittests
  • Loading branch information
nilmerg committed Jan 19, 2023
2 parents ef16387 + 5c20275 commit d14c732
Show file tree
Hide file tree
Showing 6 changed files with 877 additions and 7 deletions.
23 changes: 18 additions & 5 deletions library/Icingadb/Util/PerfDataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function readLabel(): string
$this->skipSpaces();
if (in_array($this->perfdataStr[$this->parserPos], array('"', "'"))) {
$quoteChar = $this->perfdataStr[$this->parserPos++];
$label = $this->readUntil($quoteChar);
$label = $this->readUntil($quoteChar, '=');
$this->parserPos++;

if ($this->perfdataStr[$this->parserPos] === '=') {
Expand All @@ -133,17 +133,30 @@ protected function readLabel(): string
/**
* Return all characters between the current parser position and the given character
*
* @param string $stopChar The character on which to stop
* @param string $stopChar The character on which to stop
* @param string $backtrackOn The character on which to backtrack
*
* @return string
* @return string
*/
protected function readUntil(string $stopChar): string
protected function readUntil(string $stopChar, string $backtrackOn = null): string
{
$start = $this->parserPos;
while ($this->parserPos < strlen($this->perfdataStr) && $this->perfdataStr[$this->parserPos] !== $stopChar) {
$breakCharEncounteredAt = null;
$stringExhaustedAt = strlen($this->perfdataStr);
while ($this->parserPos < $stringExhaustedAt) {
if ($this->perfdataStr[$this->parserPos] === $stopChar) {
break;
} elseif ($breakCharEncounteredAt === null && $this->perfdataStr[$this->parserPos] === $backtrackOn) {
$breakCharEncounteredAt = $this->parserPos;
}

$this->parserPos++;
}

if ($breakCharEncounteredAt !== null && $this->parserPos === $stringExhaustedAt) {
$this->parserPos = $breakCharEncounteredAt;
}

return substr($this->perfdataStr, $start, $this->parserPos - $start);
}

Expand Down
4 changes: 2 additions & 2 deletions library/Icingadb/Util/ThresholdRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static function fromString(string $rawRange): self
*
* @return $this
*/
public function setMin(float $min): self
public function setMin(?float $min): self
{
$this->min = $min;
return $this;
Expand All @@ -115,7 +115,7 @@ public function getMin()
*
* @return $this
*/
public function setMax(float $max): self
public function setMax(?float $max): self
{
$this->max = $max;
return $this;
Expand Down
12 changes: 12 additions & 0 deletions test/php/Lib/PerfdataSetWithPublicData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/* Icinga DB Web | (c) 2023 Icinga GmbH | GPLv2 */

namespace Tests\Icinga\Module\Icingadb\Lib;

use Icinga\Module\Icingadb\Util\PerfDataSet;

class PerfdataSetWithPublicData extends PerfdataSet
{
public $perfdata = [];
}
120 changes: 120 additions & 0 deletions test/php/library/Icingadb/Util/PerfdataSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

/* Icinga DB Web | (c) 2023 Icinga GmbH | GPLv2 */

namespace Tests\Icinga\Module\Icingadb\Util;

use Icinga\Module\Icingadb\Util\PerfDataSet;
use PHPUnit\Framework\TestCase;
use Tests\Icinga\Module\Icingadb\Lib\PerfdataSetWithPublicData;

class PerfdataSetTest extends TestCase
{
public function testWhetherValidSimplePerfdataLabelsAreProperlyParsed()
{
$pset = PerfdataSetWithPublicData::fromString('key1=val1 key2=val2 key3 =val3');
$this->assertSame(
'key1',
$pset->perfdata[0]->getLabel(),
'PerfdataSet does not correctly parse valid simple labels'
);
$this->assertSame(
'key2',
$pset->perfdata[1]->getLabel(),
'PerfdataSet does not correctly parse valid simple labels'
);
$this->assertSame(
'key3',
$pset->perfdata[2]->getLabel(),
'PerfdataSet does not correctly parse valid simple labels'
);
}

public function testWhetherNonQuotedPerfdataLablesWithSpacesAreProperlyParsed()
{
$pset = PerfdataSetWithPublicData::fromString('key 1=val1 key 1 + 1=val2');
$this->assertSame(
'key 1',
$pset->perfdata[0]->getLabel(),
'PerfdataSet does not correctly parse non quoted labels with spaces'
);
$this->assertSame(
'key 1 + 1',
$pset->perfdata[1]->getLabel(),
'PerfdataSet does not correctly parse non quoted labels with spaces'
);
}

public function testWhetherValidQuotedPerfdataLabelsAreProperlyParsed()
{
$pset = PerfdataSetWithPublicData::fromString('\'key 1\'=val1 "key 2"=val2 \'a=b\'=0%;;2');
$this->assertSame(
'key 1',
$pset->perfdata[0]->getLabel(),
'PerfdataSet does not correctly parse valid quoted labels'
);
$this->assertSame(
'key 2',
$pset->perfdata[1]->getLabel(),
'PerfdataSet does not correctly parse valid quoted labels'
);
$this->assertSame(
'a=b',
$pset->perfdata[2]->getLabel(),
'PerfdataSet does not correctly parse labels with equal signs'
);
}

public function testWhetherInvalidQuotedPerfdataLabelsAreProperlyParsed()
{
$pset = PerfdataSetWithPublicData::fromString('\'key 1=1 key 2"=2');
$this->assertSame(
'key 1',
$pset->perfdata[0]->getLabel(),
'PerfdataSet does not correctly parse invalid quoted labels'
);
$this->assertSame(
'key 2"',
$pset->perfdata[1]->getLabel(),
'PerfdataSet does not correctly parse invalid quoted labels'
);
$pset = PerfdataSetWithPublicData::fromString('"key 1=1 "key 2"=2');
$this->assertSame(
'key 1=1',
$pset->perfdata[0]->getLabel(),
'PerfdataSet does not correctly parse invalid quoted labels'
);
$this->assertNull(
$pset->perfdata[0]->getValue()
);
$this->assertSame(
'2"',
$pset->perfdata[1]->getLabel(),
'PerfdataSet does not correctly parse invalid quoted labels'
);
$this->assertSame(
'2',
$pset->perfdata[1]->getValue()
);
}

/**
* @depends testWhetherValidSimplePerfdataLabelsAreProperlyParsed
*/
public function testWhetherAPerfdataSetIsIterable()
{
$pset = PerfdataSet::fromString('key=value');
foreach ($pset as $p) {
$this->assertSame('key', $p->getLabel());
return;
}

$this->fail('PerfdataSet objects cannot be iterated');
}

public function testWhetherPerfdataSetsCanBeInitializedWithEmptyStrings()
{
$pset = PerfdataSetWithPublicData::fromString('');
$this->assertEmpty($pset->perfdata, 'PerfdataSet::fromString does not accept emtpy strings');
}
}
Loading

0 comments on commit d14c732

Please sign in to comment.