1 #
   2 # Copyright (c) 2015, Red Hat Inc.
   3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4 #
   5 # This code is free software; you can redistribute it and/or modify it
   6 # under the terms of the GNU General Public License version 2 only, as
   7 # published by the Free Software Foundation.
   8 #
   9 # This code is distributed in the hope that it will be useful, but WITHOUT
  10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 # version 2 for more details (a copy is included in the LICENSE file that
  13 # accompanied this code).
  14 #
  15 # You should have received a copy of the GNU General Public License version
  16 # 2 along with this work; if not, write to the Free Software Foundation,
  17 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18 #
  19 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20 # or visit www.oracle.com if you need additional information or have any
  21 # questions.
  22 #
  23 
  24 # NOTE:
  25 #    This test requires at least a setup similar to the following in
  26 #    /etc/hosts file (or the windows equivalent). I.e. it expects it to
  27 #    be multi-homed and not both being the loop-back interface.
  28 #    For example:
  29 #    ----->8-------- /etc/hosts ----------->8---
  30 #    127.0.0.1   localhost
  31 #    192.168.0.1 localhost
  32 #    ----->8-------- /etc/hosts ----------->8---
  33 #
  34 # @test
  35 # @bug     6425769
  36 # @summary Test JMX agent host address binding. Same ports but different
  37 #          interfaces to bind to (non-ssl version).
  38 #
  39 # @modules java.management/sun.management
  40 #          java.management/sun.management.jmxremote
  41 # @build IPForLocalHostResolver JMXInterfaceBindingTest
  42 # @run shell/timeout=5000  JMXInterfaceBindingTest.sh
  43 
  44 # Define the Java class test name
  45 TESTCLASS="JMXInterfaceBindingTest"
  46 export TESTCLASS
  47 
  48 # Fixed ports we use for JMX/RMI on all interfaces
  49 JMX_PORT=9111
  50 RMI_PORT=9112
  51 
  52 # See JMXInterfaceBindingTest.java which prints this once the agent is ready
  53 # and connections to the JMX/RMI ports worked.
  54 READY_MSG="MainThread: Ready for connections"
  55 MAX_SLEEP=3
  56 
  57 
  58 ips=$( ${TESTJAVA}/bin/java ${TESTVMOPTS} -classpath ${TESTCLASSPATH} \
  59  -Dtest.src=${TESTCLASSES} IPForLocalHostResolver );
  60 
  61 count=0
  62 for i in ${ips}; do
  63   count=$(( ${count} + 1 ))
  64 done
  65 
  66 if [ ${count} -lt 2 ]; then
  67    echo "Ignoring manual test since no more than one IPs are configured for 'localhost'"
  68    exit 0
  69 fi
  70 echo "Going to attempt binding to the following specific localhost interfaces: "
  71 echo ${ips}
  72 
  73 # Launch it once binding to interface A then another time binding to interface B and so on
  74 # using the same ports.
  75 for iface in ${ips}; do
  76    echo "Launching Java tester for triplet (HOSTNAME,JMX_PORT,RMI_PORT) == (${iface},${JMX_PORT},${RMI_PORT})"
  77    echo ${TESTJAVA}/bin/java ${TESTVMOPTS} -Dtest.src=${TESTCLASSES} \
  78      -classpath ${TESTCLASSPATH} \
  79      -Dcom.sun.management.jmxremote.host=${iface} \
  80      -Dcom.sun.management.jmxremote.port=${JMX_PORT} \
  81      -Dcom.sun.management.jmxremote.rmi.port=${RMI_PORT} \
  82      -Dcom.sun.management.jmxremote.authenticate=false \
  83      -Dcom.sun.management.jmxremote.ssl=false \
  84      ${TESTCLASS} ${iface} ${JMX_PORT} ${RMI_PORT}
  85    ${TESTJAVA}/bin/java ${TESTVMOPTS} -Dtest.src=${TESTCLASSES} \
  86      -classpath ${TESTCLASSPATH} \
  87      -Dcom.sun.management.jmxremote.host=${iface} \
  88      -Dcom.sun.management.jmxremote.port=${JMX_PORT} \
  89      -Dcom.sun.management.jmxremote.rmi.port=${RMI_PORT} \
  90      -Dcom.sun.management.jmxremote.authenticate=false \
  91      -Dcom.sun.management.jmxremote.ssl=false \
  92      ${TESTCLASS} ${iface} ${JMX_PORT} ${RMI_PORT} "false" 2>&1 > java_${iface}.log & 
  93    echo $! > java_${iface}.pid
  94    # wait for jmx agent to start
  95    echo "Waiting for java process to become available"
  96    attempt=0
  97    while ! grep -q "${READY_MSG}" java_${iface}.log; do
  98        sleep 1
  99        attempt=$(( ${attempt} + 1 ))
 100        if [ ${attempt} -eq ${MAX_SLEEP} ]; then
 101            echo "Message '${READY_MSG}' failed to appear in java_${iface}.log"
 102            cat java_${iface}.log
 103            break
 104        fi
 105    done
 106    echo "Finished waiting for java process (${attempt} sec)"
 107 done
 108 
 109 procs_started_successfully=0
 110 for iface in ${ips}; do
 111    pid=$(cat java_${iface}.pid)
 112    ps ${pid} > /dev/null 2>&1
 113    if [ $? -eq 0 ]; then
 114        procs_started_successfully=$(( ${procs_started_successfully} + 1 ))
 115    fi
 116 done
 117 
 118 FAILED=0
 119 if [ ${procs_started_successfully} -ne ${count} ]; then
 120    echo
 121    echo "Test failed, expected ${count} java processes with"
 122    echo "JMX bound to ${JMX_PORT}, RMI registry bound to ${RMI_PORT}"
 123    echo "but only got ${procs_started_successfully} java processes."
 124    echo
 125    FAILED=1
 126 fi
 127 
 128 # Kill java processes started in background
 129 for iface in ${ips}; do
 130     pid=$(cat java_${iface}.pid)
 131     # This might fail if no-such-process which is benign.
 132     kill ${pid}
 133     rm -rf java_${iface}.pid
 134 done
 135 
 136 if [ ${FAILED} -eq 0 ]; then
 137    echo
 138    echo "Test passed."
 139    echo
 140    exit 0
 141 else
 142    echo
 143    echo "Test FAILED."
 144    echo
 145    exit 20
 146 fi