39 require_once(__DIR__ .
'/../lib/libTestDB.php');
41 $Options = getopt(
'c:d:esh');
42 $usage = $argv[0] .
": [-h] -c path [-d name] [-s]\n" .
43 "-c path: The path to the fossology system configuration directory\n" .
44 "-d name: Drop the named data base.\n" .
45 "-e: create ONLY an empty db, sysconf dir and repository\n" .
46 "-h: This message (Usage)\n" .
47 "-s: Start the scheduler with the new sysconfig directory\n" .
49 " Create a test DB: 'createTestDb.php' \n" .
50 " Drop the database fosstest1537938: 'createTestDb.php -d fosstest1537938'\n" .
51 " Create test DB, start scheduler: 'createTestDb.php -s'\n" .
52 " Create empty DB, sysconf and repo: 'createTestDb.php -e'\n";
54 $pathPrefix =
'/srv/fossologyTestRepo';
55 $dbPrefix =
'fosstest';
58 $gid_array = posix_getgroups();
60 foreach($gid_array as $gid)
62 $gid_info = posix_getgrgid ($gid);
63 if ($gid_info[
'name'] ===
'fossy')
69 $uid = posix_getuid();
70 $uid_info = posix_getpwuid($uid);
71 if ($uid_info[
'name'] ===
'root') $gflag = 1;
81 $user = getenv(
'USER');
82 $userHome = getenv(
'HOME');
83 $ipv4 = gethostbyname(gethostname());
84 $fullHostName = gethostbyaddr(gethostbyname($ipv4));
85 $contents =
"$fullHostName:*:*:fossy:fossy\n";
86 $pgpass =
"$userHome/.pgpass";
91 $pg_pass_contents =
"";
92 if (file_exists($pgpass)) {
94 $pg_pass_contents = file_get_contents($pgpass);
99 if (!preg_match(
'/\:fossy\:fossy/', $pg_pass_contents)) {
100 $FD = fopen($pgpass,
'w');
101 $howmany = fwrite($FD, $contents);
102 if($howmany === FALSE)
104 echo
"FATAL! Could not write .pgpass file to $pgpass\n";
112 if(!chmod($pgpass, 0600))
114 echo
"Warning! could not set $pgpass to 0600\n";
117 if(array_key_exists(
'h',$Options))
124 if(array_key_exists(
'c', $Options))
126 $sysconfig = $Options[
'c'];
127 if(empty($sysconfig))
136 if(array_key_exists(
'd', $Options))
138 $dropName = $Options[
'd'];
146 $ckCmd =
"psql -c '\q' postgres -U fossy";
147 $lastCmd = exec($ckCmd, $ckOut, $ckRtn);
150 echo
"ERROR: postgresql isn't running, not deleting database $dropName\n";
153 $existCmd =
"psql -l postgres -U fossy|grep -q $dropName";
154 $lastExist = exec($existCmd, $existkOut, $existRtn);
158 # stop all users of the fossology db 159 $pkillCmd =
"sudo pkill -f -u postgres fossy || true";
160 $lKill = exec($pkillCmd, $killOut, $killRtn);
161 $dropCmd =
"sudo su postgres -c 'echo \"drop database $dropName;\"|psql'";
162 $lastDrop = exec($dropCmd, $dropOut, $dropRtn);
165 echo
"ERROR: failed to delete database $dropName\n";
171 echo
"NOTE: database $dropName does not exist, nothing to delete\n";
175 $len = strlen($dbPrefix);
176 $uni = substr($dropName,$len);
177 $rmRepo = $pathPrefix .
'/testDbRepo' .$uni;
178 $rmConf = $pathPrefix .
'/testDbConf' .$uni;
179 $last = system(
"sudo rm -rf $rmConf $rmRepo", $rmRtn);
183 if(empty($sysconfig))
185 $sysconfig = getenv(
'SYSCONFDIR');
187 if(empty($sysconfig))
189 echo
"FATAL!, no SYSCONFDIR defined\n";
190 echo
"either export SYSCONFDIR path and rerun or use -c <sysconfdirpath>\n";
196 putenv(
"SYSCONFDIR=$sysconfig");
197 $_ENV[
'SYSCONFDIR'] = $sysconfig;
200 $DbName = $dbPrefix . $unique;
202 $createEmpty = array_key_exists(
'e', $Options);
203 $startSched = array_key_exists(
's', $Options);
206 $newDB = createTestDB($DbName);
209 echo
"ERROR, could not create database $DbName\n";
214 $confName =
'testDbConf' . $unique;
215 $confPath =
"$pathPrefix/$confName";
216 $repoName =
'testDbRepo' . $unique;
217 $repoPath =
"$pathPrefix/$repoName";
221 if(mkdir($confPath,0755,TRUE) === FALSE)
223 echo
"FATAL! Cannot create test sysconf at:$confPath\n" .
224 __FILE__ .
" at line " . __LINE__ .
"\n";
227 if(chmod($confPath, 0755) === FALSE )
229 echo
"ERROR: Cannot set mode to 755 on $confPath\n" .
230 __FILE__ .
" at line " . __LINE__ .
"\n";
232 if(mkdir($repoPath,0755,TRUE) === FALSE)
234 echo
"FATAL! Cannot create test repository at:$repoPath\n" .
235 __FILE__ .
" at line " . __LINE__ .
"\n";
238 if(chmod($repoPath, 0755) === FALSE )
240 echo
"ERROR: Cannot set mode to 755 on $repoPath\n" .
241 __FILE__ .
" at line " . __LINE__ .
"\n";
245 $conf =
"dbname=$DbName;\n" .
246 "host=localhost;\n" .
250 if(file_put_contents($confPath .
"/Db.conf", $conf) === FALSE)
252 echo
"FATAL! Could not create Db.conf file at:$confPath\n";
257 $fossConf = $sysconfig .
'/fossology.conf';
258 $myConf = $confPath .
'/fossology.conf';
260 if(file_exists($fossConf))
262 if(copy($fossConf, $myConf) === FALSE)
264 echo
"FATAL! cannot copy $fossConf to $myConf\n";
269 if(setRepo($confPath, $repoPath) === FALSE)
271 echo
"ERROR!, could not change $sysconfig/fossology.conf, please change by " .
272 "hand before running tests\n";
277 $modConf = $sysconfig .
'/mods-enabled';
278 $cmd =
"cp -RP $modConf $confPath";
279 if(system($cmd) === FALSE)
287 $version = $sysconfig .
'/VERSION';
288 $myVersion = $confPath .
'/VERSION';
289 if(file_exists($fossConf))
291 if(copy($version, $myVersion) === FALSE)
293 echo
"FATAL! cannot copy $version to $myVersion\n";
297 putenv(
"SYSCONFDIR=$confPath");
298 $_ENV[
'SYSCONFDIR'] = $confPath;
299 $GLOBALS[
'SYSCONFDIR'] = $confPath;
303 echo $confPath .
"\n";
308 $loaded = TestDBInit(NULL, $DbName);
312 echo
"ERROR, could not load schema\n";
321 putenv(
"SYSCONFDIR=$confPath");
322 $_ENV[
'SYSCONFDIR'] = $confPath;
323 $GLOBALS[
'SYSCONFDIR'] = $confPath;
331 $cmd =
"sudo $MODDIR/scheduler/agent/fo_scheduler -d -c $confPath";
332 $skedLast = exec($cmd, $skedOut, $skedRtn);
335 echo
"FATAL! could not start scheduler with -d -c $confPath\n";
336 echo implode(
"\n", $skedOut) .
"\n";
340 echo $confPath .
"\n";