Home | History | Annotate | Line # | Download | only in dist
atf-run.sh revision 1.1.1.1
      1 #! __SH__
      2 # Copyright 2012 Google Inc.
      3 # All rights reserved.
      4 #
      5 # Redistribution and use in source and binary forms, with or without
      6 # modification, are permitted provided that the following conditions are
      7 # met:
      8 #
      9 # * Redistributions of source code must retain the above copyright
     10 #   notice, this list of conditions and the following disclaimer.
     11 # * Redistributions in binary form must reproduce the above copyright
     12 #   notice, this list of conditions and the following disclaimer in the
     13 #   documentation and/or other materials provided with the distribution.
     14 # * Neither the name of Google Inc. nor the names of its contributors
     15 #   may be used to endorse or promote products derived from this software
     16 #   without specific prior written permission.
     17 #
     18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 
     30 # \file atf-run.sh
     31 # Kyua-based compatibility replacement for atf-run.
     32 
     33 
     34 . "${KYUA_ATF_COMPAT_PKGDATADIR:-__PKGDATADIR__}/lib.subr"
     35 
     36 
     37 # Path to ATF's configuration directory.
     38 ATF_CONFDIR="${ATF_CONFDIR:-__ATF_CONFDIR__}"
     39 
     40 
     41 # Path to the bin directory where we got installed.
     42 BINDIR="${KYUA_ATF_COMPAT_BINDIR:-__BINDIR__}"
     43 
     44 
     45 # Loads configuration variables from a set of files.
     46 #
     47 # \param dir The directory from which to load the configuration files.
     48 # \param output_var The name of the variable that will accumulate all the
     49 #     --variable flags to represent the configuration file.
     50 load_configs() {
     51     local dir="${1}"; shift
     52     local output_var="${1}"; shift
     53 
     54     local all_vars=
     55     for file in "${dir}"/*; do
     56         [ "${file}" != "${dir}/*" ] || break
     57 
     58         lib_info "Loading configuration from ${file}"
     59 
     60         local prefix
     61         case "${file}" in
     62         *common.conf) prefix= ;;
     63         *) prefix="test_suites.$(basename "${file}" | sed -e 's,.conf$,,')." ;;
     64         esac
     65 
     66         local ws='[ \t]*'
     67         local name='[a-zA-Z][-_a-zA-Z0-9]*'
     68         local repl="--variable='${prefix}\\1=\\2'"
     69         local vars=$(grep "^${ws}${name}${ws}=" "${file}" | \
     70             sed -e 's,#(.*)$,,;s,unprivileged-user,unprivileged_user,g' \
     71             -e "s,^${ws}\(${name}\)${ws}=${ws}'\([^']*\)'${ws}$,${repl}," \
     72             -e "s,^${ws}\(${name}\)${ws}=${ws}\"\([^\"]*\)\"${ws}$,${repl}," \
     73             -e "s,^${ws}\(${name}\)${ws}=${ws}\(.*\)$,${repl},")
     74 
     75         lib_info "Extracted arguments: ${vars}"
     76         all_vars="${all_vars} ${vars}"
     77     done
     78     eval ${output_var}=\'"${all_vars}"\'
     79 }
     80 
     81 
     82 # Transforms an atf-run -v specification to Kyua's semantics.
     83 #
     84 # \param raw The key=value argument to -v.
     85 # \param ... The names of all the possible test suites.  For test-suite specific
     86 #     variables, we expand them to all their possibilities as Kyua needs to know
     87 #     what test suite a particular variable belongs to.
     88 #
     89 # \post Prints one or more --variable arguments that match the input variable
     90 # name.
     91 convert_variable() {
     92     local raw="${1}"; shift
     93 
     94     case "${raw}" in
     95     unprivileged-user=*)
     96         echo "--variable=${raw}" | \
     97             sed -e 's,unprivileged-user,unprivileged_user,g'
     98         ;;
     99     *)
    100         for test_suite in "${@}"; do
    101             echo "--variable=test_suites.${test_suite}.${raw}"
    102         done
    103         ;;
    104     esac
    105 }
    106 
    107 
    108 # Collects all the test suite names defined in a subtree.
    109 #
    110 # \param dir The subtree to scan for Kyuafiles.
    111 #
    112 # \post Prints the names of all found test suites.
    113 grab_test_suites() {
    114     local dir="${1}"; shift
    115 
    116     find "${dir}" -name Kyuafile -exec grep "test_suite('" "{}" \; | \
    117         cut -d "'" -f 2
    118 }
    119 
    120 
    121 # Gets the path to the compatibility Kyuafile.
    122 #
    123 # If a Kyuafile is found in the current directory, use that directly.
    124 # Otherwise, generate a fake Kyuafile in a temporary directory and return
    125 # that instead.
    126 #
    127 # \param [out] output_var The name of the variable to set with the path
    128 #     of the Kyuafile to be used.
    129 select_kyuafile() {
    130     local output_var="${1}"; shift
    131 
    132     if [ -f Kyuafile ]; then
    133         eval ${output_var}=\'"$(pwd)/Kyuafile"\'
    134     elif [ -f Atffile ]; then
    135         "${BINDIR}/atf2kyua" -s "$(pwd)" -t "${Lib_TempDir}" || \
    136             lib_error "Cannot generate fake Kyuafile"
    137         eval ${output_var}=\'"${Lib_TempDir}/Kyuafile"\'
    138     else
    139         lib_error "Cannot find Atffile nor Kyuafile"
    140     fi
    141 }
    142 
    143 
    144 # Prints program usage to stdout.
    145 #
    146 # \param progname The name of the program to use for the syntax help.
    147 usage() {
    148     local progname="${1}"; shift
    149     echo "Usage: ${progname} [-v var-value] [program1 [.. programN]]"
    150 }
    151 
    152 
    153 # Entry point for the program.
    154 #
    155 # \param ... The user-provided arguments.
    156 main() {
    157     local vflags=
    158 
    159     while getopts ':v:' arg "${@}"; do
    160         case "${arg}" in
    161         v)
    162             vflags="${OPTARG} ${cli_variables}"
    163             ;;
    164         \?)
    165             lib_usage_error "Unknown option -${OPTARG}"
    166             ;;
    167         esac
    168     done
    169     shift $((${OPTIND} - 1))
    170 
    171     lib_init_tempdir
    172 
    173     load_configs "${ATF_CONFDIR}" "system_variables"  # Sets system_variables.
    174     load_configs "${HOME}/.atf" "user_variables"  # Sets user_variables.
    175 
    176     local kyuafile_path
    177     select_kyuafile "kyuafile_path"  # Sets kyuafile_path.
    178 
    179     local test_suites="$(grab_test_suites "$(dirname "${kyuafile_path}")")"
    180 
    181     cli_variables=
    182     for vflag in ${vflags}; do
    183         local values="$(convert_variable "${vflag}" ${test_suites})"
    184         cli_variables="${values} ${cli_variables}"
    185     done
    186 
    187     kyua ${system_variables} ${user_variables} ${cli_variables} \
    188         test --kyuafile="${kyuafile_path}" --build-root="$(pwd)" "${@}"
    189     local ret="${?}"
    190 
    191     lib_cleanup
    192 
    193     return "${ret}"
    194 }
    195 
    196 
    197 main "${@}"
    198