Skip to content

Commit d14c732

Browse files
authored
Merge pull request #702 from Icinga/add-perfdata-unittests
Add perfdata unittests
2 parents ef16387 + 5c20275 commit d14c732

File tree

6 files changed

+877
-7
lines changed

6 files changed

+877
-7
lines changed

library/Icingadb/Util/PerfDataSet.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected function readLabel(): string
115115
$this->skipSpaces();
116116
if (in_array($this->perfdataStr[$this->parserPos], array('"', "'"))) {
117117
$quoteChar = $this->perfdataStr[$this->parserPos++];
118-
$label = $this->readUntil($quoteChar);
118+
$label = $this->readUntil($quoteChar, '=');
119119
$this->parserPos++;
120120

121121
if ($this->perfdataStr[$this->parserPos] === '=') {
@@ -133,17 +133,30 @@ protected function readLabel(): string
133133
/**
134134
* Return all characters between the current parser position and the given character
135135
*
136-
* @param string $stopChar The character on which to stop
136+
* @param string $stopChar The character on which to stop
137+
* @param string $backtrackOn The character on which to backtrack
137138
*
138-
* @return string
139+
* @return string
139140
*/
140-
protected function readUntil(string $stopChar): string
141+
protected function readUntil(string $stopChar, string $backtrackOn = null): string
141142
{
142143
$start = $this->parserPos;
143-
while ($this->parserPos < strlen($this->perfdataStr) && $this->perfdataStr[$this->parserPos] !== $stopChar) {
144+
$breakCharEncounteredAt = null;
145+
$stringExhaustedAt = strlen($this->perfdataStr);
146+
while ($this->parserPos < $stringExhaustedAt) {
147+
if ($this->perfdataStr[$this->parserPos] === $stopChar) {
148+
break;
149+
} elseif ($breakCharEncounteredAt === null && $this->perfdataStr[$this->parserPos] === $backtrackOn) {
150+
$breakCharEncounteredAt = $this->parserPos;
151+
}
152+
144153
$this->parserPos++;
145154
}
146155

156+
if ($breakCharEncounteredAt !== null && $this->parserPos === $stringExhaustedAt) {
157+
$this->parserPos = $breakCharEncounteredAt;
158+
}
159+
147160
return substr($this->perfdataStr, $start, $this->parserPos - $start);
148161
}
149162

library/Icingadb/Util/ThresholdRange.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static function fromString(string $rawRange): self
9292
*
9393
* @return $this
9494
*/
95-
public function setMin(float $min): self
95+
public function setMin(?float $min): self
9696
{
9797
$this->min = $min;
9898
return $this;
@@ -115,7 +115,7 @@ public function getMin()
115115
*
116116
* @return $this
117117
*/
118-
public function setMax(float $max): self
118+
public function setMax(?float $max): self
119119
{
120120
$this->max = $max;
121121
return $this;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/* Icinga DB Web | (c) 2023 Icinga GmbH | GPLv2 */
4+
5+
namespace Tests\Icinga\Module\Icingadb\Lib;
6+
7+
use Icinga\Module\Icingadb\Util\PerfDataSet;
8+
9+
class PerfdataSetWithPublicData extends PerfdataSet
10+
{
11+
public $perfdata = [];
12+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
/* Icinga DB Web | (c) 2023 Icinga GmbH | GPLv2 */
4+
5+
namespace Tests\Icinga\Module\Icingadb\Util;
6+
7+
use Icinga\Module\Icingadb\Util\PerfDataSet;
8+
use PHPUnit\Framework\TestCase;
9+
use Tests\Icinga\Module\Icingadb\Lib\PerfdataSetWithPublicData;
10+
11+
class PerfdataSetTest extends TestCase
12+
{
13+
public function testWhetherValidSimplePerfdataLabelsAreProperlyParsed()
14+
{
15+
$pset = PerfdataSetWithPublicData::fromString('key1=val1 key2=val2 key3 =val3');
16+
$this->assertSame(
17+
'key1',
18+
$pset->perfdata[0]->getLabel(),
19+
'PerfdataSet does not correctly parse valid simple labels'
20+
);
21+
$this->assertSame(
22+
'key2',
23+
$pset->perfdata[1]->getLabel(),
24+
'PerfdataSet does not correctly parse valid simple labels'
25+
);
26+
$this->assertSame(
27+
'key3',
28+
$pset->perfdata[2]->getLabel(),
29+
'PerfdataSet does not correctly parse valid simple labels'
30+
);
31+
}
32+
33+
public function testWhetherNonQuotedPerfdataLablesWithSpacesAreProperlyParsed()
34+
{
35+
$pset = PerfdataSetWithPublicData::fromString('key 1=val1 key 1 + 1=val2');
36+
$this->assertSame(
37+
'key 1',
38+
$pset->perfdata[0]->getLabel(),
39+
'PerfdataSet does not correctly parse non quoted labels with spaces'
40+
);
41+
$this->assertSame(
42+
'key 1 + 1',
43+
$pset->perfdata[1]->getLabel(),
44+
'PerfdataSet does not correctly parse non quoted labels with spaces'
45+
);
46+
}
47+
48+
public function testWhetherValidQuotedPerfdataLabelsAreProperlyParsed()
49+
{
50+
$pset = PerfdataSetWithPublicData::fromString('\'key 1\'=val1 "key 2"=val2 \'a=b\'=0%;;2');
51+
$this->assertSame(
52+
'key 1',
53+
$pset->perfdata[0]->getLabel(),
54+
'PerfdataSet does not correctly parse valid quoted labels'
55+
);
56+
$this->assertSame(
57+
'key 2',
58+
$pset->perfdata[1]->getLabel(),
59+
'PerfdataSet does not correctly parse valid quoted labels'
60+
);
61+
$this->assertSame(
62+
'a=b',
63+
$pset->perfdata[2]->getLabel(),
64+
'PerfdataSet does not correctly parse labels with equal signs'
65+
);
66+
}
67+
68+
public function testWhetherInvalidQuotedPerfdataLabelsAreProperlyParsed()
69+
{
70+
$pset = PerfdataSetWithPublicData::fromString('\'key 1=1 key 2"=2');
71+
$this->assertSame(
72+
'key 1',
73+
$pset->perfdata[0]->getLabel(),
74+
'PerfdataSet does not correctly parse invalid quoted labels'
75+
);
76+
$this->assertSame(
77+
'key 2"',
78+
$pset->perfdata[1]->getLabel(),
79+
'PerfdataSet does not correctly parse invalid quoted labels'
80+
);
81+
$pset = PerfdataSetWithPublicData::fromString('"key 1=1 "key 2"=2');
82+
$this->assertSame(
83+
'key 1=1',
84+
$pset->perfdata[0]->getLabel(),
85+
'PerfdataSet does not correctly parse invalid quoted labels'
86+
);
87+
$this->assertNull(
88+
$pset->perfdata[0]->getValue()
89+
);
90+
$this->assertSame(
91+
'2"',
92+
$pset->perfdata[1]->getLabel(),
93+
'PerfdataSet does not correctly parse invalid quoted labels'
94+
);
95+
$this->assertSame(
96+
'2',
97+
$pset->perfdata[1]->getValue()
98+
);
99+
}
100+
101+
/**
102+
* @depends testWhetherValidSimplePerfdataLabelsAreProperlyParsed
103+
*/
104+
public function testWhetherAPerfdataSetIsIterable()
105+
{
106+
$pset = PerfdataSet::fromString('key=value');
107+
foreach ($pset as $p) {
108+
$this->assertSame('key', $p->getLabel());
109+
return;
110+
}
111+
112+
$this->fail('PerfdataSet objects cannot be iterated');
113+
}
114+
115+
public function testWhetherPerfdataSetsCanBeInitializedWithEmptyStrings()
116+
{
117+
$pset = PerfdataSetWithPublicData::fromString('');
118+
$this->assertEmpty($pset->perfdata, 'PerfdataSet::fromString does not accept emtpy strings');
119+
}
120+
}

0 commit comments

Comments
 (0)