FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_common.php
1 <?php
2 /*
3  Copyright (C) 2012 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  global $PG_CONN;
81 
82  $db_conf_file = $fossology_testconfig . "/Db.conf";
83 
84  $PG_CONN = pg_connect(str_replace(";", " ", file_get_contents($db_conf_file)));
85 
86  if (empty($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  }
93 
102  function check_agent_status($test_dbh, $agent_name, $upload_id) {
103  #global $PG_CONN;
104  $ars_table_name = $agent_name."_ars";
105  $count = 0;
106  $sql = "SELECT count(*) FROM $ars_table_name where upload_fk = $upload_id and ars_success=true;";
107  // print "sql is:$sql\n";
108  #$result = pg_query($PG_CONN, $sql);
109  $result = pg_query($test_dbh, $sql);
110  $count = pg_num_rows($result);
111  pg_free_result($result);
112  if(1 == $count) return 1;
113  else return 0;
114  }
115 
119  function add_user($user='fossy', $password='fossy') {
120  global $PG_CONN;
121  /* User does not exist. Create it. */
122  $Seed = rand() . rand();
123  $Hash = sha1($Seed . $password);
124  $sql = "SELECT * FROM users WHERE user_name = '$user';";
125  $result = pg_query($PG_CONN, $sql);
126  $row0 = pg_fetch_assoc($result);
127  pg_free_result($result);
128  if (empty($row0['user_name'])) {
129  /* User does not exist. Create it. */
130  $SQL = "INSERT INTO users (user_name,user_desc,user_seed,user_pass," .
131  "user_perm,user_email,email_notify,root_folder_fk)
132  VALUES ('$user','Default Administrator','$Seed','$Hash',10,'$password','y',1);";
133  // $text = _("*** Created default administrator: '$user' with password '$password'.");
134  $result = pg_query($PG_CONN, $SQL);
135  pg_free_result($result);
136  }
137  }
138 
142  function preparations() {
143  global $SYSCONF_DIR;
144  global $REPO_NAME;
145  add_proxy(); // add proxy
146  if (is_dir("/srv/fossologyTestRepo/$REPO_NAME")) {
147  exec("sudo chmod 2770 /srv/fossologyTestRepo/$REPO_NAME"); // change mode to 2770
148  exec("sudo chown fossy /srv/fossologyTestRepo/$REPO_NAME -R"); // change owner of REPO to fossy
149  exec("sudo chgrp fossy /srv/fossologyTestRepo/$REPO_NAME -R"); // change grp of REPO to fossy
150  }
151  if (is_dir($SYSCONF_DIR)) {
152  exec("sudo chown fossy $SYSCONF_DIR -R"); // change owner of sysconfdir to fossy
153  exec("sudo chgrp fossy $SYSCONF_DIR -R"); // change grp of sysconfdir to fossy
154  }
155  }
156 
160  function stop_scheduler() {
161  global $SYSCONF_DIR;
163  $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler";
164  exec("sudo $scheduler_path -k"); // kill the running scheduler
165  }
166 
170  function scheduler_operation() {
171  global $SYSCONF_DIR;
172  $scheduler_path = "/usr/local/share/fossology/scheduler/agent/fo_scheduler";
173  exec("sudo $scheduler_path -k"); // kill the default scheduler if running
174  $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler";
175  exec("sudo $scheduler_path -k"); // kill the running scheduler
176  exec("sudo $scheduler_path --daemon --reset --verbose=952 -c $SYSCONF_DIR"); // start the scheduler
177  }
178 
182 function add_proxy($proxy_type='http_proxy', $porxy='web-proxy.cce.hp.com:8088') {
183  global $SYSCONF_DIR;
184 
185  $foss_conf = $SYSCONF_DIR."/fossology.conf";
186  exec("sudo sed 's/.$proxy_type.*=.*/$proxy_type=$porxy/' $foss_conf >/tmp/fossology.conf");
187  exec("sudo mv /tmp/fossology.conf $foss_conf");
188 }
189 
198 function get_uploadtree_id($test_dbh, $upload_id) {
199  $sql = "SELECT uploadtree_pk from uploadtree where upload_fk =$upload_id order by uploadtree_pk limit 1;";
200  $result = pg_query($test_dbh, $sql);
201  $row = pg_fetch_assoc($result);
202  $uploadtree_id = $row['uploadtree_pk'];
203  pg_free_result($result);
204  return $uploadtree_id;
205 }
206 
207 ?>
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