Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 1 | # |
Pete Wyckoff | 6ab1d76 | 2012-04-08 20:18:02 -0400 | [diff] [blame] | 2 | # Library code for git p4 tests |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 3 | # |
| 4 | |
Pete Wyckoff | c88015a | 2012-06-27 08:00:57 -0400 | [diff] [blame] | 5 | # p4 tests never use the top-level repo; always build/clone into |
| 6 | # a subdirectory called "$git" |
| 7 | TEST_NO_CREATE_REPO=NoThanks |
| 8 | |
Lars Schneider | 23aee41 | 2015-11-19 09:58:08 +0100 | [diff] [blame] | 9 | # Some operations require multiple attempts to be successful. Define |
| 10 | # here the maximal retry timeout in seconds. |
| 11 | RETRY_TIMEOUT=60 |
| 12 | |
Lars Schneider | 842adde | 2015-11-19 09:58:09 +0100 | [diff] [blame] | 13 | # Sometimes p4d seems to hang. Terminate the p4d process automatically after |
| 14 | # the defined timeout in seconds. |
| 15 | P4D_TIMEOUT=300 |
| 16 | |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 17 | . ./test-lib.sh |
| 18 | |
Pete Wyckoff | cfa9649 | 2013-01-26 22:11:11 -0500 | [diff] [blame] | 19 | if ! test_have_prereq PYTHON |
| 20 | then |
Pete Wyckoff | 6ab1d76 | 2012-04-08 20:18:02 -0400 | [diff] [blame] | 21 | skip_all='skipping git p4 tests; python not available' |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 22 | test_done |
| 23 | fi |
| 24 | ( p4 -h && p4d -h ) >/dev/null 2>&1 || { |
Pete Wyckoff | 6ab1d76 | 2012-04-08 20:18:02 -0400 | [diff] [blame] | 25 | skip_all='skipping git p4 tests; no p4 or p4d' |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 26 | test_done |
| 27 | } |
| 28 | |
Pete Wyckoff | cfa9649 | 2013-01-26 22:11:11 -0500 | [diff] [blame] | 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. |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 36 | native_path () { |
Pete Wyckoff | cfa9649 | 2013-01-26 22:11:11 -0500 | [diff] [blame] | 37 | 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 Duy | b8d5cf4 | 2018-03-24 08:44:49 +0100 | [diff] [blame] | 42 | path=$(test-tool path-utils real_path "$path") |
Pete Wyckoff | cfa9649 | 2013-01-26 22:11:11 -0500 | [diff] [blame] | 43 | fi && |
| 44 | echo "$path" |
| 45 | } |
| 46 | |
Lars Schneider | 23aee41 | 2015-11-19 09:58:08 +0100 | [diff] [blame] | 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. |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 52 | time_in_seconds () { |
Luke Diamand | 1fb3fb4 | 2016-04-26 08:51:01 +0100 | [diff] [blame] | 53 | (cd / && "$PYTHON_PATH" -c 'import time; print(int(time.time()))') |
Lars Schneider | 23aee41 | 2015-11-19 09:58:08 +0100 | [diff] [blame] | 54 | } |
| 55 | |
SZEDER Gábor | fa84058 | 2019-01-05 02:08:58 +0100 | [diff] [blame] | 56 | test_set_port P4DPORT |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 57 | |
Pete Wyckoff | 8c29135 | 2012-09-09 16:16:02 -0400 | [diff] [blame] | 58 | P4PORT=localhost:$P4DPORT |
| 59 | P4CLIENT=client |
Pete Wyckoff | 0055b56 | 2014-01-21 18:16:43 -0500 | [diff] [blame] | 60 | P4USER=author |
Pete Wyckoff | 0cf1b72 | 2014-01-21 18:16:44 -0500 | [diff] [blame] | 61 | P4EDITOR=true |
kazuki saitoh | 7994603 | 2013-08-10 16:15:12 -0400 | [diff] [blame] | 62 | unset P4CHARSET |
Pete Wyckoff | 0055b56 | 2014-01-21 18:16:43 -0500 | [diff] [blame] | 63 | export P4PORT P4CLIENT P4USER P4EDITOR P4CHARSET |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 64 | |
| 65 | db="$TRASH_DIRECTORY/db" |
Pete Wyckoff | cfa9649 | 2013-01-26 22:11:11 -0500 | [diff] [blame] | 66 | cli="$TRASH_DIRECTORY/cli" |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 67 | git="$TRASH_DIRECTORY/git" |
| 68 | pidfile="$TRASH_DIRECTORY/p4d.pid" |
| 69 | |
Lars Schneider | dfe90e8 | 2015-11-19 09:58:10 +0100 | [diff] [blame] | 70 | # Sometimes "prove" seems to hang on exit because p4d is still running |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 71 | cleanup () { |
Lars Schneider | dfe90e8 | 2015-11-19 09:58:10 +0100 | [diff] [blame] | 72 | if test -f "$pidfile" |
| 73 | then |
| 74 | kill -9 $(cat "$pidfile") 2>/dev/null && exit 255 |
| 75 | fi |
| 76 | } |
Lars Schneider | dfe90e8 | 2015-11-19 09:58:10 +0100 | [diff] [blame] | 77 | |
Pete Wyckoff | 0cf1b72 | 2014-01-21 18:16:44 -0500 | [diff] [blame] | 78 | # git p4 submit generates a temp file, which will |
| 79 | # not get cleaned up if the submission fails. Don't |
| 80 | # clutter up /tmp on the test machine. |
| 81 | TMPDIR="$TRASH_DIRECTORY" |
| 82 | export TMPDIR |
| 83 | |
Johannes Schindelin | 99e37c2 | 2019-03-13 13:24:15 +0100 | [diff] [blame^] | 84 | registered_stop_p4d_atexit_handler= |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 85 | start_p4d () { |
Johannes Schindelin | 99e37c2 | 2019-03-13 13:24:15 +0100 | [diff] [blame^] | 86 | # One of the test scripts stops and then re-starts p4d. |
| 87 | # Don't register and then run the same atexit handlers several times. |
| 88 | if test -z "$registered_stop_p4d_atexit_handler" |
| 89 | then |
| 90 | test_atexit 'kill_p4d; cleanup' |
| 91 | registered_stop_p4d_atexit_handler=AlreadyDone |
| 92 | fi |
| 93 | |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 94 | mkdir -p "$db" "$cli" "$git" && |
Pete Wyckoff | f89f35a | 2012-06-27 22:48:07 -0400 | [diff] [blame] | 95 | rm -f "$pidfile" && |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 96 | ( |
Pete Wyckoff | 6492a10 | 2013-01-26 22:11:10 -0500 | [diff] [blame] | 97 | cd "$db" && |
| 98 | { |
Luke Diamand | e80967b | 2015-04-28 10:08:01 +0100 | [diff] [blame] | 99 | p4d -q -p $P4DPORT "$@" & |
Pete Wyckoff | 6492a10 | 2013-01-26 22:11:10 -0500 | [diff] [blame] | 100 | echo $! >"$pidfile" |
| 101 | } |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 102 | ) && |
Pete Wyckoff | f89f35a | 2012-06-27 22:48:07 -0400 | [diff] [blame] | 103 | |
| 104 | # This gives p4d a long time to start up, as it can be |
| 105 | # quite slow depending on the machine. Set this environment |
| 106 | # variable to something smaller to fail faster in, say, |
| 107 | # an automated test setup. If the p4d process dies, that |
| 108 | # will be caught with the "kill -0" check below. |
| 109 | i=${P4D_START_PATIENCE:-300} |
| 110 | pid=$(cat "$pidfile") |
Lars Schneider | 842adde | 2015-11-19 09:58:09 +0100 | [diff] [blame] | 111 | |
| 112 | timeout=$(($(time_in_seconds) + $P4D_TIMEOUT)) |
| 113 | while true |
| 114 | do |
| 115 | if test $(time_in_seconds) -gt $timeout |
| 116 | then |
| 117 | kill -9 $pid |
| 118 | exit 1 |
| 119 | fi |
| 120 | sleep 1 |
| 121 | done & |
| 122 | watchdog_pid=$! |
| 123 | |
Pete Wyckoff | f89f35a | 2012-06-27 22:48:07 -0400 | [diff] [blame] | 124 | ready= |
| 125 | while test $i -gt 0 |
| 126 | do |
| 127 | # succeed when p4 client commands start to work |
| 128 | if p4 info >/dev/null 2>&1 |
| 129 | then |
| 130 | ready=true |
| 131 | break |
| 132 | fi |
| 133 | # fail if p4d died |
| 134 | kill -0 $pid 2>/dev/null || break |
| 135 | echo waiting for p4d to start |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 136 | sleep 1 |
Pete Wyckoff | f89f35a | 2012-06-27 22:48:07 -0400 | [diff] [blame] | 137 | i=$(( $i - 1 )) |
| 138 | done |
| 139 | |
| 140 | if test -z "$ready" |
| 141 | then |
| 142 | # p4d failed to start |
| 143 | return 1 |
| 144 | fi |
| 145 | |
Pete Wyckoff | 0055b56 | 2014-01-21 18:16:43 -0500 | [diff] [blame] | 146 | # build a p4 user so author@example.com has an entry |
| 147 | p4_add_user author |
| 148 | |
Pete Wyckoff | f89f35a | 2012-06-27 22:48:07 -0400 | [diff] [blame] | 149 | # build a client |
Pete Wyckoff | daa38f4 | 2013-01-26 22:11:07 -0500 | [diff] [blame] | 150 | client_view "//depot/... //client/..." && |
| 151 | |
Pete Wyckoff | f89f35a | 2012-06-27 22:48:07 -0400 | [diff] [blame] | 152 | return 0 |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 155 | p4_add_user () { |
Pete Wyckoff | 0055b56 | 2014-01-21 18:16:43 -0500 | [diff] [blame] | 156 | name=$1 && |
| 157 | p4 user -f -i <<-EOF |
| 158 | User: $name |
| 159 | Email: $name@example.com |
| 160 | FullName: Dr. $name |
| 161 | EOF |
| 162 | } |
| 163 | |
Jan Durovec | 26e6a27 | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 164 | p4_add_job () { |
| 165 | p4 job -f -i <<-EOF |
| 166 | Job: $1 |
| 167 | Status: open |
| 168 | User: dummy |
| 169 | Description: |
| 170 | EOF |
| 171 | } |
| 172 | |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 173 | retry_until_success () { |
Lars Schneider | 23aee41 | 2015-11-19 09:58:08 +0100 | [diff] [blame] | 174 | 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 Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 181 | retry_until_fail () { |
Lars Schneider | 23aee41 | 2015-11-19 09:58:08 +0100 | [diff] [blame] | 182 | timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT)) |
| 183 | until ! "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout |
| 184 | do |
| 185 | sleep 1 |
| 186 | done |
| 187 | } |
| 188 | |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 189 | kill_p4d () { |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 190 | pid=$(cat "$pidfile") |
Lars Schneider | 23aee41 | 2015-11-19 09:58:08 +0100 | [diff] [blame] | 191 | retry_until_fail kill $pid |
| 192 | retry_until_fail kill -9 $pid |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 193 | # complain if it would not die |
| 194 | test_must_fail kill $pid >/dev/null 2>&1 && |
Lars Schneider | 842adde | 2015-11-19 09:58:09 +0100 | [diff] [blame] | 195 | rm -rf "$db" "$cli" "$pidfile" && |
| 196 | retry_until_fail kill -9 $watchdog_pid |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 199 | cleanup_git () { |
Lars Schneider | 23aee41 | 2015-11-19 09:58:08 +0100 | [diff] [blame] | 200 | retry_until_success rm -r "$git" |
| 201 | test_must_fail test -d "$git" && |
| 202 | retry_until_success mkdir "$git" |
Pete Wyckoff | fc00233 | 2011-08-22 22:20:33 -0400 | [diff] [blame] | 203 | } |
Pete Wyckoff | 798d598 | 2012-07-04 09:34:19 -0400 | [diff] [blame] | 204 | |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 205 | marshal_dump () { |
Pete Wyckoff | 798d598 | 2012-07-04 09:34:19 -0400 | [diff] [blame] | 206 | what=$1 && |
| 207 | line=${2:-1} && |
| 208 | cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF && |
| 209 | import marshal |
| 210 | import sys |
Luke Diamand | 8409681 | 2016-04-26 08:51:00 +0100 | [diff] [blame] | 211 | instream = getattr(sys.stdin, 'buffer', sys.stdin) |
Pete Wyckoff | 798d598 | 2012-07-04 09:34:19 -0400 | [diff] [blame] | 212 | for i in range($line): |
Luke Diamand | 8409681 | 2016-04-26 08:51:00 +0100 | [diff] [blame] | 213 | d = marshal.load(instream) |
| 214 | print(d[b'$what'].decode('utf-8')) |
Pete Wyckoff | 798d598 | 2012-07-04 09:34:19 -0400 | [diff] [blame] | 215 | EOF |
| 216 | "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py" |
| 217 | } |
Pete Wyckoff | d201829 | 2012-08-11 12:55:00 -0400 | [diff] [blame] | 218 | |
| 219 | # |
| 220 | # Construct a client with this list of View lines |
| 221 | # |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 222 | client_view () { |
Pete Wyckoff | d201829 | 2012-08-11 12:55:00 -0400 | [diff] [blame] | 223 | ( |
| 224 | cat <<-EOF && |
Pete Wyckoff | 50038ba | 2013-01-26 22:11:09 -0500 | [diff] [blame] | 225 | Client: $P4CLIENT |
| 226 | Description: $P4CLIENT |
Pete Wyckoff | d201829 | 2012-08-11 12:55:00 -0400 | [diff] [blame] | 227 | Root: $cli |
Pete Wyckoff | cfa9649 | 2013-01-26 22:11:11 -0500 | [diff] [blame] | 228 | AltRoots: $(native_path "$cli") |
Pete Wyckoff | e93f869 | 2013-01-26 22:11:15 -0500 | [diff] [blame] | 229 | LineEnd: unix |
Pete Wyckoff | d201829 | 2012-08-11 12:55:00 -0400 | [diff] [blame] | 230 | View: |
| 231 | EOF |
Pete Wyckoff | 6112541 | 2013-01-26 22:11:08 -0500 | [diff] [blame] | 232 | printf "\t%s\n" "$@" |
Pete Wyckoff | d201829 | 2012-08-11 12:55:00 -0400 | [diff] [blame] | 233 | ) | p4 client -i |
| 234 | } |
Pete Wyckoff | e9df0f9 | 2013-01-26 22:11:17 -0500 | [diff] [blame] | 235 | |
Jan Durovec | a98772c | 2016-04-19 19:49:41 +0000 | [diff] [blame] | 236 | is_cli_file_writeable () { |
Pete Wyckoff | e9df0f9 | 2013-01-26 22:11:17 -0500 | [diff] [blame] | 237 | # cygwin version of p4 does not set read-only attr, |
| 238 | # will be marked 444 but -w is true |
| 239 | file="$1" && |
| 240 | if test_have_prereq CYGWIN && p4 -V | grep -q CYGWIN |
| 241 | then |
| 242 | stat=$(stat --format=%a "$file") && |
| 243 | test $stat = 644 |
| 244 | else |
| 245 | test -w "$file" |
| 246 | fi |
| 247 | } |