FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_common.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2012-2014 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
15 function create_db()
16 {
17  global $SYSCONF_DIR;
18  global $DB_NAME;
19  global $REPO_NAME;
20  global $PG_CONN;
21  global $DB_COMMAND;
22 
23  // print "DB_COMMAND is:$DB_COMMAND\n";
24  exec($DB_COMMAND, $dbout, $rc);
25  preg_match("/(\d+)/", $dbout[0], $matches);
26  $test_name = $matches[1];
27  $DB_NAME = "fosstest".$test_name;
28  $REPO_NAME = "testDbRepo".$test_name;
29  $SYSCONF_DIR = $dbout[0];
30  $PG_CONN = pg_connect("host=localhost port=5432 dbname=$DB_NAME user=fossy password=fossy")
31  or die("Could not connect");
32  // print "DB_NAME is:$DB_NAME, $SYSCONF_DIR\n";
33 }
34 
38 function drop_db()
39 {
40  global $PG_CONN;
41  global $DB_COMMAND;
42  global $DB_NAME;
43  pg_close($PG_CONN);
44  exec("$DB_COMMAND -d $DB_NAME");
45 }
46 
54 function get_upload_id($upload_info)
55 {
56  $upload_id = 0;
57  preg_match("/UploadPk is: '(\d+)'/", $upload_info, $matches);
58  $upload_id = $matches[1];
59  if (!$upload_id) {
60  return false;
61  } else {
62  return $upload_id;
63  }
64 }
65 
66 
67 
74 function connect_to_DB($fossology_testconfig)
75 {
76  $test_pg_conn = FALSE;
77 
78  $db_conf_file = $fossology_testconfig . "/Db.conf";
79 
80  $test_pg_conn = pg_connect(
81  str_replace(";", " ", file_get_contents($db_conf_file)));
82 
83  if (empty($test_pg_conn)) {
84  print "Error - could not connect to test db via $db_conf_file\n";
85  } else {
86  print "Successfully connected to test db\n";
87  }
88  return ($test_pg_conn);
89 }
90 
99 function check_agent_status($test_dbh, $agent_name, $upload_id)
100 {
101  #global $PG_CONN;
102  $ars_table_name = $agent_name."_ars";
103  $count = 0;
104  $sql = "SELECT upload_fk FROM $ars_table_name where upload_fk = $upload_id and ars_success=true;";
105  // print "sql is:$sql\n";
106  #$result = pg_query($PG_CONN, $sql);
107  $result = pg_query($test_dbh, $sql);
108  $count = pg_num_rows($result);
109  pg_free_result($result);
110  if (1 == $count) {
111  return 1;
112  } else {
113  return 0;
114  }
115 }
116 
125 function check_file_uploadtree($test_dbh, $file_name, $upload_id)
126 {
127  $count = 0;
128  $sql = "SELECT uploadtree_pk FROM uploadtree where upload_fk = $upload_id and ufile_name = '$file_name';";
129  // print "sql is:$sql\n";
130  $result = pg_query($test_dbh, $sql);
131  $count = pg_num_rows($result);
132  pg_free_result($result);
133  if ($count > 0) {
134  return 1; // exist
135  } else {
136  return 0; // not exist
137  }
138 }
139 
143 function add_user($user='fossy', $password='fossy')
144 {
145  global $PG_CONN;
146  /* User does not exist. Create it. */
147  $options = array('cost' => 10);
148  $Hash = password_hash($password, PASSWORD_DEFAULT, $options);
149  $sql = "SELECT * FROM users WHERE user_name = '$user';";
150  $result = pg_query($PG_CONN, $sql);
151  $row0 = pg_fetch_assoc($result);
152  pg_free_result($result);
153  if (empty($row0['user_name'])) {
154  /* User does not exist. Create it. */
155  $SQL = "INSERT INTO users (user_name,user_desc,user_seed,user_pass," .
156  "user_perm,user_email,email_notify,root_folder_fk)
157  VALUES ('$user','Default Administrator','Seed','$Hash',10,'$password','y',1);";
158  // $text = _("*** Created default administrator: '$user' with password '$password'.");
159  $result = pg_query($PG_CONN, $SQL);
160  pg_free_result($result);
161  }
162 }
163 
167 function preparations()
168 {
169  global $SYSCONF_DIR;
170  global $REPO_NAME;
171  add_proxy(); // add proxy
172  if (is_dir("/srv/fossology/$REPO_NAME")) {
173  exec("sudo chmod 2770 /srv/fossology/$REPO_NAME"); // change mode to 2770
174  exec("sudo chown fossy /srv/fossology/$REPO_NAME -R"); // change owner of REPO to fossy
175  exec("sudo chgrp fossy /srv/fossology/$REPO_NAME -R"); // change grp of REPO to fossy
176  }
177  if (is_dir($SYSCONF_DIR)) {
178  exec("sudo chown fossy $SYSCONF_DIR -R"); // change owner of sysconfdir to fossy
179  exec("sudo chgrp fossy $SYSCONF_DIR -R"); // change grp of sysconfdir to fossy
180  }
181 }
182 
186 function stop_scheduler()
187 {
188  global $SYSCONF_DIR;
190  $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler";
191  exec("sudo $scheduler_path -k"); // kill the running scheduler
192 }
193 
197 function scheduler_operation()
198 {
199  global $SYSCONF_DIR;
200  $scheduler_path = "/usr/local/share/fossology/scheduler/agent/fo_scheduler";
201  exec("sudo $scheduler_path -k"); // kill the default scheduler if running
202  $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler";
203  exec("sudo $scheduler_path -k"); // kill the running scheduler
204  exec("sudo $scheduler_path --daemon --reset --verbose=952 -c $SYSCONF_DIR"); // start the scheduler
205 }
206 
210 function add_proxy($proxy_type='http_proxy', $porxy='web-proxy.cce.hp.com:8088')
211 {
212  global $SYSCONF_DIR;
213 
214  $foss_conf = $SYSCONF_DIR."/fossology.conf";
215  exec("sudo sed 's/.$proxy_type.*=.*/$proxy_type=$porxy/' $foss_conf >/tmp/fossology.conf");
216  exec("sudo mv /tmp/fossology.conf $foss_conf");
217 }
218 
227 function get_uploadtree_id($test_dbh, $upload_id)
228 {
229  $sql = "SELECT uploadtree_pk from uploadtree where upload_fk =$upload_id order by uploadtree_pk limit 1;";
230  $result = pg_query($test_dbh, $sql);
231  $row = pg_fetch_assoc($result);
232  $uploadtree_id = $row['uploadtree_pk'];
233  pg_free_result($result);
234  return $uploadtree_id;
235 }
236 
237 
add_user($User, $Desc, $Hash, $Perm, $Email, $Email_notify, $Upload_visibility, $agentList, $Folder, $default_bucketpool_fk='')
Add a user.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN