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