1 #!/bin/bash
   2 
   3 #
   4 # Script to run jpackage tests.
   5 #
   6 
   7 
   8 # Fail fast
   9 set -e; set -o pipefail;
  10 
  11 
  12 workdir=/tmp/jpackage_jtreg_testing
  13 jtreg_jar=$workdir/jtreg/lib/jtreg.jar
  14 jpackage_test_selector=test/jdk/tools/jpackage
  15 
  16 
  17 find_packaging_tests ()
  18 {
  19   (cd "$open_jdk_with_jpackage_jtreg_tests" && \
  20     find "$jpackage_test_selector/$1" -type f -name '*.java' \
  21     | xargs grep -E -l '@key[[:space:]]+jpackagePlatformPackage')
  22 }
  23 
  24 
  25 find_all_packaging_tests ()
  26 {
  27   find_packaging_tests share
  28   case "$(uname -s)" in
  29     Darwin)
  30       find_packaging_tests macosx;;
  31     Linux)
  32       find_packaging_tests linux;;
  33     CYGWIN*|MINGW32*|MSYS*)
  34       find_packaging_tests windows;;
  35     *)
  36       fatal Failed to detect OS type;;
  37   esac
  38 }
  39 
  40 
  41 help_usage ()
  42 {
  43   echo "Usage: `basename $0` [options] [test_names]"
  44   echo "Options:"
  45   echo "  -h              - print this message"
  46   echo "  -v              - verbose output"
  47   echo "  -c              - keep jtreg cache"
  48   echo "  -a              - run all, not only SQE tests"
  49   echo "  -d              - dry run. Print jtreg command line, but don't execute it"
  50   echo "  -t <jdk>        - path to JDK to be tested [ mandatory ]"
  51   echo "  -j <openjdk>    - path to local copy of openjdk repo with jpackage jtreg tests"
  52   echo "                    Optional, default is openjdk repo where this script resides"
  53   echo "  -o <outputdir>  - path to folder where to copy artifacts for testing."
  54   echo "                    Optional, default is the current directory."
  55   echo '  -r <runtimedir> - value for `jpackage.test.runtime-image` property.'
  56   echo "                    Optional, for jtreg tests debug purposes only."
  57   echo '  -l <logfile>    - value for `jpackage.test.logfile` property.'
  58   echo "                    Optional, for jtreg tests debug purposes only."
  59   echo "  -m <mode>       - mode to run jtreg tests."
  60   echo '                    Should be one of `create`, `update`, `verify-install` or `verify-uninstall`.'
  61   echo '                    Optional, default mode is `update`.'
  62   echo '                    - `create`'
  63   echo '                      Remove all package bundles from the output directory before running jtreg tests.'
  64   echo '                    - `update`'
  65   echo '                      Run jtreg tests and overrite existing package bundles in the output directory.'
  66   echo '                    - `verify-install`'
  67   echo '                      Verify installed packages created with the previous run of the script.'
  68   echo '                    - `verify-uninstall`'
  69   echo '                      Verify packages created with the previous run of the script were uninstalled cleanly.'
  70   echo '                    - `print-default-tests`'
  71   echo '                      Print default list of packaging tests and exit.'
  72 }
  73 
  74 error ()
  75 {
  76   echo "$@" > /dev/stderr
  77 }
  78 
  79 fatal ()
  80 {
  81   error "$@"
  82   exit 1
  83 }
  84 
  85 fatal_with_help_usage ()
  86 {
  87   error "$@"
  88   help_usage
  89   exit 1
  90 }
  91 
  92 if command -v cygpath &> /dev/null; then
  93 to_native_path ()
  94 {
  95   cygpath -m "$@"
  96 }
  97 else
  98 to_native_path ()
  99 {
 100   echo "$@"
 101 }
 102 fi
 103 
 104 exec_command ()
 105 {
 106   if [ -n "$dry_run" ]; then
 107     echo "$@"
 108   else
 109     eval "$@"
 110   fi
 111 }
 112 
 113 
 114 # Path to JDK to be tested.
 115 test_jdk=
 116 
 117 # Path to local copy of open jdk repo with jpackage jtreg tests
 118 # hg clone http://hg.openjdk.java.net/jdk/sandbox
 119 # cd sandbox; hg update -r JDK-8200758-branch
 120 open_jdk_with_jpackage_jtreg_tests=$(dirname $0)/../../../../
 121 
 122 # Directory where to save artifacts for testing.
 123 output_dir=$PWD
 124 
 125 # Script and jtreg debug.
 126 verbose=
 127 jtreg_verbose="-verbose:fail,error,summary"
 128 
 129 keep_jtreg_cache=
 130 
 131 # Mode in which to run jtreg tests
 132 mode=update
 133 
 134 # jtreg extra arguments
 135 declare -a jtreg_args
 136 
 137 # Run all tests
 138 run_all_tests=
 139 
 140 mapfile -t tests < <(find_all_packaging_tests)
 141 
 142 while getopts "vahdct:j:o:r:m:l:" argname; do
 143   case "$argname" in
 144     v) verbose=yes;;
 145     a) run_all_tests=yes;;
 146     d) dry_run=yes;;
 147     c) keep_jtreg_cache=yes;;
 148     t) test_jdk="$OPTARG";;
 149     j) open_jdk_with_jpackage_jtreg_tests="$OPTARG";;
 150     o) output_dir="$OPTARG";;
 151     r) runtime_dir="$OPTARG";;
 152     l) logfile="$OPTARG";;
 153     m) mode="$OPTARG";;
 154     h) help_usage; exit 0;;
 155     ?) help_usage; exit 1;;
 156   esac
 157 done
 158 shift $(( OPTIND - 1 ))
 159 
 160 [ -z "$verbose" ] || { set -x; jtreg_verbose=-va; }
 161 
 162 if [ -z "$open_jdk_with_jpackage_jtreg_tests" ]; then
 163   fatal_with_help_usage "Path to openjdk repo with jpackage jtreg tests not specified"
 164 fi
 165 
 166 if [ "$mode" = "print-default-tests" ]; then
 167   exec_command for t in ${tests[@]}";" do echo '$t;' done
 168   exit
 169 fi
 170 
 171 if [ -z "$test_jdk" ]; then
 172   fatal_with_help_usage Path to test JDK not specified
 173 fi
 174 
 175 if [ -z "$JAVA_HOME" ]; then
 176   echo JAVA_HOME environment variable not set, will use java from test JDK [$test_jdk] to run jtreg
 177   JAVA_HOME="$test_jdk"
 178 fi
 179 if [ ! -e "$JAVA_HOME/bin/java" ]; then
 180   fatal JAVA_HOME variable is set to [$JAVA_HOME] value, but $JAVA_HOME/bin/java not found.
 181 fi
 182 
 183 if [ -n "$runtime_dir" ]; then
 184   if [ ! -d "$runtime_dir" ]; then
 185     fatal 'Value of `-r` option is set to non-existing directory'.
 186   fi
 187   jtreg_args+=("-Djpackage.test.runtime-image=$(to_native_path "$(cd "$runtime_dir" && pwd)")")
 188 fi
 189 
 190 if [ -n "$logfile" ]; then
 191   if [ ! -d "$(dirname "$logfile")" ]; then
 192     fatal 'Value of `-l` option specified a file in non-existing directory'.
 193   fi
 194   logfile="$(cd "$(dirname "$logfile")" && pwd)/$(basename "$logfile")"
 195   jtreg_args+=("-Djpackage.test.logfile=$(to_native_path "$logfile")")
 196 fi
 197 
 198 if [ "$mode" = create ]; then
 199   true
 200 elif [ "$mode" = update ]; then
 201   true
 202 elif [ "$mode" = verify-install ]; then
 203   jtreg_args+=("-Djpackage.test.action=$mode")
 204 elif [ "$mode" = verify-uninstall ]; then
 205   jtreg_args+=("-Djpackage.test.action=$mode")
 206 else
 207   fatal_with_help_usage 'Invalid value of -m option:' [$mode]
 208 fi
 209 
 210 if [ -z "$run_all_tests" ]; then
 211   jtreg_args+=(-Djpackage.test.SQETest=yes)
 212 fi
 213 
 214 # All remaining command line arguments are tests to run that should override the defaults
 215 [ $# -eq 0 ] || tests=($@)
 216 
 217 
 218 installJtreg ()
 219 {
 220   # Install jtreg if missing
 221   if [ ! -f "$jtreg_jar" ]; then
 222     exec_command mkdir -p "$workdir"
 223     # TODO - restore code to download or copy jtreg.jar
 224     # to $workdir/jtreg/lib/jtreg.jar
 225     fatal "ERROR: All Tests Disabled until locating jtreg.jar implemented."
 226   fi
 227 }
 228 
 229 
 230 preRun ()
 231 {
 232   local xargs_args=(-t --no-run-if-empty rm)
 233   if [ -n "$dry_run" ]; then
 234     xargs_args=(--no-run-if-empty echo rm)
 235   fi
 236 
 237   if [ ! -d "$output_dir" ]; then
 238     exec_command mkdir -p "$output_dir"
 239   fi
 240   [ ! -d "$output_dir" ] || output_dir=$(cd "$output_dir" && pwd)
 241 
 242   # Clean output directory
 243   [ "$mode" != "create" ] || find $output_dir -maxdepth 1 -type f -name '*.exe' -or -name '*.msi' -or -name '*.rpm' -or -name '*.deb' | xargs "${xargs_args[@]}"
 244 }
 245 
 246 
 247 run ()
 248 {
 249   local jtreg_cmdline=(\
 250     $JAVA_HOME/bin/java -jar $(to_native_path "$jtreg_jar") \
 251     "-Djpackage.test.output=$(to_native_path "$output_dir")" \
 252     "${jtreg_args[@]}" \
 253     -nr \
 254     "$jtreg_verbose" \
 255     -retain:all \
 256     -automatic \
 257     -ignore:run \
 258     -testjdk:"$(to_native_path $test_jdk)" \
 259     -dir:"$(to_native_path $open_jdk_with_jpackage_jtreg_tests)" \
 260     -reportDir:"$(to_native_path $workdir/run/results)" \
 261     -workDir:"$(to_native_path $workdir/run/support)" \
 262     "${tests[@]}" \
 263   )
 264 
 265   # Clear previous results
 266   [ -n "$keep_jtreg_cache" ] || exec_command rm -rf "$workdir"/run
 267 
 268   # Run jpackage jtreg tests to create artifacts for testing
 269   exec_command ${jtreg_cmdline[@]}
 270 }
 271 
 272 
 273 installJtreg
 274 preRun
 275 run