Skip to content

Commit

Permalink
Add unit tests for invalid performance data evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Aug 31, 2023
1 parent 6402367 commit 0b30541
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 0 deletions.
192 changes: 192 additions & 0 deletions test/php/library/Icingadb/Util/PerfdataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,196 @@ public function testWhetherPercentagesAreHandledCorrectly()
'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()
);
}

/**
* @depends testWhetherFromStringParsesAGivenStringCorrectly
*/
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()
);
}
}
20 changes: 20 additions & 0 deletions test/php/library/Icingadb/Util/ThresholdRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,25 @@ public function testInvalidThresholdNotationsAreRenderedAsIs()
'foo',
(string) ThresholdRange::fromString('foo')
);
$this->assertSame(
'4,4:2,2',
(string) ThresholdRange::fromString('4,4:2,2')
);
}

public function testInvalidThresholdNotationsConsideredInValid()
{
$this->assertFalse(
ThresholdRange::fromString('10@')->isValid(),
'Invalid threshold notation 10@ considered as valid'
);
$this->assertFalse(
ThresholdRange::fromString('foo')->isValid(),
'Invalid threshold notation foo considered as valid'
);
$this->assertFalse(
ThresholdRange::fromString('4,4:2,2')->isValid(),
'Invalid threshold notation 4,4:2,2 considered as valid'
);
}
}

0 comments on commit 0b30541

Please sign in to comment.