14 $start_time = get_time();
108 $test_username =
'fossologytest';
109 $test_environment_variable =
'FOSSOLOGY_TESTCONFIG';
114 $fossology_testconfig = getenv($test_environment_variable);
116 if ($fossology_testconfig && strlen($fossology_testconfig) > 1) {
119 echo
"$fossology_testconfig\n";
124 debug(
"Did not find a valid $test_environment_variable environment variable");
134 $pgpass_file = getenv(
'HOME') .
'/.pgpass';
136 $pg_password_environment = getenv(
'PGPASSWORD');
137 $pg_passfile_environment = getenv(
'PGPASSFILE');
139 if ( $pg_password_environment ) {
142 debug(
"Found a PGPASSWORD environment variable of '$pg_password_environment' overriding any PGPASSFILE or ~/.pgpass authentication");
145 if ( $pg_passfile_environment ) {
148 debug(
"Found a PGPASSFILE environment variable of '$pg_passfile_environment' overriding any ~/.pgpass file");
149 $pgpass_file = $pg_passfile_environment;
151 if (is_file($pgpass_file)) {
152 $pgpass_perms = substr( sprintf(
"%o", fileperms($pgpass_file)), -4);
153 if ($pgpass_perms ==
'0600') {
154 debug(
"Permissions for $pgpass_file are correct (0600)");
155 $pgpass_contents = file($pgpass_file);
158 $testuser_found = FALSE;
159 foreach ($pgpass_contents as $line) {
160 if ( preg_match(
"/$test_username:[^:]*$/", $line) ) {
161 $testuser_found = TRUE;
165 if ( $testuser_found == TRUE ) {
166 debug(
"Found a '$test_username' user in $pgpass_file");
169 echo
"FAIL: Did not find a '$test_username' user in $pgpass_file\n";
170 echo
"Before you can run FOSSology tests, you must first create a Postgres user called '$test_username'\n";
171 echo
"which has the CREATEDB permission. This can be done using the following SQL command (as the 'psql' Postgres super user):\n";
172 echo
"\n CREATE USER $test_username WITH CREATEDB LOGIN PASSWORD '$test_username';\n";
173 echo
"\nOnce done, this user needs to be added to a ~/.pgpass file with the following contents:\n";
175 echo
"\nlocalhost:*:*:$test_username:$test_username\n";
176 echo
"\nAnd this file must have permissions set to 0600\n";
181 echo
"FAIL - Permissions for $pgpass_file are NOT correct, must be 0600\n";
186 echo
"FAIL - Pgpass file $pgpass_file does not exist, or is not a regular file\n";
199 $template_db =
'template1';
200 $initial_postgres_params =
"dbname=$template_db ";
201 $initial_postgres_params .=
"host=localhost ";
204 $initial_postgres_params .=
"user=$test_username ";
207 $test_pg_conn = @pg_connect($initial_postgres_params);
214 if ( $test_pg_conn == FALSE ) {
216 $error_array = error_get_last();
217 $pg_error_message = $error_array[
'message'];
218 echo
"FAIL: Cannot connect to the local Postgres server. ";
220 if ( preg_match(
'/no password supplied/', $pg_error_message) ) {
221 echo
"The '$test_username' user must already exist and be included in a ~/.pgpass file, or the PGPASSWORD environment variable must be set!\n";
222 echo
"Before you can run FOSSology tests, you must first create a Postgres user called '$test_username'\n";
223 echo
"which has the CREATEDB permission. This can be done using the following SQL command (as the 'psql' Postgres super user):\n";
224 echo
"\n CREATE USER $test_username WITH CREATEDB LOGIN PASSWORD '$test_username';\n";
225 echo
"\nOnce done, this user needs to be added to a ~/.pgpass file with the following contents:\n";
227 echo
"\nlocalhost:*:*:$test_username:$test_username\n";
228 echo
"\nAnd this file must have permissions set to 0600\n";
230 elseif ( preg_match(
'/authentication failed/', $pg_error_message) ) {
231 echo
"The password for user '$test_username' is not correct!\n";
233 elseif ( preg_match(
"/database \"$template_db\" does not exist/", $pg_error_message) ) {
234 echo
"The database '$template_db' does not exist!\n";
237 echo
"Unknown problem: $pg_error_message\n";
243 debug(
"Successfully connected to local Postgres server as user '$test_username'");
246 pg_close($test_pg_conn) or die (
"FAIL: We could not close the posgres connection!");
251 $system_temp_dir = sys_get_temp_dir();
256 $testing_timestamp = date(
"Ymd_His");
257 $testing_temp_dir = $system_temp_dir .
'/fossologytest_' . $testing_timestamp;
259 mkdir($testing_temp_dir, 0755, TRUE)
260 or die(
"FAIL! Cannot create test configuration directory at: $testing_temp_dir\n");
262 $elapsed = get_time() - $start_time;
263 debug(
"Elapsed Time = $elapsed");
265 debug(
"Creating test database... ");
266 $test_db_name =
"fossologytest_$testing_timestamp";
269 $test_pg_conn = @pg_connect($initial_postgres_params)
270 or die(
"FAIL: Could not connect to Postgres server!");
272 $sql_statement=
"CREATE DATABASE $test_db_name ENCODING='UTF8' TEMPLATE template1";
273 $result = pg_query($test_pg_conn, $sql_statement)
274 or die(
"FAIL: Could not create test database!\n");
278 pg_close($test_pg_conn);
280 debug(
"Done creating test database");
281 $elapsed = get_time() - $start_time;
282 debug(
"Elapsed Time = $elapsed");
285 $test_db_params =
"dbname=$test_db_name ";
286 $test_db_params .=
"host=localhost ";
289 $test_db_params .=
"user=$test_username ";
291 $test_db_conn = pg_connect($test_db_params)
292 or die (
"Could not connect to the new test database '$test_db_name'\n");
300 $sql_statement =
"select lanname from pg_language where lanname = 'plpgsql'";
302 $result = pg_query($test_db_conn, $sql_statement)
303 or die(
"Could not check the database for plpgsql language\n");
305 $plpgsql_already_installed = FALSE;
306 if ( $row = pg_fetch_row($result) ) {
307 $plpgsql_already_installed = TRUE;
311 if ( $plpgsql_already_installed == FALSE ) {
312 $sql_statement =
"CREATE LANGUAGE plpgsql";
313 $result = pg_query($test_db_conn, $sql_statement)
314 or die(
"Could not create plpgsql language in the database\n");
317 $sql_statement =
"select extname from pg_extension where extname = 'uuid-ossp'";
319 $result = pg_query($test_db_conn, $sql_statement)
320 or die(
"Could not check the database for uuid-ossp extension\n");
322 $uuid_already_installed = FALSE;
323 if ( $row = pg_fetch_row($result) ) {
324 $uuid_already_installed = TRUE;
328 if ( $uuid_already_installed == FALSE ) {
329 $sql_statement =
'CREATE EXTENSION "uuid-ossp";';
330 $result = pg_query($test_db_conn, $sql_statement)
331 or die(
"Could not create uuid-ossp extension in the database\n");
337 $db_conf_fh = fopen(
"$testing_temp_dir/Db.conf",
'w')
338 or die("FAIL! Cannot write $testing_temp_dir/Db.
conf\n");
339 fwrite($db_conf_fh, "dbname = $test_db_name;\n");
340 fwrite($db_conf_fh, "host = localhost;\n");
341 fwrite($db_conf_fh, "user = $test_username;\n");
352 $mods_enabled_dir = "$testing_temp_dir/mods-enabled";
354 mkdir($mods_enabled_dir, 0755, TRUE)
355 or die("FAIL! Cannot create test mods-enabled directory at: $mods_enabled_dir\n");
366 $fo_base_dir = realpath(__DIR__ . '/../..');
367 $src_dirs = scandir($fo_base_dir);
369 foreach ($src_dirs as $src_dir) {
370 $full_src_dir = $fo_base_dir .
"/" . $src_dir;
372 if ( preg_match(
"/^\./", $src_dir)
375 || $src_dir ==
'example_wc_agent'
376 || $src_dir ==
'tutorials'
377 || $src_dir ==
'srcdocs'
378 || $src_dir ==
'testing'
379 || $src_dir ==
'demomod'
383 $splitModuls = array(
'monk'=>array(
'monk',
'monkbulk'),
'copyright'=>array(
'copyright',
'ecc'));
384 if ( array_key_exists($src_dir,$splitModuls) ) {
385 foreach($splitModuls[$src_dir] as $agent){
386 mkdir(
"$mods_enabled_dir/$agent");
387 symlink(
"$full_src_dir/agent",
"$mods_enabled_dir/$agent/agent");
388 symlink(
"$full_src_dir/ui",
"$mods_enabled_dir/$agent/ui");
389 symlink(
"$full_src_dir/$agent.conf",
"$mods_enabled_dir/$agent/$agent.conf");
390 symlink(
"$full_src_dir/VERSION-$agent",
"$mods_enabled_dir/$agent/VERSION");
394 if (is_dir($full_src_dir)) {
395 symlink($full_src_dir,
"$mods_enabled_dir/$src_dir")
396 or die("FAIL - could not create symlink for $src_dir in $mods_enabled_dir\n");
413 $test_repo_dir = "$testing_temp_dir/repository";
414 mkdir($test_repo_dir, 0755, TRUE)
415 or die ("FAIL! Cannot create test repository directory at: $test_repo_dir\n");
422 $user_name = rtrim(`
id -un`);
423 $group_name = rtrim(`
id -gn`);
425 $fo_port_number = mt_rand(10001, 32768);
427 $fo_conf_fh = fopen("$testing_temp_dir/fossology.
conf", 'w')
428 or die("FAIL: Could not open $testing_temp_dir/fossology.
conf for writing\n");
429 fwrite($fo_conf_fh, "; fossology.
conf for testing\n");
430 fwrite($fo_conf_fh, "[FOSSOLOGY]\n");
431 fwrite($fo_conf_fh, "port = $fo_port_number\n");
432 fwrite($fo_conf_fh, "address = localhost\n");
433 fwrite($fo_conf_fh, "depth = 3\n");
434 fwrite($fo_conf_fh, "path = $test_repo_dir\n");
435 fwrite($fo_conf_fh, "[HOSTS]\n");
436 fwrite($fo_conf_fh, "localhost = localhost AGENT_DIR 10\n");
437 fwrite($fo_conf_fh, "[REPOSITORY]\n");
438 fwrite($fo_conf_fh, "localhost = * 00 ff\n");
439 fwrite($fo_conf_fh, "[DIRECTORIES]\n");
440 fwrite($fo_conf_fh, "PROJECTUSER=$user_name\n");
441 fwrite($fo_conf_fh, "PROJECTGROUP=$group_name\n");
442 fwrite($fo_conf_fh, "MODDIR=$fo_base_dir\n");
443 fwrite($fo_conf_fh, "BINDIR=$fo_base_dir/cli\n");
444 fwrite($fo_conf_fh, "SBINDIR=$fo_base_dir/cli\n");
445 fwrite($fo_conf_fh, "LIBEXECDIR=$fo_base_dir/lib\n");
446 fwrite($fo_conf_fh, "LOGDIR=$testing_temp_dir\n");
452 $fo_version_fh = fopen("$testing_temp_dir/VERSION", 'w')
453 or die("FAIL: Could not open $testing_temp_dir/VERSION for writing\n");
454 fwrite($fo_version_fh, "[BUILD]\n");
455 fwrite($fo_version_fh, "VERSION=test\n");
456 fwrite($fo_version_fh, "COMMIT_HASH=0000\n");
457 $build_date = date("Y/m/d H:i");
458 fwrite($fo_version_fh, "BUILD_DATE=$build_date\n");
459 fclose($fo_version_fh);
462 $core_schema_dat_file = $fo_base_dir . "/www/ui/core-schema.dat";
486 if(!is_file(__DIR__ .
'/../../vendor/autoload.php'))
488 throw new Exception(
'you need to run "composer install" before creating a database via ApplySchema');
491 require_once(__DIR__ .
'/../../lib/php/libschema.php');
492 require_once(__DIR__ .
'/../../lib/php/common-db.php');
493 require_once(__DIR__ .
'/../../lib/php/common-cache.php');
494 require_once(__DIR__ .
'/../../../install/fossinit-common.php');
504 debug(
"Applying the FOSSOlogy schema to test database via ApplySchema()");
506 $apply_result =
ApplySchema($core_schema_dat_file,
false, $test_db_name);
509 if (isset($previous_PG_CONN)) {
513 if (!empty($apply_result)) {
514 die(
"FAIL: ApplySchema did not succeed. Output was:\n$apply_result\n");
516 debug(
"Done Applying the FOSSOlogy schema to test database via ApplySchema()");
517 $elapsed = get_time() - $start_time;
518 debug(
"Elapsed Time = $elapsed");
522 $random_seed =
'Seed';
523 $options = array(
'cost' => 10);
524 $hash = password_hash(
"fossy", PASSWORD_DEFAULT, $options);
525 $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);";
526 pg_query($test_db_conn, $user_sql)
527 or die(
"FAIL: could not insert default user into user table\n");
530 $folder_sql =
"INSERT INTO folder(folder_pk, folder_name, folder_desc) values(1, 'Software Repository', 'Top Folder');";
531 pg_query($test_db_conn, $folder_sql)
532 or die(
"FAIL: could not insert top folder\n");
533 $folder_sql =
"INSERT INTO foldercontents (parent_fk, foldercontents_mode, child_id) VALUES (1, 0, 0);";
534 pg_query($test_db_conn, $folder_sql)
535 or die(
"FAIL: could not insert top folder contents\n");
536 $folder_sql =
"SELECT setval('folder_folder_pk_seq', (SELECT max(folder_pk) + 1 FROM folder LIMIT 1));";
537 pg_query($test_db_conn, $folder_sql)
538 or die(
"FAIL: could not change folder sequence\n");
540 $LIBEXECDIR =
"$fo_base_dir/lib/";
541 $MODDIR =
"$fo_base_dir/";
543 require_once(__DIR__ .
'/../../lib/php/libschema.php');
544 require_once(__DIR__ .
"/../../../install/db/dbmigrate_2.0-2.1.php");
545 require_once(__DIR__ .
"/../../../install/db/dbmigrate_2.1-2.2.php");
552 pg_close($test_db_conn);
558 debug(
"Successful test database creation");
559 $elapsed = get_time() - $start_time;
560 debug(
"Elapsed Time = $elapsed");
561 echo
"$testing_temp_dir\n";
580 function debug($message) {
582 if ($debug == TRUE) {
583 echo
"DEBUG: $message\n";
588 function get_time() {
590 list($usec,$sec) = explode(
' ', microtime());
591 return ((
float)$usec + (
float)$sec);
get_pg_conn($sysconfdir, &$SysConf, $exitOnDbFail=true)
Migrate_20_21($DryRun)
Migrate to the uploadtree_a table.
Migrate_21_22($Verbose)
Create new groups, group_user_member, perm_upload and perm_folder records to support 2....
fo_conf * conf
The loaded configuration data.
ApplySchema($Filename=NULL, $Debug=false, $Catalog='fossology')
Make schema match $Filename. This is a single transaction.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
bootstrap($sysconfdir="")
Bootstrap the fossology php library.
if(!preg_match("/\s$projectGroup\s/", $groups) &&(posix_getgid() !=$gInfo[ 'gid']))
get monk license list of one specified uploadtree_id
list_t type structure used to keep various lists. (e.g. there are multiple lists).