1 #!/bin/sh
   2 
   3 # Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
   4 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 # 
   6 # This code is free software; you can redistribute it and/or modify it
   7 # under the terms of the GNU General Public License version 2 only, as
   8 # published by the Free Software Foundation.
   9 # 
  10 # This code is distributed in the hope that it will be useful, but WITHOUT
  11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13 # version 2 for more details (a copy is included in the LICENSE file that
  14 # accompanied this code).
  15 # 
  16 # You should have received a copy of the GNU General Public License version
  17 # 2 along with this work; if not, write to the Free Software Foundation,
  18 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19 #
  20 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21 # or visit www.oracle.com if you need additional information or have any
  22 # questions.
  23 
  24 # @test
  25 # @bug 7110104
  26 # @build JMXStartStopTest JMXStartStopDoSomething
  27 # @run shell JMXStartStopTest.sh --jtreg --no-compile
  28 # @summary No word Failed expected in the test output
  29 
  30 _server=no
  31 _jtreg=no
  32 _compile=yes
  33 _testsuite="01,02,03,04,05,06,07,08,09,10,11,12,13"
  34 _port_one=50234
  35 _port_two=50235
  36 
  37 
  38 _testclasses=".classes"
  39 _testsrc=`pwd`
  40 
  41 _logname=".classes/output.txt"
  42 _lockFileName="JMXStartStop.lck"
  43 
  44 _compile(){
  45 
  46     if [ ! -d ${_testclasses} ]
  47     then
  48       mkdir -p ${_testclasses} 
  49     fi   
  50 
  51     rm -f ${_testclasses}/JMXStartStopTest.class
  52 
  53     # Compile testcase
  54     ${COMPILEJAVA}/bin/javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d ${_testclasses} \
  55         JMXStartStopDoSomething.java JMXStartStopTest.java 
  56 
  57     if [ ! -f ${_testclasses}/JMXStartStopTest.class ]
  58     then
  59       echo "ERROR: Can't compile"
  60       exit -1
  61     fi
  62 }
  63 
  64 _app_start(){
  65   ${TESTJAVA}/bin/java ${TESTVMOPTS} $* -cp ${_testclasses} JMXStartStopDoSomething  >> ${_logname} 2>&1 &
  66 
  67   x=0
  68   while [ ! -f ${_lockFileName} ]
  69   do
  70      if [ $x -gt 20 ]
  71      then
  72         echo "ERROR: Test app not started"
  73         if [ "${_jtreg}" = "yes" ]
  74         then
  75            exit -1
  76         fi   
  77      fi    
  78         
  79      echo "Waiting JMXStartStopDoSomething to start: $x"
  80      x=`expr $x + 1`
  81      sleep 1
  82   done
  83 }
  84 
  85 _get_pid(){
  86     ${COMPILEJAVA}/bin/jps ${TESTTOOLVMOPTS} | sed -n "/JMXStartStopDoSomething/s/ .*//p"
  87 }
  88 
  89 _app_stop(){
  90   rm ${_lockFileName}
  91 
  92   # wait until VM is actually shuts down
  93   while true 
  94   do
  95     npid=`_get_pid`
  96     if [ "${npid}" = "" ] 
  97     then
  98       break
  99     fi
 100     sleep 1
 101   done 
 102 }
 103 
 104 _exit_on_jtreg(){
 105   # Stop on first failed test under jtreg
 106   if [ "${_jtreg}" = "yes" ]
 107   then
 108       _app_stop
 109       exit -1
 110   fi
 111 }
 112 
 113 _testme(){
 114   ${TESTJAVA}/bin/java ${TESTVMOPTS} -cp ${_testclasses} JMXStartStopTest $*
 115 }   
 116 
 117   
 118 _jcmd(){
 119   ${TESTJAVA}/bin/jcmd ${TESTTOOLVMOPTS} JMXStartStopDoSomething $* > /dev/null 2>/dev/null
 120 } 
 121 
 122 _echo(){
 123     echo "$*"
 124     echo "$*" >> ${_logname}
 125 }
 126    
 127 # ============= TESTS ======================================
 128    
 129 test_01(){
 130 # Run an app with JMX enabled stop it and 
 131 # restart on other port
 132         
 133     _echo "**** Test one ****"      
 134 
 135     _app_start  -Dcom.sun.management.jmxremote.port=$1 \
 136                 -Dcom.sun.management.jmxremote.authenticate=false \
 137                 -Dcom.sun.management.jmxremote.ssl=false 
 138 
 139     res1=`_testme $1`
 140 
 141     _jcmd ManagementAgent.stop
 142 
 143     res2=`_testme $1`
 144 
 145     _jcmd ManagementAgent.start jmxremote.port=$2
 146 
 147     res3=`_testme $2`
 148 
 149     if [ "${res1}" = "OK_CONN" -a "${res2}" = "NO_CONN" -a "${res3}" = "OK_CONN" ] 
 150     then
 151         _echo "Passed"
 152     else
 153         _echo "Failed r1(OK):${res1} r2(NO):${res2} r3(OK):${res3}"
 154         _exit_on_jtreg
 155     fi
 156 
 157     _app_stop
 158 }  
 159    
 160 test_02(){
 161 # Run an app without JMX enabled 
 162 # start JMX by jcmd
 163 
 164     _echo "**** Test two ****"      
 165     _app_start  
 166 
 167     _jcmd ManagementAgent.start jmxremote.port=$1 jmxremote.authenticate=false jmxremote.ssl=false 
 168 
 169     res1=`_testme $1`
 170 
 171     if [ "${res1}" = "OK_CONN" ] 
 172     then
 173         _echo "Passed"
 174     else
 175         _echo "Failed r1(OK):${res1}"
 176         _exit_on_jtreg
 177     fi
 178     _app_stop
 179 }   
 180    
 181 test_03(){
 182 # Run an app without JMX enabled 
 183 # start JMX by jcmd on one port than on other one
 184 
 185     _echo "**** Test three ****"        
 186     _app_start  
 187 
 188     _jcmd ManagementAgent.start jmxremote.port=$1 jmxremote.authenticate=false jmxremote.ssl=false 
 189 
 190 # Second agent shouldn't start
 191     _jcmd ManagementAgent.start jmxremote.port=$2 jmxremote.authenticate=false jmxremote.ssl=false
 192 
 193 # First agent should connect
 194     res1=`_testme $1`
 195 
 196     if [ "${res1}" = "OK_CONN" ] 
 197     then
 198         _echo "Passed $1"
 199     else
 200         _echo "Failed r1(NO):${res1}"
 201         _exit_on_jtreg
 202     fi
 203 
 204 #Second agent shouldn't connect
 205     res1=`_testme $2`
 206 
 207     if [ "${res1}" = "NO_CONN" ] 
 208     then
 209         _echo "Passed $2"
 210     else
 211         _echo "Failed r1(OK):${res1}"
 212         _exit_on_jtreg
 213     fi
 214 
 215     _app_stop
 216 }   
 217    
 218 test_04(){
 219 # Run an app without JMX enabled 
 220 # start JMX by jcmd on one port, specify rmi port explicitly
 221 
 222     _echo "**** Test four ****"     
 223     _app_start  
 224 
 225     _jcmd ManagementAgent.start jmxremote.port=$1 jmxremote.rmi.port=$2 jmxremote.authenticate=false jmxremote.ssl=false 
 226 
 227 # First agent should connect
 228     res1=`_testme $1 $2`
 229 
 230     if [ "${res1}" = "OK_CONN" ] 
 231     then
 232         _echo "Passed $1 $2"
 233     else
 234         _echo "Failed r1(NO):${res1}"
 235         _exit_on_jtreg
 236     fi
 237 
 238     _app_stop
 239 }   
 240 
 241 test_05(){
 242 # Run an app without JMX enabled, it will enable local server
 243 # but should leave remote server disabled  
 244 
 245     _echo "**** Test five ****"     
 246     _app_start  
 247 
 248     _jcmd ManagementAgent.start jmxremote=1 
 249 
 250     # First agent should connect
 251     res1=`_testme $1`
 252 
 253     if [ "${res1}" = "NO_CONN" ] 
 254     then
 255         _echo "Passed $1 $2"
 256     else
 257         _echo "Failed r1(OK):${res1}"
 258         _exit_on_jtreg
 259     fi
 260 
 261     _app_stop
 262 }   
 263 
 264 test_06(){
 265 # Run an app without JMX enabled 
 266 # start JMX by jcmd on one port, specify rmi port explicitly
 267 # attempt to start it again
 268 # 1) with the same port 
 269 # 2) with other port
 270 # 3) attempt to stop it twice
 271 # Check for valid messages in the output    
 272 
 273     _echo "**** Test six ****"      
 274     _app_start  
 275 
 276     _jcmd ManagementAgent.start jmxremote.port=$1 jmxremote.authenticate=false jmxremote.ssl=false 
 277 
 278     # First agent should connect
 279     res1=`_testme $1 $2`
 280 
 281     if [ "${res1}" = "OK_CONN" ] 
 282     then
 283         _echo "Passed $1 $2"
 284     else
 285         _echo "Failed r1(NO):${res1}"
 286         _exit_on_jtreg
 287     fi
 288 
 289     _jcmd ManagementAgent.start jmxremote.port=$1 jmxremote.authenticate=false jmxremote.ssl=false 
 290 
 291     _jcmd ManagementAgent.start jmxremote.port=$2 jmxremote.authenticate=false jmxremote.ssl=false 
 292 
 293     _jcmd ManagementAgent.stop
 294 
 295     _jcmd ManagementAgent.stop
 296 
 297     _jcmd ManagementAgent.start jmxremote.port=22 jmxremote.rmi.port=$2 jmxremote.authenticate=false jmxremote.ssl=false 
 298 
 299     _app_stop
 300 }   
 301 
 302 test_07(){
 303 # Run an app without JMX enabled, but with some properties set 
 304 # in command line.
 305 # make sure these properties overriden corectly 
 306 
 307     _echo "**** Test seven ****"        
 308 
 309     _app_start   -Dcom.sun.management.jmxremote.authenticate=false \
 310                  -Dcom.sun.management.jmxremote.ssl=true 
 311 
 312     res1=`_testme $1`
 313 
 314     _jcmd ManagementAgent.start jmxremote.port=$2 jmxremote.authenticate=false jmxremote.ssl=false
 315 
 316     res2=`_testme $2`
 317 
 318     if [ "${res1}" = "NO_CONN" -a "${res2}" = "OK_CONN" ] 
 319     then
 320         echo "Passed"
 321     else
 322         _echo "Failed r1(NO):${res1} r2(OK):${res2}"
 323         _exit_on_jtreg
 324     fi
 325 
 326     _app_stop
 327 }   
 328 
 329 test_08(){
 330 # Run an app with JMX enabled and with some properties set 
 331 # in command line.
 332 # stop JMX agent and then start it again with different property values
 333 # make sure these properties overriden corectly 
 334 
 335     _echo "**** Test eight ****"        
 336 
 337     _app_start  -Dcom.sun.management.jmxremote.port=$1 \
 338                 -Dcom.sun.management.jmxremote.authenticate=false \
 339                 -Dcom.sun.management.jmxremote.ssl=true 
 340 
 341     res1=`_testme $1`
 342 
 343     _jcmd ManagementAgent.stop
 344 
 345     res2=`_testme $1`
 346 
 347     _jcmd ManagementAgent.start jmxremote.port=$2 jmxremote.authenticate=false jmxremote.ssl=false
 348 
 349     res3=`_testme $2`
 350 
 351     if [ "${res1}" = "NO_CONN" -a "${res2}" = "NO_CONN" -a "${res3}" = "OK_CONN" ] 
 352     then
 353         _echo "Passed"
 354     else
 355         _echo "Failed r1(NO):${res1} r2(NO):${res2} r3(OK):${res3}"
 356         _exit_on_jtreg
 357     fi
 358  
 359     _app_stop
 360 }   
 361 
 362 test_09(){
 363 # Run an app with JMX enabled and with some properties set 
 364 # in command line.
 365 # stop JMX agent and then start it again with different property values
 366 # specifing some property in management config file and some of them
 367 # in command line
 368 # make sure these properties overriden corectly 
 369 
 370     _echo "**** Test nine ****"     
 371 
 372     _app_start -Dcom.sun.management.config.file=${_testsrc}/management_cl.properties \
 373                -Dcom.sun.management.jmxremote.authenticate=false 
 374 
 375     res1=`_testme $1`
 376 
 377     _jcmd ManagementAgent.stop
 378 
 379     res2=`_testme $1`
 380 
 381     _jcmd ManagementAgent.start config.file=${_testsrc}/management_jcmd.properties \
 382                                 jmxremote.authenticate=false jmxremote.port=$2
 383 
 384     res3=`_testme $2`
 385 
 386     if [ "${res1}" = "NO_CONN" -a "${res2}" = "NO_CONN" -a "${res3}" = "OK_CONN" ] 
 387     then
 388         _echo "Passed"
 389     else
 390         _echo "Failed r1(NO):${res1} r2(NO):${res2} r3(OK):${res3}"
 391         _exit_on_jtreg
 392     fi
 393  
 394     _app_stop
 395 }   
 396 
 397 test_10(){
 398 # Run an app with JMX enabled and with some properties set 
 399 # in command line.
 400 # stop JMX agent and then start it again with different property values
 401 # stop JMX agent again and then start it without property value
 402 # make sure these properties overriden corectly 
 403 
 404     _echo "**** Test ten ****"      
 405 
 406     _app_start  -Dcom.sun.management.jmxremote.port=$1 \
 407                 -Dcom.sun.management.jmxremote.authenticate=false \
 408                 -Dcom.sun.management.jmxremote.ssl=true 
 409 
 410     res1=`_testme $1`
 411 
 412     _jcmd ManagementAgent.stop
 413     _jcmd ManagementAgent.start jmxremote.ssl=false jmxremote.port=$1
 414 
 415 
 416     res2=`_testme $1`
 417 
 418     _jcmd ManagementAgent.stop
 419     _jcmd ManagementAgent.start jmxremote.port=$1
 420 
 421     res3=`_testme $1`
 422 
 423     if [ "${res1}" = "NO_CONN" -a "${res2}" = "OK_CONN" -a "${res3}" = "NO_CONN" ] 
 424     then
 425         _echo "Passed"
 426     else
 427         _echo "Failed r1(NO):${res1} r2(OK):${res2} r3(NO):${res3}"
 428         _exit_on_jtreg
 429     fi
 430  
 431     _app_stop
 432 }   
 433 
 434 test_11(){
 435 # Run an app with JMX enabled 
 436 # stop remote agent 
 437 # make sure local agent is not affected
 438 
 439     _echo "**** Test eleven ****"       
 440 
 441     _app_start  -Dcom.sun.management.jmxremote.port=$2 \
 442                 -Dcom.sun.management.jmxremote.authenticate=false \
 443                 -Dcom.sun.management.jmxremote.ssl=false 
 444       
 445     res1=`_testme $2`
 446 
 447     _jcmd ManagementAgent.stop
 448 
 449     pid=`${COMPILEJAVA}/bin/jps ${TESTTOOLVMOPTS} | sed -n "/JMXStartStopDoSomething/s/ .*//p"`
 450     res2=`_testme local ${pid}`
 451 
 452     if [ "${res1}" = "OK_CONN" -a "${res2}" = "OK_CONN" ] 
 453     then
 454         _echo "Passed"
 455     else
 456         _echo "Failed r1(OK):${res1} r2(OK):${res2}"
 457         _exit_on_jtreg
 458     fi
 459  
 460     _app_stop
 461 }   
 462 
 463 test_12(){
 464 # Run an app with JMX disabled 
 465 # start local agent only
 466 
 467     _echo "**** Test twelve ****"       
 468 
 469     _app_start 
 470       
 471     res1=`_testme $1`
 472 
 473     _jcmd ManagementAgent.start_local
 474 
 475     pid=`_get_pid`
 476     if [ "x${pid}" = "x" ]
 477     then
 478         res2="NO_CONN"
 479     else
 480         res2=`_testme local ${pid}`
 481     fi
 482 
 483     if [ "${res1}" = "NO_CONN" -a "${res2}" = "OK_CONN" ] 
 484     then
 485         _echo "Passed"
 486     else
 487         _echo "Failed r1(NO):${res1} r2(OK):${res2}"
 488         _exit_on_jtreg
 489     fi
 490  
 491     _app_stop
 492 }   
 493 
 494 test_13(){
 495 # Run an app with -javaagent make sure it works as expected - system properties are ignored
 496 
 497     _echo "**** Test thirteen ****"       
 498                            
 499     AGENT="${TESTJAVA}/jre/lib/management-agent.jar"
 500     if [ ! -f ${AGENT} ]
 501     then
 502         AGENT="${TESTJAVA}/lib/management-agent.jar"
 503     fi
 504 
 505     _app_start -javaagent:${AGENT}=com.sun.management.jmxremote.port=$1,com.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false 
 506       
 507     res1=`_testme $1`
 508 
 509     if [ "${res1}" = "NO_CONN" ] 
 510     then
 511         _echo "Passed"
 512     else
 513         _echo "Failed r1(NO):${res1}"
 514         _exit_on_jtreg
 515     fi
 516  
 517     _app_stop
 518 }   
 519 
 520 # ============= MAIN =======================================
 521 
 522 if [ "x${TESTJAVA}" = "x" ]
 523 then
 524   echo "TESTJAVA env have to be set"
 525   exit
 526 fi
 527 
 528 if [ ! -x "${TESTJAVA}/bin/jcmd" ]
 529 then
 530   echo "${TESTJAVA}/bin/jcmd"
 531   echo "Doesn't exist or not an executable"
 532   exit
 533 fi
 534 
 535 
 536 #------------------------------------------------------------------------------
 537 # reading parameters 
 538 
 539 for parm in "$@"  
 540 do
 541    case $parm in
 542   --jtreg)        _jtreg=yes    ;;
 543   --no-compile)   _compile=no   ;;
 544   --testsuite=*)  _testsuite=`_echo $parm | sed "s,^--.*=\(.*\),\1,"`  ;;
 545   --port-one=*)   _port_one=`_echo $parm | sed "s,^--.*=\(.*\),\1,"`  ;;
 546   --port-two=*)   _port_two=`_echo $parm | sed "s,^--.*=\(.*\),\1,"`  ;;
 547   *) 
 548      echo "Undefined parameter $parm. Try --help for help" 
 549      exit 
 550    ;;
 551  esac 
 552 done
 553 
 554 if [ ${_compile} = "yes" ]
 555 then
 556  _compile
 557 fi
 558 
 559 if [ ${_jtreg} = "yes" ]
 560 then
 561  _testclasses=${TESTCLASSES}
 562  _testsrc=${TESTSRC}
 563  _logname="JMXStartStopTest_output.txt"
 564 fi
 565 
 566 rm -f ${_logname}
 567 
 568 # Local mode tests
 569 for i in `echo ${_testsuite} | sed -e "s/,/ /g"`
 570 do
 571   test_${i} ${_port_one} ${_port_two}
 572 done
 573 
 574