1#!/usr/bin/env bash
2
3# Copyright (C) 2016-2017 Alexey Kopytov <akopytov@gmail.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19set -eu
20
21testroot=$(cd $(dirname "$0"); echo $PWD)
22
23# Find the sysbench binary to use
24dirlist=( "$testroot/../src"       # source directory
25          "$testroot/../bin"       # standalone install root directory
26          "$testroot/../../../bin" # system-wide install (e.g. /usr/local/share/sysbench/tests)
27          "$PWD/../src" )          # build directory by 'make distcheck'
28
29for dir in ${dirlist[@]}
30do
31    if [ -x "$dir/sysbench" ]
32    then
33        sysbench_dir="$dir"
34        break
35    fi
36done
37
38if [ -z ${sysbench_dir+x} ]
39then
40    echo "Cannot find sysbench in the following list of directories: \
41${dirlist[@]}"
42    exit 1
43fi
44
45if [ -z ${srcdir+x} ]
46then
47    SBTEST_INCDIR="$PWD/include"
48    SBTEST_CONFIG="$SBTEST_INCDIR/config.sh"
49    if [ $# -lt 1 ]
50    then
51        tests="t/*.t"
52    fi
53else
54    # SBTEST_INCDIR must be an absolute path, because cram changes CWD to a
55    # temporary directory when executing tests. That's why we can just use
56    # $srcdir here
57    SBTEST_INCDIR="$(cd $srcdir; echo $PWD)/include"
58    SBTEST_CONFIG="$PWD/include/config.sh"
59    if [ $# -lt 1 ]
60    then
61        tests="$srcdir/t/*.t"
62    fi
63fi
64if [ -z ${tests+x} ]
65then
66   tests="$*"
67fi
68
69export SBTEST_ROOTDIR="$testroot"
70export SBTEST_SCRIPTDIR="$testroot/../src/lua"
71export SBTEST_SUITEDIR="$testroot/t"
72export SBTEST_CONFIG
73export SBTEST_INCDIR
74
75# Add directories containing sysbench and cram to PATH
76export PATH="${sysbench_dir}:${SBTEST_ROOTDIR}/../third_party/cram/scripts:$PATH"
77
78export PYTHONPATH="${SBTEST_ROOTDIR}/../third_party/cram:${PYTHONPATH:-}"
79
80LUA_PATH="$SBTEST_SCRIPTDIR/?;$SBTEST_SCRIPTDIR/?.lua"
81LUA_PATH="$LUA_PATH;$SBTEST_INCDIR/?;$SBTEST_INCDIR/?.lua"
82export LUA_PATH
83
84. $SBTEST_CONFIG
85
86if $(command -v python >/dev/null 2>&1)
87then
88    PYTHON=python
89elif $(command -v python2 >/dev/null 2>&1)
90then
91    PYTHON=python2
92else
93    echo "Cannot find python interpreter in PATH"
94    exit 1
95fi
96
97$PYTHON $(command -v cram) --shell=/bin/bash --verbose $tests
98