blob: 5aff2abe8b5490156e7745beb5ad01d9cacc0d58 [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
SZEDER Gáborfa840582019-01-05 02:08:58 +010047test_set_port P4DPORT
Pete Wyckofffc002332011-08-22 22:20:33 -040048
Pete Wyckoff8c291352012-09-09 16:16:02 -040049P4PORT=localhost:$P4DPORT
50P4CLIENT=client
Pete Wyckoff0055b562014-01-21 18:16:43 -050051P4USER=author
Pete Wyckoff0cf1b722014-01-21 18:16:44 -050052P4EDITOR=true
kazuki saitoh79946032013-08-10 16:15:12 -040053unset P4CHARSET
Pete Wyckoff0055b562014-01-21 18:16:43 -050054export P4PORT P4CLIENT P4USER P4EDITOR P4CHARSET
Pete Wyckofffc002332011-08-22 22:20:33 -040055
56db="$TRASH_DIRECTORY/db"
Pete Wyckoffcfa96492013-01-26 22:11:11 -050057cli="$TRASH_DIRECTORY/cli"
Pete Wyckofffc002332011-08-22 22:20:33 -040058git="$TRASH_DIRECTORY/git"
59pidfile="$TRASH_DIRECTORY/p4d.pid"
60
SZEDER Gábor07353d92019-03-13 13:24:16 +010061stop_p4d_and_watchdog () {
62 kill -9 $p4d_pid $watchdog_pid
Lars Schneiderdfe90e82015-11-19 09:58:10 +010063}
Lars Schneiderdfe90e82015-11-19 09:58:10 +010064
Pete Wyckoff0cf1b722014-01-21 18:16:44 -050065# git p4 submit generates a temp file, which will
66# not get cleaned up if the submission fails. Don't
67# clutter up /tmp on the test machine.
68TMPDIR="$TRASH_DIRECTORY"
69export TMPDIR
70
Johannes Schindelin99e37c22019-03-13 13:24:15 +010071registered_stop_p4d_atexit_handler=
Jan Duroveca98772c2016-04-19 19:49:41 +000072start_p4d () {
Johannes Schindelin99e37c22019-03-13 13:24:15 +010073 # One of the test scripts stops and then re-starts p4d.
74 # Don't register and then run the same atexit handlers several times.
75 if test -z "$registered_stop_p4d_atexit_handler"
76 then
SZEDER Gábor07353d92019-03-13 13:24:16 +010077 test_atexit 'stop_p4d_and_watchdog'
Johannes Schindelin99e37c22019-03-13 13:24:15 +010078 registered_stop_p4d_atexit_handler=AlreadyDone
79 fi
80
Pete Wyckofffc002332011-08-22 22:20:33 -040081 mkdir -p "$db" "$cli" "$git" &&
Pete Wyckofff89f35a2012-06-27 22:48:07 -040082 rm -f "$pidfile" &&
Pete Wyckofffc002332011-08-22 22:20:33 -040083 (
Pete Wyckoff6492a102013-01-26 22:11:10 -050084 cd "$db" &&
85 {
Luke Diamande80967b2015-04-28 10:08:01 +010086 p4d -q -p $P4DPORT "$@" &
Pete Wyckoff6492a102013-01-26 22:11:10 -050087 echo $! >"$pidfile"
88 }
Pete Wyckofffc002332011-08-22 22:20:33 -040089 ) &&
SZEDER Gábor07353d92019-03-13 13:24:16 +010090 p4d_pid=$(cat "$pidfile")
Pete Wyckofff89f35a2012-06-27 22:48:07 -040091
92 # This gives p4d a long time to start up, as it can be
93 # quite slow depending on the machine. Set this environment
94 # variable to something smaller to fail faster in, say,
95 # an automated test setup. If the p4d process dies, that
96 # will be caught with the "kill -0" check below.
97 i=${P4D_START_PATIENCE:-300}
Lars Schneider842adde2015-11-19 09:58:09 +010098
SZEDER Gábor0e67c322019-03-13 13:24:17 +010099 nr_tries_left=$P4D_TIMEOUT
Lars Schneider842adde2015-11-19 09:58:09 +0100100 while true
101 do
SZEDER Gábor0e67c322019-03-13 13:24:17 +0100102 if test $nr_tries_left -eq 0
Lars Schneider842adde2015-11-19 09:58:09 +0100103 then
SZEDER Gábor07353d92019-03-13 13:24:16 +0100104 kill -9 $p4d_pid
Lars Schneider842adde2015-11-19 09:58:09 +0100105 exit 1
106 fi
107 sleep 1
SZEDER Gábor0e67c322019-03-13 13:24:17 +0100108 nr_tries_left=$(($nr_tries_left - 1))
SZEDER Gábor3fae7ad2019-03-13 13:24:18 +0100109 done 2>/dev/null 4>&2 &
Lars Schneider842adde2015-11-19 09:58:09 +0100110 watchdog_pid=$!
111
Pete Wyckofff89f35a2012-06-27 22:48:07 -0400112 ready=
113 while test $i -gt 0
114 do
115 # succeed when p4 client commands start to work
116 if p4 info >/dev/null 2>&1
117 then
118 ready=true
119 break
120 fi
121 # fail if p4d died
SZEDER Gábor07353d92019-03-13 13:24:16 +0100122 kill -0 $p4d_pid 2>/dev/null || break
Pete Wyckofff89f35a2012-06-27 22:48:07 -0400123 echo waiting for p4d to start
Pete Wyckofffc002332011-08-22 22:20:33 -0400124 sleep 1
Pete Wyckofff89f35a2012-06-27 22:48:07 -0400125 i=$(( $i - 1 ))
126 done
127
128 if test -z "$ready"
129 then
130 # p4d failed to start
131 return 1
132 fi
133
Pete Wyckoff0055b562014-01-21 18:16:43 -0500134 # build a p4 user so author@example.com has an entry
135 p4_add_user author
136
Pete Wyckofff89f35a2012-06-27 22:48:07 -0400137 # build a client
Pete Wyckoffdaa38f42013-01-26 22:11:07 -0500138 client_view "//depot/... //client/..." &&
139
Pete Wyckofff89f35a2012-06-27 22:48:07 -0400140 return 0
Pete Wyckofffc002332011-08-22 22:20:33 -0400141}
142
Jan Duroveca98772c2016-04-19 19:49:41 +0000143p4_add_user () {
Pete Wyckoff0055b562014-01-21 18:16:43 -0500144 name=$1 &&
145 p4 user -f -i <<-EOF
146 User: $name
147 Email: $name@example.com
148 FullName: Dr. $name
149 EOF
150}
151
Jan Durovec26e6a272016-04-19 19:49:41 +0000152p4_add_job () {
153 p4 job -f -i <<-EOF
154 Job: $1
155 Status: open
156 User: dummy
157 Description:
158 EOF
159}
160
Jan Duroveca98772c2016-04-19 19:49:41 +0000161retry_until_success () {
SZEDER Gábor0e67c322019-03-13 13:24:17 +0100162 nr_tries_left=$RETRY_TIMEOUT
163 until "$@" 2>/dev/null || test $nr_tries_left -eq 0
Lars Schneider23aee412015-11-19 09:58:08 +0100164 do
165 sleep 1
SZEDER Gábor0e67c322019-03-13 13:24:17 +0100166 nr_tries_left=$(($nr_tries_left - 1))
Lars Schneider23aee412015-11-19 09:58:08 +0100167 done
168}
169
SZEDER Gábor07353d92019-03-13 13:24:16 +0100170stop_and_cleanup_p4d () {
171 kill -9 $p4d_pid $watchdog_pid
172 wait $p4d_pid
173 rm -rf "$db" "$cli" "$pidfile"
Pete Wyckofffc002332011-08-22 22:20:33 -0400174}
175
Jan Duroveca98772c2016-04-19 19:49:41 +0000176cleanup_git () {
Lars Schneider23aee412015-11-19 09:58:08 +0100177 retry_until_success rm -r "$git"
Denton Liub8afb902019-12-20 10:15:48 -0800178 test_path_is_missing "$git" &&
Lars Schneider23aee412015-11-19 09:58:08 +0100179 retry_until_success mkdir "$git"
Pete Wyckofffc002332011-08-22 22:20:33 -0400180}
Pete Wyckoff798d5982012-07-04 09:34:19 -0400181
Jan Duroveca98772c2016-04-19 19:49:41 +0000182marshal_dump () {
Pete Wyckoff798d5982012-07-04 09:34:19 -0400183 what=$1 &&
184 line=${2:-1} &&
185 cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF &&
186 import marshal
187 import sys
Luke Diamand84096812016-04-26 08:51:00 +0100188 instream = getattr(sys.stdin, 'buffer', sys.stdin)
Pete Wyckoff798d5982012-07-04 09:34:19 -0400189 for i in range($line):
Luke Diamand84096812016-04-26 08:51:00 +0100190 d = marshal.load(instream)
191 print(d[b'$what'].decode('utf-8'))
Pete Wyckoff798d5982012-07-04 09:34:19 -0400192 EOF
193 "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py"
194}
Pete Wyckoffd2018292012-08-11 12:55:00 -0400195
196#
197# Construct a client with this list of View lines
198#
Jan Duroveca98772c2016-04-19 19:49:41 +0000199client_view () {
Pete Wyckoffd2018292012-08-11 12:55:00 -0400200 (
201 cat <<-EOF &&
Pete Wyckoff50038ba2013-01-26 22:11:09 -0500202 Client: $P4CLIENT
203 Description: $P4CLIENT
Pete Wyckoffd2018292012-08-11 12:55:00 -0400204 Root: $cli
Pete Wyckoffcfa96492013-01-26 22:11:11 -0500205 AltRoots: $(native_path "$cli")
Pete Wyckoffe93f8692013-01-26 22:11:15 -0500206 LineEnd: unix
Pete Wyckoffd2018292012-08-11 12:55:00 -0400207 View:
208 EOF
Pete Wyckoff61125412013-01-26 22:11:08 -0500209 printf "\t%s\n" "$@"
Pete Wyckoffd2018292012-08-11 12:55:00 -0400210 ) | p4 client -i
211}
Pete Wyckoffe9df0f92013-01-26 22:11:17 -0500212
Jan Duroveca98772c2016-04-19 19:49:41 +0000213is_cli_file_writeable () {
Pete Wyckoffe9df0f92013-01-26 22:11:17 -0500214 # cygwin version of p4 does not set read-only attr,
215 # will be marked 444 but -w is true
216 file="$1" &&
217 if test_have_prereq CYGWIN && p4 -V | grep -q CYGWIN
218 then
219 stat=$(stat --format=%a "$file") &&
220 test $stat = 644
221 else
222 test -w "$file"
223 fi
224}