1# 2# Library code for git p4 tests 3# 4 5# p4 tests never use the top-level repo; always build/clone into 6# a subdirectory called "$git" 7TEST_NO_CREATE_REPO=NoThanks 8 9# Some operations require multiple attempts to be successful. Define 10# here the maximal retry timeout in seconds. 11RETRY_TIMEOUT=60 12 13# Sometimes p4d seems to hang. Terminate the p4d process automatically after 14# the defined timeout in seconds. 15P4D_TIMEOUT=300 16 17. ./test-lib.sh 18 19if! test_have_prereq PYTHON 20then 21 skip_all='skipping git p4 tests; python not available' 22 test_done 23fi 24( p4 -h&& p4d -h) >/dev/null 2>&1|| { 25 skip_all='skipping git p4 tests; no p4 or p4d' 26 test_done 27} 28 29# On cygwin, the NT version of Perforce can be used. When giving 30# it paths, either on the command-line or in client specifications, 31# be sure to use the native windows form. 32# 33# Older versions of perforce were available compiled natively for 34# cygwin. Those do not accept native windows paths, so make sure 35# not to convert for them. 36native_path() { 37 path="$1"&& 38if test_have_prereq CYGWIN && ! p4 -V|grep-q CYGWIN 39then 40 path=$(cygpath --windows "$path") 41else 42 path=$(test-path-utils real_path "$path") 43fi&& 44echo"$path" 45} 46 47# On Solaris the 'date +%s' function is not supported and therefore we 48# need this replacement. 49# Attention: This function is not safe again against time offset updates 50# at runtime (e.g. via NTP). The 'clock_gettime(CLOCK_MONOTONIC)' 51# function could fix that but it is not in Python until 3.3. 52time_in_seconds() { 53(cd/ &&"$PYTHON_PATH"-c'import time; print(int(time.time()))') 54} 55 56# Try to pick a unique port: guess a large number, then hope 57# no more than one of each test is running. 58# 59# This does not handle the case where somebody else is running the 60# same tests and has chosen the same ports. 61testid=${this_test#t} 62git_p4_test_start=9800 63P4DPORT=$((10669 + ($testid - $git_p4_test_start))) 64 65P4PORT=localhost:$P4DPORT 66P4CLIENT=client 67P4USER=author 68P4EDITOR=true 69unset P4CHARSET 70export P4PORT P4CLIENT P4USER P4EDITOR P4CHARSET 71 72db="$TRASH_DIRECTORY/db" 73cli="$TRASH_DIRECTORY/cli" 74git="$TRASH_DIRECTORY/git" 75pidfile="$TRASH_DIRECTORY/p4d.pid" 76 77# Sometimes "prove" seems to hang on exit because p4d is still running 78cleanup() { 79iftest -f"$pidfile" 80then 81kill -9$(cat "$pidfile")2>/dev/null &&exit255 82fi 83} 84trap cleanup EXIT 85 86# git p4 submit generates a temp file, which will 87# not get cleaned up if the submission fails. Don't 88# clutter up /tmp on the test machine. 89TMPDIR="$TRASH_DIRECTORY" 90export TMPDIR 91 92start_p4d() { 93mkdir-p"$db""$cli""$git"&& 94rm-f"$pidfile"&& 95( 96cd"$db"&& 97{ 98 p4d -q -p$P4DPORT"$@"& 99echo $! >"$pidfile" 100} 101) && 102 103# This gives p4d a long time to start up, as it can be 104# quite slow depending on the machine. Set this environment 105# variable to something smaller to fail faster in, say, 106# an automated test setup. If the p4d process dies, that 107# will be caught with the "kill -0" check below. 108 i=${P4D_START_PATIENCE:-300} 109 pid=$(cat "$pidfile") 110 111 timeout=$(($(time_in_seconds)+$P4D_TIMEOUT)) 112while true 113do 114iftest$(time_in_seconds)-gt$timeout 115then 116kill -9$pid 117exit1 118fi 119sleep1 120done& 121 watchdog_pid=$! 122 123 ready= 124whiletest$i-gt0 125do 126# succeed when p4 client commands start to work 127if p4 info >/dev/null 2>&1 128then 129 ready=true 130break 131fi 132# fail if p4d died 133kill -0$pid2>/dev/null ||break 134echo waiting for p4d to start 135sleep1 136 i=$(( $i - 1 )) 137done 138 139iftest -z"$ready" 140then 141# p4d failed to start 142return1 143fi 144 145# build a p4 user so author@example.com has an entry 146 p4_add_user author 147 148# build a client 149 client_view "//depot/... //client/..."&& 150 151return0 152} 153 154p4_add_user() { 155 name=$1&& 156 p4 user -f -i<<-EOF 157 User:$name 158 Email:$name@example.com 159 FullName: Dr.$name 160 EOF 161} 162 163retry_until_success() { 164 timeout=$(($(time_in_seconds)+$RETRY_TIMEOUT)) 165until"$@"2>/dev/null ||test$(time_in_seconds)-gt$timeout 166do 167sleep1 168done 169} 170 171retry_until_fail() { 172 timeout=$(($(time_in_seconds)+$RETRY_TIMEOUT)) 173until!"$@"2>/dev/null ||test$(time_in_seconds)-gt$timeout 174do 175sleep1 176done 177} 178 179kill_p4d() { 180 pid=$(cat "$pidfile") 181 retry_until_fail kill$pid 182 retry_until_fail kill -9$pid 183# complain if it would not die 184 test_must_fail kill$pid>/dev/null 2>&1&& 185rm-rf"$db""$cli""$pidfile"&& 186 retry_until_fail kill -9$watchdog_pid 187} 188 189cleanup_git() { 190 retry_until_success rm-r"$git" 191 test_must_fail test -d"$git"&& 192 retry_until_success mkdir"$git" 193} 194 195marshal_dump() { 196 what=$1&& 197 line=${2:-1}&& 198cat>"$TRASH_DIRECTORY/marshal-dump.py"<<-EOF && 199 import marshal 200 import sys 201 instream = getattr(sys.stdin, 'buffer', sys.stdin) 202 for i in range($line): 203 d = marshal.load(instream) 204 print(d[b'$what'].decode('utf-8')) 205 EOF 206"$PYTHON_PATH""$TRASH_DIRECTORY/marshal-dump.py" 207} 208 209# 210# Construct a client with this list of View lines 211# 212client_view() { 213( 214cat<<-EOF && 215 Client:$P4CLIENT 216 Description:$P4CLIENT 217 Root:$cli 218 AltRoots:$(native_path "$cli") 219 LineEnd: unix 220 View: 221 EOF 222printf"\t%s\n""$@" 223) | p4 client -i 224} 225 226is_cli_file_writeable() { 227# cygwin version of p4 does not set read-only attr, 228# will be marked 444 but -w is true 229file="$1"&& 230if test_have_prereq CYGWIN && p4 -V|grep-q CYGWIN 231then 232 stat=$(stat --format=%a "$file")&& 233test$stat=644 234else 235test -w"$file" 236fi 237}