mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
7 hours ago 0db6a25e6bba295ce7ed840c2da08e8012d72384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env bash
# The contents of this file are subject to the terms of the Common Development and
# Distribution License (the License). You may not use this file except in compliance with the
# License.
#
# You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
# specific language governing permission and limitations under the License.
#
# When distributing Covered Software, include this CDDL Header Notice in each file and include
# the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
# Header, with the fields enclosed by brackets [] replaced by your own identifying
# information: "Portions copyright [year] [name of copyright owner]".
#
# Copyright 2026 3A Systems, LLC.
 
set -eu
REPO=$(cd "$(dirname "$0")/../.." && pwd)
SHELL_UNDER_TEST=${1:-/bin/sh}
FIXTURES=$(mktemp -d)
LIVE_PID=
trap 'if [ -n "$LIVE_PID" ]; then
        kill -KILL "$LIVE_PID" 2>/dev/null || :
        wait "$LIVE_PID" 2>/dev/null || :
      fi
      rm -rf "$FIXTURES"' EXIT
export TMPDIR="$FIXTURES"
 
mkdir -p "$FIXTURES/bin" "$FIXTURES/lib"
cp "$REPO/opendj-server-legacy/resource/bin/start-ds" \
   "$REPO/opendj-server-legacy/resource/bin/stop-ds" "$FIXTURES/bin/"
cp "$REPO/opendj-server-legacy/resource/bin/_script-util.sh" "$FIXTURES/lib/script-util-real.sh"
cat > "$FIXTURES/lib/_script-util.sh" <<'UTIL'
# Load the launcher helpers while using fixture Java and a shorter shutdown timer.
SCRIPT_UTIL_CMD=
. "$INSTALL_ROOT/lib/script-util-real.sh"
OPENDJ_JAVA_BIN="$INSTALL_ROOT/java"
dd() {
  if [ "${FIXTURE_CASE:-}" = stop-sparse ]; then
    case " $* " in
      *' count='*) ;;
      *)
        touch "$INSTANCE_ROOT/unbounded-log-read"
        return 1
        ;;
    esac
  fi
  command dd "$@"
}
stat() {
  [ "${FIXTURE_CASE:-}" != stop-no-stat ] || return 1
  command stat "$@"
}
sleep() {
  case "${FIXTURE_CASE:-}:$1" in
    stop-timeout:200) command sleep 1 ;;
    stop-rotate:0.1)
      touch "$INSTANCE_ROOT/waiting-for-shutdown"
      while [ -e "$INSTANCE_ROOT/logs/server.pid" ]; do command sleep 0.05; done
      ;;
    *) command sleep "$@" ;;
  esac
}
UTIL
cat > "$FIXTURES/java" <<'JAVA'
#!/bin/sh
case " $* " in
  *' --help '*)
    printf 'fixture help\n'
    exit 0
    ;;
  *' --checkStartability '*)
    if [ -f "$INSTANCE_ROOT/checked" ]; then
      touch "$INSTANCE_ROOT/final-check"
      printf 'probe output\n' >&2
      exit "${FIXTURE_PROBE_STATUS:-98}"
    fi
    if [ -n "${FIXTURE_TIMEOUT_ARG:-}" ]; then
      found_timeout=false
      for arg do
        if [ "$arg" = "$FIXTURE_TIMEOUT_ARG" ]; then
          found_timeout=true
          break
        fi
      done
      if [ "$found_timeout" = false ]; then
        printf 'Expected preflight argument %s\n' "$FIXTURE_TIMEOUT_ARG" >&2
        exit 1
      fi
    fi
    touch "$INSTANCE_ROOT/checked"
    while [ "$#" -gt 0 ]; do
      case "$1" in
        --startupTimeoutFile)
          shift
          printf '%s\n' "$FIXTURE_TIMEOUT" > "$1"
          ;;
      esac
      shift
    done
    exit "$FIXTURE_START_CODE"
    ;;
  *org.opends.server.tools.WaitForFileDelete*)
    touch "$INSTANCE_ROOT/waiter-jvm"
    if [ "${FIXTURE_CASE#stop-}" != "$FIXTURE_CASE" ]; then
      exit 2
    fi
    exec sleep 30
    ;;
  *' --checkStoppability '*)
    exit "$FIXTURE_STOP_CODE"
    ;;
  *org.opends.server.tools.StopDS*)
    touch "$INSTANCE_ROOT/ldap-stop"
    exit 7
    ;;
esac
printf 'first line\n'
case "$FIXTURE_CASE" in
  early|quiet-failure|write-only-early)
    printf 'startup failed\n' >&2
    exit 42
    ;;
  timeout|write-only-timeout)
    exec sleep 30
    ;;
  *)
    sleep 0.2
    printf 'last message'
    rm "$INSTANCE_ROOT/logs/server.starting"
    exec sleep 30
    ;;
esac
JAVA
chmod +x "$FIXTURES/java"
 
run_case() {
  export FIXTURE_CASE=$1 FIXTURE_TIMEOUT=$2 FIXTURE_START_CODE=$3
  expected=$4
  shift 4
  export INSTANCE_ROOT="$FIXTURES/$FIXTURE_CASE instance"
  mkdir -p "$INSTANCE_ROOT/logs" "$INSTANCE_ROOT/tmp"
  printf 'stale log\n' > "$INSTANCE_ROOT/logs/server.out"
  if [[ "$FIXTURE_CASE" == write-only-* ]]; then
    chmod 200 "$INSTANCE_ROOT/logs/server.out"
  fi
  status=0
  timeout 8 "$SHELL_UNDER_TEST" "$FIXTURES/bin/start-ds" "$@" \
    > "$INSTANCE_ROOT/stdout" 2> "$INSTANCE_ROOT/stderr" || status=$?
  if [ -f "$INSTANCE_ROOT/logs/server.pid" ]; then
    read -r LIVE_PID < "$INSTANCE_ROOT/logs/server.pid"
  fi
  if [ "$status" -ne "$expected" ] || [ -e "$INSTANCE_ROOT/waiter-jvm" ]; then
    echo "$FIXTURE_CASE: expected exit $expected with no waiter JVM, got $status" >&2
    cat "$INSTANCE_ROOT/stderr" >&2
    exit 1
  fi
  [ -z "$(ls -A "$INSTANCE_ROOT/tmp")" ]
}
 
help=$(TMPDIR="$FIXTURES/unavailable" INSTANCE_ROOT="$FIXTURES/missing instance" \
  "$SHELL_UNDER_TEST" "$FIXTURES/bin/start-ds" --help)
[ "$help" = "fixture help" ]
 
run_case early 0 99 1 --timeout 0
[ -e "$INSTANCE_ROOT/logs/server.starting" ]
[ ! -e "$INSTANCE_ROOT/final-check" ]
grep -Fxq 'startup failed' "$INSTANCE_ROOT/stdout"
grep -Eq 'server pid [0-9]+, status 42' "$INSTANCE_ROOT/stderr"
grep -Fq 'status 42' "$INSTANCE_ROOT/logs/start-ds.log"
LIVE_PID=
 
run_case success 200 99 0
[ -e "$INSTANCE_ROOT/final-check" ]
printf 'first line\nlast message' | cmp - "$INSTANCE_ROOT/stdout"
kill "$LIVE_PID"
LIVE_PID=
 
run_case quiet 0 103 0 --quiet --timeout=0
[ -e "$INSTANCE_ROOT/final-check" ]
[ ! -s "$INSTANCE_ROOT/stdout" ]
kill "$LIVE_PID"
LIVE_PID=
 
run_case quiet-failure 60 103 1 --quiet --timeout=60
[ ! -e "$INSTANCE_ROOT/final-check" ]
[ ! -s "$INSTANCE_ROOT/stdout" ]
grep -Fxq 'startup failed' "$INSTANCE_ROOT/logs/server.out"
grep -Fq 'status 42' "$INSTANCE_ROOT/stderr"
LIVE_PID=
 
FIXTURE_TIMEOUT_ARG=-t1 run_case timeout 1 99 2 -t1
[ ! -e "$INSTANCE_ROOT/final-check" ]
[ -e "$INSTANCE_ROOT/logs/server.starting" ]
kill -0 "$LIVE_PID"
grep -Fq 'timed out after 1' "$INSTANCE_ROOT/stderr"
kill "$LIVE_PID"
LIVE_PID=
 
touch "$FIXTURES/write-only"
chmod 200 "$FIXTURES/write-only"
if [ -r "$FIXTURES/write-only" ]; then
  echo 'Skipping write-only log scenarios because the test user can read mode 0200 files'
else
  run_case write-only-success 200 99 0
  [ -e "$INSTANCE_ROOT/final-check" ]
  [ ! -e "$INSTANCE_ROOT/logs/server.starting" ]
  [ ! -s "$INSTANCE_ROOT/stdout" ]
  kill "$LIVE_PID"
  LIVE_PID=
 
  run_case write-only-early 0 99 1 --timeout 0
  [ ! -e "$INSTANCE_ROOT/final-check" ]
  [ -e "$INSTANCE_ROOT/logs/server.starting" ]
  grep -Eq 'server pid [0-9]+, status 42' "$INSTANCE_ROOT/stderr"
  LIVE_PID=
 
  FIXTURE_TIMEOUT_ARG=-t1 run_case write-only-timeout 1 99 2 -t1
  [ ! -e "$INSTANCE_ROOT/final-check" ]
  [ -e "$INSTANCE_ROOT/logs/server.starting" ]
  kill -0 "$LIVE_PID"
  grep -Fq 'timed out after 1' "$INSTANCE_ROOT/stderr"
  kill "$LIVE_PID"
  LIVE_PID=
fi
 
export FIXTURE_PROBE_STATUS=99
run_case failed-probe 3 99 1 --timeout 3
[ -e "$INSTANCE_ROOT/final-check" ]
grep -Fq 'checkStartability exited with status 99' "$INSTANCE_ROOT/stderr"
grep -Fxq 'probe output' "$INSTANCE_ROOT/logs/start-ds.log"
kill "$LIVE_PID"
LIVE_PID=
 
cat > "$FIXTURES/bin/start-ds" <<'START'
#!/bin/sh
[ ! -e "$INSTANCE_ROOT/logs/server.pid" ] || exit 1
printf '%s\n' "$@" > "$INSTANCE_ROOT/restart-args"
START
chmod +x "$FIXTURES/bin/start-ds"
 
run_stop_case() {
  export FIXTURE_CASE=$1 FIXTURE_STOP_CODE=$2
  expected=$3
  shift 3
  export INSTANCE_ROOT="$FIXTURES/$FIXTURE_CASE instance"
  mkdir -p "$INSTANCE_ROOT/logs"
  if [ "$FIXTURE_CASE" = stop-sparse ]; then
    # Model a 64 GiB log without allocating or reading 64 GiB in the fixture.
    printf x | dd of="$INSTANCE_ROOT/logs/errors" bs=1 seek=68719476742 count=1 2>/dev/null
  else
    awk 'BEGIN { for (i = 0; i < 8250; i++) printf "x"; print ""; }' > "$INSTANCE_ROOT/logs/errors"
  fi
  if [ "$FIXTURE_CASE" = stop-write-only ]; then
    chmod 200 "$INSTANCE_ROOT/logs/errors"
  fi
  (
    trap 'printf "shutdown in progress\n" >> "$INSTANCE_ROOT/logs/errors"
          if [ "$FIXTURE_CASE" != stop-timeout ]; then
            if [ "$FIXTURE_CASE" = stop-rotate ]; then
              while [ ! -e "$INSTANCE_ROOT/waiting-for-shutdown" ]; do sleep 0.05; done
            else
              sleep 0.2
            fi
            printf "shutdown complete" >> "$INSTANCE_ROOT/logs/errors"
            if [ "$FIXTURE_CASE" = stop-rotate ]; then
              mv "$INSTANCE_ROOT/logs/errors" "$INSTANCE_ROOT/logs/errors.1"
              : > "$INSTANCE_ROOT/logs/errors"
            fi
            touch "$INSTANCE_ROOT/shutdown-complete"
            rm "$INSTANCE_ROOT/logs/server.pid"
            exit 0
          fi' TERM
    touch "$INSTANCE_ROOT/ready"
    while :; do sleep 0.1; done
  ) &
  LIVE_PID=$!
  printf '%s\n' "$LIVE_PID" > "$INSTANCE_ROOT/logs/server.pid"
  timeout 5 sh -c 'while [ ! -e "$INSTANCE_ROOT/ready" ]; do sleep 0.1; done'
 
  status=0
  timeout 8 "$SHELL_UNDER_TEST" "$FIXTURES/bin/stop-ds" "$@" \
    > "$INSTANCE_ROOT/stdout" 2> "$INSTANCE_ROOT/stderr" || status=$?
  if [ "$status" -ne "$expected" ] || [ -e "$INSTANCE_ROOT/waiter-jvm" ]; then
    echo "$FIXTURE_CASE: expected exit $expected with no waiter JVM, got $status" >&2
    cat "$INSTANCE_ROOT/stderr" >&2
    exit 1
  fi
  if [ -e "$INSTANCE_ROOT/unbounded-log-read" ]; then
    echo "$FIXTURE_CASE: stop-ds attempted a full scan of the existing log" >&2
    exit 1
  fi
  if [ "$FIXTURE_CASE" = stop-timeout ]; then
    kill -KILL "$LIVE_PID"
    wait "$LIVE_PID" 2>/dev/null || :
  else
    [ ! -e "$INSTANCE_ROOT/logs/server.pid" ]
    [ -e "$INSTANCE_ROOT/shutdown-complete" ]
    wait "$LIVE_PID"
    [ ! -e "$INSTANCE_ROOT/ldap-stop" ]
  fi
  LIVE_PID=
}
 
run_stop_case stop-success 100 0
printf 'shutdown in progress\nshutdown complete' | cmp - "$INSTANCE_ROOT/stdout"
 
run_stop_case stop-rotate 100 0
printf 'shutdown in progress\nshutdown complete' | cmp - "$INSTANCE_ROOT/stdout"
 
run_stop_case stop-sparse 100 0
printf 'shutdown in progress\nshutdown complete' | cmp - "$INSTANCE_ROOT/stdout"
 
run_stop_case stop-no-stat 100 0
[ ! -s "$INSTANCE_ROOT/stdout" ]
 
run_stop_case stop-restart 106 0 --restart --quiet
[ ! -s "$INSTANCE_ROOT/stdout" ]
grep -Fxq -- '--quiet' "$INSTANCE_ROOT/restart-args"
 
run_stop_case stop-timeout 100 7
[ -e "$INSTANCE_ROOT/ldap-stop" ]
[ -e "$INSTANCE_ROOT/logs/server.pid" ]
grep -Fq 'timed out after 200' "$INSTANCE_ROOT/stderr"
 
if [ ! -r "$FIXTURES/write-only" ]; then
  run_stop_case stop-write-only 100 0
  [ ! -s "$INSTANCE_ROOT/stdout" ]
fi
 
echo "Unix startup and shutdown scenarios passed with $SHELL_UNDER_TEST"