1279377Simp#!/bin/sh
2279377Simp## $OpenLDAP$
3279377Simp## This work is part of OpenLDAP Software <http://www.openldap.org/>.
4279377Simp##
5279377Simp## Copyright 1998-2021 The OpenLDAP Foundation.
6279377Simp## All rights reserved.
7279377Simp##
8279377Simp## Redistribution and use in source and binary forms, with or without
9279377Simp## modification, are permitted only as authorized by the OpenLDAP
10279377Simp## Public License.
11279377Simp##
12279377Simp## A copy of this license is available in the file LICENSE in the
13295436Sandrew## top-level directory of the distribution or, alternatively, at
14279377Simp## <http://www.OpenLDAP.org/license.html>.
15279377Simp##
16279377Simp## ACKNOWLEDGEMENTS:
17279377Simp## This module was written in 2016 by Ond��ej Kuzn��k for Symas Corp.
18279377Simp
19279377SimpUSAGE="$0 [-b <backend>] [-c] [-k] [-l #] [-p] [-s {ro|rp}] [-u] [-w] <script>"
20279377Simp
21279377SimpTOPSRCDIR="${SRCDIR-$LDAP_SRC}"
22279377SimpSRCDIR="${TOPSRCDIR}/tests"
23279377Simpeval `grep EGREP_CMD= ${LDAP_BUILD}/tests/run`
24279377Simpeval `$EGREP_CMD -e '^LN_S=' ${LDAP_BUILD}/tests/run`
25279377Simp
26279377Simpexport SRCDIR TOPSRCDIR LN_S EGREP_CMD
27279377Simp
28279377Simp. "${SRCDIR}/scripts/defines.sh"
29279377Simp
30279377SimpBACKEND=
31279377SimpCLEAN=no
32279377SimpWAIT=0
33279377SimpKILLSERVERS=yes
34279377SimpPRESERVE=${PRESERVE-no}
35279377SimpSYNCMODE=${SYNCMODE-rp}
36279377SimpUSERDATA=no
37279377SimpLOOP=1
38279377SimpCOUNTER=1
39279377Simp
40279377Simpwhile test $# -gt 0 ; do
41279377Simp	case "$1" in
42279377Simp		-b | -backend)
43279377Simp			BACKEND="$2"
44279377Simp			shift; shift ;;
45279377Simp
46279377Simp		-c | -clean)
47279377Simp			CLEAN=yes
48279377Simp			shift ;;
49279377Simp
50279377Simp		-k | -kill)
51279377Simp			KILLSERVERS=no
52279377Simp			shift ;;
53279377Simp		-l | -loop)
54279377Simp			NUM="`echo $2 | sed 's/[0-9]//g'`"
55279377Simp			if [ -z "$NUM" ]; then
56279377Simp				LOOP=$2
57279377Simp			else
58279377Simp				echo "Loop variable not an int: $2"
59279377Simp				echo "$USAGE"; exit 1
60279377Simp			fi
61279377Simp			shift ;
62279377Simp			shift ;;
63279377Simp
64279377Simp		-p | -preserve)
65279377Simp			PRESERVE=yes
66279377Simp			shift ;;
67279377Simp
68279377Simp		-s | -syncmode)
69279377Simp			case "$2" in
70279377Simp				ro | rp)
71279377Simp					SYNCMODE="$2"
72279377Simp					;;
73279377Simp				*)
74279377Simp					echo "unknown sync mode $2"
75279377Simp					echo "$USAGE"; exit 1
76279377Simp					;;
77279377Simp			esac
78279377Simp			shift; shift ;;
79279377Simp
80279377Simp		-u | -userdata)
81279377Simp			USERDATA=yes
82279377Simp			shift ;;
83279377Simp
84279377Simp		-w | -wait)
85			WAIT=1
86			shift ;;
87
88		-)
89			shift
90			break ;;
91
92		-*)
93			echo "$USAGE"; exit 1
94			;;
95
96		*)
97			break ;;
98	esac
99done
100
101eval `$EGREP_CMD -e '^AC' ${LDAP_BUILD}/tests/run`
102export `$EGREP_CMD -e '^AC' ${LDAP_BUILD}/tests/run | sed 's/=.*//'`
103
104if test -z "$BACKEND" ; then
105	for b in mdb ; do
106		if eval "test \"\$AC_$b\" != no" ; then
107			BACKEND=$b
108			break
109		fi
110	done
111	if test -z "$BACKEND" ; then
112		echo "No suitable default database backend configured" >&2
113		exit 1
114	fi
115fi
116
117BACKENDTYPE=`eval 'echo $AC_'$BACKEND`
118if test "x$BACKENDTYPE" = "x" ; then
119	BACKENDTYPE="unknown"
120fi
121
122# Backend features.  indexdb: indexing and unchecked limit.
123# maindb: main storage backend.  Currently index,limits,mode,paged results.
124INDEXDB=noindexdb MAINDB=nomaindb
125case $BACKEND in
126	mdb) INDEXDB=indexdb MAINDB=maindb ;;
127	ndb) INDEXDB=indexdb ;;
128esac
129
130export	BACKEND BACKENDTYPE INDEXDB MAINDB \
131	WAIT KILLSERVERS PRESERVE SYNCMODE USERDATA \
132	SRCDIR
133
134if test $# = 0 ; then
135	echo "$USAGE"; exit 1
136fi
137
138# need defines.sh for the definitions of the directories
139. $SRCDIR/scripts/defines.sh
140
141SCRIPTDIR="${TOPDIR}/tests/scripts"
142
143export SCRIPTDIR
144
145SCRIPTNAME="$1"
146shift
147
148if test -x "${SCRIPTDIR}/${SCRIPTNAME}" ; then
149	SCRIPT="${SCRIPTDIR}/${SCRIPTNAME}"
150elif test -x "`echo ${SCRIPTDIR}/test*-${SCRIPTNAME}`"; then
151	SCRIPT="`echo ${SCRIPTDIR}/test*-${SCRIPTNAME}`"
152elif test -x "`echo ${SCRIPTDIR}/${SCRIPTNAME}-*`"; then
153	SCRIPT="`echo ${SCRIPTDIR}/${SCRIPTNAME}-*`"
154else
155	echo "run: ${SCRIPTNAME} not found (or not executable)"
156	exit 1;
157fi
158
159if test ! -r ${DATADIR}/test.ldif ; then
160	${LN_S} ${SRCDIR}/data ${DATADIR}
161fi
162if test ! -r ${SCHEMADIR}/core.schema ; then
163	${LN_S} ${TOPSRCDIR}/servers/slapd/schema ${SCHEMADIR}
164fi
165if test ! -r ./data; then
166	${LN_S} ${TOPDIR}/tests/data ./
167fi
168
169if test -d ${TESTDIR} ; then
170	if test $PRESERVE = no ; then
171		echo "Cleaning up test run directory leftover from previous run."
172		/bin/rm -rf ${TESTDIR}
173	elif test $PRESERVE = yes ; then
174		echo "Cleaning up only database directories leftover from previous run."
175		/bin/rm -rf ${TESTDIR}/db.*
176	fi
177fi
178if test $BACKEND = ndb ; then
179	mysql --user root <<EOF
180	drop database if exists db_1;
181	drop database if exists db_2;
182	drop database if exists db_3;
183	drop database if exists db_4;
184	drop database if exists db_5;
185	drop database if exists db_6;
186EOF
187fi
188mkdir -p ${TESTDIR}
189
190if test $USERDATA = yes ; then
191	if test ! -d userdata ; then
192		echo "User data directory (userdata) does not exist."
193		exit 1
194	fi
195	cp -R userdata/* ${TESTDIR}
196fi
197
198# disable LDAP initialization
199LDAPNOINIT=true; export LDAPNOINIT
200
201echo "Running ${SCRIPT} for ${BACKEND}..."
202while [ $COUNTER -le $LOOP ]; do
203	if [ $LOOP -gt 1 ]; then
204		echo "Running $COUNTER of $LOOP iterations"
205	fi
206	$SCRIPT $*
207	RC=$?
208
209	if test $CLEAN = yes ; then
210		echo "Cleaning up test run directory from this run."
211		/bin/rm -rf ${TESTDIR}
212		echo "Cleaning up symlinks."
213		/bin/rm -f ${DATADIR} ${SCHEMADIR}
214	fi
215
216	if [ $RC -ne 0 ]; then
217		if [ $LOOP -gt 1 ]; then
218			echo "Failed after $COUNTER of $LOOP iterations"
219		fi
220		exit $RC
221	else
222		COUNTER=`expr $COUNTER + 1`
223		if [ $COUNTER -le $LOOP ]; then
224			echo "Cleaning up test run directory from this run."
225			/bin/rm -rf ${TESTDIR}
226		fi
227	fi
228done
229exit $RC
230