1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
|
<?php
/* Icinga DB Web | (c) 2023 Icinga GmbH | GPLv2 */
namespace Tests\Icinga\Module\Icingadb\Util;
use Icinga\Module\Icingadb\Util\PerfData;
use PHPUnit\Framework\TestCase;
class PerfdataTest extends TestCase
{
public function testWhetherFromStringThrowsExceptionWhenGivenAnEmptyString()
{
$this->expectException(\InvalidArgumentException::class);
Perfdata::fromString('');
}
public function testWhetherFromStringThrowsExceptionWhenGivenAnInvalidString()
{
$this->expectException(\InvalidArgumentException::class);
Perfdata::fromString('test');
}
public function testWhetherFromStringParsesAGivenStringCorrectly()
{
$p = Perfdata::fromString('key=1234');
$this->assertSame(
'key',
$p->getLabel(),
'Perfdata::fromString does not properly parse performance data labels'
);
$this->assertSame(
1234.0,
$p->getValue(),
'Perfdata::fromString does not properly parse performance data values'
);
}
/**
* @depends testWhetherFromStringParsesAGivenStringCorrectly
*/
public function testWhetherGetValueReturnsValidValues()
{
$this->assertSame(
1337.0,
Perfdata::fromString('test=1337')->getValue(),
'Perfdata::getValue does not return correct values'
);
$this->assertSame(
1337.0,
Perfdata::fromString('test=1337;;;;')->getValue(),
'Perfdata::getValue does not return correct values'
);
}
/**
* @depends testWhetherFromStringParsesAGivenStringCorrectly
*/
public function testWhetherDecimalValuesAreCorrectlyParsed()
{
$this->assertSame(
1337.5,
Perfdata::fromString('test=1337.5')->getValue(),
'Perfdata objects do not parse decimal values correctly'
);
$this->assertSame(
1337.5,
Perfdata::fromString('test=1337.5B')->getValue(),
'Perfdata objects do not parse decimal values correctly'
);
}
/**
* @depends testWhetherFromStringParsesAGivenStringCorrectly
*/
public function testWhetherGetValueReturnsNullForInvalidOrUnknownValues()
{
$this->assertNull(
Perfdata::fromString('test=U')->getValue(),
'Perfdata::getValue does not return null for unknown values'
);
$this->assertNull(
Perfdata::fromString('test=i am not a value')->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
$this->assertNull(
PerfData::fromString('test=')->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
$this->assertNull(
PerfData::fromString('test=-kW')->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
$this->assertNull(
PerfData::fromString('test=kW')->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
$this->assertNull(
PerfData::fromString('test=-')->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
}
/**
* @depends testWhetherFromStringParsesAGivenStringCorrectly
*/
public function testWhetherUnitOfUnkownValuesIsCorrectlyIdentified()
{
$this->assertNull(
Perfdata::fromString('test=U')->getUnit(),
'Perfdata::getUnit does not return null for unknown values'
);
$this->assertNull(
Perfdata::fromString('test=i am not a value')->getUnit(),
'Perfdata::getUnit does not return null for unknown values'
);
$this->assertNull(
PerfData::fromString('test=')->getUnit(),
'Perfdata::getUnit does not return null for unknown values'
);
$this->assertSame(
'kW',
PerfData::fromString('test=-kW')->getUnit(),
'Perfdata::getUnit does not return correct unit for invalid values'
);
$this->assertSame(
'kW',
PerfData::fromString('test=kW')->getUnit(),
'Perfdata::getUnit does not return correct unit for invalid values'
);
$this->assertNull(
PerfData::fromString('test=-')->getUnit(),
'Perfdata::getUnit does not return null for unknown values'
);
}
/**
* @depends testWhetherFromStringParsesAGivenStringCorrectly
*/
public function testWhethergetWarningThresholdReturnsCorrectValues()
{
$zeroToTen = Perfdata::fromString('test=1;10')->getWarningThreshold();
$this->assertSame(
0.0,
$zeroToTen->getMin(),
'Perfdata::getWarningThreshold does not return correct values'
);
$this->assertSame(
10.0,
$zeroToTen->getMax(),
'Perfdata::getWarningThreshold does not return correct values'
);
$tenToInfinity = Perfdata::fromString('test=1;10:')->getWarningThreshold();
$this->assertSame(
10.0,
$tenToInfinity->getMin(),
'Perfdata::getWarningThreshold does not return correct values'
);
$this->assertNull(
$tenToInfinity->getMax(),
'Perfdata::getWarningThreshold does not return correct values'
);
$infinityToTen = Perfdata::fromString('test=1;~:10')->getWarningThreshold();
$this->assertNull(
$infinityToTen->getMin(),
'Perfdata::getWarningThreshold does not return correct values'
);
$this->assertSame(
10.0,
$infinityToTen->getMax(),
'Perfdata::getWarningThreshold does not return correct values'
);
$tenToTwenty = Perfdata::fromString('test=1;10:20')->getWarningThreshold();
$this->assertSame(
10.0,
$tenToTwenty->getMin(),
'Perfdata::getWarningThreshold does not return correct values'
);
$this->assertSame(
20.0,
$tenToTwenty->getMax(),
'Perfdata::getWarningThreshold does not return correct values'
);
$tenToTwentyInverted = Perfdata::fromString('test=1;@10:20')->getWarningThreshold();
$this->assertTrue(
$tenToTwentyInverted->isInverted(),
'Perfdata::getWarningThreshold does not return correct values'
);
}
/**
* @depends testWhetherFromStringParsesAGivenStringCorrectly
*/
public function testWhetherGetCriticalThresholdReturnsCorrectValues()
{
$zeroToTen = Perfdata::fromString('test=1;;10')->getCriticalThreshold();
$this->assertSame(
0.0,
$zeroToTen->getMin(),
'Perfdata::getCriticalThreshold does not return correct values'
);
$this->assertSame(
10.0,
$zeroToTen->getMax(),
'Perfdata::getCriticalThreshold does not return correct values'
);
$tenToInfinity = Perfdata::fromString('test=1;;10:')->getCriticalThreshold();
$this->assertSame(
10.0,
$tenToInfinity->getMin(),
'Perfdata::getCriticalThreshold does not return correct values'
);
$this->assertNull(
$tenToInfinity->getMax(),
'Perfdata::getCriticalThreshold does not return correct values'
);
$infinityToTen = Perfdata::fromString('test=1;;~:10')->getCriticalThreshold();
$this->assertNull(
$infinityToTen->getMin(),
'Perfdata::getCriticalThreshold does not return correct values'
);
$this->assertSame(
10.0,
$infinityToTen->getMax(),
'Perfdata::getCriticalThreshold does not return correct values'
);
$tenToTwenty = Perfdata::fromString('test=1;;10:20')->getCriticalThreshold();
$this->assertSame(
10.0,
$tenToTwenty->getMin(),
'Perfdata::getCriticalThreshold does not return correct values'
);
$this->assertSame(
20.0,
$tenToTwenty->getMax(),
'Perfdata::getCriticalThreshold does not return correct values'
);
$tenToTwentyInverted = Perfdata::fromString('test=1;;@10:20')->getCriticalThreshold();
$this->assertTrue(
$tenToTwentyInverted->isInverted(),
'Perfdata::getCriticalThreshold does not return correct values'
);
}
/**
* @depends testWhetherFromStringParsesAGivenStringCorrectly
*/
public function testWhetherGetMinimumValueReturnsCorrectValues()
{
$this->assertSame(
1337.0,
Perfdata::fromString('test=1;;;1337')->getMinimumValue(),
'Perfdata::getMinimumValue does not return correct values'
);
$this->assertSame(
1337.5,
Perfdata::fromString('test=1;;;1337.5')->getMinimumValue(),
'Perfdata::getMinimumValue does not return correct values'
);
}
/**
* @depends testWhetherFromStringParsesAGivenStringCorrectly
*/
public function testWhetherGetMaximumValueReturnsCorrectValues()
{
$this->assertSame(
1337.0,
Perfdata::fromString('test=1;;;;1337')->getMaximumValue(),
'Perfdata::getMaximumValue does not return correct values'
);
$this->assertSame(
1337.5,
Perfdata::fromString('test=1;;;;1337.5')->getMaximumValue(),
'Perfdata::getMaximumValue does not return correct values'
);
}
/**
* @depends testWhetherFromStringParsesAGivenStringCorrectly
*/
public function testWhetherMissingValuesAreProperlyHandled()
{
$perfdata = Perfdata::fromString('test=1;;3;5');
$this->assertEmpty(
(string) $perfdata->getWarningThreshold(),
'Perfdata objects do not correctly identify omitted warning tresholds'
);
$this->assertNull(
$perfdata->getMaximumValue(),
'Perfdata objects do not return null for missing maximum values'
);
}
/**
* @depends testWhetherGetValueReturnsValidValues
*/
public function testWhetherValuesAreIdentifiedAsNumber()
{
$this->assertTrue(
Perfdata::fromString('test=666')->isNumber(),
'Perfdata objects do not identify ordinary digits as number'
);
}
/**
* @depends testWhetherGetValueReturnsValidValues
*/
public function testWhetherValuesAreIdentifiedAsSeconds()
{
$this->assertTrue(
Perfdata::fromString('test=666s')->isSeconds(),
'Perfdata objects do not identify seconds as seconds'
);
}
/**
* @depends testWhetherGetValueReturnsValidValues
*/
public function testWhetherValuesAreIdentifiedAsPercentage()
{
$this->assertTrue(
Perfdata::fromString('test=66%')->isPercentage(),
'Perfdata objects do not identify percentages as percentages'
);
}
/**
* @depends testWhetherValuesAreIdentifiedAsPercentage
*/
public function testWhetherMinAndMaxAreNotRequiredIfUnitIsInPercent()
{
$perfdata = Perfdata::fromString('test=1%');
$this->assertSame(
0.0,
$perfdata->getMinimumValue(),
'Perfdata objects do not set minimum value to 0 if UOM is %'
);
$this->assertSame(
100.0,
$perfdata->getMaximumValue(),
'Perfdata objects do not set maximum value to 100 if UOM is %'
);
}
/**
* @depends testWhetherGetValueReturnsValidValues
*/
public function testWhetherValuesAreIdentifiedAsBytes()
{
$this->assertTrue(
Perfdata::fromString('test=66666B')->isBytes(),
'Perfdata objects do not identify bytes as bytes'
);
}
/**
* @depends testWhetherGetValueReturnsValidValues
*/
public function testWhetherValuesAreIdentifiedAsCounter()
{
$this->assertTrue(
Perfdata::fromString('test=123c')->isCounter(),
'Perfdata objects do not identify counters as counters'
);
}
/**
* @depends testWhetherValuesAreIdentifiedAsPercentage
*/
public function testWhetherPercentagesAreHandledCorrectly()
{
$this->assertSame(
66.0,
Perfdata::fromString('test=66%')->getPercentage(),
'Perfdata objects do not correctly handle native percentages'
);
$this->assertSame(
50.0,
Perfdata::fromString('test=0;;;-250;250')->getPercentage(),
'Perfdata objects do not correctly convert suitable values to percentages'
);
$this->assertNull(
Perfdata::fromString('test=50')->getPercentage(),
'Perfdata objects do return a percentage though their unit is not % and no maximum is given'
);
$this->assertNull(
Perfdata::fromString('test=25;;;50;100')->getPercentage(),
'Perfdata objects do return a percentage though their value is lower than it\'s allowed minimum'
);
$this->assertNull(
Perfdata::fromString('test=25;;;0;')->getPercentage(),
'Perfdata objects do not ignore empty max values when returning percentages'
);
$this->assertNull(
Perfdata::fromString('test=25;;;0;0')->getPercentage(),
'Perfdata objects do not ignore impossible min/max combinations when returning percentages'
);
}
public function testWhetherInvalidValueInPerfDataHandledCorrectly()
{
$p1 = Perfdata::fromString('test=2,0');
$this->assertFalse($p1->isValid());
$this->assertNull(
$p1->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
$this->assertSame(
'2,0',
$p1->toArray()['value']
);
$p2 = Perfdata::fromString('test=i am not a value');
$this->assertFalse($p2->isValid());
$this->assertNull(
$p2->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
$this->assertSame(
'i am not a value',
$p2->toArray()['value']
);
$p3 = Perfdata::fromString('test=');
$this->assertFalse($p3->isValid());
$this->assertNull(
$p3->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
$this->assertSame(
'',
$p3->toArray()['value']
);
$p4 = Perfdata::fromString('test=-kW');
$this->assertFalse($p4->isValid());
$this->assertNull(
$p4->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
$this->assertSame(
'-kW',
$p4->toArray()['value']
);
$p5 = Perfdata::fromString('test=kW');
$this->assertFalse($p5->isValid());
$this->assertNull(
$p5->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
$this->assertSame(
'kW',
$p5->toArray()['value']
);
$p6 = Perfdata::fromString('test=-');
$this->assertFalse($p6->isValid());
$this->assertNull(
$p6->getValue(),
'Perfdata::getValue does not return null for invalid values'
);
$this->assertSame(
'-',
$p6->toArray()['value']
);
}
public function testWhetherInvalidMinInPerfDataHandledCorrectly()
{
$p1 = Perfdata::fromString('test=1;;;2,0');
$this->assertFalse($p1->isValid());
$this->assertNull(
$p1->getMinimumValue(),
'Perfdata::getMinimumValue does not return null for invalid min values'
);
$this->assertSame(
'2,0',
$p1->toArray()['min']
);
$p2 = Perfdata::fromString('test=1;;;foo');
$this->assertFalse($p2->isValid());
$this->assertNull(
$p2->getMinimumValue(),
'Perfdata::getMinimumValue does not return null for invalid min values'
);
$this->assertSame(
'foo',
$p2->toArray()['min']
);
}
public function testWhetherInvalidMaxInPerfDataHandledCorrectly()
{
$p1 = Perfdata::fromString('test=1;;;;2,0');
$this->assertFalse($p1->isValid());
$this->assertNull(
$p1->getMaximumValue(),
'Perfdata::getMaximumValue does not return null for invalid max values'
);
$this->assertSame(
'2,0',
$p1->toArray()['max']
);
$p2 = Perfdata::fromString('test=1;;;;foo');
$this->assertFalse($p2->isValid());
$this->assertNull(
$p2->getMaximumValue(),
'Perfdata::getMaximumValue does not return null for invalid max values'
);
$this->assertSame(
'foo',
$p2->toArray()['max']
);
}
public function testWhetherInvalidWarningThresholdInPerfDataHandledCorrectly()
{
$p1 = Perfdata::fromString('test=1;2,0:');
$this->assertFalse($p1->getWarningThreshold()->isValid());
$this->assertFalse($p1->isValid());
$this->assertSame(
'2,0:',
(string) $p1->getWarningThreshold()
);
$p2 = Perfdata::fromString('test=1;0:4,0');
$this->assertFalse($p2->getWarningThreshold()->isValid());
$this->assertFalse($p2->isValid());
$this->assertSame(
'0:4,0',
(string) $p2->getWarningThreshold()
);
$p3 = Perfdata::fromString('test=1;foo');
$this->assertFalse($p2->getWarningThreshold()->isValid());
$this->assertFalse($p3->isValid());
$this->assertSame(
'foo',
(string) $p3->getWarningThreshold()
);
$p4 = Perfdata::fromString('test=1;10@');
$this->assertFalse($p2->getWarningThreshold()->isValid());
$this->assertFalse($p4->isValid());
$this->assertSame(
'10@',
(string) $p4->getWarningThreshold()
);
}
public function testWhetherInvalidCriticalThresholdInPerfDataHandledCorrectly()
{
$p1 = Perfdata::fromString('test=1;;2,0:');
$this->assertFalse($p1->getCriticalThreshold()->isValid());
$this->assertFalse($p1->isValid());
$this->assertSame(
'2,0:',
(string) $p1->getCriticalThreshold()
);
$p2 = Perfdata::fromString('test=1;;0:4,0');
$this->assertFalse($p2->getCriticalThreshold()->isValid());
$this->assertFalse($p2->isValid());
$this->assertSame(
'0:4,0',
(string) $p2->getCriticalThreshold()
);
$p3 = Perfdata::fromString('test=1;;foo');
$this->assertFalse($p2->getCriticalThreshold()->isValid());
$this->assertFalse($p3->isValid());
$this->assertSame(
'foo',
(string) $p3->getCriticalThreshold()
);
$p4 = Perfdata::fromString('test=1;;10@');
$this->assertFalse($p2->getCriticalThreshold()->isValid());
$this->assertFalse($p4->isValid());
$this->assertSame(
'10@',
(string) $p4->getCriticalThreshold()
);
}
}
|