blob: b3be3ba011a71ce11f11901c8472cb25ec21133a [file] [log] [blame]
Pete Wyckofffc002332011-08-22 22:20:33 -04001#
Pete Wyckoff6ab1d762012-04-08 20:18:02 -04002# Library code for git p4 tests
Pete Wyckofffc002332011-08-22 22:20:33 -04003#
4
Pete Wyckoffc88015a2012-06-27 08:00:57 -04005# p4 tests never use the top-level repo; always build/clone into
6# a subdirectory called "$git"
7TEST_NO_CREATE_REPO=NoThanks
8
Lars Schneider23aee412015-11-19 09:58:08 +01009# Some operations require multiple attempts to be successful. Define
10# here the maximal retry timeout in seconds.
11RETRY_TIMEOUT=60
12
Lars Schneider842adde2015-11-19 09:58:09 +010013# Sometimes p4d seems to hang. Terminate the p4d process automatically after
14# the defined timeout in seconds.
15P4D_TIMEOUT=300
16
Pete Wyckofffc002332011-08-22 22:20:33 -040017. ./test-lib.sh
18
Pete Wyckoffcfa96492013-01-26 22:11:11 -050019if ! test_have_prereq PYTHON
20then
Pete Wyckoff6ab1d762012-04-08 20:18:02 -040021 skip_all='skipping git p4 tests; python not available'
Pete Wyckofffc002332011-08-22 22:20:33 -040022 test_done
23fi
24( p4 -h && p4d -h ) >/dev/null 2>&1 || {
Pete Wyckoff6ab1d762012-04-08 20:18:02 -040025 skip_all='skipping git p4 tests; no p4 or p4d'
Pete Wyckofffc002332011-08-22 22:20:33 -040026 test_done
27}
28
Pete Wyckoffcfa96492013-01-26 22:11:11 -050029# 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.
Jan Duroveca98772c2016-04-19 19:49:41 +000036native_path () {
Pete Wyckoffcfa96492013-01-26 22:11:11 -050037 path="$1" &&
38 if test_have_prereq CYGWIN && ! p4 -V | grep -q CYGWIN
39 then
40 path=$(cygpath --windows "$path")
41 else
Nguyễn Thái Ngọc Duyb8d5cf42018-03-24 08:44:49 +010042 path=$(test-tool path-utils real_path "$path")
Pete Wyckoffcfa96492013-01-26 22:11:11 -050043 fi &&
44 echo "$path"
45}
46
Lars Schneider23aee412015-11-19 09:58:08 +010047# 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.
Jan Duroveca98772c2016-04-19 19:49:41 +000052time_in_seconds () {
Luke Diamand1fb3fb42016-04-26 08:51:01 +010053 (cd / && "$PYTHON_PATH" -c 'import time; print(int(time.time()))')
Lars Schneider23aee412015-11-19 09:58:08 +010054}
55
SZEDER Gáborfa840582019-01-05 02:08:58 +010056test_set_port P4DPORT
Pete Wyckofffc002332011-08-22 22:20:33 -040057
Pete Wyckoff8c291352012-09-09 16:16:02 -040058P4PORT=localhost:$P4DPORT
59P4CLIENT=client
Pete Wyckoff0055b562014-01-21 18:16:43 -050060P4USER=author
Pete Wyckoff0cf1b722014-01-21 18:16:44 -050061P4EDITOR=true
kazuki saitoh79946032013-08-10 16:15:12 -040062unset P4CHARSET
Pete Wyckoff0055b562014-01-21 18:16:43 -050063export P4PORT P4CLIENT P4USER P4EDITOR P4CHARSET
Pete Wyckofffc002332011-08-22 22:20:33 -040064
65db="$TRASH_DIRECTORY/db"
Pete Wyckoffcfa96492013-01-26 22:11:11 -050066cli="$TRASH_DIRECTORY/cli"
Pete Wyckofffc002332011-08-22 22:20:33 -040067git="$TRASH_DIRECTORY/git"
68pidfile="$TRASH_DIRECTORY/p4d.pid"
69
Lars Schneiderdfe90e82015-11-19 09:58:10 +010070# Sometimes "prove" seems to hang on exit because p4d is still running
Jan Duroveca98772c2016-04-19 19:49:41 +000071cleanup () {
Lars Schneiderdfe90e82015-11-19 09:58:10 +010072 if test -f "$pidfile"
73 then
74 kill -9 $(cat "$pidfile") 2>/dev/null && exit 255
75 fi
76}
77trap cleanup EXIT
78
Pete Wyckoff0cf1b722014-01-21 18:16:44 -050079# git p4 submit generates a temp file, which will
80# not get cleaned up if the submission fails. Don't
81# clutter up /tmp on the test machine.
82TMPDIR="$TRASH_DIRECTORY"
83export TMPDIR
84
Jan Duroveca98772c2016-04-19 19:49:41 +000085start_p4d () {
Pete Wyckofffc002332011-08-22 22:20:33 -040086 mkdir -p "$db" "$cli" "$git" &&
Pete Wyckofff89f35a2012-06-27 22:48:07 -040087 rm -f "$pidfile" &&
Pete Wyckofffc002332011-08-22 22:20:33 -040088 (
Pete Wyckoff6492a102013-01-26 22:11:10 -050089 cd "$db" &&
90 {
Luke Diamande80967b2015-04-28 10:08:01 +010091 p4d -q -p $P4DPORT "$@" &
Pete Wyckoff6492a102013-01-26 22:11:10 -050092 echo $! >"$pidfile"
93 }
Pete Wyckofffc002332011-08-22 22:20:33 -040094 ) &&
Pete Wyckofff89f35a2012-06-27 22:48:07 -040095
96 # This gives p4d a long time to start up, as it can be
97 # quite slow depending on the machine. Set this environment
98 # variable to something smaller to fail faster in, say,
99 # an automated test setup. If the p4d process dies, that
100 # will be caught with the "kill -0" check below.
101 i=${P4D_START_PATIENCE:-300}
102 pid=$(cat "$pidfile")
Lars Schneider842adde2015-11-19 09:58:09 +0100103
104 timeout=$(($(time_in_seconds) + $P4D_TIMEOUT))
105 while true
106 do
107 if test $(time_in_seconds) -gt $timeout
108 then
109 kill -9 $pid
110 exit 1
111 fi
112 sleep 1
113 done &
114 watchdog_pid=$!
115
Pete Wyckofff89f35a2012-06-27 22:48:07 -0400116 ready=
117 while test $i -gt 0
118 do
119 # succeed when p4 client commands start to work
120 if p4 info >/dev/null 2>&1
121 then
122 ready=true
123 break
124 fi
125 # fail if p4d died
126 kill -0 $pid 2>/dev/null || break
127 echo waiting for p4d to start
Pete Wyckofffc002332011-08-22 22:20:33 -0400128 sleep 1
Pete Wyckofff89f35a2012-06-27 22:48:07 -0400129 i=$(( $i - 1 ))
130 done
131
132 if test -z "$ready"
133 then
134 # p4d failed to start
135 return 1
136 fi
137
Pete Wyckoff0055b562014-01-21 18:16:43 -0500138 # build a p4 user so author@example.com has an entry
139 p4_add_user author
140
Pete Wyckofff89f35a2012-06-27 22:48:07 -0400141 # build a client
Pete Wyckoffdaa38f42013-01-26 22:11:07 -0500142 client_view "//depot/... //client/..." &&
143
Pete Wyckofff89f35a2012-06-27 22:48:07 -0400144 return 0
Pete Wyckofffc002332011-08-22 22:20:33 -0400145}
146
Jan Duroveca98772c2016-04-19 19:49:41 +0000147p4_add_user () {
Pete Wyckoff0055b562014-01-21 18:16:43 -0500148 name=$1 &&
149 p4 user -f -i <<-EOF
150 User: $name
151 Email: $name@example.com
152 FullName: Dr. $name
153 EOF
154}
155
Jan Durovec26e6a272016-04-19 19:49:41 +0000156p4_add_job () {
157 p4 job -f -i <<-EOF
158 Job: $1
159 Status: open
160 User: dummy
161 Description:
162 EOF
163}
164
Jan Duroveca98772c2016-04-19 19:49:41 +0000165retry_until_success () {
Lars Schneider23aee412015-11-19 09:58:08 +0100166 timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT))
167 until "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout
168 do
169 sleep 1
170 done
171}
172
Jan Duroveca98772c2016-04-19 19:49:41 +0000173retry_until_fail () {
Lars Schneider23aee412015-11-19 09:58:08 +0100174 timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT))
175 until ! "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout
176 do
177 sleep 1
178 done
179}
180
Jan Duroveca98772c2016-04-19 19:49:41 +0000181kill_p4d () {
Pete Wyckofffc002332011-08-22 22:20:33 -0400182 pid=$(cat "$pidfile")
Lars Schneider23aee412015-11-19 09:58:08 +0100183 retry_until_fail kill $pid
184 retry_until_fail kill -9 $pid
Pete Wyckofffc002332011-08-22 22:20:33 -0400185 # complain if it would not die
186 test_must_fail kill $pid >/dev/null 2>&1 &&
Lars Schneider842adde2015-11-19 09:58:09 +0100187 rm -rf "$db" "$cli" "$pidfile" &&
188 retry_until_fail kill -9 $watchdog_pid
Pete Wyckofffc002332011-08-22 22:20:33 -0400189}
190
Jan Duroveca98772c2016-04-19 19:49:41 +0000191cleanup_git () {
Lars Schneider23aee412015-11-19 09:58:08 +0100192 retry_until_success rm -r "$git"
193 test_must_fail test -d "$git" &&
194 retry_until_success mkdir "$git"
Pete Wyckofffc002332011-08-22 22:20:33 -0400195}
Pete Wyckoff798d5982012-07-04 09:34:19 -0400196
Jan Duroveca98772c2016-04-19 19:49:41 +0000197marshal_dump () {
Pete Wyckoff798d5982012-07-04 09:34:19 -0400198 what=$1 &&
199 line=${2:-1} &&
200 cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF &&
201 import marshal
202 import sys
Luke Diamand84096812016-04-26 08:51:00 +0100203 instream = getattr(sys.stdin, 'buffer', sys.stdin)
Pete Wyckoff798d5982012-07-04 09:34:19 -0400204 for i in range($line):
Luke Diamand84096812016-04-26 08:51:00 +0100205 d = marshal.load(instream)
206 print(d[b'$what'].decode('utf-8'))
Pete Wyckoff798d5982012-07-04 09:34:19 -0400207 EOF
208 "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py"
209}
Pete Wyckoffd2018292012-08-11 12:55:00 -0400210
211#
212# Construct a client with this list of View lines
213#
Jan Duroveca98772c2016-04-19 19:49:41 +0000214client_view () {
Pete Wyckoffd2018292012-08-11 12:55:00 -0400215 (
216 cat <<-EOF &&
Pete Wyckoff50038ba2013-01-26 22:11:09 -0500217 Client: $P4CLIENT
218 Description: $P4CLIENT
Pete Wyckoffd2018292012-08-11 12:55:00 -0400219 Root: $cli
Pete Wyckoffcfa96492013-01-26 22:11:11 -0500220 AltRoots: $(native_path "$cli")
Pete Wyckoffe93f8692013-01-26 22:11:15 -0500221 LineEnd: unix
Pete Wyckoffd2018292012-08-11 12:55:00 -0400222 View:
223 EOF
Pete Wyckoff61125412013-01-26 22:11:08 -0500224 printf "\t%s\n" "$@"
Pete Wyckoffd2018292012-08-11 12:55:00 -0400225 ) | p4 client -i
226}
Pete Wyckoffe9df0f92013-01-26 22:11:17 -0500227
Jan Duroveca98772c2016-04-19 19:49:41 +0000228is_cli_file_writeable () {
Pete Wyckoffe9df0f92013-01-26 22:11:17 -0500229 # cygwin version of p4 does not set read-only attr,
230 # will be marked 444 but -w is true
231 file="$1" &&
232 if test_have_prereq CYGWIN && p4 -V | grep -q CYGWIN
233 then
234 stat=$(stat --format=%a "$file") &&
235 test $stat = 644
236 else
237 test -w "$file"
238 fi
239}