23 public function setupTestDb($dbName=NULL)
25 date_default_timezone_set(
"UTC");
26 if (!is_callable(
'pg_connect'))
28 throw new \Exception(
"php-psql not found");
30 $sub = chr(mt_rand(97, 122)) . chr(mt_rand(97, 122)) . chr(mt_rand(97, 122)) . chr(mt_rand(97, 122));
33 $dbName =
"fosstestone";
35 if ($dbName ===
"fossology") {
36 throw new \Exception(
"cannot use production database for tests");
39 $dbName = strtolower($dbName);
40 $this->ensurePgPassFileEntry();
42 $sys_conf = sys_get_temp_dir() .
"/$dbName" . time() . $sub;
43 if (!mkdir($sys_conf, $mode = 0755))
45 throw new \Exception(
"FATAL! Cannot create test repository at " . $sys_conf);
47 if (chmod($sys_conf, 0755) === FALSE)
49 throw new \Exception(
"ERROR: Cannot set mode to 755 on " . $sys_conf .
"\n" . __FILE__ .
" at line " . __LINE__ .
"\n");
51 $conf =
"dbname=$dbName;\nhost=localhost;\nuser=fossy;\npassword=fossy;\n";
52 if (file_put_contents($sys_conf .
"/Db.conf", $conf) === FALSE)
54 throw new \Exception(
"FATAL! Could not create Db.conf file at " . $sys_conf);
57 exec($cmd =
"psql -Ufossy -h localhost -lqtA | cut -f 1 -d '|' | grep -q '^$dbName\$'", $cmdOut, $cmdRtn);
60 exec($cmd =
"echo 'SELECT * FROM pg_language;' | psql -Ufossy -h localhost -t $dbName | grep -q plpgsql", $cmdOut, $cmdRtn);
63 exec($cmd =
"echo 'CREATE LANGUAGE plpgsql;' | psql -Ufossy -h localhost $dbName", $cmdOut, $cmdRtn);
65 throw new \Exception(
"ERROR: failed to add plpgsql to $dbName database");
69 $fosstestSql = file_get_contents(dirname(__FILE__) .
'/../../lib/php/Test/fosstestinit.sql');
70 $fossSql = str_replace(
'fosstest', $dbName, $fosstestSql);
71 $pathSql = $sys_conf .
'/dbinit.sql';
72 file_put_contents($pathSql, $fossSql);
73 exec($cmd =
"psql -Ufossy -h localhost fossology < $pathSql", $cmdOut, $cmdRtn);
76 throw new \Exception(
"ERROR: Database failed during configuration.");
84 private function ensurePgPassFileEntry()
86 $userHome = getenv(
'HOME');
87 $ipv4 = gethostbyname(gethostname());
89 $contents =
"localhost:*:*:fossy:fossy\n";
90 $pgpass =
"$userHome/.pgpass";
91 putenv(
"PGPASSFILE=$pgpass");
92 $pg_pass_contents = file_exists($pgpass) ? file_get_contents($pgpass) :
'';
93 if (!preg_match(
'/\:fossy\:fossy/', $pg_pass_contents))
95 $pgpassHandle = fopen($pgpass,
'w');
96 $howmany = fwrite($pgpassHandle, $contents);
97 if ($howmany === FALSE)
99 throw new \Exception(
"FATAL! Could not write .pgpass file to $pgpassHandle");
101 fclose($pgpassHandle);
103 if (!chmod($pgpass, 0600))
105 throw new \Exception(
"Warning! could not set $pgpass to 0600\n");
109 public function getDbName($sys_conf)
111 $dbConfig = file_get_contents(
"$sys_conf/Db.conf");
112 if (!preg_match(
"/dbname=([[:alnum:]]+);.*/", $dbConfig, $matches))
114 throw new \Exception(
"could not parse db name");
119 public function purgeTestDb($sys_conf=null)
121 if (empty($sys_conf)) {
122 $sys_conf = getenv(
'SYSCONFDIR');
124 if (empty($sys_conf)) {
125 throw new \Exception(
"refusing to purge from /");
128 $dbName = $this->getDbName($sys_conf);
129 if (empty($dbName)) {
130 throw new \Exception(
"cannot determine db to empty");
133 $existCmd =
"psql -Ufossy -h localhost -l | grep -q " . $dbName;
134 exec($existCmd, $existkOut, $existRtn);
137 echo
"NOTE: database " . $dbName .
" does not exist, nothing to delete\n";
140 $dropCmd =
"dropdb -Ufossy -h localhost " . $dbName;
141 exec($dropCmd, $dropOut, $dropRtn);
144 throw new \Exception(
"failed to delete database " . $dbName);
147 foreach (glob($sys_conf .
"/*.*") as $filename)
151 exec(
"rm -rf $sys_conf");