File macros.apache-module-test of Package apache-rpm-macros

%__test_dir   /tmp/%{name}-test
%__test_mpm   prefork
%__test_user  abuild
%__test_group abuild
%__test_port  60080

# world writeable dir for testing purposes (logs, document root, ..)
%apache_test_module_dir %{__test_dir}

#
# macro: apache_test_module_start_apache -- start apache with specified modules loaded
# 
#        usage: apache_test_module_start_apache [-m module_list] [-i include_list] [-r document_root] 
#                                               [-u user] [-g group] [-p port] [-t mpm]
#
#        module_list:   colon separated list of module names to be loaded with test run
#                       search path:  %{buildroot} tree
#                                     %{apache_libexecdir}-prefork
#                                     %{apache_libexecdir}
#                       [example value: asn:dbd]
#        include_list:  colon separated list of names of apache configuration files
#                       to be included; can be either existing path or just name
#                       of the configuration file,which will be searched under:
#                         %{buildroot}%{apache_sysconfdir} tree
#                         $PWD tree
#                         %{apache_test_module_dir}
#                       [example value: mod_asn.conf]
#        document_root: document root of the test server, 
#                       [default value: /tmp/%%{name}-test/htdocs]
#        user:          user under which will run test instance (usable only
#                       when the test is run by root)
#                       [default value: abuild]
#        group:         group under which will run test instance (usable only
#                       when the test is run by root)
#                       [default value: abuild]
#        port:          listen on port
#                       [default value: 60080]
#        mpm:           multiprocessing module (prefork, worker, event)
#                       [default value: prefork]
#        
#
%apache_test_module_start_apache(m:,i:,r:,u:,g:,p:,t:) \
  # constants \
  TEST_DIR='%{__test_dir}' \
  # arguments \
  # modules to load, from BUILD tree or system \
  MODULES=$(echo %{-m:%{-m*}} | tr ':' ' ') \
  # configs to include \
  CONFIGS="%{-i:%{-i*}}" \
  # document root of the test server \
  DOCUMENT_ROOT="%{-r:%{-r*}}" \
  if [ -z "$DOCUMENT_ROOT" ]; then \
    DOCUMENT_ROOT=$TEST_DIR/htdocs  \
  fi \
  # user and group running the apache instance \
  # (usable only when the test is run as root) \
  TEST_USER="%{-u:%{-u*}}" \
  if [ -z "$TEST_USER" ]; then \
    TEST_USER='%{__test_user}' \
  fi \
  TEST_GROUP="%{-g:%{-g*}}" \
  if [ -z "$TEST_GROUP" ]; then \
    TEST_GROUP='%{__test_group}' \
  fi \
  TEST_PORT="%{-p:%{-p*}}" \
  if [ -z "$TEST_PORT" ]; then \
    TEST_PORT='%{__test_port}' \
  fi \
  TEST_MPM="%{-t:%{-t*}}" \
  if [ -z "$TEST_MPM" ]; then \
    TEST_MPM='%{__test_mpm}' \
  fi \
  # apache binary \
  CMD=$(ls %{_sbindir}/httpd*-$TEST_MPM | head -n 1) \
  # begin \
  echo "-----------------------------------------------------------" \
  echo "APACHE MODULE TEST" \
  echo \
  echo "modules to load:     $MODULES" \
  echo "configs to include:  $CONFIGS" \
  # create test dir \
  mkdir -p $TEST_DIR \
  # create document root if not exist \
  mkdir -p $DOCUMENT_ROOT \
  # create test/httpd-test.conf \
  TEST_CONF_FILE="$TEST_DIR/httpd.conf" \
  SYSTEM_MODULE_PATH="%{apache_libexecdir}-$TEST_MPM" \
  echo "ServerName test"                                                        > $TEST_CONF_FILE \
  echo "User $TEST_USER"                                                       >> $TEST_CONF_FILE \
  echo "Group $TEST_GROUP"                                                     >> $TEST_CONF_FILE \
  echo "Listen $TEST_PORT"                                                     >> $TEST_CONF_FILE \
  echo "PidFile $TEST_DIR/pid"                                                 >> $TEST_CONF_FILE \
  echo "ErrorLog $TEST_DIR/error_log"                                          >> $TEST_CONF_FILE \
  echo "LoadModule dir_module        $SYSTEM_MODULE_PATH/mod_dir.so"           >> $TEST_CONF_FILE \
  echo "LoadModule auth_basic_module $SYSTEM_MODULE_PATH/mod_auth_basic.so"    >> $TEST_CONF_FILE \
  if [ %{apache_branch} -ge 204 ]; then \
    echo "LoadModule authz_core_module $SYSTEM_MODULE_PATH/mod_authz_core.so"  >> $TEST_CONF_FILE \
  fi \
  echo "LoadModule authz_host_module $SYSTEM_MODULE_PATH/mod_authz_host.so"    >> $TEST_CONF_FILE \
  for m in $(echo $MODULES | tr ':' ' '); do \
    # if module is compiled in statically, do not load \
    $CMD -f $TEST_CONF_FILE -M | grep $m && continue \
    module_path=$(find %{buildroot} %{apache_libexecdir}-$TEST_MPM %{apache_libexecdir} -name "mod_$m.so" | tail -n 1) \
    if [ -z "$module_path" ]; then \
      echo "ERROR: Module $m not found." \
      exit 1 \
    fi \
    echo "Will load ${m}_module $module_path" \
    echo "LoadModule ${m}_module $module_path"                                 >> $TEST_CONF_FILE \
  done \
  for c in $(echo $CONFIGS | tr ':' ' '); do \
    if [ -f $c ]; then \
      if [[ ! "$c" = /* ]]; then \
        c="$PWD/$c" \
      fi \
      include_path="$c" \
    else \
      include_path=$(find %{buildroot}%{apache_sysconfdir} %{apache_test_module_dir} $PWD -name "$c" 2>/dev/null | tail -n 1) \
    fi \
    if [ -z "$include_path" ]; then \
      echo "ERROR: Config file $c not found." \
      exit 1 \
    fi \
    echo "Will include $include_path" \
    echo "Include $include_path"                                               >> $TEST_CONF_FILE \
  done \
  echo "DocumentRoot $DOCUMENT_ROOT"                                           >> $TEST_CONF_FILE \
  echo "DirectoryIndex index.html"                                             >> $TEST_CONF_FILE \
  # run apache \
  echo -n "Starting Apache ... " \
  $CMD -f $TEST_CONF_FILE -k start \
  # wait to be sure apache finished start \
  sleep 2 \
  if [ ! -f $TEST_DIR/pid ]; then \
    echo "FAILED, $TEST_DIR/error_log: >&2" \
    cat $TEST_DIR/error_log >&2 \
    echo "See $TEST_DIR for details" >&2 \
    echo "FAILED, $TEST_DIR/error_log:" \
    cat $TEST_DIR/error_log \
    echo "See $TEST_DIR for details" \
    exit 1  \
  fi \
  echo "SUCCESS" \
  %nil

#
# macro: apache_test_module_stop_apache -- stops apache previously started with *_start_apache
# 
#        usage: apache_test_module_stop_apache [-f]
#
#               -f: force
#
%apache_test_module_stop_apache(f) \
  TEST_DIR='%{__test_dir}' \
  SIGNAL=%{!-f:TERM}%{-f:KILL} \
  # stop apache \
  echo "Stopping Apache ..." \
  kill -$SIGNAL -$(cat $TEST_DIR/pid) \
  echo "Done." \
  echo "-----------------------------------------------------------" \
  %nil

#
# macro: apache_test_module_curl -- outputs curl on particular document relative to 
#                                   document root of test instance
#
#        usage:   apache_test_module_curl -d document_to_curl -o output_of_curl 
#                                         -p port -u user:password -r protocol
# 
#                 document_to_curl: relative to DocumentRoot [default: /]
#                 output_of_curl:   where to save document (required)
#                 port:             from which port of localhost will be document
#                                   downloaded [default value: 60080]
#                 user:password:    --user parameter of curl
#                 protocol:         protocol to download document
#                                   [default: http]
#                
#        example: apache_test_module_curl -d foo/test.html -o test.html
#                 apache_test_module_curl -d foo/ -o output.txt
#
%apache_test_module_curl(d:,o:,p:,u:,r:) \
  TEST_DOCUMENT="%{-d:%{-d*}}" \
  TEST_OUTPUT="%{-o:%{-o*}}" \
  TEST_PORT="%{-p:%{-p*}}" \
  TEST_CURL_USER="%{-u:%{-u*}}" \
  TEST_PROTO="%{-r:%{-r*}}" \
  if [ -z "$TEST_PORT" ]; then \
    TEST_PORT='%{__test_port}' \
  fi \
  if [ -z "$TEST_OUTPUT" ]; then \
    echo "Missing argument -o to apache_test_module_curl." \
    exit 1 \
  fi \
  USER_PARAM="" \
  if [ ! -z "$TEST_CURL_USER" ]; then \
    USER_PARAM="-u $TEST_CURL_USER" \
  fi \
  if [ -z "$TEST_PROTO" ]; then \
    TEST_PROTO="http" \
  fi \
  curl -s -k "$TEST_PROTO://127.0.0.1:$TEST_PORT/$TEST_DOCUMENT" --create-dirs -o "$TEST_OUTPUT" $USER_PARAM \
  %nil

#
# macro: apache_test_module_load -- tests that module(s) can be loaded
# 
#        usage: apache_test_module_load [-m module_list] [-i include_list] [-t mpm]
#
#        module_list:  colon separated list of module names to be loaded with test run
#                      search path:  %{buildroot} tree
#                                    %{apache_libexecdir}-prefork
#                                    %{apache_libexecdir}
#                      [example value: asn:dbd]
#        include_list: colon separated list of names of apache configuration files
#                      to be included (path relative to current dir)
#                      search path   %{buildroot}%{apache_sysconfdir} tree
#                                    $PWD tree
#                      [example value: mod_asn.conf]
#        mpm:          multiprocessing module (prefork, worker, event)
#                      [default value: prefork]
#
%apache_test_module_load(m:,i:,t:) \
  %apache_test_module_start_apache %{-m:-m %{-m*}} %{-i:-i %{-i*}} %{-t:-t %{-t*}} \
  %apache_test_module_stop_apache \
  %nil