25 $start_time = get_time();
119 $test_username =
'fossologytest';
120 $test_environment_variable =
'FOSSOLOGY_TESTCONFIG';
125 $fossology_testconfig = getenv($test_environment_variable);
127 if ($fossology_testconfig && strlen($fossology_testconfig) > 1) {
130 echo
"$fossology_testconfig\n";
135 debug(
"Did not find a valid $test_environment_variable environment variable");
145 $pgpass_file = getenv(
'HOME') .
'/.pgpass';
147 $pg_password_environment = getenv(
'PGPASSWORD');
148 $pg_passfile_environment = getenv(
'PGPASSFILE');
150 if ( $pg_password_environment ) {
153 debug(
"Found a PGPASSWORD environment variable of '$pg_password_environment' overriding any PGPASSFILE or ~/.pgpass authentication");
156 if ( $pg_passfile_environment ) {
159 debug(
"Found a PGPASSFILE environment variable of '$pg_passfile_environment' overriding any ~/.pgpass file");
160 $pgpass_file = $pg_passfile_environment;
162 if (is_file($pgpass_file)) {
163 $pgpass_perms = substr( sprintf(
"%o", fileperms($pgpass_file)), -4);
164 if ($pgpass_perms ==
'0600') {
165 debug(
"Permissions for $pgpass_file are correct (0600)");
166 $pgpass_contents = file($pgpass_file);
169 $testuser_found = FALSE;
170 foreach ($pgpass_contents as $line) {
171 if ( preg_match(
"/$test_username:[^:]*$/", $line) ) {
172 $testuser_found = TRUE;
176 if ( $testuser_found == TRUE ) {
177 debug(
"Found a '$test_username' user in $pgpass_file");
180 echo
"FAIL: Did not find a '$test_username' user in $pgpass_file\n";
181 echo
"Before you can run FOSSology tests, you must first create a Postgres user called '$test_username'\n";
182 echo
"which has the CREATEDB permission. This can be done using the following SQL command (as the 'psql' Postgres super user):\n";
183 echo
"\n CREATE USER $test_username WITH CREATEDB LOGIN PASSWORD '$test_username';\n";
184 echo
"\nOnce done, this user needs to be added to a ~/.pgpass file with the following contents:\n";
186 echo
"\nlocalhost:*:*:$test_username:$test_username\n";
187 echo
"\nAnd this file must have permissions set to 0600\n";
192 echo
"FAIL - Permissions for $pgpass_file are NOT correct, must be 0600\n";
197 echo
"FAIL - Pgpass file $pgpass_file does not exist, or is not a regular file\n";
210 $template_db =
'template1';
211 $initial_postgres_params =
"dbname=$template_db ";
212 $initial_postgres_params .=
"host=localhost ";
215 $initial_postgres_params .=
"user=$test_username ";
218 $test_pg_conn = @pg_connect($initial_postgres_params);
225 if ( $test_pg_conn == FALSE ) {
227 $error_array = error_get_last();
228 $pg_error_message = $error_array[
'message'];
229 echo
"FAIL: Cannot connect to the local Postgres server. ";
231 if ( preg_match(
'/no password supplied/', $pg_error_message) ) {
232 echo
"The '$test_username' user must already exist and be included in a ~/.pgpass file, or the PGPASSWORD environment variable must be set!\n";
233 echo
"Before you can run FOSSology tests, you must first create a Postgres user called '$test_username'\n";
234 echo
"which has the CREATEDB permission. This can be done using the following SQL command (as the 'psql' Postgres super user):\n";
235 echo
"\n CREATE USER $test_username WITH CREATEDB LOGIN PASSWORD '$test_username';\n";
236 echo
"\nOnce done, this user needs to be added to a ~/.pgpass file with the following contents:\n";
238 echo
"\nlocalhost:*:*:$test_username:$test_username\n";
239 echo
"\nAnd this file must have permissions set to 0600\n";
241 elseif ( preg_match(
'/authentication failed/', $pg_error_message) ) {
242 echo
"The password for user '$test_username' is not correct!\n";
244 elseif ( preg_match(
"/database \"$template_db\" does not exist/", $pg_error_message) ) {
245 echo
"The database '$template_db' does not exist!\n";
248 echo
"Unknown problem: $pg_error_message\n";
254 debug(
"Successfully connected to local Postgres server as user '$test_username'");
257 pg_close($test_pg_conn) or die ("FAIL: We could not close the posgres connection!");
262 $system_temp_dir = sys_get_temp_dir();
267 $testing_timestamp = date("Ymd_His");
268 $testing_temp_dir = $system_temp_dir . '/fossologytest_' . $testing_timestamp;
270 mkdir($testing_temp_dir, 0755, TRUE)
271 or die("FAIL! Cannot create test configuration directory at: $testing_temp_dir\n");
273 $elapsed = get_time() - $start_time;
274 debug("Elapsed Time = $elapsed");
276 debug("Creating test database... ");
277 $test_db_name = "fossologytest_$testing_timestamp";
280 $test_pg_conn = @pg_connect($initial_postgres_params)
281 or die("FAIL: Could not connect to Postgres server!");
287 $sql_statement="CREATE DATABASE $test_db_name ENCODING='UTF8' TEMPLATE template0";
288 $result = pg_query($test_pg_conn, $sql_statement)
289 or die("FAIL: Could not create test database!\n");
293 pg_close($test_pg_conn);
295 debug("Done creating test database");
296 $elapsed = get_time() - $start_time;
297 debug("Elapsed Time = $elapsed");
300 $test_db_params = "dbname=$test_db_name ";
301 $test_db_params .= "host=localhost ";
304 $test_db_params .= "user=$test_username ";
306 $test_db_conn = pg_connect($test_db_params)
307 or die ("Could not connect to the new test database '$test_db_name'\n");
315 $sql_statement = "select lanname from pg_language where lanname = 'plpgsql'";
317 $result = pg_query($test_db_conn, $sql_statement)
318 or die("Could not check the database for plpgsql language\n");
320 $plpgsql_already_installed = FALSE;
321 if ( $row = pg_fetch_row($result) ) {
322 $plpgsql_already_installed = TRUE;
326 if ( $plpgsql_already_installed == FALSE ) {
327 $sql_statement =
"CREATE LANGUAGE plpgsql";
328 $result = pg_query($test_db_conn, $sql_statement)
329 or die("Could not create plpgsql language in the database\n");
335 $db_conf_fh = fopen("$testing_temp_dir/Db.
conf", 'w')
336 or die("FAIL! Cannot write $testing_temp_dir/Db.conf\n");
337 fwrite($db_conf_fh, "dbname = $test_db_name;\n");
338 fwrite($db_conf_fh, "host = localhost;\n");
339 fwrite($db_conf_fh, "user = $test_username;\n");
350 $mods_enabled_dir = "$testing_temp_dir/mods-enabled";
352 mkdir($mods_enabled_dir, 0755, TRUE)
353 or die("FAIL! Cannot create test mods-enabled directory at: $mods_enabled_dir\n");
364 $fo_base_dir = realpath(__DIR__ . '/../..');
365 $src_dirs = scandir($fo_base_dir);
367 foreach ($src_dirs as $src_dir) {
368 $full_src_dir = $fo_base_dir .
"/" . $src_dir;
370 if ( preg_match(
"/^\./", $src_dir)
373 || $src_dir ==
'example_wc_agent' 374 || $src_dir ==
'tutorials' 375 || $src_dir ==
'srcdocs' 376 || $src_dir ==
'testing' 377 || $src_dir ==
'demomod' 381 $splitModuls = array(
'monk'=>array(
'monk',
'monkbulk'),
'copyright'=>array(
'copyright',
'ecc'));
382 if ( array_key_exists($src_dir,$splitModuls) ) {
383 foreach($splitModuls[$src_dir] as $agent){
384 mkdir(
"$mods_enabled_dir/$agent");
385 symlink(
"$full_src_dir/agent",
"$mods_enabled_dir/$agent/agent");
386 symlink(
"$full_src_dir/ui",
"$mods_enabled_dir/$agent/ui");
387 symlink(
"$full_src_dir/$agent.conf",
"$mods_enabled_dir/$agent/$agent.conf");
388 symlink(
"$full_src_dir/VERSION-$agent",
"$mods_enabled_dir/$agent/VERSION");
392 if (is_dir($full_src_dir)) {
393 symlink($full_src_dir,
"$mods_enabled_dir/$src_dir")
394 or die("FAIL - could not create symlink for $src_dir in $mods_enabled_dir\n");
411 $test_repo_dir = "$testing_temp_dir/repository";
412 mkdir($test_repo_dir, 0755, TRUE)
413 or die ("FAIL! Cannot create test repository directory at: $test_repo_dir\n");
420 $user_name = rtrim(`
id -un`);
421 $group_name = rtrim(`
id -gn`);
423 $fo_port_number = mt_rand(10001, 32768);
425 $fo_conf_fh = fopen("$testing_temp_dir/fossology.conf", 'w')
426 or die("FAIL: Could not open $testing_temp_dir/fossology.conf for writing\n");
427 fwrite($fo_conf_fh, "; fossology.conf for testing\n");
428 fwrite($fo_conf_fh, "[FOSSOLOGY]\n");
429 fwrite($fo_conf_fh, "port = $fo_port_number\n");
430 fwrite($fo_conf_fh, "address = localhost\n");
431 fwrite($fo_conf_fh, "depth = 3\n");
432 fwrite($fo_conf_fh, "path = $test_repo_dir\n");
433 fwrite($fo_conf_fh, "[HOSTS]\n");
434 fwrite($fo_conf_fh, "localhost = localhost AGENT_DIR 10\n");
435 fwrite($fo_conf_fh, "[REPOSITORY]\n");
436 fwrite($fo_conf_fh, "localhost = * 00 ff\n");
437 fwrite($fo_conf_fh, "[DIRECTORIES]\n");
438 fwrite($fo_conf_fh, "PROJECTUSER=$user_name\n");
439 fwrite($fo_conf_fh, "PROJECTGROUP=$group_name\n");
440 fwrite($fo_conf_fh, "MODDIR=$fo_base_dir\n");
441 fwrite($fo_conf_fh, "BINDIR=$fo_base_dir/cli\n");
442 fwrite($fo_conf_fh, "SBINDIR=$fo_base_dir/cli\n");
443 fwrite($fo_conf_fh, "LIBEXECDIR=$fo_base_dir/lib\n");
444 fwrite($fo_conf_fh, "LOGDIR=$testing_temp_dir\n");
450 $fo_version_fh = fopen("$testing_temp_dir/VERSION", 'w')
451 or die("FAIL: Could not open $testing_temp_dir/VERSION for writing\n");
452 fwrite($fo_version_fh, "[BUILD]\n");
453 fwrite($fo_version_fh, "VERSION=test\n");
454 fwrite($fo_version_fh, "COMMIT_HASH=0000\n");
455 $build_date = date("Y/m/d H:i");
456 fwrite($fo_version_fh, "BUILD_DATE=$build_date\n");
457 fclose($fo_version_fh);
460 $core_schema_dat_file = $fo_base_dir . "/www/ui/core-schema.dat";
484 if(!is_file(__DIR__ .
'/../../vendor/autoload.php'))
486 throw new Exception(
'you need to run "composer install" before creating adatabase via ApplySchema');
489 require_once(__DIR__ .
'/../../lib/php/libschema.php');
490 require_once(__DIR__ .
'/../../lib/php/common-db.php');
491 require_once(__DIR__ .
'/../../lib/php/common-cache.php');
497 debug(
"Applying the FOSSOlogy schema to test database via ApplySchema()");
499 $apply_result =
ApplySchema($core_schema_dat_file,
false, $test_db_name);
502 if (isset($previous_PG_CONN)) {
506 if (!empty($apply_result)) {
507 die(
"FAIL: ApplySchema did not succeed. Output was:\n$apply_result\n");
509 debug(
"Done Applying the FOSSOlogy schema to test database via ApplySchema()");
510 $elapsed = get_time() - $start_time;
511 debug(
"Elapsed Time = $elapsed");
515 $random_seed = rand().rand();
516 $hash = sha1($random_seed .
"fossy");
517 $user_sql =
"INSERT INTO users (user_pk, user_name, user_desc, user_seed, user_pass, user_perm, user_email, email_notify, root_folder_fk) VALUES (1, 'fossy', 'Default Administrator', '$random_seed', '$hash', 10, 'fossy', 'n', 1);";
518 pg_query($test_db_conn, $user_sql)
519 or die("FAIL: could not insert default user into user table\n");
522 $folder_sql = "INSERT INTO folder(folder_pk,
folder_name, folder_desc) values(1, 'Software Repository', 'Top Folder');";
523 pg_query($test_db_conn, $folder_sql)
524 or die("FAIL: could not insert top folder\n");
525 $folder_sql = "INSERT INTO foldercontents (parent_fk, foldercontents_mode, child_id) VALUES (1, 0, 0);";
526 pg_query($test_db_conn, $folder_sql)
527 or die("FAIL: could not insert top folder contents\n");
528 $folder_sql = "SELECT setval('folder_folder_pk_seq', (SELECT
max(folder_pk) + 1 FROM folder LIMIT 1));";
529 pg_query($test_db_conn, $folder_sql)
530 or die("FAIL: could not change folder sequence\n");
532 $LIBEXECDIR = "$fo_base_dir/lib/";
533 $MODDIR = "$fo_base_dir/";
535 require_once(__DIR__ . '/../../lib/php/libschema.php');
536 require_once(__DIR__ . "/../../../install/
db/dbmigrate_2.0-2.1.php");
537 require_once(__DIR__ . "/../../../install/
db/dbmigrate_2.1-2.2.php");
544 pg_close($test_db_conn);
550 debug("Successful test database creation");
551 $elapsed = get_time() - $start_time;
552 debug("Elapsed Time = $elapsed");
553 echo "$testing_temp_dir\n";
572 function
debug($message) {
574 if ($debug == TRUE) {
575 echo
"DEBUG: $message\n";
580 function get_time() {
582 list($usec,$sec) = explode(
' ', microtime());
583 return ((
float)$usec + (
float)$sec);
ApplySchema($Filename=NULL, $Debug=false, $Catalog= 'fossology')
Make schema match $Filename. This is a single transaction.
if(!preg_match("/\s$projectGroup\s/", $groups)&&(posix_getgid()!=$gInfo['gid']))
get monk license list of one specified uploadtree_id
Migrate_20_21($DryRun)
Migrate to the uploadtree_a table.
fo_conf * conf
The loaded configuration data.
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
Migrate_21_22($Verbose)
Create new groups, group_user_member, perm_upload and perm_folder records to support 2...
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
list_t type structure used to keep various lists. (e.g. there are multiple lists).