28 require_once(__DIR__ .
'/../lib/libTestDB.php');
30 $Options = getopt(
'c:d:esh');
31 $usage = $argv[0] .
": [-h] -c path [-d name] [-s]\n" .
32 "-c path: The path to the fossology system configuration directory\n" .
33 "-d name: Drop the named data base.\n" .
34 "-e: create ONLY an empty db, sysconf dir and repository\n" .
35 "-h: This message (Usage)\n" .
36 "-s: Start the scheduler with the new sysconfig directory\n" .
38 " Create a test DB: 'createTestDb.php' \n" .
39 " Drop the database fosstest1537938: 'createTestDb.php -d fosstest1537938'\n" .
40 " Create test DB, start scheduler: 'createTestDb.php -s'\n" .
41 " Create empty DB, sysconf and repo: 'createTestDb.php -e'\n";
43 $pathPrefix =
'/srv/fossologyTestRepo';
44 $dbPrefix =
'fosstest';
47 $gid_array = posix_getgroups();
49 foreach($gid_array as $gid)
51 $gid_info = posix_getgrgid ($gid);
52 if ($gid_info[
'name'] ===
'fossy')
58 $uid = posix_getuid();
59 $uid_info = posix_getpwuid($uid);
60 if ($uid_info[
'name'] ===
'root') $gflag = 1;
70 $user = getenv(
'USER');
71 $userHome = getenv(
'HOME');
72 $ipv4 = gethostbyname(gethostname());
73 $fullHostName = gethostbyaddr(gethostbyname($ipv4));
74 $contents =
"$fullHostName:*:*:fossy:fossy\n";
75 $pgpass =
"$userHome/.pgpass";
80 $pg_pass_contents =
"";
81 if (file_exists($pgpass)) {
83 $pg_pass_contents = file_get_contents($pgpass);
88 if (!preg_match(
'/\:fossy\:fossy/', $pg_pass_contents)) {
89 $FD = fopen($pgpass,
'w');
90 $howmany = fwrite($FD, $contents);
91 if($howmany === FALSE)
93 echo
"FATAL! Could not write .pgpass file to $pgpass\n";
101 if(!chmod($pgpass, 0600))
103 echo
"Warning! could not set $pgpass to 0600\n";
106 if(array_key_exists(
'h',$Options))
113 if(array_key_exists(
'c', $Options))
115 $sysconfig = $Options[
'c'];
116 if(empty($sysconfig))
125 if(array_key_exists(
'd', $Options))
127 $dropName = $Options[
'd'];
135 $ckCmd =
"psql -c '\q' postgres -U fossy";
136 $lastCmd = exec($ckCmd, $ckOut, $ckRtn);
139 echo
"ERROR: postgresql isn't running, not deleting database $dropName\n";
142 $existCmd =
"psql -l postgres -U fossy|grep -q $dropName";
143 $lastExist = exec($existCmd, $existkOut, $existRtn);
147 # stop all users of the fossology db
148 $pkillCmd =
"sudo pkill -f -u postgres fossy || true";
149 $lKill = exec($pkillCmd, $killOut, $killRtn);
150 $dropCmd =
"sudo su postgres -c 'echo \"drop database $dropName;\"|psql'";
151 $lastDrop = exec($dropCmd, $dropOut, $dropRtn);
154 echo
"ERROR: failed to delete database $dropName\n";
160 echo
"NOTE: database $dropName does not exist, nothing to delete\n";
164 $len = strlen($dbPrefix);
165 $uni = substr($dropName,$len);
166 $rmRepo = $pathPrefix .
'/testDbRepo' .$uni;
167 $rmConf = $pathPrefix .
'/testDbConf' .$uni;
168 $last = system(
"sudo rm -rf $rmConf $rmRepo", $rmRtn);
172 if(empty($sysconfig))
174 $sysconfig = getenv(
'SYSCONFDIR');
176 if(empty($sysconfig))
178 echo
"FATAL!, no SYSCONFDIR defined\n";
179 echo
"either export SYSCONFDIR path and rerun or use -c <sysconfdirpath>\n";
185 putenv(
"SYSCONFDIR=$sysconfig");
186 $_ENV[
'SYSCONFDIR'] = $sysconfig;
189 $DbName = $dbPrefix . $unique;
191 $createEmpty = array_key_exists(
'e', $Options);
192 $startSched = array_key_exists(
's', $Options);
195 $newDB = createTestDB($DbName);
198 echo
"ERROR, could not create database $DbName\n";
203 $confName =
'testDbConf' . $unique;
204 $confPath =
"$pathPrefix/$confName";
205 $repoName =
'testDbRepo' . $unique;
206 $repoPath =
"$pathPrefix/$repoName";
210 if(mkdir($confPath,0755,TRUE) === FALSE)
212 echo
"FATAL! Cannot create test sysconf at:$confPath\n" .
213 __FILE__ .
" at line " . __LINE__ .
"\n";
216 if(chmod($confPath, 0755) === FALSE )
218 echo
"ERROR: Cannot set mode to 755 on $confPath\n" .
219 __FILE__ .
" at line " . __LINE__ .
"\n";
221 if(mkdir($repoPath,0755,TRUE) === FALSE)
223 echo
"FATAL! Cannot create test repository at:$repoPath\n" .
224 __FILE__ .
" at line " . __LINE__ .
"\n";
227 if(chmod($repoPath, 0755) === FALSE )
229 echo
"ERROR: Cannot set mode to 755 on $repoPath\n" .
230 __FILE__ .
" at line " . __LINE__ .
"\n";
234 $conf =
"dbname=$DbName;\n" .
235 "host=localhost;\n" .
239 if(file_put_contents($confPath .
"/Db.conf", $conf) === FALSE)
241 echo
"FATAL! Could not create Db.conf file at:$confPath\n";
246 $fossConf = $sysconfig .
'/fossology.conf';
247 $myConf = $confPath .
'/fossology.conf';
249 if(file_exists($fossConf))
251 if(copy($fossConf, $myConf) === FALSE)
253 echo
"FATAL! cannot copy $fossConf to $myConf\n";
258 if(setRepo($confPath, $repoPath) === FALSE)
260 echo
"ERROR!, could not change $sysconfig/fossology.conf, please change by " .
261 "hand before running tests\n";
266 $modConf = $sysconfig .
'/mods-enabled';
267 $cmd =
"cp -RP $modConf $confPath";
268 if(system($cmd) === FALSE)
276 $version = $sysconfig .
'/VERSION';
277 $myVersion = $confPath .
'/VERSION';
278 if(file_exists($fossConf))
280 if(copy($version, $myVersion) === FALSE)
282 echo
"FATAL! cannot copy $version to $myVersion\n";
286 putenv(
"SYSCONFDIR=$confPath");
287 $_ENV[
'SYSCONFDIR'] = $confPath;
288 $GLOBALS[
'SYSCONFDIR'] = $confPath;
292 echo $confPath .
"\n";
297 $loaded = TestDBInit(NULL, $DbName);
301 echo
"ERROR, could not load schema\n";
310 putenv(
"SYSCONFDIR=$confPath");
311 $_ENV[
'SYSCONFDIR'] = $confPath;
312 $GLOBALS[
'SYSCONFDIR'] = $confPath;
320 $cmd =
"sudo $MODDIR/scheduler/agent/fo_scheduler -d -c $confPath";
321 $skedLast = exec($cmd, $skedOut, $skedRtn);
324 echo
"FATAL! could not start scheduler with -d -c $confPath\n";
325 echo implode(
"\n", $skedOut) .
"\n";
329 echo $confPath .
"\n";