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 {
28  global $SYSCONF_DIR;
29  global $DB_NAME;
30  global $REPO_NAME;
31  global $PG_CONN;
32  global $DB_COMMAND;
33 
34  // print "DB_COMMAND is:$DB_COMMAND\n";
35  exec($DB_COMMAND, $dbout, $rc);
36  preg_match("/(\d+)/", $dbout[0], $matches);
37  $test_name = $matches[1];
38  $DB_NAME = "fosstest".$test_name;
39  $REPO_NAME = "testDbRepo".$test_name;
40  $SYSCONF_DIR = $dbout[0];
41  $PG_CONN = pg_connect("host=localhost port=5432 dbname=$DB_NAME user=fossy password=fossy")
42  or die("Could not connect");
43  // print "DB_NAME is:$DB_NAME, $SYSCONF_DIR\n";
44 }
45 
49 function drop_db()
50 {
51  global $PG_CONN;
52  global $DB_COMMAND;
53  global $DB_NAME;
54  pg_close($PG_CONN);
55  exec("$DB_COMMAND -d $DB_NAME");
56 }
57 
65 function get_upload_id($upload_info)
66 {
67  $upload_id = 0;
68  preg_match("/UploadPk is: '(\d+)'/", $upload_info, $matches);
69  $upload_id = $matches[1];
70  if (!$upload_id) {
71  return false;
72  } else {
73  return $upload_id;
74  }
75 }
76 
77 
78 
85 function connect_to_DB($fossology_testconfig)
86 {
87  $test_pg_conn = FALSE;
88 
89  $db_conf_file = $fossology_testconfig . "/Db.conf";
90 
91  $test_pg_conn = pg_connect(
92  str_replace(";", " ", file_get_contents($db_conf_file)));
93 
94  if (empty($test_pg_conn)) {
95  print "Error - could not connect to test db via $db_conf_file\n";
96  } else {
97  print "Successfully connected to test db\n";
98  }
99  return ($test_pg_conn);
100 }
101 
110 function check_agent_status($test_dbh, $agent_name, $upload_id)
111 {
112  #global $PG_CONN;
113  $ars_table_name = $agent_name."_ars";
114  $count = 0;
115  $sql = "SELECT upload_fk FROM $ars_table_name where upload_fk = $upload_id and ars_success=true;";
116  // print "sql is:$sql\n";
117  #$result = pg_query($PG_CONN, $sql);
118  $result = pg_query($test_dbh, $sql);
119  $count = pg_num_rows($result);
120  pg_free_result($result);
121  if (1 == $count) {
122  return 1;
123  } else {
124  return 0;
125  }
126 }
127 
136 function check_file_uploadtree($test_dbh, $file_name, $upload_id)
137 {
138  $count = 0;
139  $sql = "SELECT uploadtree_pk FROM uploadtree where upload_fk = $upload_id and ufile_name = '$file_name';";
140  // print "sql is:$sql\n";
141  $result = pg_query($test_dbh, $sql);
142  $count = pg_num_rows($result);
143  pg_free_result($result);
144  if ($count > 0) {
145  return 1; // exist
146  } else {
147  return 0; // not exist
148  }
149 }
150 
154 function add_user($user='fossy', $password='fossy')
155 {
156  global $PG_CONN;
157  /* User does not exist. Create it. */
158  $Seed = rand() . rand();
159  $Hash = sha1($Seed . $password);
160  $sql = "SELECT * FROM users WHERE user_name = '$user';";
161  $result = pg_query($PG_CONN, $sql);
162  $row0 = pg_fetch_assoc($result);
163  pg_free_result($result);
164  if (empty($row0['user_name'])) {
165  /* User does not exist. Create it. */
166  $SQL = "INSERT INTO users (user_name,user_desc,user_seed,user_pass," .
167  "user_perm,user_email,email_notify,root_folder_fk)
168  VALUES ('$user','Default Administrator','$Seed','$Hash',10,'$password','y',1);";
169  // $text = _("*** Created default administrator: '$user' with password '$password'.");
170  $result = pg_query($PG_CONN, $SQL);
171  pg_free_result($result);
172  }
173 }
174 
178 function preparations()
179 {
180  global $SYSCONF_DIR;
181  global $REPO_NAME;
182  add_proxy(); // add proxy
183  if (is_dir("/srv/fossology/$REPO_NAME")) {
184  exec("sudo chmod 2770 /srv/fossology/$REPO_NAME"); // change mode to 2770
185  exec("sudo chown fossy /srv/fossology/$REPO_NAME -R"); // change owner of REPO to fossy
186  exec("sudo chgrp fossy /srv/fossology/$REPO_NAME -R"); // change grp of REPO to fossy
187  }
188  if (is_dir($SYSCONF_DIR)) {
189  exec("sudo chown fossy $SYSCONF_DIR -R"); // change owner of sysconfdir to fossy
190  exec("sudo chgrp fossy $SYSCONF_DIR -R"); // change grp of sysconfdir to fossy
191  }
192 }
193 
197 function stop_scheduler()
198 {
199  global $SYSCONF_DIR;
201  $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler";
202  exec("sudo $scheduler_path -k"); // kill the running scheduler
203 }
204 
208 function scheduler_operation()
209 {
210  global $SYSCONF_DIR;
211  $scheduler_path = "/usr/local/share/fossology/scheduler/agent/fo_scheduler";
212  exec("sudo $scheduler_path -k"); // kill the default scheduler if running
213  $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler";
214  exec("sudo $scheduler_path -k"); // kill the running scheduler
215  exec("sudo $scheduler_path --daemon --reset --verbose=952 -c $SYSCONF_DIR"); // start the scheduler
216 }
217 
221 function add_proxy($proxy_type='http_proxy', $porxy='web-proxy.cce.hp.com:8088')
222 {
223  global $SYSCONF_DIR;
224 
225  $foss_conf = $SYSCONF_DIR."/fossology.conf";
226  exec("sudo sed 's/.$proxy_type.*=.*/$proxy_type=$porxy/' $foss_conf >/tmp/fossology.conf");
227  exec("sudo mv /tmp/fossology.conf $foss_conf");
228 }
229 
238 function get_uploadtree_id($test_dbh, $upload_id)
239 {
240  $sql = "SELECT uploadtree_pk from uploadtree where upload_fk =$upload_id order by uploadtree_pk limit 1;";
241  $result = pg_query($test_dbh, $sql);
242  $row = pg_fetch_assoc($result);
243  $uploadtree_id = $row['uploadtree_pk'];
244  pg_free_result($result);
245  return $uploadtree_id;
246 }
247 
248 
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