From 4601873cb0d7b50cb5abcf03ec7b8db114b48923 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Wed, 30 Aug 2023 17:22:17 +0200 Subject: [PATCH] Add unit tests for invalid performance data evaluation --- .../library/Icingadb/Util/PerfdataTest.php | 33 +++++++++++++++++++ .../Icingadb/Util/ThresholdRangeTest.php | 20 +++++++++++ 2 files changed, 53 insertions(+) diff --git a/test/php/library/Icingadb/Util/PerfdataTest.php b/test/php/library/Icingadb/Util/PerfdataTest.php index e31c86d00..d3e63d14f 100644 --- a/test/php/library/Icingadb/Util/PerfdataTest.php +++ b/test/php/library/Icingadb/Util/PerfdataTest.php @@ -259,6 +259,17 @@ public function testWhetherGetMinimumValueReturnsCorrectValues() Perfdata::fromString('test=1;;;1337.5')->getMinimumValue(), 'Perfdata::getMinimumValue does not return correct values' ); + + $this->assertSame( + '10,00', + Perfdata::fromString('test=1;;;;10,00')->getMinimumValue(), + 'Perfdata::getMinimumValue does not return correct values' + ); + $this->assertSame( + 'foo', + Perfdata::fromString('test=1;;;;foo')->getMinimumValue(), + 'Perfdata::getMinimumValue does not return correct values' + ); } /** @@ -276,6 +287,16 @@ public function testWhetherGetMaximumValueReturnsCorrectValues() Perfdata::fromString('test=1;;;;1337.5')->getMaximumValue(), 'Perfdata::getMaximumValue does not return correct values' ); + $this->assertSame( + '10,00', + Perfdata::fromString('test=1;;;;10,00')->getMaximumValue(), + 'Perfdata::getMaximumValue does not return correct values' + ); + $this->assertSame( + 'foo', + Perfdata::fromString('test=1;;;;foo')->getMaximumValue(), + 'Perfdata::getMaximumValue does not return correct values' + ); } /** @@ -399,4 +420,16 @@ public function testWhetherPercentagesAreHandledCorrectly() 'Perfdata objects do not ignore impossible min/max combinations when returning percentages' ); } + + /** + * @depends testWhetherFromStringParsesAGivenStringCorrectly + */ + public function testInvalidPerfDataHandling() + { + $this->assertFalse(Perfdata::fromString('test=1;;;2,00;10,00')); + $this->assertFalse(Perfdata::fromString('test=1;;;foo;1000')); + $this->assertFalse(Perfdata::fromString('test=1;10@:')); + $this->assertFalse(Perfdata::fromString('test=1;foo:')); + $this->assertFalse(Perfdata::fromString('test=1;2,2:4,4')); + } } diff --git a/test/php/library/Icingadb/Util/ThresholdRangeTest.php b/test/php/library/Icingadb/Util/ThresholdRangeTest.php index c0d757371..b191e8804 100644 --- a/test/php/library/Icingadb/Util/ThresholdRangeTest.php +++ b/test/php/library/Icingadb/Util/ThresholdRangeTest.php @@ -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' + ); } }