1 #!/bin/sh
   2 
   3 #
   4 # Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved.
   5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6 #
   7 # This code is free software; you can redistribute it and/or modify it
   8 # under the terms of the GNU General Public License version 2 only, as
   9 # published by the Free Software Foundation.
  10 #
  11 # This code is distributed in the hope that it will be useful, but WITHOUT
  12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14 # version 2 for more details (a copy is included in the LICENSE file that
  15 # accompanied this code).
  16 #
  17 # You should have received a copy of the GNU General Public License version
  18 # 2 along with this work; if not, write to the Free Software Foundation,
  19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20 #
  21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22 # or visit www.oracle.com if you need additional information or have any
  23 # questions.
  24 #
  25 
  26 
  27 # @test
  28 # @bug 5016507 6173612 6319776 6342019 6484550
  29 # @summary Start a managed VM and test that a management tool can connect
  30 #          without connection or username/password details.
  31 #          TestManager will attempt a connection to the address obtained from 
  32 #          both agent properties and jvmstat buffer.
  33 #
  34 # @build TestManager TestApplication
  35 # @run shell/timeout=300 LocalManagementTest.sh
  36 
  37 
  38 doTest()
  39 {
  40     echo ''
  41 
  42     outputfile=${TESTCLASSES}/Test.out
  43     rm -f ${outputfile}
  44 
  45     # Start VM with given options
  46     echo "+ $JAVA ${TESTVMOPTS} $1 Test"
  47     $JAVA ${TESTVMOPTS} $1 TestApplication > ${outputfile}&
  48     pid=$!
  49  
  50     # Wait for managed VM to startup
  51     echo "Waiting for VM to startup..."
  52     attempts=0
  53     while true; do
  54         sleep 1
  55         port=`tail -1 ${outputfile}`
  56         if [ ! -z "$port" ]; then
  57             # In case of errors wait time for output to be flushed
  58             sleep 1
  59             cat ${outputfile}
  60             break
  61         fi
  62       attempts=`expr $attempts + 1`
  63       echo "Waiting $attempts second(s) ..."
  64     done
  65 
  66     # Start the manager - this should connect to VM
  67     sh -xc "$JAVA ${TESTVMOPTS} -classpath ${TESTCLASSES}:${TESTJAVA}/lib/tools.jar \
  68         TestManager $pid $port"  2>&1
  69     if [ $? != 0 ]; then failures=`expr $failures + 1`; fi
  70 }
  71 
  72 
  73 # Check we are run from jtreg
  74 if [ -z "${TESTCLASSES}" ]; then
  75     echo "Test is designed to be run from jtreg only"
  76     exit 0
  77 fi
  78 
  79 # For now this test passes silently on Windows - there are 2 reasons
  80 # to skip it :-
  81 #
  82 # 1. No jstat instrumentation buffers if FAT32 so need
  83 #    -XX:+PerfBypassFileSystemCheck 
  84 # 2. $! is used to get the pid of the created process but it's not
  85 #    reliable on older versions of MKS. Also negative pids are returned
  86 #    on Windows 98.
  87 
  88 os=`uname -s`
  89 if [ "$os" != "Linux" -a "$os" != "SunOS" ]; then
  90     echo "Test not designed to run on this operating system, skipping..."
  91     exit 0
  92 fi
  93 
  94 JAVA=${TESTJAVA}/bin/java
  95 CLASSPATH=${TESTCLASSES}
  96 export CLASSPATH
  97 
  98 failures=0
  99 
 100 # Test 1 
 101 doTest "-Dcom.sun.management.jmxremote" 
 102 
 103 # Test 2
 104 AGENT="${TESTJAVA}/jre/lib/management-agent.jar"
 105 if [ ! -f ${AGENT} ]; then
 106   AGENT="${TESTJAVA}/lib/management-agent.jar"
 107 fi
 108 doTest "-javaagent:${AGENT}" 
 109 
 110 # Test 3 - no args (blank) - manager should attach and start agent
 111 doTest " " 
 112 
 113 # Test 4 - sanity check arguments to management-agent.jar
 114 echo ' '
 115 sh -xc "${JAVA} ${TESTVMOPTS} -javaagent:${AGENT}=com.sun.management.jmxremote.port=7775,\
 116 com.sun.management.jmxremote.authenticate=false,com.sun.management.jmxremote.ssl=false \
 117   TestApplication -exit" 2>&1
 118 if [ $? != 0 ]; then failures=`expr $failures + 1`; fi
 119 
 120 # Test 5 - use DNS-only name service
 121 doTest "-Dsun.net.spi.namservice.provider.1=\"dns,sun\"" 
 122 
 123 #
 124 # Results
 125 #
 126 echo ''
 127 if [ $failures -gt 0 ];
 128   then echo "$failures test(s) failed";
 129   else echo "All test(s) passed"; fi
 130 exit $failures
 131