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_HOME" ]; then
 185   if [ -z "$JTREG_BUNDLE_URL" ]; then
 186     fatal 'JTREG_HOME or JTREG_BUNDLE_URL environment variable is not set. Link to JTREG bundle can be found at https://openjdk.java.net/jtreg/'.
 187   fi
 188 fi
 189 
 190 if [ -n "$runtime_dir" ]; then
 191   if [ ! -d "$runtime_dir" ]; then
 192     fatal 'Value of `-r` option is set to non-existing directory'.
 193   fi
 194   jtreg_args+=("-Djpackage.test.runtime-image=$(to_native_path "$(cd "$runtime_dir" && pwd)")")
 195 fi
 196 
 197 if [ -n "$logfile" ]; then
 198   if [ ! -d "$(dirname "$logfile")" ]; then
 199     fatal 'Value of `-l` option specified a file in non-existing directory'.
 200   fi
 201   logfile="$(cd "$(dirname "$logfile")" && pwd)/$(basename "$logfile")"
 202   jtreg_args+=("-Djpackage.test.logfile=$(to_native_path "$logfile")")
 203 fi
 204 
 205 if [ "$mode" = create ]; then
 206   true
 207 elif [ "$mode" = update ]; then
 208   true
 209 elif [ "$mode" = verify-install ]; then
 210   jtreg_args+=("-Djpackage.test.action=$mode")
 211 elif [ "$mode" = verify-uninstall ]; then
 212   jtreg_args+=("-Djpackage.test.action=$mode")
 213 else
 214   fatal_with_help_usage 'Invalid value of -m option:' [$mode]
 215 fi
 216 
 217 if [ -z "$run_all_tests" ]; then
 218   jtreg_args+=(-Djpackage.test.SQETest=yes)
 219 fi
 220 
 221 # All remaining command line arguments are tests to run that should override the defaults
 222 [ $# -eq 0 ] || tests=($@)
 223 
 224 
 225 installJtreg ()
 226 {
 227   # Install jtreg if missing
 228   if [ -z "$JTREG_HOME" ]; then
 229     if [ ! -f "$jtreg_jar" ]; then
 230       exec_command mkdir -p "$workdir"
 231       if [[ ${jtreg_bundle: -7} == ".tar.gz" ]]; then
 232         exec_command "(" cd "$workdir" "&&" wget "$jtreg_bundle" "&&" tar -xzf "$(basename $jtreg_bundle)" ";" rm -f "$(basename $jtreg_bundle)" ")"
 233       else
 234         if [[ ${jtreg_bundle: -4} == ".zip" ]]; then
 235           exec_command "(" cd "$workdir" "&&" wget "$jtreg_bundle" "&&" unzip "$(basename $jtreg_bundle)" ";" rm -f "$(basename $jtreg_bundle)" ")"
 236         else
 237           fatal 'Unsupported extension of JREG bundle ['$JTREG_BUNDLE_URL']. Only *.zip or *.tar.gz is supported.'
 238         fi
 239       fi
 240     fi
 241   else
 242     # use jtreg provided via JTREG_HOME
 243     jtreg_jar=$JTREG_HOME/lib/jtreg.jar
 244   fi
 245 
 246   echo 'jtreg jar file: '$jtreg_jar
 247 }
 248 
 249 
 250 preRun ()
 251 {
 252   local xargs_args=(-t --no-run-if-empty rm)
 253   if [ -n "$dry_run" ]; then
 254     xargs_args=(--no-run-if-empty echo rm)
 255   fi
 256 
 257   if [ ! -d "$output_dir" ]; then
 258     exec_command mkdir -p "$output_dir"
 259   fi
 260   [ ! -d "$output_dir" ] || output_dir=$(cd "$output_dir" && pwd)
 261 
 262   # Clean output directory
 263   [ "$mode" != "create" ] || find $output_dir -maxdepth 1 -type f -name '*.exe' -or -name '*.msi' -or -name '*.rpm' -or -name '*.deb' | xargs "${xargs_args[@]}"
 264 }
 265 
 266 
 267 run ()
 268 {
 269   local jtreg_cmdline=(\
 270     $JAVA_HOME/bin/java -jar $(to_native_path "$jtreg_jar") \
 271     "-Djpackage.test.output=$(to_native_path "$output_dir")" \
 272     "${jtreg_args[@]}" \
 273     -nr \
 274     "$jtreg_verbose" \
 275     -retain:all \
 276     -automatic \
 277     -ignore:run \
 278     -testjdk:"$(to_native_path $test_jdk)" \
 279     -dir:"$(to_native_path $open_jdk_with_jpackage_jtreg_tests)" \
 280     -reportDir:"$(to_native_path $workdir/run/results)" \
 281     -workDir:"$(to_native_path $workdir/run/support)" \
 282     "${tests[@]}" \
 283   )
 284 
 285   # Clear previous results
 286   [ -n "$keep_jtreg_cache" ] || exec_command rm -rf "$workdir"/run
 287 
 288   # Run jpackage jtreg tests to create artifacts for testing
 289   exec_command ${jtreg_cmdline[@]}
 290 }
 291 
 292 
 293 installJtreg
 294 preRun
 295 run
--- EOF ---