| 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" |
| 7 | TEST_NO_CREATE_REPO=NoThanks |
| 8 | |
| 9 | # Some operations require multiple attempts to be successful. Define |
| 10 | # here the maximal retry timeout in seconds. |
| 11 | RETRY_TIMEOUT=60 |
| 12 | |
| 13 | # Sometimes p4d seems to hang. Terminate the p4d process automatically after |
| 14 | # the defined timeout in seconds. |
| 15 | P4D_TIMEOUT=300 |
| 16 | |
| 17 | . ./test-lib.sh |
| 18 | |
| 19 | if test -n "$NO_P4_TESTS" |
| 20 | then |
| 21 | skip_all='skipping git p4 tests, NO_P4_TESTS defined' |
| 22 | test_done |
| 23 | fi |
| 24 | if ! test_have_prereq PYTHON |
| 25 | then |
| 26 | skip_all='skipping git p4 tests; python not available' |
| 27 | test_done |
| 28 | fi |
| 29 | ( p4 -h && p4d -h ) >/dev/null 2>&1 || { |
| 30 | skip_all='skipping git p4 tests; no p4 or p4d' |
| 31 | test_done |
| 32 | } |
| 33 | |
| 34 | # On cygwin, the NT version of Perforce can be used. When giving |
| 35 | # it paths, either on the command-line or in client specifications, |
| 36 | # be sure to use the native windows form. |
| 37 | # |
| 38 | # Older versions of perforce were available compiled natively for |
| 39 | # cygwin. Those do not accept native windows paths, so make sure |
| 40 | # not to convert for them. |
| 41 | native_path () { |
| 42 | path="$1" && |
| 43 | if test_have_prereq CYGWIN && ! p4 -V | grep -q CYGWIN |
| 44 | then |
| 45 | path=$(cygpath --windows "$path") |
| 46 | else |
| 47 | path=$(test-tool path-utils real_path "$path") |
| 48 | fi && |
| 49 | echo "$path" |
| 50 | } |
| 51 | |
| 52 | test_set_port P4DPORT |
| 53 | |
| 54 | P4PORT=localhost:$P4DPORT |
| 55 | P4CLIENT=client |
| 56 | P4USER=author |
| 57 | P4EDITOR=true |
| 58 | unset P4CHARSET |
| 59 | export P4PORT P4CLIENT P4USER P4EDITOR P4CHARSET |
| 60 | |
| 61 | db="$TRASH_DIRECTORY/db" |
| 62 | cli="$TRASH_DIRECTORY/cli" |
| 63 | git="$TRASH_DIRECTORY/git" |
| 64 | pidfile="$TRASH_DIRECTORY/p4d.pid" |
| 65 | |
| 66 | stop_p4d_and_watchdog () { |
| 67 | kill -9 $p4d_pid $watchdog_pid |
| 68 | wait $p4d_pid $watchdog_pid 2>/dev/null |
| 69 | } |
| 70 | |
| 71 | # git p4 submit generates a temp file, which will |
| 72 | # not get cleaned up if the submission fails. Don't |
| 73 | # clutter up /tmp on the test machine. |
| 74 | TMPDIR="$TRASH_DIRECTORY" |
| 75 | export TMPDIR |
| 76 | |
| 77 | registered_stop_p4d_atexit_handler= |
| 78 | start_p4d () { |
| 79 | # One of the test scripts stops and then re-starts p4d. |
| 80 | # Don't register and then run the same atexit handlers several times. |
| 81 | if test -z "$registered_stop_p4d_atexit_handler" |
| 82 | then |
| 83 | test_atexit 'stop_p4d_and_watchdog' |
| 84 | registered_stop_p4d_atexit_handler=AlreadyDone |
| 85 | fi |
| 86 | |
| 87 | mkdir -p "$db" "$cli" "$git" && |
| 88 | rm -f "$pidfile" && |
| 89 | ( |
| 90 | cd "$db" && |
| 91 | { |
| 92 | p4d -q -p $P4DPORT "$@" & |
| 93 | echo $! >"$pidfile" |
| 94 | } |
| 95 | ) && |
| 96 | p4d_pid=$(cat "$pidfile") |
| 97 | |
| 98 | # This gives p4d a long time to start up, as it can be |
| 99 | # quite slow depending on the machine. Set this environment |
| 100 | # variable to something smaller to fail faster in, say, |
| 101 | # an automated test setup. If the p4d process dies, that |
| 102 | # will be caught with the "kill -0" check below. |
| 103 | i=${P4D_START_PATIENCE:-300} |
| 104 | |
| 105 | nr_tries_left=$P4D_TIMEOUT |
| 106 | while true |
| 107 | do |
| 108 | if test $nr_tries_left -eq 0 |
| 109 | then |
| 110 | kill -9 $p4d_pid |
| 111 | exit 1 |
| 112 | fi |
| 113 | sleep 1 |
| 114 | nr_tries_left=$(($nr_tries_left - 1)) |
| 115 | done 2>/dev/null 4>&2 & |
| 116 | watchdog_pid=$! |
| 117 | |
| 118 | ready= |
| 119 | while test $i -gt 0 |
| 120 | do |
| 121 | # succeed when p4 client commands start to work |
| 122 | if p4 info >/dev/null 2>&1 |
| 123 | then |
| 124 | ready=true |
| 125 | break |
| 126 | fi |
| 127 | # fail if p4d died |
| 128 | kill -0 $p4d_pid 2>/dev/null || break |
| 129 | echo waiting for p4d to start |
| 130 | sleep 1 |
| 131 | i=$(( $i - 1 )) |
| 132 | done |
| 133 | |
| 134 | if test -z "$ready" |
| 135 | then |
| 136 | # p4d failed to start |
| 137 | return 1 |
| 138 | fi |
| 139 | |
| 140 | # build a p4 user so author@example.com has an entry |
| 141 | p4_add_user author |
| 142 | |
| 143 | # build a client |
| 144 | client_view "//depot/... //client/..." && |
| 145 | |
| 146 | return 0 |
| 147 | } |
| 148 | |
| 149 | p4_add_user () { |
| 150 | name=$1 && |
| 151 | fullname="${2:-Dr. $1}" |
| 152 | p4 user -f -i <<-EOF |
| 153 | User: $name |
| 154 | Email: $name@example.com |
| 155 | FullName: $fullname |
| 156 | EOF |
| 157 | } |
| 158 | |
| 159 | p4_add_job () { |
| 160 | p4 job -f -i <<-EOF |
| 161 | Job: $1 |
| 162 | Status: open |
| 163 | User: dummy |
| 164 | Description: |
| 165 | EOF |
| 166 | } |
| 167 | |
| 168 | retry_until_success () { |
| 169 | nr_tries_left=$RETRY_TIMEOUT |
| 170 | until "$@" 2>/dev/null || test $nr_tries_left -eq 0 |
| 171 | do |
| 172 | sleep 1 |
| 173 | nr_tries_left=$(($nr_tries_left - 1)) |
| 174 | done |
| 175 | } |
| 176 | |
| 177 | stop_and_cleanup_p4d () { |
| 178 | stop_p4d_and_watchdog |
| 179 | rm -rf "$db" "$cli" "$pidfile" |
| 180 | } |
| 181 | |
| 182 | cleanup_git () { |
| 183 | retry_until_success rm -r "$git" |
| 184 | test_path_is_missing "$git" && |
| 185 | retry_until_success mkdir "$git" |
| 186 | } |
| 187 | |
| 188 | marshal_dump () { |
| 189 | what=$1 && |
| 190 | line=${2:-1} && |
| 191 | cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF && |
| 192 | import marshal |
| 193 | import sys |
| 194 | instream = getattr(sys.stdin, 'buffer', sys.stdin) |
| 195 | for i in range($line): |
| 196 | d = marshal.load(instream) |
| 197 | print(d[b'$what'].decode('utf-8')) |
| 198 | EOF |
| 199 | "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py" |
| 200 | } |
| 201 | |
| 202 | # |
| 203 | # Construct a client with this list of View lines |
| 204 | # |
| 205 | client_view () { |
| 206 | ( |
| 207 | cat <<-EOF && |
| 208 | Client: $P4CLIENT |
| 209 | Description: $P4CLIENT |
| 210 | Root: $cli |
| 211 | AltRoots: $(native_path "$cli") |
| 212 | LineEnd: unix |
| 213 | View: |
| 214 | EOF |
| 215 | printf "\t%s\n" "$@" |
| 216 | ) | p4 client -i |
| 217 | } |
| 218 | |
| 219 | is_cli_file_writeable () { |
| 220 | # cygwin version of p4 does not set read-only attr, |
| 221 | # will be marked 444 but -w is true |
| 222 | file="$1" && |
| 223 | if test_have_prereq CYGWIN && p4 -V | grep -q CYGWIN |
| 224 | then |
| 225 | stat=$(stat --format=%a "$file") && |
| 226 | test $stat = 644 |
| 227 | else |
| 228 | test -w "$file" |
| 229 | fi |
| 230 | } |