Skip to content

Commit

Permalink
refactor: pre-commit.php updates (librenms#8679)
Browse files Browse the repository at this point in the history
Disable the log for unit tests to speed things up hopefully
  • Loading branch information
murrant authored and laf committed May 9, 2018
1 parent 5273b29 commit 8afafe7
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 61 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ after_failure:
- tail /tmp/snmpsimd.log

script:
- php scripts/pre-commit.php -l
- php scripts/pre-commit.php -s
- php scripts/pre-commit.php -u --db --snmpsim --fail-fast
- php scripts/pre-commit.php -q -l
- php scripts/pre-commit.php -q -s
- php scripts/pre-commit.php -q -u --db --snmpsim --fail-fast
- bash -n daily.sh
- pylint -E poller-wrapper.py discovery-wrapper.py
- bash scripts/deploy-docs.sh
10 changes: 8 additions & 2 deletions LibreNMS/Util/Snmpsim.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ public function fork($wait = 2)
}

if (isCli() && !$this->proc->isRunning()) {
echo `tail -5 $this->log` . PHP_EOL;
// if starting failed, run snmpsim again and output to the console and validate the data
passthru($this->getCmd(false) . ' --validate-data');

echo "\nFailed to start Snmpsim. Scroll up for error.\n";
exit;
}
}

Expand Down Expand Up @@ -149,7 +153,9 @@ private function getCmd($with_log = true)
{
$cmd = "snmpsimd.py --data-dir={$this->snmprec_dir} --agent-udpv4-endpoint={$this->ip}:{$this->port}";

if ($with_log) {
if (is_null($this->log)) {
$cmd .= " --logging-method=null";
} elseif ($with_log) {
$cmd .= " --logging-method=file:{$this->log}";
}

Expand Down
2 changes: 1 addition & 1 deletion doc/Developing/Creating-Release.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ We utilise [Readmegen](https://github.com/fojuth/readmegen) to automatically pop
Install `readmegen` using `composer`:

```bash
./composer.phar update
./scripts/composer_wrapper.php install
```

You can now create the update change log by running (201608 was our last release):
Expand Down
7 changes: 3 additions & 4 deletions doc/Developing/Licensing.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,11 @@ project.
- Include its name, source URL, copyright notice, and license in `doc/General/Credits.md`

- To add a php dependency, please use composer
- Add the dependency `composer require --update-no-dev slim/slim`
- Add the files and commit `composer commit` or `git add -f vendor/ composer.json; git commit`
- Add the dependency `composer require slim/slim`

- Updating php dependencies
- Update dependencies `composer update --no-dev`
- Add the files and commit `composer commit` or `git add -f vendor/; git commit`
- Update dependencies `FORCE=1 php56 ./scripts/composer_wrapper.php update`
- Commit the updated composer.lock file

- To add a javascript dependency
- Where possible please include minimized libraries in the html/js/ folder.
Expand Down
59 changes: 17 additions & 42 deletions scripts/pre-commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
-o, --os Specific OS to run tests on. Implies --unit, --db, --snmpsim
-m, --module Specific Module to run tests on. Implies --unit, --db, --snmpsim
-f, --fail-fast Quit when any failure is encountered
-p, --passthru Display output from checks as it comes
-q, --quiet Hide output unless there is an error
--db Run unit tests that require a database
--snmpsim Use snmpsim for unit tests
-c, --commands Print commands only, no checks
Expand All @@ -41,7 +41,7 @@
}

// set up some variables
$passthru = check_opt($options, 'p', 'passthru');
$passthru = !check_opt($options, 'q', 'quiet');
$command_only = check_opt($options, 'c', 'commands');
$fail_fast = check_opt($options, 'f', 'fail-fast');
$return = 0;
Expand Down Expand Up @@ -302,36 +302,6 @@ function build_excludes($exclude_string, $excludes)
return $result;
}

/**
* Find an executable
*
* @param string|array $execs executable names to find
* @return string the path to the executable, or '' if it is not found
* @throws Exception Could not find the Executable
*/
function find_exec($execs)
{
foreach ((array)$execs as $exec) {
// check vendor bin first
$vendor_bin_dir = './vendor/bin/';
if (is_executable($vendor_bin_dir . $exec)) {
return $vendor_bin_dir . $exec;
}

// check path
$path_exec = shell_exec("which $exec 2> /dev/null");
if (!empty($path_exec)) {
return trim($path_exec);
}

// check the cwd
if (is_executable('./' . $exec)) {
return './' . $exec;
}
}
throw new Exception('Executable not found');
}

/**
* Check for an executable and return the path to it
* If it does not exist, run composer update.
Expand All @@ -342,15 +312,20 @@ function find_exec($execs)
*/
function check_exec($exec)
{
try {
return find_exec($exec);
} catch (Exception $e) {
try {
shell_exec("scripts/composer_wrapper.php install");
return find_exec($exec);
} catch (Exception $ce) {
echo "\nCould not find $exec. Please install composer.\nYou can find more info at http://docs.librenms.org/Developing/Validating-Code/\n";
exit(1);
}
$path = "vendor/bin/$exec";

if (is_executable($path)) {
return $path;
}

echo "Running composer install to install developer dependencies.\n";
passthru("scripts/composer_wrapper.php install");

if (is_executable($path)) {
return $path;
}

echo "\nRunning installing deps with composer failed.\n You should try running './scripts/composer_wrapper.php install' by hand\n";
echo "You can find more info at http://docs.librenms.org/Developing/Validating-Code/\n";
exit(1);
}
3 changes: 2 additions & 1 deletion scripts/save-test-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@

if (!$snmpsim->isRunning()) {
echo "Failed to start snmpsim, make sure it is installed, working, and there are no bad snmprec files.\n";
exit;
echo "Run ./scripts/save-test-data.php --snmpsim to see the log output\n";
exit(1);
}


Expand Down
11 changes: 6 additions & 5 deletions tests/OSDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ public function testAllFilesTested()
private function checkOS($expected_os, $filename = null)
{
$community = $filename ?: $expected_os;
global $debug;
global $debug, $vdebug;
$debug = true;
$vdebug = true;
ob_start();
$os = getHostOS($this->genDevice($community));
$output = ob_get_contents();
Expand All @@ -119,18 +120,18 @@ private function checkOS($expected_os, $filename = null)
*/
private function genDevice($community)
{
return array(
return [
'device_id' => 1,
'hostname' => $this->snmpsimIp,
'hostname' => $this->snmpsim->getIP(),
'snmpver' => 'v2c',
'port' => $this->snmpsimPort,
'port' => $this->snmpsim->getPort(),
'timeout' => 3,
'retries' => 0,
'snmp_max_repeaters' => 10,
'community' => $community,
'os' => 'generic',
'os_group' => '',
);
];
}

/**
Expand Down
14 changes: 12 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@

namespace LibreNMS\Tests;

use LibreNMS\Util\Snmpsim;

abstract class TestCase extends \PHPUnit_Framework_TestCase
{
protected $snmpsimIp = '127.1.6.2';
protected $snmpsimPort = 1161;
/** @var Snmpsim snmpsim instance */
protected $snmpsim = null;

public function __construct($name = null, $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
// grab global $snmpsim from bootstrap and make it accessible
global $snmpsim;
$this->snmpsim = $snmpsim;
}

public function dbSetUp()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@

update_os_cache(true); // Force update of OS Cache

$snmpsim = new Snmpsim('127.1.6.2', 1162, null);
if (getenv('SNMPSIM')) {
$snmpsim = new Snmpsim('127.1.6.2');
$snmpsim->fork();

// make PHP hold on a reference to $snmpsim so it doesn't get destructed
Expand Down

0 comments on commit 8afafe7

Please sign in to comment.