Files
librenms-librenms/tests/Unit/CiHelperTest.php
Tony Murray ce21011aff Rewrite development helper to lnms dev:check (#11650)
* Refactor pre-commit to class

* docs build

* dusk check

* ci mode for checks

* full checks

* other mysql

* make other lint checks actually work
fix pylint finding

* ci is a long opt

* fix undefined index

* dusk fully working

* ask for forgiveness, not permission

* fix whitespace

* skip dusk sometimes

* Handle 3com and other os with digits

* flags instead of if else spaghetti

* convert to command

* cleanup

* missed check

* fixes

* case

* self-check :D

* argument now

* fix bugs from refactors

* another fix

* adjust file change parsing

* refactor execut a bit

* fallback to global quiet when unknown type.

* allow quiet override for specific commands

* output cleanup

* check flow

* start of tests

* file categorizer tests and fixes

* fixes and cleanup

* skipable not implemented...

* more tests, fix bugs

* more tests and cleanup

* wrong command

* fix canCheck and set env properly

* full env fix

* don't allow dusk on user's run as it will erase their db.

* fix os option

* fix whitespace

* don't need to start server

* ci doesn't like that
2020-05-22 20:27:48 -05:00

283 lines
8.5 KiB
PHP

<?php
/**
* CiHelperTest.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests\Unit;
use LibreNMS\Tests\TestCase;
use LibreNMS\Util\CiHelper;
class CiHelperTest extends TestCase
{
public function testSetFlags()
{
$helper = new CiHelper();
$allFalse = array_map(function ($flag) {
return false;
}, $this->getDefaultFlags());
$allTrue = array_map(function ($flag) {
return false;
}, $this->getDefaultFlags());
$helper->setFlags($allFalse);
$this->assertEquals($allFalse, $helper->getFlags());
$helper->setFlags($allTrue);
$this->assertEquals($allTrue, $helper->getFlags());
$helper->setFlags(['undefined_flag' => false]);
$this->assertEquals($allTrue, $helper->getFlags());
$helper->setFlags(['full' => false]);
$testOne = $allTrue;
$testOne['full'] = false;
$this->assertEquals($testOne, $helper->getFlags());
}
public function testDefaults()
{
$helper = new CiHelper();
$this->assertEquals($this->getDefaultFlags(), $helper->getFlags());
}
public function testNoFiles()
{
putenv('FILES=none');
$helper = new CiHelper();
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip' => true,
'style_skip' => true,
'unit_skip' => true,
'web_skip' => true,
'lint_skip_php' => true,
'lint_skip_python' => true,
'lint_skip_bash' => true,
]);
}
public function testSetOs()
{
$helper = new CiHelper();
$helper->setOS(['netonix', 'e3meter']);
$this->assertFlagsSet($helper, [
'unit_os' => true,
]);
putenv('FILES=none');
$helper = new CiHelper();
$helper->setOS(['netonix']);
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip' => true,
'style_skip' => true,
'web_skip' => true,
'unit_os' => true,
'lint_skip_php' => true,
'lint_skip_python' => true,
'lint_skip_bash' => true,
]);
putenv('FILES=includes/definitions/ios.yaml tests/data/fxos.json');
$helper = new CiHelper();
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip' => true,
'style_skip' => true,
'web_skip' => true,
'unit_os' => true,
'lint_skip_php' => true,
'lint_skip_python' => true,
'lint_skip_bash' => true,
]);
}
public function testSetModules()
{
$helper = new CiHelper();
$helper->setModules(['sensors', 'processors']);
$this->assertFlagsSet($helper, [
'unit_modules' => true,
]);
putenv('FILES=none');
$helper = new CiHelper();
$helper->setModules(['os']);
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip' => true,
'style_skip' => true,
'web_skip' => true,
'unit_modules' => true,
'lint_skip_php' => true,
'lint_skip_python' => true,
'lint_skip_bash' => true,
]);
putenv('FILES=none');
$helper = new CiHelper();
$helper->setOS(['linux']);
$helper->setModules(['os']);
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip' => true,
'style_skip' => true,
'web_skip' => true,
'unit_os' => true,
'unit_modules' => true,
'lint_skip_php' => true,
'lint_skip_python' => true,
'lint_skip_bash' => true,
]);
putenv('FILES=includes/definitions/ios.yaml tests/data/fxos.json');
$helper = new CiHelper();
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip' => true,
'style_skip' => true,
'web_skip' => true,
'unit_os' => true,
'lint_skip_php' => true,
'lint_skip_python' => true,
'lint_skip_bash' => true,
]);
putenv('FILES=includes/definitions/ios.yaml tests/data/fxos.json');
$helper = new CiHelper();
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip' => true,
'style_skip' => true,
'web_skip' => true,
'unit_os' => true,
'lint_skip_php' => true,
'lint_skip_python' => true,
'lint_skip_bash' => true,
]);
}
public function testFileCategorization()
{
putenv('FILES=LibreNMS/Alert/Transport/Sensu.php includes/services.inc.php');
$helper = new CiHelper();
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip_python' => true,
'lint_skip_bash' => true,
]);
putenv('FILES=/daily.sh includes/services.inc.php');
$helper = new CiHelper();
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip_python' => true,
]);
putenv('FILES=daily.sh LibreNMS/__init__.py');
$helper = new CiHelper();
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'style_skip' => true,
'unit_skip' => true,
'web_skip' => true,
'lint_skip_php' => true,
]);
putenv('FILES=includes/polling/sensors/ios.inc.php');
$helper = new CiHelper();
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip_python' => true,
'lint_skip_bash' => true,
'unit_os' => true,
]);
putenv('FILES=html/images/os/ios.svg');
$helper = new CiHelper();
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip' => true,
'style_skip' => true,
'web_skip' => true,
'lint_skip_php' => true,
'lint_skip_python' => true,
'lint_skip_bash' => true,
'unit_svg' => true,
]);
putenv('FILES=html/images/os/ios.svg');
$helper = new CiHelper();
$helper->detectChangedFiles();
$this->assertFlagsSet($helper, [
'lint_skip' => true,
'style_skip' => true,
'web_skip' => true,
'lint_skip_php' => true,
'lint_skip_python' => true,
'lint_skip_bash' => true,
'unit_svg' => true,
]);
}
private function assertFlagsSet(CiHelper $helper, $flags = [])
{
$full = $this->getDefaultFlags();
foreach ($flags as $name => $value) {
$full[$name] = $value;
$this->assertEquals($value, $helper->getFlag($name), "Flag $name incorrect.");
}
$this->assertEquals($full, $helper->getFlags());
}
private function getDefaultFlags()
{
return [
'lint_enable' => true,
'style_enable' => true,
'unit_enable' => true,
'web_enable' => false,
'lint_skip' => false,
'style_skip' => false,
'unit_skip' => false,
'web_skip' => false,
'lint_skip_php' => false,
'lint_skip_python' => false,
'lint_skip_bash' => false,
'unit_os' => false,
'unit_docs' => false,
'unit_svg' => false,
'unit_modules' => false,
'docs_changed' => false,
'ci' => false,
'commands' => false,
'fail-fast' => false,
'full' => false,
'quiet' => false,
];
}
}