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 19 /** 20 * \file test library 21 */ 22 23 /** 24 * \brief create DB 25 */ 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 45 /** 46 * \brief drop db 47 */ 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 56 /** 57 * \brief get upload id 58 * 59 * \param $upload_info - The string to search in. 60 * 61 * \return upload Id, false on failure. 62 */ 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 73 /** 74 * connect to a database 75 * \param $fossology_testconfig the testing SYSCONFDIR provided by create_test_database.pl 76 * 77 * \return A valid postgres database handle, or false if connection cannot be made 78 */ 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 94 /** 95 * \brief check if the agent you specify is complete 96 * 97 * \param $agent_name agent name, such as: ununpack, nomos, etc 98 * \param $upload_id upload id 99 * 100 * \return 1 as complete sucessfully, other as failed or not scheduled 101 */ 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 116 /** 117 * \brief add a admin user, default fossy/fosssy 118 */ 119 function add_user($user='fossy', $password='fossy') { 120 global $PG_CONN; 121 /* User does not exist. Create it. */ 122 $options = array('cost' => 10); 123 $Hash = password_hash($password, PASSWORD_DEFAULT, $options); 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 139 /** 140 * \brief replace default repo with new repo 141 */ 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 157 /** 158 * \brief at the end of this testing, stop the testing scheduler 159 */ 160 function stop_scheduler() { 161 global $SYSCONF_DIR; 162 /** stop the scheduler in this test */ 163 $scheduler_path = "$SYSCONF_DIR/mods-enabled/scheduler/agent/fo_scheduler"; 164 exec("sudo $scheduler_path -k"); // kill the running scheduler 165 } 166 167 /** 168 * \brief stop the running scheduler and start new schduler with new sysconfdir 169 */ 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 179/** 180 * \brief add proxy for testing 181 */ 182function 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 190/** 191 * \brief get primary uploadtree_pk 192 * 193 * \param $upload_id - upload_id 194 * \param $test_dbh - db connection 195 * 196 * \return the first uploadtree id of this upload 197 */ 198function 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?> 208