automated: linux: ltp: incorporate building LTP from git

Added variables: TEST_GIT_URL url to the git repository, TEST_DIR
download directory where the build happens, and a BUILD_FROM_TAR flag.

Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
diff --git a/automated/linux/ltp/ltp.sh b/automated/linux/ltp/ltp.sh
index 7536242..e2f1f08 100755
--- a/automated/linux/ltp/ltp.sh
+++ b/automated/linux/ltp/ltp.sh
@@ -22,9 +22,15 @@
 ENVIRONMENT=""
 # LTP version
 LTP_VERSION="20180926"
+TEST_PROGRAM=ltp
+# https://github.com/linux-test-project/ltp.git
+TEST_GIT_URL=""
+TEST_DIR="$(pwd)/${TEST_PROGRAM}"
+BUILD_FROM_TAR="false"
+
 LTP_TMPDIR=/ltp-tmp
 
-LTP_PATH=/opt/ltp
+LTP_INSTALL_PATH=/opt/ltp
 
 usage() {
     echo "Usage: ${0} [-T mm,math,syscalls]
@@ -36,11 +42,15 @@
                       [-s True|False]
                       [-v LTP_VERSION]
                       [-M Timeout_Multiplier]
-                      [-R root_password]" 1>&2
+                      [-R root_password]
+                      [-u git url]
+                      [-p build directory]
+                      [-t build from tarfile ]
+" 1>&2
     exit 0
 }
 
-while getopts "M:T:S:b:d:g:e:s:v:R:" arg; do
+while getopts "M:T:S:b:d:g:e:s:v:R:u:p:t:" arg; do
    case "$arg" in
      T)
         TST_CMDFILES="${OPTARG}"
@@ -89,6 +99,20 @@
      # Slow machines need more timeout Default is 5min and multiply * MINUTES
      M) export LTP_TIMEOUT_MUL="${OPTARG}";;
      R) export PASSWD="${OPTARG}";;
+     u)
+        if [[ "$OPTARG" != '' ]]; then
+          TEST_GIT_URL="$OPTARG"
+          TEST_TARFILE=""
+        fi
+        ;;
+     p)
+        if [[ "$OPTARG" != '' ]]; then
+          TEST_DIR="$OPTARG"
+        fi
+        ;;
+     t)
+        BUILD_FROM_TAR="$OPTARG"
+        ;;
      *)
         usage
         error_msg "No flag ${OPTARG}"
@@ -96,6 +120,8 @@
   esac
 done
 
+TEST_TARFILE=https://github.com/linux-test-project/ltp/releases/download/"${LTP_VERSION}"/ltp-full-"${LTP_VERSION}".tar.xz
+
 if [ -n "${SKIPFILE_YAML}" ]; then
     export SKIPFILE_PATH="${SCRIPTPATH}/generated_skipfile"
     generate_skipfile
@@ -105,20 +131,6 @@
     SKIPFILE="-S ${SKIPFILE_PATH}"
 fi
 
-# Install LTP test suite
-install_ltp() {
-    rm -rf /opt/ltp
-    mkdir -p /opt/ltp
-    # shellcheck disable=SC2164
-    cd /opt/ltp
-    # shellcheck disable=SC2140
-    wget https://github.com/linux-test-project/ltp/releases/download/"${LTP_VERSION}"/ltp-full-"${LTP_VERSION}".tar.xz
-    tar --strip-components=1 -Jxf ltp-full-"${LTP_VERSION}".tar.xz
-    ./configure
-    make -j8 all
-    make SKIP_IDCHECK=1 install
-}
-
 # Parse LTP output
 parse_ltp_output() {
     grep -E "PASS|FAIL|CONF"  "$1" \
@@ -129,7 +141,7 @@
 # Run LTP test suite
 run_ltp() {
     # shellcheck disable=SC2164
-    cd "${LTP_PATH}"
+    cd "${LTP_INSTALL_PATH}"
     # shellcheck disable=SC2174
     mkdir -m 777 -p "${LTP_TMPDIR}"
 
@@ -162,25 +174,39 @@
     fi
 }
 
-# Test run.
-! check_root && error_msg "This script must be run as root"
-create_out_dir "${OUTPUT}"
+get_tarfile() {
+    local test_tarfile="$1"
+    mkdir "${TEST_DIR}"
+    pushd "${TEST_DIR}" || exit 1
 
-info_msg "About to run ltp test..."
-info_msg "Output directory: ${OUTPUT}"
+    wget "${test_tarfile}"
+    tar --strip-components=1 -Jxf "$(basename "${test_tarfile}")"
+    popd || exit 1
+}
 
-if [ "${SKIP_INSTALL}" = "True" ] || [ "${SKIP_INSTALL}" = "true" ]; then
-    info_msg "install_ltp skipped"
-else
+build_install_tests() {
+    rm -rf "${LTP_INSTALL_PATH}"
+    pushd "${TEST_DIR}" || exit 1
+    [[ -n "${TEST_GIT_URL}" ]] && make autotools
+    ./configure
+    make -j"$(proc)" all
+    make SKIP_IDCHECK=1 install
+    popd || exit 1
+}
+
+install() {
+    dist=
     dist_name
     # shellcheck disable=SC2154
     case "${dist}" in
       debian|ubuntu)
-        pkgs="xz-utils flex bison build-essential wget curl net-tools quota genisoimage sudo libaio-dev expect automake acl"
+        [[ -n "${TEST_GIT_URL}" ]] && pkgs="git"
+        pkgs="${pkgs} xz-utils flex bison build-essential wget curl net-tools quota genisoimage sudo libaio-dev libattr1-dev libcap-dev expect automake acl autotools-dev autoconf m4 pkgconf"
         install_deps "${pkgs}" "${SKIP_INSTALL}"
         ;;
       centos|fedora)
-        pkgs="xz flex bison make automake gcc gcc-c++ kernel-devel wget curl net-tools quota genisoimage sudo libaio expect acl"
+        [[ -n "${TEST_GIT_URL}" ]] && pkgs="git-core"
+        pkgs="${pkgs} xz flex bison make automake gcc gcc-c++ kernel-devel wget curl net-tools quota genisoimage sudo libaio-devel libattr-devel libcap-devel m4 au expect acl pkgconf"
         install_deps "${pkgs}" "${SKIP_INSTALL}"
         ;;
       *)
@@ -198,9 +224,30 @@
             fi
         fi
     fi
+}
 
-    info_msg "Run install_ltp"
-    install_ltp
+# Test run.
+! check_root && error_msg "This script must be run as root"
+create_out_dir "${OUTPUT}"
+
+info_msg "About to run ltp test..."
+info_msg "Output directory: ${OUTPUT}"
+
+if [ "${SKIP_INSTALL}" = "true" ] || [ "${SKIP_INSTALL}" = "True" ]; then
+    info_msg "${TEST_PROGRAM} installation skipped altogether"
+else
+    install
+fi
+
+if [ ! -d ${LTP_INSTALL_PATH} ]; then
+    if [ "${BUILD_FROM_TAR}" = "true" ] || [ "${BUILD_FROM_TAR}" = "True" ]; then
+        get_tarfile "${TEST_TARFILE}"
+    elif [ -n "${TEST_GIT_URL}" ]; then
+        get_test_program "${TEST_GIT_URL}" "${TEST_DIR}" "${LTP_VERSION}" "${TEST_PROGRAM}"
+    else
+        error_msg "I'm confused, get me out of here, can't fetch tar or test version."
+    fi
+    build_install_tests
 fi
 info_msg "Running prep_system"
 prep_system
diff --git a/automated/linux/ltp/ltp.yaml b/automated/linux/ltp/ltp.yaml
index ee6b0c6..5f5471b 100644
--- a/automated/linux/ltp/ltp.yaml
+++ b/automated/linux/ltp/ltp.yaml
@@ -17,8 +17,6 @@
         - vexpress-tc2
         - ifc6410
 params:
-    # LTP version
-    LTP_VERSION: 20180926
     TST_CMDFILES: syscalls,mm,math,timers,fcntl-locktests,ipc,fsx,fs,hugetlb,io,nptl,pty,containers,fs_bind,filecaps,admin_tools,connectors
 
     # SKIPFILE can be a filename from dir ./automated/linux/ltp/, an http URL,
@@ -45,8 +43,31 @@
     # root's password. Needed by ltp/su01.
     ROOT_PASSWD: root
 
+    # If the following parameter is set, then the LTP suite is
+    # cloned and used unconditionally. In particular, the version
+    # of the suite is set to the commit pointed to by the
+    # parameter. A simple choice for the value of the parameter
+    # is, e.g., HEAD.  If, instead, the parameter is
+    # not set, then the suite present in TEST_DIR is used.
+    # LTP version
+    LTP_VERSION: ""
+
+    # If next parameter is set, then the LTP suite is cloned
+    # from the URL in TEST_GIT_URL. Otherwise it is cloned from the
+    # standard repository for the suite. Note that cloning is done
+    # only if LTP_VERSION is not empty
+    TEST_GIT_URL: ""
+
+    # If next parameter is set, then the LTP suite is cloned to or
+    # looked for in TEST_DIR. Otherwise it is cloned to $(pwd)/ltp
+    TEST_DIR: ""
+
+    # BUILD_FROM_TAR, if you want to download and build LTP from
+    # a released tarball, set BUILD_FROM_TAR to 'true'. You have to
+    # specify the LTP_VERSION to a release e.g., 20180926.
+    BUILD_FROM_TAR: "false"
 run:
     steps:
         - cd ./automated/linux/ltp/
-        - ./ltp.sh -T "${TST_CMDFILES}" -s "${SKIP_INSTALL}" -v "${LTP_VERSION}" -M "${TIMEOUT_MULTIPLIER}" -R "${ROOT_PASSWD}" -b "${BOARD}" -d "${LTP_TMPDIR}" -g "${BRANCH}" -e "${ENVIRONMENT}" -S "${SKIPFILE}"
+        - ./ltp.sh -T "${TST_CMDFILES}" -s "${SKIP_INSTALL}" -v "${LTP_VERSION}" -M "${TIMEOUT_MULTIPLIER}" -R "${ROOT_PASSWD}" -b "${BOARD}" -d "${LTP_TMPDIR}" -g "${BRANCH}" -e "${ENVIRONMENT}" -S "${SKIPFILE}" -p "${TEST_DIR}" -u "${TEST_GIT_URL}" -t "${BUILD_FROM_TAR}"
         - ../../utils/send-to-lava.sh ./output/result.txt