Skip to content

Commit

Permalink
Merge pull request #83 from Icinga/chore/more-tests
Browse files Browse the repository at this point in the history
Extend tests
  • Loading branch information
martialblog committed Aug 13, 2024
2 parents 6f3002a + 2e0fb5c commit 00e1500
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ reports/
/Icinga
/Zend
/vendor
test/testdata/views/deleteme*
5 changes: 0 additions & 5 deletions library/Toplevelview/ViewConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ public function delete($view): void
*
* @param $view The view to store
* @param $force Stores a backup even if the content hasn't changed
* @throws ProgrammingError if the file with timestamp already exists
* @throws NotReadableError if the file cannot be read
*/
protected function storeBackup($view, $force = false): void
Expand All @@ -338,10 +337,6 @@ protected function storeBackup($view, $force = false): void
$ts = (string) time();
$backup = $backup_dir . DIRECTORY_SEPARATOR . $ts . '.' . $view->getFormat();

if (file_exists($backup)) {
throw new ProgrammingError('History file with timestamp already present: %s', $backup);
}

$existing_file = $this->getConfigDir() . DIRECTORY_SEPARATOR . $view->getName() . '.' . $view->getFormat();
$oldText = file_get_contents($existing_file);

Expand Down
58 changes: 58 additions & 0 deletions test/php/library/Toplevelview/Tree/TLVStatusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Tests\Icinga\Module\Toplevelview;

use Icinga\Module\Toplevelview\Tree\TLVStatus;

use PHPUnit\Framework\TestCase;

final class TLVStatusTest extends TestCase
{
public function testGetOverall()
{
$t = new TLVStatus();
$t->add('ok', 1);
$this->assertSame('ok', $t->getOverall());

$t->add('missing', 1);
$this->assertSame('ok', $t->getOverall());

$t->add('critical_handled', 1);
$this->assertSame('critical handled', $t->getOverall());

$t->zero();
$t->add('total');

$this->assertSame(1, $t->get('total'));
$this->assertSame(0, $t->get('missing'));
}

public function testGetterSetter()
{
$t = new TLVStatus();
$t->set('missing', 123);
$this->assertSame(123, $t->get('missing'));
}

public function testMerge()
{
$b = new TLVStatus();
$b->set('ok', 1);
$b->set('missing', 2);
$b->set('warning_handled', 3);
$b->set('critical_unhandled', 4);

$a = new TLVStatus();
$a->set('ok', 3);
$a->set('unknown_unhandled', 2);
$a->set('missing', 1);

$a->merge($b);
$this->assertSame(3, $a->get('missing'));
$this->assertSame(4, $a->get('ok'));
$this->assertSame(3, $a->get('warning_handled'));
$this->assertSame(4, $a->get('critical_unhandled'));
$this->assertSame(2, $a->get('unknown_unhandled'));
$this->assertSame(null, $a->get('warning_unhandled'));
}
}
29 changes: 29 additions & 0 deletions test/php/library/Toplevelview/ViewConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Icinga\Module\Toplevelview;

use Icinga\Module\Toplevelview\ViewConfig;
use Icinga\Module\Toplevelview\Model\View;

use Icinga\Exception\NotReadableError;
use Icinga\Exception\NotFoundError;
Expand All @@ -18,6 +19,34 @@ public function testViewConfigWithNoSuchView()
$view = $c->loadByName('nosuchview');
}

public function testViewConfigDelete()
{
$c = new ViewConfig('test/testdata');
$v = new View('deleteme', 'yml');

// Generate the file
$c->storeToFile($v);
$this->assertFileExists('test/testdata/views/deleteme.yml');
// Generate the backup dir
$c->storeToFile($v);
$this->assertDirectoryExists('test/testdata/views/deleteme');
// Delete the file
$c->delete($v);
$this->assertFalse(file_exists('test/testdata/views/deleteme.yml'));

// Remove generated files afterwards
array_map('unlink', array_filter((array) glob("test/testdata/views/deleteme/*")));
rmdir('test/testdata/views/deleteme/');
}

public function testViewConfigLoadAll()
{
$c = new ViewConfig('test/testdata');
$views = $c->loadAll();

$this->assertArrayHasKey('example', $views);
}

public function testViewConfigWithNoError()
{
$c = new ViewConfig('test/testdata');
Expand Down

0 comments on commit 00e1500

Please sign in to comment.