1#!/bin/bash
2#
3# Copyright(C) 2012-2016 Kouhei Sutou <kou@clear-code.com>
4#
5# This library is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9#
10# This library 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 GNU
13# Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with this library; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
18
19# set -x
20set -e
21
22base_dir="$(cd $(dirname $0); pwd)"
23top_dir="${base_dir}/../.."
24
25bundled_mroonga_dir="${top_dir}/storage/mroonga"
26if [ -f "${bundled_mroonga_dir}/config.sh" ]; then
27  mroonga_dir="${bundled_mroonga_dir}"
28  . "${bundled_mroonga_dir}/config.sh"
29else
30  mroonga_dir="${top_dir}"
31  . "${top_dir}/config.sh"
32fi
33
34n_processors="$(grep '^processor' /proc/cpuinfo | wc -l)"
35if [ "${MROONGA_BUNDLED}" = "yes" ]; then
36  max_n_processors=2
37else
38  max_n_processors=4
39fi
40if (( $n_processors > $max_n_processors )); then
41  n_processors=$max_n_processors
42fi
43
44build()
45{
46  make -j${n_processors} > /dev/null
47}
48
49run_unit_test()
50{
51  if [ "${MROONGA_BUNDLED}" != "yes" ]; then
52    NO_MAKE=yes ${mroonga_dir}/test/run-unit-test.sh
53  fi
54}
55
56prepare_mysql_test_dir()
57{
58  mysql_test_dir=/usr/mysql-test
59  if [ -d /usr/lib/mysql-testsuite/ ]; then
60    sudo cp -a /usr/lib/mysql-testsuite/ ${mysql_test_dir}/
61  elif [ -d /usr/lib/mysql-test/ ]; then
62    sudo cp -a /usr/lib/mysql-test/ ${mysql_test_dir}/
63  elif [ -d /usr/share/mysql/mysql-test/ ]; then
64    sudo cp -a /usr/share/mysql/mysql-test/ ${mysql_test_dir}/
65  elif [ -d /usr/share/mysql-test/ ]; then
66    sudo cp -a /usr/share/mysql-test/ ${mysql_test_dir}/
67  elif [ -d /opt/mysql/ ]; then
68    mysql_test_dir=$(echo /opt/mysql/server-*/mysql-test)
69  else
70    sudo cp -a ${MYSQL_SOURCE_DIR}/mysql-test/ ${mysql_test_dir}/
71  fi
72  sudo chown -R $(id -u):$(id -g) ${mysql_test_dir}/
73
74  cp -a ${mroonga_dir}/mysql-test/mroonga/ ${mysql_test_dir}/suite/
75}
76
77collect_test_suite_names()
78{
79  cd ${mysql_test_dir}/suite/
80  test_suite_names=""
81  for test_suite_name in $(find mroonga -type d '!' -name '[tr]'); do
82    if [ -n "${test_suite_names}" ]; then
83      test_suite_names="${test_suite_names},"
84    fi
85    test_suite_names="${test_suite_names}${test_suite_name}"
86  done
87  cd -
88}
89
90prepare_sql_test()
91{
92  sudo make install > /dev/null
93  prepare_mysql_test_dir
94  collect_test_suite_names
95}
96
97run_sql_test()
98{
99  test_args=()
100  if [ "${MROONGA_TEST_EMBEDDED}" = "yes" ]; then
101    test_args=("${test_args[@]}" "--embedded-server")
102  fi
103  if [ "${MROONGA_TEST_PS_PROTOCOL}" = "yes" ]; then
104    test_args=("${test_args[@]}" "--ps-protocol")
105  fi
106
107  if [ "${MROONGA_BUNDLED}" = "yes" ]; then
108    # Plugins aren't supported.
109    cd ${mroonga_dir}/mysql-test/mroonga/storage
110    rm -rf alter_table/add_index/token_filters/
111    rm -rf alter_table/t/change_token_filter.test
112    rm -rf create/table/token_filters/
113    rm -rf fulltext/token_filters/
114    cd -
115
116    ${mroonga_dir}/test/run-sql-test.sh \
117                  "${test_args[@]}" \
118                  --parallel="${n_processors}" \
119                  --retry=3
120  else
121    prepare_sql_test
122
123    cd ${mysql_test_dir}/
124    perl ./mysql-test-run.pl \
125      "${test_args[@]}" \
126      --no-check-testcases \
127      --parallel="${n_processors}" \
128      --retry=3 \
129      --suite="${test_suite_names}" \
130      --force
131  fi
132}
133
134build
135# run_unit_test
136run_sql_test
137