FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_common.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
15  function create_db() {
16  global $SYSCONF_DIR;
17  global $DB_NAME;
18  global $REPO_NAME;
19  global $PG_CONN;
20  global $DB_COMMAND;
21 
22  // print "DB_COMMAND is:$DB_COMMAND\n";
23  exec($DB_COMMAND, $dbout, $rc);
24  preg_match("/(\d+)/", $dbout[0], $matches);
25  $test_name = $matches[1];
26  $DB_NAME = "fosstest".$test_name;
27  $REPO_NAME = "testDbRepo".$test_name;
28  $SYSCONF_DIR = $dbout[0];
29  $PG_CONN = pg_connect("host=localhost port=5432 dbname=$DB_NAME user=fossy password=fossy")
30  or die("Could not connect");
31  // print "DB_NAME is:$DB_NAME, $SYSCONF_DIR\n";
32  }
33 
37  function drop_db() {
38  global $PG_CONN;
39  global $DB_COMMAND;
40  global $DB_NAME;
41  pg_close($PG_CONN);
42  exec("$DB_COMMAND -d $DB_NAME");
43  }
44 
52  function get_upload_id($upload_info) {
53  $upload_id = 0;
54  preg_match("/UploadPk is: '(\d+)'/", $upload_info, $matches);
55  $upload_id = $matches[1];
56  if (!$upload_id) return false;
57  else return $upload_id;
58  }
59 
60 
61 
68  function connect_to_DB($fossology_testconfig) {
69  global $PG_CONN;
70 
71  $db_conf_file = $fossology_testconfig . "/Db.conf";
72 
73  $PG_CONN = pg_connect(str_replace(";", " ", file_get_contents($db_conf_file)));
74 
75  if (empty($PG_CONN)) {
76  print "Error - could not connect to test db via $db_conf_file\n";
77  }
78  else {
79  print "Successfully connected to test db\n";
80  }
81  }
82 
91  function check_agent_status($test_dbh, $agent_name, $upload_id) {
92  #global $PG_CONN;
93  $ars_table_name = $agent_name."_ars";
94  $count = 0;
95  $sql = "SELECT count(*) FROM $ars_table_name where upload_fk = $upload_id and ars_success=true;";
96  // print "sql is:$sql\n";
97  #$result = pg_query($PG_CONN, $sql);
98  $result = pg_query($test_dbh, $sql);
99  $count = pg_num_rows($result);
100  pg_free_result($result);
101  if(1 == $count) return 1;
102  else return 0;
103  }
104 
108  function add_user($user='fossy', $password='fossy') {
109  global $PG_CONN;
110  /* User does not exist. Create it. */
111  $options = array('cost' => 10);
112  $Hash = password_hash($password, PASSWORD_DEFAULT, $options);
113  $sql = "SELECT * FROM users WHERE user_name = '$user';";
114  $result = pg_query($PG_CONN, $sql);
115  $row0 = pg_fetch_assoc($result);
116  pg_free_result($result);
117  if (empty($row0['user_name'])) {
118  /* User does not exist. Create it. */
119  $SQL = "INSERT INTO users (user_name,user_desc,user_seed,user_pass," .
120  "user_perm,user_email,email_notify,root_folder_fk)
121  VALUES ('$user','Default Administrator','Seed','$Hash',10,'$password','y',1);";
122  // $text = _("*** Created default administrator: '$user' with password '$password'.");
123  $result = pg_query($PG_CONN, $SQL);
124  pg_free_result($result);
125  }
126  }
127 
131  function preparations() {
132  global $SYSCONF_DIR;
133  global $REPO_NAME;
134  add_proxy(); // add proxy
135  if (is_dir("/srv/fossologyTestRepo/$REPO_NAME")) {
136  exec("sudo chmod 2770 /srv/fossologyTestRepo/$REPO_NAME"); // change mode to 2770
137  exec("sudo chown fossy /srv/fossologyTestRepo/$REPO_NAME -R"); // change owner of REPO to fossy
138  exec("sudo chgrp fossy /srv/fossologyTestRepo/$REPO_NAME -R"); // change grp of REPO to fossy
139  }
140  if (is_dir($SYSCONF_DIR)) {
141  exec("sudo chown fossy $SYSCONF_DIR -R"); // change owner of sysconfdir to fossy
142  exec("sudo chgrp fossy $SYSCONF_DIR -R"); // change grp of sysconfdir to fossy
143  }
144  }
145 
149  function stop_scheduler() {
150  global $SYSCONF_DIR;
152  $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler";
153  exec("sudo $scheduler_path -k"); // kill the running scheduler
154  }
155 
159  function scheduler_operation() {
160  global $SYSCONF_DIR;
161  $scheduler_path = "/usr/local/share/fossology/scheduler/agent/fo_scheduler";
162  exec("sudo $scheduler_path -k"); // kill the default scheduler if running
163  $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler";
164  exec("sudo $scheduler_path -k"); // kill the running scheduler
165  exec("sudo $scheduler_path --daemon --reset --verbose=952 -c $SYSCONF_DIR"); // start the scheduler
166  }
167 
171 function add_proxy($proxy_type='http_proxy', $porxy='web-proxy.cce.hp.com:8088') {
172  global $SYSCONF_DIR;
173 
174  $foss_conf = $SYSCONF_DIR."/fossology.conf";
175  exec("sudo sed 's/.$proxy_type.*=.*/$proxy_type=$porxy/' $foss_conf >/tmp/fossology.conf");
176  exec("sudo mv /tmp/fossology.conf $foss_conf");
177 }
178 
187 function get_uploadtree_id($test_dbh, $upload_id) {
188  $sql = "SELECT uploadtree_pk from uploadtree where upload_fk =$upload_id order by uploadtree_pk limit 1;";
189  $result = pg_query($test_dbh, $sql);
190  $row = pg_fetch_assoc($result);
191  $uploadtree_id = $row['uploadtree_pk'];
192  pg_free_result($result);
193  return $uploadtree_id;
194 }
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