FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_common.php
1 <?php
2 /*
3  Copyright (C) 2012-2014 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
26  function create_db() {
27  global $SYSCONF_DIR;
28  global $DB_NAME;
29  global $REPO_NAME;
30  global $PG_CONN;
31  global $DB_COMMAND;
32 
33  // print "DB_COMMAND is:$DB_COMMAND\n";
34  exec($DB_COMMAND, $dbout, $rc);
35  preg_match("/(\d+)/", $dbout[0], $matches);
36  $test_name = $matches[1];
37  $DB_NAME = "fosstest".$test_name;
38  $REPO_NAME = "testDbRepo".$test_name;
39  $SYSCONF_DIR = $dbout[0];
40  $PG_CONN = pg_connect("host=localhost port=5432 dbname=$DB_NAME user=fossy password=fossy")
41  or die("Could not connect");
42  // print "DB_NAME is:$DB_NAME, $SYSCONF_DIR\n";
43  }
44 
48  function drop_db() {
49  global $PG_CONN;
50  global $DB_COMMAND;
51  global $DB_NAME;
52  pg_close($PG_CONN);
53  exec("$DB_COMMAND -d $DB_NAME");
54  }
55 
63  function get_upload_id($upload_info) {
64  $upload_id = 0;
65  preg_match("/UploadPk is: '(\d+)'/", $upload_info, $matches);
66  $upload_id = $matches[1];
67  if (!$upload_id) return false;
68  else return $upload_id;
69  }
70 
71 
72 
79  function connect_to_DB($fossology_testconfig) {
80  $test_pg_conn = FALSE;
81 
82  $db_conf_file = $fossology_testconfig . "/Db.conf";
83 
84  $test_pg_conn = pg_connect(str_replace(";", " ", file_get_contents($db_conf_file)));
85 
86  if (empty($test_pg_conn)) {
87  print "Error - could not connect to test db via $db_conf_file\n";
88  }
89  else {
90  print "Successfully connected to test db\n";
91  }
92  return($test_pg_conn);
93  }
94 
103  function check_agent_status($test_dbh, $agent_name, $upload_id) {
104  #global $PG_CONN;
105  $ars_table_name = $agent_name."_ars";
106  $count = 0;
107  $sql = "SELECT upload_fk FROM $ars_table_name where upload_fk = $upload_id and ars_success=true;";
108  // print "sql is:$sql\n";
109  #$result = pg_query($PG_CONN, $sql);
110  $result = pg_query($test_dbh, $sql);
111  $count = pg_num_rows($result);
112  pg_free_result($result);
113  if(1 == $count) return 1;
114  else return 0;
115  }
116 
125  function check_file_uploadtree($test_dbh, $file_name, $upload_id) {
126  $count = 0;
127  $sql = "SELECT uploadtree_pk FROM uploadtree where upload_fk = $upload_id and ufile_name = '$file_name';";
128  // print "sql is:$sql\n";
129  $result = pg_query($test_dbh, $sql);
130  $count = pg_num_rows($result);
131  pg_free_result($result);
132  if ($count > 0) {
133  return 1; // exist
134  } else {
135  return 0; // not exist
136  }
137  }
138 
142  function add_user($user='fossy', $password='fossy') {
143  global $PG_CONN;
144  /* User does not exist. Create it. */
145  $Seed = rand() . rand();
146  $Hash = sha1($Seed . $password);
147  $sql = "SELECT * FROM users WHERE user_name = '$user';";
148  $result = pg_query($PG_CONN, $sql);
149  $row0 = pg_fetch_assoc($result);
150  pg_free_result($result);
151  if (empty($row0['user_name'])) {
152  /* User does not exist. Create it. */
153  $SQL = "INSERT INTO users (user_name,user_desc,user_seed,user_pass," .
154  "user_perm,user_email,email_notify,root_folder_fk)
155  VALUES ('$user','Default Administrator','$Seed','$Hash',10,'$password','y',1);";
156  // $text = _("*** Created default administrator: '$user' with password '$password'.");
157  $result = pg_query($PG_CONN, $SQL);
158  pg_free_result($result);
159  }
160  }
161 
165  function preparations() {
166  global $SYSCONF_DIR;
167  global $REPO_NAME;
168  add_proxy(); // add proxy
169  if (is_dir("/srv/fossologyTestRepo/$REPO_NAME")) {
170  exec("sudo chmod 2770 /srv/fossologyTestRepo/$REPO_NAME"); // change mode to 2770
171  exec("sudo chown fossy /srv/fossologyTestRepo/$REPO_NAME -R"); // change owner of REPO to fossy
172  exec("sudo chgrp fossy /srv/fossologyTestRepo/$REPO_NAME -R"); // change grp of REPO to fossy
173  }
174  if (is_dir($SYSCONF_DIR)) {
175  exec("sudo chown fossy $SYSCONF_DIR -R"); // change owner of sysconfdir to fossy
176  exec("sudo chgrp fossy $SYSCONF_DIR -R"); // change grp of sysconfdir to fossy
177  }
178  }
179 
183  function stop_scheduler() {
184  global $SYSCONF_DIR;
186  $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler";
187  exec("sudo $scheduler_path -k"); // kill the running scheduler
188  }
189 
193  function scheduler_operation() {
194  global $SYSCONF_DIR;
195  $scheduler_path = "/usr/local/share/fossology/scheduler/agent/fo_scheduler";
196  exec("sudo $scheduler_path -k"); // kill the default scheduler if running
197  $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler";
198  exec("sudo $scheduler_path -k"); // kill the running scheduler
199  exec("sudo $scheduler_path --daemon --reset --verbose=952 -c $SYSCONF_DIR"); // start the scheduler
200  }
201 
205 function add_proxy($proxy_type='http_proxy', $porxy='web-proxy.cce.hp.com:8088') {
206  global $SYSCONF_DIR;
207 
208  $foss_conf = $SYSCONF_DIR."/fossology.conf";
209  exec("sudo sed 's/.$proxy_type.*=.*/$proxy_type=$porxy/' $foss_conf >/tmp/fossology.conf");
210  exec("sudo mv /tmp/fossology.conf $foss_conf");
211 }
212 
221 function get_uploadtree_id($test_dbh, $upload_id) {
222  $sql = "SELECT uploadtree_pk from uploadtree where upload_fk =$upload_id order by uploadtree_pk limit 1;";
223  $result = pg_query($test_dbh, $sql);
224  $row = pg_fetch_assoc($result);
225  $uploadtree_id = $row['uploadtree_pk'];
226  pg_free_result($result);
227  return $uploadtree_id;
228 }
229 
230 ?>
add_user($User, $Desc, $Seed, $Hash, $Perm, $Email, $Email_notify, $agentList, $Folder, $default_bucketpool_fk='')
Add a user.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN