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