FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TestDbFactory.php
1 <?php
2 
3 /*
4 Copyright (C) 2014-2015, Siemens AG
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
21 {
22 
23  public function setupTestDb($dbName=NULL)
24  {
25  date_default_timezone_set("UTC");
26  if (!is_callable('pg_connect'))
27  {
28  throw new \Exception("php-psql not found");
29  }
30  $sub = chr(mt_rand(97, 122)) . chr(mt_rand(97, 122)) . chr(mt_rand(97, 122)) . chr(mt_rand(97, 122));
31  if (empty($dbName))
32  {
33  $dbName = "fosstestone";
34  } else {
35  if ($dbName === "fossology") {
36  throw new \Exception("cannot use production database for tests");
37  }
38  }
39  $dbName = strtolower($dbName);
40  $this->ensurePgPassFileEntry();
41 
42  $sys_conf = sys_get_temp_dir() . "/$dbName" . time() . $sub;
43  if (!mkdir($sys_conf, $mode = 0755))
44  {
45  throw new \Exception("FATAL! Cannot create test repository at " . $sys_conf);
46  }
47  if (chmod($sys_conf, 0755) === FALSE)
48  {
49  throw new \Exception("ERROR: Cannot set mode to 755 on " . $sys_conf . "\n" . __FILE__ . " at line " . __LINE__ . "\n");
50  }
51  $conf = "dbname=$dbName;\nhost=localhost;\nuser=fossy;\npassword=fossy;\n";
52  if (file_put_contents($sys_conf . "/Db.conf", $conf) === FALSE)
53  {
54  throw new \Exception("FATAL! Could not create Db.conf file at " . $sys_conf);
55  }
56 
57  exec($cmd = "psql -Ufossy -h localhost -lqtA | cut -f 1 -d '|' | grep -q '^$dbName\$'", $cmdOut, $cmdRtn);
58  if ($cmdRtn == 0)
59  {
60  exec($cmd = "echo 'SELECT * FROM pg_language;' | psql -Ufossy -h localhost -t $dbName | grep -q plpgsql", $cmdOut, $cmdRtn);
61  if ($cmdRtn != 0)
62  {
63  exec($cmd = "echo 'CREATE LANGUAGE plpgsql;' | psql -Ufossy -h localhost $dbName", $cmdOut, $cmdRtn);
64  if ($cmdRtn != 0)
65  throw new \Exception("ERROR: failed to add plpgsql to $dbName database");
66  }
67  } else
68  {
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); // 2>&1
74  if ($cmdRtn != 0)
75  {
76  throw new \Exception("ERROR: Database failed during configuration.");
77  }
78  unlink($pathSql);
79  }
80 
81  return $sys_conf;
82  }
83 
84  private function ensurePgPassFileEntry()
85  {
86  $userHome = getenv('HOME');
87  $ipv4 = gethostbyname(gethostname());
88 
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))
94  {
95  $pgpassHandle = fopen($pgpass, 'w');
96  $howmany = fwrite($pgpassHandle, $contents);
97  if ($howmany === FALSE)
98  {
99  throw new \Exception("FATAL! Could not write .pgpass file to $pgpassHandle");
100  }
101  fclose($pgpassHandle);
102  }
103  if (!chmod($pgpass, 0600))
104  {
105  throw new \Exception("Warning! could not set $pgpass to 0600\n");
106  }
107  }
108 
109  public function getDbName($sys_conf)
110  {
111  $dbConfig = file_get_contents("$sys_conf/Db.conf");
112  if (!preg_match("/dbname=([[:alnum:]]+);.*/", $dbConfig, $matches))
113  {
114  throw new \Exception("could not parse db name");
115  }
116  return $matches[1];
117  }
118 
119  public function purgeTestDb($sys_conf=null)
120  {
121  if (empty($sys_conf)) {
122  $sys_conf = getenv('SYSCONFDIR');
123  }
124  if (empty($sys_conf)) {
125  throw new \Exception( "refusing to purge from /");
126  }
127 
128  $dbName = $this->getDbName($sys_conf);
129  if (empty($dbName)) {
130  throw new \Exception( "cannot determine db to empty");
131  }
132 
133  $existCmd = "psql -Ufossy -h localhost -l | grep -q " . $dbName;
134  exec($existCmd, $existkOut, $existRtn);
135  if ($existRtn != 0)
136  {
137  echo "NOTE: database " . $dbName . " does not exist, nothing to delete\n";
138  } else
139  {
140  $dropCmd = "dropdb -Ufossy -h localhost " . $dbName;
141  exec($dropCmd, $dropOut, $dropRtn);
142  if ($dropRtn != 0)
143  {
144  throw new \Exception("failed to delete database " . $dbName);
145  }
146  }
147  foreach (glob($sys_conf . "/*.*") as $filename)
148  {
149  unlink($filename);
150  }
151  exec("rm -rf $sys_conf");
152  }
153 }