Skip to content

Commit

Permalink
Test(fossdash-config) : unit-test for fossdash_config.php
Browse files Browse the repository at this point in the history
  • Loading branch information
darshank15 committed Aug 26, 2020
1 parent d844e6d commit aeb848b
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
163 changes: 163 additions & 0 deletions src/lib/php/tests/test_fossdash_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php
/*
Copyright Darshan Kansagara <kansagara.darshan97@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/**
* \file test_common_sysconfig.php
* \brief unit tests for common-sysconfig.php
*/

require_once(dirname(dirname(__FILE__)) . '/common-container.php');
require_once(dirname(dirname(__FILE__)) . '/common-db.php');
require_once(dirname(dirname(__FILE__)) . '/fossdash-config.php');

/**
* \class test_fossdash_config
*/
class test_fossdash_config extends \PHPUnit\Framework\TestCase
{
public $PG_CONN;
public $DB_COMMAND = "";
public $DB_NAME = "";
public $sys_conf = "";

/**
* \brief initialization with db
*/
protected function setUpDb()
{
if (!is_callable('pg_connect')) {
$this->markTestSkipped("php-psql not found");
}
global $PG_CONN;
global $DB_COMMAND;
global $sys_conf;

$DB_COMMAND = dirname(dirname(dirname(dirname(__FILE__))))."/testing/db/createTestDB.php";
exec($DB_COMMAND, $dbout, $rc);
$sys_conf = $dbout[0];
$PG_CONN = DBconnect($sys_conf);
}

/**
* \brief test for ConfigInit()
* after ConfigInit() is executed, we can get some sysconfig information,
* include: SupportEmailLabel, SupportEmailAddr, SupportEmailSubject,
* BannerMsg, LogoImage, LogoLink, FOSSologyURL
*/
function testConfigInit()
{
$this->setUpDb();
global $sys_conf;
FossdashConfigInit($sys_conf, $SysConf);
$this->assertEquals("0", $SysConf['FOSSDASHCONFIG']['FossdashEnableDisable']);
$this->assertEquals("* * * * *", $SysConf['FOSSDASHCONFIG']['FossDashScriptCronSchedule']);
$this->tearDownDb();
}

/**
* \brief clean the env db
*/
protected function tearDownDb()
{
if (!is_callable('pg_connect')) {
return;
}
global $PG_CONN;
global $DB_COMMAND;
global $DB_NAME;

pg_close($PG_CONN);
exec("$DB_COMMAND -d $DB_NAME");
}

/**
* \brief check fossdash url
*/
public function test_check_fossdash_url()
{
foreach (array('http://localhost:8086/write?db=fossology_db'=>true,
'http://influxdb:8086/write?db=fossology_db'=>true,
'http://127.0.0.1:8086/write?db=fossology_db'=>true,
'ssh://127.0.0.1:8086'=>false,) as $url=>$correct) {
$this->assertEquals(check_fossdash_url($url),$correct,$message="result for URL $url is false");
print('.');
}
}

/**
* \brief check cron job inteval
*/
public function test_check_cron_job_inteval()
{
foreach (array('* * * * *'=>true,
'1 1 * 1 *'=>true,
'*/1 * * * *'=>true,
'abbkbkb'=>false,
'* * * * * 23567'=>false,
'1 1 1 1 1 1'=>false) as $cron=>$correct) {
$this->assertEquals(check_cron_job_inteval($cron),$correct,$message="result for CRON $cron is false");
print('.');
}
}

/**
* \brief check fossology instance name
*/
public function test_check_fossology_instance_name()
{
foreach (array('fossology_instance_1'=>true,
'fossology-instance-1'=>true,
'instance=1'=>false,
''=>false,
'fossology instance 1'=>false,
'instance1'=>true) as $instance_name=>$correct) {
$this->assertEquals(check_fossology_instance_name($instance_name),$correct,$message="result for CRON $instance_name is false");
print('.');
}
}

/**
* \brief check fossdash cleaning days
*/
public function test_check_fossdash_cleaning()
{
foreach (array('22'=>true,
'1'=>true,
'0'=>true,
'ghgl'=>false,
'one'=>false) as $clening_interval=>$correct) {
$this->assertEquals(check_fossdash_cleaning($clening_interval),$correct,$message="result for CRON $clening_interval is false");
print('.');
}
}

/**
* \brief check fossdash metric config
*/
public function test_check_fossdash_config()
{
foreach (array('sfksb ghs 124 () * * ?'=>true,
'drop table sysconfig'=>false,
'alter table sysconfig'=>false,
'INSERT INTO sysconfig'=>false) as $metric=>$correct) {
$this->assertEquals(check_fossdash_config($metric),$correct,$message="result for metric $metric is false");
print('.');
}
}
}

1 change: 1 addition & 0 deletions src/lib/php/tests/tests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<file>test_common_parm.php</file>
<file>test_common_pkg.php</file>
<file>test_common_sysconfig.php</file>
<file>test_fossdash_config.php</file>
<file>test_common_ui.php</file>
<file></file>
</testsuite>
Expand Down

0 comments on commit aeb848b

Please sign in to comment.